summaryrefslogtreecommitdiff
path: root/task.cc
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2021-06-13 12:42:52 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2021-06-13 12:42:52 +0200
commitf6f5f31067cdee2d7003d8209361ac9e5b6975c5 (patch)
treec967ca817c31fd5681032d1c545f0c812aabb236 /task.cc
parent6bcbf18a6a67707c13d66b96770c8b210a45b9f4 (diff)
Use vfork/exec instead of system for compiler invocation.
Diffstat (limited to 'task.cc')
-rw-r--r--task.cc84
1 files changed, 74 insertions, 10 deletions
diff --git a/task.cc b/task.cc
index 29e18cb..030932e 100644
--- a/task.cc
+++ b/task.cc
@@ -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()