diff --git a/flang/tools/f18-parse-demo/f18-parse-demo.cpp b/flang/tools/f18-parse-demo/f18-parse-demo.cpp --- a/flang/tools/f18-parse-demo/f18-parse-demo.cpp +++ b/flang/tools/f18-parse-demo/f18-parse-demo.cpp @@ -33,6 +33,7 @@ #include "flang/Parser/unparse.h" #include "llvm/Support/Errno.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/Program.h" #include "llvm/Support/raw_ostream.h" #include #include @@ -42,9 +43,7 @@ #include #include #include -#include #include -#include #include static std::list argList(int argc, char *const argv[]) { @@ -98,36 +97,24 @@ const char *prefix{nullptr}; }; -bool ParentProcess() { - if (fork() == 0) { - return false; // in child process - } - int childStat{0}; - wait(&childStat); - if (!WIFEXITED(childStat) || WEXITSTATUS(childStat) != 0) { - exit(EXIT_FAILURE); - } - return true; -} - -void Exec(std::vector &argv, bool verbose = false) { +void Exec(std::vector &argv, bool verbose = false) { if (verbose) { for (size_t j{0}; j < argv.size(); ++j) { llvm::errs() << (j > 0 ? " " : "") << argv[j]; } llvm::errs() << '\n'; } - argv.push_back(nullptr); - execvp(argv[0], &argv[0]); - llvm::errs() << "execvp(" << argv[0] - << ") failed: " << llvm::sys::StrError(errno) << '\n'; - exit(EXIT_FAILURE); + std::string ErrMsg; + if (llvm::sys::ExecuteAndWait(argv[0], llvm::makeArrayRef(argv).slice(1), + llvm::None, {}, 0, 0, &ErrMsg)) + llvm::report_fatal_error( + llvm::Twine("execvp(") + argv[0] + ") failed: " + ErrMsg); } void RunOtherCompiler(DriverOptions &driver, char *source, char *relo) { - std::vector argv; + std::vector argv; for (size_t j{0}; j < driver.fcArgs.size(); ++j) { - argv.push_back(driver.fcArgs[j].data()); + argv.push_back(driver.fcArgs[j]); } char dashC[3] = "-c", dashO[3] = "-o"; argv.push_back(dashC); @@ -230,60 +217,50 @@ std::string relo{RelocatableName(driver, path)}; - char tmpSourcePath[32]; - std::snprintf(tmpSourcePath, sizeof tmpSourcePath, "/tmp/f18-%lx.f90", - static_cast(getpid())); + llvm::SmallString<32> tmpSourcePath; { - std::error_code EC; - llvm::raw_fd_ostream tmpSource(tmpSourcePath, EC, llvm::sys::fs::F_None); - if (EC) { - llvm::errs() << EC.message(); - std::exit(EXIT_FAILURE); - } + int fd; + std::error_code EC = + llvm::sys::fs::createUniqueFile("f18-%%%%.f90", fd, tmpSourcePath); + if (EC) + llvm::report_fatal_error(EC.message()); + llvm::raw_fd_ostream tmpSource(fd, /*shouldClose*/ true); Unparse(tmpSource, parseTree, driver.encoding, true /*capitalize*/, options.features.IsEnabled( Fortran::common::LanguageFeature::BackslashEscapes)); } - if (ParentProcess()) { - filesToDelete.push_back(tmpSourcePath); - if (!driver.compileOnly && driver.outputPath.empty()) { - filesToDelete.push_back(relo); - } - return relo; + RunOtherCompiler(driver, tmpSourcePath.data(), relo.data()); + filesToDelete.emplace_back(tmpSourcePath); + if (!driver.compileOnly && driver.outputPath.empty()) { + filesToDelete.push_back(relo); } - RunOtherCompiler(driver, tmpSourcePath, relo.data()); - return {}; + return relo; } std::string CompileOtherLanguage(std::string path, DriverOptions &driver) { std::string relo{RelocatableName(driver, path)}; - if (ParentProcess()) { - if (!driver.compileOnly && driver.outputPath.empty()) { - filesToDelete.push_back(relo); - } - return relo; - } RunOtherCompiler(driver, path.data(), relo.data()); - return {}; + if (!driver.compileOnly && driver.outputPath.empty()) { + filesToDelete.push_back(relo); + } + return relo; } void Link(std::vector &relocatables, DriverOptions &driver) { - if (!ParentProcess()) { - std::vector argv; - for (size_t j{0}; j < driver.fcArgs.size(); ++j) { - argv.push_back(driver.fcArgs[j].data()); - } - for (auto &relo : relocatables) { - argv.push_back(relo.data()); - } - if (!driver.outputPath.empty()) { - char dashO[3] = "-o"; - argv.push_back(dashO); - argv.push_back(driver.outputPath.data()); - } - Exec(argv, driver.verbose); + std::vector argv; + for (size_t j{0}; j < driver.fcArgs.size(); ++j) { + argv.push_back(driver.fcArgs[j].data()); + } + for (auto &relo : relocatables) { + argv.push_back(relo.data()); } + if (!driver.outputPath.empty()) { + char dashO[3] = "-o"; + argv.push_back(dashO); + argv.push_back(driver.outputPath.data()); + } + Exec(argv, driver.verbose); } int main(int argc, char *const argv[]) { diff --git a/flang/tools/f18/f18.cpp b/flang/tools/f18/f18.cpp --- a/flang/tools/f18/f18.cpp +++ b/flang/tools/f18/f18.cpp @@ -25,6 +25,7 @@ #include "flang/Semantics/unparse-with-symbols.h" #include "llvm/Support/Errno.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/Program.h" #include "llvm/Support/raw_ostream.h" #include #include @@ -34,8 +35,6 @@ #include #include #include -#include -#include #include static std::list argList(int argc, char *const argv[]) { @@ -68,7 +67,7 @@ void CleanUpAtExit() { for (const auto &path : filesToDelete) { if (!path.empty()) { - unlink(path.data()); + llvm::sys::fs::remove(path); } } } @@ -110,36 +109,24 @@ bool getSymbolsSources{false}; }; -bool ParentProcess() { - if (fork() == 0) { - return false; // in child process - } - int childStat{0}; - wait(&childStat); - if (!WIFEXITED(childStat) || WEXITSTATUS(childStat) != 0) { - exit(EXIT_FAILURE); - } - return true; -} - -void Exec(std::vector &argv, bool verbose = false) { +void Exec(std::vector &argv, bool verbose = false) { if (verbose) { for (size_t j{0}; j < argv.size(); ++j) { llvm::errs() << (j > 0 ? " " : "") << argv[j]; } llvm::errs() << '\n'; } - argv.push_back(nullptr); - execvp(argv[0], &argv[0]); - llvm::errs() << "execvp(" << argv[0] - << ") failed: " << llvm::sys::StrError(errno) << '\n'; - exit(EXIT_FAILURE); + std::string ErrMsg; + if (llvm::sys::ExecuteAndWait(argv[0], llvm::makeArrayRef(argv).slice(1), + llvm::None, {}, 0, 0, &ErrMsg)) + llvm::report_fatal_error( + llvm::Twine("execvp(") + argv[0] + ") failed: " + ErrMsg); } void RunOtherCompiler(DriverOptions &driver, char *source, char *relo) { - std::vector argv; + std::vector argv; for (size_t j{0}; j < driver.F18_FCArgs.size(); ++j) { - argv.push_back(driver.F18_FCArgs[j].data()); + argv.push_back(driver.F18_FCArgs[j]); } char dashC[3] = "-c", dashO[3] = "-o"; argv.push_back(dashC); @@ -329,16 +316,14 @@ std::string relo{RelocatableName(driver, path)}; - char tmpSourcePath[32]; - std::snprintf(tmpSourcePath, sizeof tmpSourcePath, "/tmp/f18-%lx.f90", - static_cast(getpid())); + llvm::SmallString<32> tmpSourcePath; { - std::error_code EC; - llvm::raw_fd_ostream tmpSource(tmpSourcePath, EC, llvm::sys::fs::F_None); - if (EC) { - llvm::errs() << EC.message() << "\n"; - std::exit(EXIT_FAILURE); - } + int fd; + std::error_code EC = + llvm::sys::fs::createUniqueFile("f18-%%%%.f90", fd, tmpSourcePath); + if (EC) + llvm::report_fatal_error(EC.message()); + llvm::raw_fd_ostream tmpSource(fd, /*shouldClose*/ true); Unparse(tmpSource, parseTree, driver.encoding, true /*capitalize*/, options.features.IsEnabled( Fortran::common::LanguageFeature::BackslashEscapes), @@ -346,49 +331,41 @@ driver.unparseTypedExprsToF18_FC ? &asFortran : nullptr); } - if (ParentProcess()) { - filesToDelete.push_back(tmpSourcePath); - if (!driver.compileOnly && driver.outputPath.empty()) { - filesToDelete.push_back(relo); - } - return relo; + RunOtherCompiler(driver, tmpSourcePath.data(), relo.data()); + filesToDelete.emplace_back(tmpSourcePath); + if (!driver.compileOnly && driver.outputPath.empty()) { + filesToDelete.push_back(relo); } - RunOtherCompiler(driver, tmpSourcePath, relo.data()); - return {}; + return relo; } std::string CompileOtherLanguage(std::string path, DriverOptions &driver) { std::string relo{RelocatableName(driver, path)}; - if (ParentProcess()) { - if (!driver.compileOnly && driver.outputPath.empty()) { - filesToDelete.push_back(relo); - } - return relo; - } RunOtherCompiler(driver, path.data(), relo.data()); - return {}; + if (!driver.compileOnly && driver.outputPath.empty()) { + filesToDelete.push_back(relo); + } + return relo; } void Link(std::vector &liblist, std::vector &objects, DriverOptions &driver) { - if (!ParentProcess()) { - std::vector argv; - for (size_t j{0}; j < driver.F18_FCArgs.size(); ++j) { - argv.push_back(driver.F18_FCArgs[j].data()); - } - for (auto &obj : objects) { - argv.push_back(obj.data()); - } - if (!driver.outputPath.empty()) { - char dashO[3] = "-o"; - argv.push_back(dashO); - argv.push_back(driver.outputPath.data()); - } - for (auto &lib : liblist) { - argv.push_back(lib.data()); - } - Exec(argv, driver.verbose); + std::vector argv; + for (size_t j{0}; j < driver.F18_FCArgs.size(); ++j) { + argv.push_back(driver.F18_FCArgs[j].data()); + } + for (auto &obj : objects) { + argv.push_back(obj.data()); + } + if (!driver.outputPath.empty()) { + char dashO[3] = "-o"; + argv.push_back(dashO); + argv.push_back(driver.outputPath.data()); } + for (auto &lib : liblist) { + argv.push_back(lib.data()); + } + Exec(argv, driver.verbose); } int main(int argc, char *const argv[]) {