summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2021-06-13 19:00:13 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2021-06-13 19:00:13 +0200
commit4f923fbdace27f27421bf18dfc9655b73bd68929 (patch)
tree7bfe332416020e9e70d807b7ca9b7b3c59bb8f1e
parent19195e2bbdcd7a0db8f84732ce54b1c9d07c006c (diff)
Move execution code into its own function and use from all tasks.
-rw-r--r--Makefile5
-rw-r--r--cppbuild.cc8
-rw-r--r--execute.cc76
-rw-r--r--execute.h9
-rw-r--r--libcppbuild.h2
-rw-r--r--task_cc.cc83
-rw-r--r--task_ld.cc26
7 files changed, 117 insertions, 92 deletions
diff --git a/Makefile b/Makefile
index a7b0ee7..b3f91cb 100644
--- a/Makefile
+++ b/Makefile
@@ -1,10 +1,11 @@
all: libcppbuild.a cppbuild
-libcppbuild.a: libcppbuild.cc libcppbuild.h task_cc.cc task_cc.h task_ld.cc task_ld.h task.h
+libcppbuild.a: libcppbuild.cc libcppbuild.h task_cc.cc task_cc.h task_ld.cc task_ld.h task.h execute.cc execute.h
g++ -g -std=c++17 libcppbuild.cc -c -o libcppbuild.o
g++ -g -std=c++17 task_cc.cc -c -o task_cc.o
g++ -g -std=c++17 task_ld.cc -c -o task_ld.o
- ar rcs libcppbuild.a libcppbuild.o task_cc.o task_ld.o
+ g++ -g -std=c++17 execute.cc -c -o execute.o
+ ar rcs libcppbuild.a libcppbuild.o task_cc.o task_ld.o execute.o
cppbuild: cppbuild.cc libcppbuild.h libcppbuild.a
g++ -g -std=c++17 cppbuild.cc libcppbuild.a -pthread -o cppbuild
diff --git a/cppbuild.cc b/cppbuild.cc
index ae6fd8d..3604260 100644
--- a/cppbuild.cc
+++ b/cppbuild.cc
@@ -161,6 +161,12 @@ BuildConfiguration configs()
},
// linker flags
- "-lm -lX11 -lXext -pthread -lsndfile",
+ {
+ "-lm",
+ "-lX11",
+ "-lXext",
+ "-pthread",
+ "-lsndfile",
+ }
};
}
diff --git a/execute.cc b/execute.cc
new file mode 100644
index 0000000..cb96bc5
--- /dev/null
+++ b/execute.cc
@@ -0,0 +1,76 @@
+#include "execute.h"
+
+#include <unistd.h>
+#include <cstring>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <spawn.h>
+#include <iostream>
+
+namespace
+{
+int parent_waitpid(pid_t pid)
+{
+ int status;
+
+ if(waitpid(pid, &status, 0) != pid)
+ {
+ return 1;
+ }
+
+ return status;
+}
+} // namespace ::
+
+int execute(const std::string& command,
+ const std::vector<std::string>& args,
+ bool verbose)
+{
+ std::vector<const char*> argv;
+ argv.push_back(command.data());
+ for(const auto& arg : args)
+ {
+ argv.push_back(arg.data());
+ }
+ argv.push_back(nullptr);
+
+ if(verbose)
+ {
+ std::string cmd;
+ for(const auto& arg : argv)
+ {
+ if(arg == nullptr)
+ {
+ break;
+ }
+ if(!cmd.empty())
+ {
+ cmd += " ";
+ }
+ cmd += arg;
+ }
+
+ std::cout << cmd << "\n";
+ }
+
+#if 1
+ auto pid = vfork();
+ if(pid == 0)
+ {
+ execv(command.data(), (char**)argv.data());
+ }
+ auto ret = parent_waitpid(pid);
+#elif 0
+ pid_t pid;
+ if(posix_spawn(&pid, command.data(), nullptr, nullptr,
+ (char**)argv.data(), nullptr))
+ {
+ return 1;
+ }
+ auto ret = parent_waitpid(pid);
+#else
+ auto ret = system(cmd.data());
+#endif
+
+ return ret;
+}
diff --git a/execute.h b/execute.h
new file mode 100644
index 0000000..f284230
--- /dev/null
+++ b/execute.h
@@ -0,0 +1,9 @@
+// -*- c++ -*-
+#pragma once
+
+#include <string>
+#include <vector>
+
+int execute(const std::string& command,
+ const std::vector<std::string>& args,
+ bool verbose = true);
diff --git a/libcppbuild.h b/libcppbuild.h
index e4a7801..a7b82d7 100644
--- a/libcppbuild.h
+++ b/libcppbuild.h
@@ -9,7 +9,7 @@ struct BuildConfiguration
std::vector<std::string> sources;
std::vector<std::string> cxxflags;
std::vector<std::string> cflags;
- std::string ldflags;
+ std::vector<std::string> ldflags;
};
BuildConfiguration configs();
diff --git a/task_cc.cc b/task_cc.cc
index 2a65698..34129a3 100644
--- a/task_cc.cc
+++ b/task_cc.cc
@@ -2,14 +2,10 @@
#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"
+#include "execute.h"
namespace
{
@@ -148,17 +144,6 @@ bool TaskCC::dirty()
return false;
}
-int parent_waitpid(pid_t pid)
-{
- int status;
-
- if(waitpid(pid, &status, 0) != pid)
- {
- return 1;
- }
-
- return status;
-}
int TaskCC::run()
{
if(!std::filesystem::exists(sourceFile))
@@ -174,69 +159,19 @@ int TaskCC::run()
comp = "/usr/bin/gcc";
flags = config.cflags;
}
-
- 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);
+ std::vector<std::string> args;
+ args.push_back("-MMD");
+ args.push_back("-c");
+ args.push_back(std::string(sourceFile));
+ args.push_back("-o");
+ args.push_back(std::string(targetFile));
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 0
- auto pid = vfork();
- if(pid == 0)
- {
- execv(comp.data(), (char**)argv.data());
- }
- auto ret = parent_waitpid(pid);
-#elif 0
- pid_t pid;
- if(posix_spawn(&pid, comp.data(), nullptr, nullptr, (char**)argv.data(), nullptr))
- {
- return 1;//exit(1);
+ args.push_back(flag);
}
- auto ret = parent_waitpid(pid);
-#else
- auto ret = system(cmd.data());
-#endif
- return ret;
+ return execute(comp, args);
}
int TaskCC::clean()
diff --git a/task_ld.cc b/task_ld.cc
index 235523f..0cd4c95 100644
--- a/task_ld.cc
+++ b/task_ld.cc
@@ -2,14 +2,10 @@
#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"
+#include "execute.h"
TaskLD::TaskLD(const BuildConfiguration& config,
const Settings& settings,
@@ -22,8 +18,6 @@ TaskLD::TaskLD(const BuildConfiguration& config,
targetFile /= target;
for(const auto& object : objects)
{
- //std::filesystem::path objectFile = settings.builddir;
- //objectFile /= object;
std::filesystem::path objectFile = object;
objectFiles.push_back(objectFile);
}
@@ -60,15 +54,19 @@ int TaskLD::run()
objectlist += std::string(objectFile);
}
- std::string compiler = "g++ " + objectlist + " " +
- config.ldflags + " " +
- "-o " + std::string(targetFile);
- std::cout << compiler << "\n";
- if(system(compiler.data()))
+ std::vector<std::string> args;
+ for(const auto& objectFile : objectFiles)
{
- return 1;
+ args.push_back(std::string(objectFile));
}
- return 0;
+ for(const auto& flag : config.ldflags)
+ {
+ args.push_back(flag);
+ }
+ args.push_back("-o");
+ args.push_back(std::string(targetFile));
+
+ return execute("/usr/bin/g++", args);
}
int TaskLD::clean()