From 9fe6626a63718635cca6ce8920e1669d099d46a7 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 19 Jun 2021 11:58:12 +0200 Subject: Make self-aware (and recompile/re-launch as needed) --- .gitignore | 4 +++- Makefile | 8 ++++++-- cppbuild.cc | 30 ++++++++++++++++++++++++++++++ execute.cc | 8 ++++++++ libcppbuild.cc | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ libcppbuild.h | 3 +++ 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 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 #include +/* +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 @@ -82,8 +83,60 @@ std::shared_ptr getNextTask(const std::list>& 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 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 #include +//#include struct BuildConfiguration { @@ -15,3 +16,5 @@ struct BuildConfiguration }; std::vector configs(); + +void reg(const std::string& location); -- cgit v1.2.3