From f6f5f31067cdee2d7003d8209361ac9e5b6975c5 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sun, 13 Jun 2021 12:42:52 +0200 Subject: Use vfork/exec instead of system for compiler invocation. --- task.cc | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 10 deletions(-) (limited to 'task.cc') diff --git a/task.cc b/task.cc index 29e18cb..030932e 100644 --- a/task.cc +++ b/task.cc @@ -2,6 +2,11 @@ #include #include +#include +#include +#include +#include +#include #include "libcppbuild.h" #include "settings.h" @@ -143,6 +148,17 @@ bool Task::dirty() return false; } +int parent_waitpid(pid_t pid) +{ + int status; + + if(waitpid(pid, &status, 0) != pid) + { + return 1; + } + + return status; +} int Task::run() { if(!std::filesystem::exists(sourceFile)) @@ -151,24 +167,72 @@ int Task::run() return 1; } - std::string comp = "g++"; - std::string flags = config.cxxflags; + std::string comp = "/usr/bin/g++"; + auto flags = config.cxxflags; if(std::string(sourceFile.extension()) == ".c") { - comp = "gcc"; + comp = "/usr/bin/gcc"; flags = config.cflags; } - std::string cmd = comp + - " -MMD -c " + std::string(sourceFile) + " " + - flags + " " + - "-o " + std::string(targetFile); + + char source[256]; + strcpy(source, std::string(sourceFile).data()); + char target[256]; + strcpy(target, std::string(targetFile).data()); + char pwd[256]; + strcpy(pwd, std::string(std::filesystem::current_path()).data()); + std::vector argv; + //args.push_back("/bin/echo"); + if(comp == "/usr/bin/gcc") + { + argv.push_back("/usr/bin/gcc"); + } + else + { + argv.push_back("/usr/bin/g++"); + } + + argv.push_back("-MMD"); + argv.push_back("-c"); + argv.push_back(source); + argv.push_back("-o"); + argv.push_back(target); + + for(const auto& flag : flags) + { + argv.push_back(flag.data()); + } + + std::string cmd; + for(const auto& arg : argv) + { + if(arg == nullptr) + { + break; + } + if(!cmd.empty()) + { + cmd += " "; + } + cmd += arg; + } std::cout << cmd << "\n"; - if(system(cmd.data())) +#if 0 + auto pid = vfork(); + if(pid == 0) { - return 1; + execv(comp.data(), (char**)argv.data()); } - return 0; +#else + pid_t pid; + if(posix_spawn(&pid, comp.data(), nullptr, nullptr, (char**)argv.data(), nullptr)) + { + return 1;//exit(1); + } +#endif + + return parent_waitpid(pid); } int Task::clean() -- cgit v1.2.3