summaryrefslogtreecommitdiff
path: root/libcppbuild.cc
diff options
context:
space:
mode:
Diffstat (limited to 'libcppbuild.cc')
-rw-r--r--libcppbuild.cc96
1 files changed, 67 insertions, 29 deletions
diff --git a/libcppbuild.cc b/libcppbuild.cc
index b400061..d58705d 100644
--- a/libcppbuild.cc
+++ b/libcppbuild.cc
@@ -9,6 +9,7 @@
#include <memory>
#include <algorithm>
#include <list>
+#include <array>
#include <getoptpp/getoptpp.hpp>
@@ -83,35 +84,65 @@ std::shared_ptr<Task> getNextTask(const std::list<std::shared_ptr<Task>>& 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<BuildConfiguration> (*cb)();
+};
+std::array<Config, 100> 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<BuildConfiguration> (*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<std::string> 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<std::string> 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<BuildConfiguration> 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<std::shared_ptr<Task>> tasks;
for(const auto& build_config : build_configs)
{