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) --- libcppbuild.cc | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'libcppbuild.cc') 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"; -- cgit v1.2.3