summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2021-06-19 11:58:12 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2021-06-19 11:58:12 +0200
commit9fe6626a63718635cca6ce8920e1669d099d46a7 (patch)
treea63b415b0f8a9c2a97ab703234214b6fa7d61eb8
parenta2be8eba3aaf11a9a73092793d27d6d46b4270cd (diff)
Make self-aware (and recompile/re-launch as needed)
-rw-r--r--.gitignore4
-rw-r--r--Makefile8
-rw-r--r--cppbuild.cc30
-rw-r--r--execute.cc8
-rw-r--r--libcppbuild.cc53
-rw-r--r--libcppbuild.h3
6 files changed, 103 insertions, 3 deletions
diff --git a/.gitignore b/.gitignore
index e518938..7d69636 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
+drumgizmo/
+build/
cppbuild
*.a
-*.o \ No newline at end of file
+*.o
diff --git a/Makefile b/Makefile
index af29818..507d841 100644
--- a/Makefile
+++ b/Makefile
@@ -1,10 +1,11 @@
-all: libcppbuild.a
+all: libcppbuild.a cppbuild
SRC = \
libcppbuild.cc \
task_cc.cc \
task_ld.cc \
task_ar.cc \
+ task_so.cc \
task.cc \
execute.cc \
@@ -18,5 +19,8 @@ CXXFLAGS = -g -O3 -std=c++17 -I.
libcppbuild.a: $(OBJ)
ar rcs $@ $(OBJ)
+cppbuild: cppbuild.cc libcppbuild.a
+ g++ $(CXXFLAGS) -pthread cppbuild.cc libcppbuild.a -o $@
+
clean:
- rm -f libcppbuild.a $(OBJ)
+ rm -f libcppbuild.a $(OBJ) cppbuild
diff --git a/cppbuild.cc b/cppbuild.cc
index 882fe63..b1b31ed 100644
--- a/cppbuild.cc
+++ b/cppbuild.cc
@@ -18,8 +18,38 @@ exit $?
#include "libcppbuild.h"
+/*
+Nested build configurations for for example unit-tests in a test folder
+#include "test/cppbuild.cc"
+*/
+
std::vector<BuildConfiguration> configs()
{
+ reg(__FILE__);
+/*
+Glob convenience methods
+std::string glob = getFilesInDir(...);
+*/
+
+/*
+Compilation database
+https://clang.llvm.org/docs/JSONCompilationDatabase.html
+*/
+
+/*
+En feature mere kunne være: pre-post build hooks
+De vil kunne udtrykkes som intra-build dependencies
+Og så selvfølgelig med conditions
+*/
+
+/*
+Target som er "shell script" eller sådan noget
+så kan man kalde f.eks. imageconvert
+*/
+
+/*
+Compiler selection per-target (for arm cross-compilation)
+*/
return
{
{
diff --git a/execute.cc b/execute.cc
index cb96bc5..bb691cf 100644
--- a/execute.cc
+++ b/execute.cc
@@ -7,6 +7,14 @@
#include <spawn.h>
#include <iostream>
+/*
+https://blog.famzah.net/2009/11/20/a-much-faster-popen-and-system-implementation-for-linux/
+https://github.com/famzah/popen-noshell/commit/1f9eaf4eeef348d1efe0f3c7fe8ab670653cfbb1
+https://blog.famzah.net/2018/12/19/posix_spawn-performance-benchmarks-and-usage-examples/
+https://stackoverflow.com/questions/4259629/what-is-the-difference-between-fork-and-vfork/5207945#5207945
+ */
+
+
namespace
{
int parent_waitpid(pid_t pid)
diff --git a/libcppbuild.cc b/libcppbuild.cc
index 69d23ba..b400061 100644
--- a/libcppbuild.cc
+++ b/libcppbuild.cc
@@ -18,6 +18,7 @@
#include "task_ar.h"
#include "task_so.h"
#include "settings.h"
+#include "execute.h"
#include <unistd.h>
@@ -82,8 +83,60 @@ std::shared_ptr<Task> getNextTask(const std::list<std::shared_ptr<Task>>& allTas
return nullptr;
}
+namespace
+{
+// Hack to get command-line args for re-launch
+int g_argc;
+char** g_argv;
+}
+
+// TODO: Use c++20 when ready, somehing like this:
+//void reg(const std::source_location location =
+// std::source_location::current())
+void reg(const std::string& location)
+{
+ std::filesystem::path configFile(location);
+ std::filesystem::path binFile(configFile.stem());
+ if(std::filesystem::last_write_time(binFile) <=
+ std::filesystem::last_write_time(configFile))
+ {
+ std::cout << "Rebuilding config\n";
+ auto ret = execute("/usr/bin/g++",
+ {
+ "-s",
+ "-O3",
+ "-std=c++17",
+ "-pthread",
+ configFile.string(),
+ "libcppbuild.a",
+ "-o",
+ binFile.string(),
+ });
+ if(ret != 0)
+ {
+ std::cerr << "Failed.\n";
+ exit(1);
+ }
+ else
+ {
+ std::cout << "Re-launch\n";
+ std::vector<std::string> args;
+ for(int i = 1; i < g_argc; ++i)
+ {
+ args.push_back(g_argv[i]);
+ }
+ exit(execute(g_argv[0], args));
+ }
+ }
+
+// g++ -s -O3 -std=c++17 -pthread $0 libcppbuild.a -o cppbuild
+}
+
int main(int argc, char* argv[])
{
+ g_argc = argc;
+ g_argv = argv;
+
Settings settings;
// TODO: Set from commandline
settings.builddir = "build/foo";
diff --git a/libcppbuild.h b/libcppbuild.h
index 50b4459..98f6716 100644
--- a/libcppbuild.h
+++ b/libcppbuild.h
@@ -3,6 +3,7 @@
#include <string>
#include <vector>
+//#include <source_location>
struct BuildConfiguration
{
@@ -15,3 +16,5 @@ struct BuildConfiguration
};
std::vector<BuildConfiguration> configs();
+
+void reg(const std::string& location);