// -*- c++ -*- // Distributed under the BSD 2-Clause License. // See accompanying file LICENSE for details. #include "rebuild.h" #include #include #include #include #include #include #include "configure.h" #include "ctor.h" #include "tasks.h" #include "build.h" #include "execute.h" #include "tools.h" #include "util.h" std::array configFiles; std::size_t numConfigFiles{0}; namespace ctor { int reg(ctor::build_configurations (*cb)(const ctor::settings&), const std::source_location location) { // NOTE: std::cout cannot be used here if(numConfigFiles >= configFiles.size()) { fprintf(stderr, "Max %d build configurations currently supported.\n", (int)configFiles.size()); exit(1); } auto loc = std::filesystem::path(location.file_name()); if(loc.is_absolute()) { auto pwd = std::filesystem::current_path(); auto rel = std::filesystem::relative(loc, pwd); auto str = rel.string(); auto file = new char[str.size() + 1]; strncpy(file, str.data(), str.size()); configFiles[numConfigFiles].file = file; // NOTE: This intentionally leaks memory } else { configFiles[numConfigFiles].file = location.file_name(); } configFiles[numConfigFiles].cb = cb; ++numConfigFiles; return 0; } } // ctor:: int reg(const char* location) { // NOTE: std::cout cannot be used here if(numConfigFiles >= configFiles.size()) { fprintf(stderr, "Max %d build configurations currently supported.\n", (int)configFiles.size()); exit(1); } configFiles[numConfigFiles].file = location; configFiles[numConfigFiles].cb = [](const ctor::settings&){ return std::vector{}; }; ++numConfigFiles; return 0; } int unreg(const char* location) { int found{0}; for(std::size_t i = 0; i < numConfigFiles;) { if(std::string(location) == configFiles[i].file) { ++found; for(std::size_t j = i; j < numConfigFiles; ++j) { configFiles[j] = configFiles[j + 1]; } --numConfigFiles; } else { ++i; } } for(std::size_t i = 0; i < numExternalConfigFiles;) { if(std::string(location) == externalConfigFiles[i].file) { ++found; for(std::size_t j = i; j < numExternalConfigFiles; ++j) { externalConfigFiles[j] = externalConfigFiles[j + 1]; } --numExternalConfigFiles; } else { ++i; } } return static_cast(found); } std::array externalConfigFiles; std::size_t numExternalConfigFiles{0}; namespace ctor { int reg(ctor::external_configurations (*cb)(const ctor::settings&), const std::source_location location) { // NOTE: std::cout cannot be used here if(numExternalConfigFiles >= externalConfigFiles.size()) { fprintf(stderr, "Max %d external configurations currently supported.\n", (int)externalConfigFiles.size()); exit(1); } externalConfigFiles[numExternalConfigFiles].file = location.file_name(); externalConfigFiles[numExternalConfigFiles].cb = cb; ++numExternalConfigFiles; return 0; } } // namespace ctor:: namespace { bool contains(const std::vector& sources, const std::string& file) { for(const auto& source : sources) { if(source.file == file) { return true; } } return false; } } bool recompileCheck(const ctor::settings& global_settings, int argc, char* argv[], bool relaunch_allowed) { auto args_span = std::span(argv, static_cast(argc)); using namespace std::string_literals; if(global_settings.verbose > 1) { std::cout << "Recompile check (" << numConfigFiles << "):\n"; } ctor::build_configuration config; config.type = ctor::target_type::executable; config.name = "ctor"; config.system = ctor::output_system::build; config.flags.cxxflags.emplace_back(ctor::cxx_opt::optimization, "3"); config.flags.cxxflags.emplace_back(ctor::cxx_opt::cpp_std, "c++20"); config.flags.cxxflags.push_back({ctor::toolchain::msvc, ctor::cxx_opt::custom, "/EHsc"}); const auto& c = ctor::get_configuration(); if(c.has(ctor::cfg::ctor_includedir)) { config.flags.cxxflags.emplace_back(ctor::cxx_opt::include_path, c.get(ctor::cfg::ctor_includedir)); } if(c.has(ctor::cfg::ctor_libdir)) { config.flags.ldflags.emplace_back(ctor::ld_opt::library_path, c.get(ctor::cfg::ctor_libdir)); } config.flags.ldflags.emplace_back(ctor::toolchain::msvc, ctor::ld_opt::link, "libctor.lib"); config.flags.ldflags.emplace_back(ctor::toolchain::gcc, ctor::ld_opt::link, "ctor"); config.flags.ldflags.emplace_back(ctor::toolchain::clang, ctor::ld_opt::link, "ctor"); config.flags.ldflags.emplace_back(ctor::ld_opt::strip); config.flags.ldflags.emplace_back(ctor::ld_opt::threads); config.flags.ldflags.push_back({ctor::toolchain::msvc, ctor::ld_opt::custom, "/subsystem:console"}); ctor::settings settings{global_settings}; settings.verbose = -1; // Make check completely silent. // override builddir to use ctor subdir auto ctor_builddir = std::filesystem::path(settings.builddir) / "ctor"; settings.builddir = ctor_builddir.string(); { std::filesystem::path buildfile = settings.builddir; std::filesystem::path currentfile = args_span[0]; config.target = std::filesystem::relative(currentfile, buildfile).string(); } if(std::filesystem::exists(configurationFile)) { config.sources.emplace_back(configurationFile.string()); } for(std::size_t i = 0; i < numConfigFiles; ++i) { std::string location = configFiles[i].file; if(global_settings.verbose > 1) { std::cout << " - " << location << "\n"; } // Ensure that files containing multiple configurations are only added once. if(!contains(config.sources, location)) { config.sources.emplace_back(location); } } for(std::size_t i = 0; i < numExternalConfigFiles; ++i) { std::string location = externalConfigFiles[i].file; if(global_settings.verbose > 1) { std::cout << " - " << location << "\n"; } // Ensure that files containing multiple configurations are only added once. if(!contains(config.sources, location)) { config.sources.emplace_back(location); } } auto tasks = taskFactory({config}, settings, {}, true); for(auto task : tasks) { if(task->registerDepTasks(tasks)) { return false; } } // Find out if reconfigure is needed bool reconfigure{false}; for(auto task : tasks) { if(task->dirty() && task->source() != "" && // only look at source files task->source() != "configuration.cc") // don't reconfigure if only configuration.cc is changed. { reconfigure |= true; } } auto dirty_tasks = build(settings, "ctor", tasks, true); // dryrun if(dirty_tasks) { std::cout << "Rebuilding config.\n"; auto ret = build(settings, "ctor", tasks); // run for real if(ret != 0) { return ret; } } if(reconfigure) { std::vector args; args.emplace_back("reconfigure"); if(!relaunch_allowed) { args.emplace_back("--no-rerun"); } for(std::size_t i = 1; i < args_span.size(); ++i) { args.emplace_back(args_span[i]); } auto ret = execute(settings, args_span[0], args); //if(ret != 0) { exit(ret); } } return dirty_tasks; }