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. --- cppbuild.cc | 21 +++++++++++++-- libcppbuild.cc | 6 ++--- libcppbuild.h | 4 +-- task.cc | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 98 insertions(+), 17 deletions(-) diff --git a/cppbuild.cc b/cppbuild.cc index 77ed1f3..ae6fd8d 100644 --- a/cppbuild.cc +++ b/cppbuild.cc @@ -138,10 +138,27 @@ BuildConfiguration configs() }, // cxx flags - "-DUI_X11 -g -Wall -Werror -std=c++11 -Idrumgizmo/getoptpp -Idrumgizmo/ -Idrumgizmo/hugin -Idrumgizmo/plugingui/ -Idrumgizmo/src/ -Idrumgizmo/zita-resampler/libs -Idrumgizmo/pugixml/src", + { + "-DUI_X11", + "-g", + "-Wall", + "-Werror", + "-std=c++11", + "-Idrumgizmo/getoptpp", + "-Idrumgizmo/", + "-Idrumgizmo/hugin", + "-Idrumgizmo/plugingui/", + "-Idrumgizmo/src/", + "-Idrumgizmo/zita-resampler/libs", + "-Idrumgizmo/pugixml/src", + }, // c flags - "-g -Wall -Werror", + { + "-g", + "-Wall", + "-Werror", + }, // linker flags "-lm -lX11 -lXext -pthread -lsndfile", diff --git a/libcppbuild.cc b/libcppbuild.cc index 82a70d1..2167173 100644 --- a/libcppbuild.cc +++ b/libcppbuild.cc @@ -23,7 +23,7 @@ int main(int argc, const char* argv[]) settings.builddir = "build/foo"; settings.parallel_processes = - std::max(1u, std::thread::hardware_concurrency() * 3); + std::max(1u, std::thread::hardware_concurrency() * 2 - 1); std::filesystem::path builddir(settings.builddir); std::filesystem::create_directories(builddir); @@ -82,7 +82,7 @@ int main(int argc, const char* argv[]) return task->run(); })); ++task; - std::this_thread::sleep_for(10ms); + std::this_thread::sleep_for(2ms); } for(auto process = processes.begin(); @@ -100,7 +100,7 @@ int main(int argc, const char* argv[]) } } - std::this_thread::sleep_for(10ms); + std::this_thread::sleep_for(2ms); } for(auto process = processes.begin(); diff --git a/libcppbuild.h b/libcppbuild.h index e9599fc..e4a7801 100644 --- a/libcppbuild.h +++ b/libcppbuild.h @@ -7,8 +7,8 @@ struct BuildConfiguration { std::string target; std::vector sources; - std::string cxxflags; - std::string cflags; + std::vector cxxflags; + std::vector cflags; std::string ldflags; }; 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