From 330336c28507e0ee3a77d20e03dbc994618c213c Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 19 Jun 2021 12:44:02 +0200 Subject: Support multiple build config files. --- libcppbuild.cc | 96 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 29 deletions(-) (limited to 'libcppbuild.cc') diff --git a/libcppbuild.cc b/libcppbuild.cc index b400061..d58705d 100644 --- a/libcppbuild.cc +++ b/libcppbuild.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include @@ -83,35 +84,65 @@ std::shared_ptr getNextTask(const std::list>& allTas return nullptr; } -namespace +struct Config { -// Hack to get command-line args for re-launch -int g_argc; -char** g_argv; -} + const char* file; + std::vector (*cb)(); +}; +std::array configFiles; +int numConfigFiles{0}; // 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) +int reg(const char* location, + std::vector (*cb)()) +{ + // NOTE: std::cout cannot be used here + configFiles[numConfigFiles].file = location; + configFiles[numConfigFiles].cb = cb; + ++numConfigFiles; + return 0; +} + +void recompileCheck(int argc, char* argv[]) { - std::filesystem::path configFile(location); - std::filesystem::path binFile(configFile.stem()); - if(std::filesystem::last_write_time(binFile) <= - std::filesystem::last_write_time(configFile)) + bool dirty{false}; + + std::vector args; + args.push_back("-s"); + args.push_back("-O3"); + args.push_back("-std=c++17"); + args.push_back("-pthread"); + + std::filesystem::path binFile("cppbuild"); + + std::cout << "Recompile check (" << numConfigFiles << "):\n"; + for(int i = 0; i < numConfigFiles; ++i) + { + std::string location = configFiles[i].file; + std::cout << " - " << location << "\n"; + std::filesystem::path configFile(location); + if(std::filesystem::last_write_time(binFile) <= + std::filesystem::last_write_time(configFile)) + { + dirty = true; + } + + // Support adding multiple config functions from the same file + if(std::find(args.begin(), args.end(), location) == std::end(args)) + { + args.push_back(location); + } + } + args.push_back("libcppbuild.a"); + args.push_back("-o"); + args.push_back(binFile.string()); + + if(dirty) { std::cout << "Rebuilding config\n"; - auto ret = execute("/usr/bin/g++", - { - "-s", - "-O3", - "-std=c++17", - "-pthread", - configFile.string(), - "libcppbuild.a", - "-o", - binFile.string(), - }); + auto ret = execute("/usr/bin/g++", args); if(ret != 0) { std::cerr << "Failed.\n"; @@ -121,21 +152,18 @@ void reg(const std::string& location) { std::cout << "Re-launch\n"; std::vector args; - for(int i = 1; i < g_argc; ++i) + for(int i = 1; i < argc; ++i) { - args.push_back(g_argv[i]); + args.push_back(argv[i]); } - exit(execute(g_argv[0], args)); + exit(execute(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; + recompileCheck(argc, argv); Settings settings; // TODO: Set from commandline @@ -183,7 +211,17 @@ int main(int argc, char* argv[]) std::filesystem::path builddir(settings.builddir); std::filesystem::create_directories(builddir); - auto build_configs = configs(); + std::vector build_configs; + for(int i = 0; i < numConfigFiles; ++i) + { + //std::string location = configFiles[i].file; + auto configs = configFiles[i].cb(); + for(const auto& config : configs) + { + build_configs.push_back(config); + } + } + std::list> tasks; for(const auto& build_config : build_configs) { -- cgit v1.2.3