diff options
| author | Bent Bisballe Nyeng <deva@aasimon.org> | 2021-06-13 12:42:52 +0200 | 
|---|---|---|
| committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2021-06-13 12:42:52 +0200 | 
| commit | f6f5f31067cdee2d7003d8209361ac9e5b6975c5 (patch) | |
| tree | c967ca817c31fd5681032d1c545f0c812aabb236 | |
| parent | 6bcbf18a6a67707c13d66b96770c8b210a45b9f4 (diff) | |
Use vfork/exec instead of system for compiler invocation.
| -rw-r--r-- | cppbuild.cc | 21 | ||||
| -rw-r--r-- | libcppbuild.cc | 6 | ||||
| -rw-r--r-- | libcppbuild.h | 4 | ||||
| -rw-r--r-- | 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<std::string> sources; -	std::string cxxflags; -	std::string cflags; +	std::vector<std::string> cxxflags; +	std::vector<std::string> cflags;  	std::string ldflags;  }; @@ -2,6 +2,11 @@  #include <iostream>  #include <fstream> +#include <unistd.h> +#include <cstring> +#include <sys/types.h> +#include <sys/wait.h> +#include <spawn.h>  #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<const char*> 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() | 
