// -*- c++ -*- // Distributed under the BSD 2-Clause License. // See accompanying file LICENSE for details. #include "task_ar.h" #include #include #include "ctor.h" #include "execute.h" #include "util.h" #include "tools.h" TaskAR::TaskAR(const ctor::build_configuration& config, const ctor::settings& settings, const std::string& target, const std::vector& objects, const std::string& sourceDir) : Task(config, settings, sourceDir) , config(config) , settings(settings) , sourceDir(sourceDir) { std::filesystem::create_directories(std::filesystem::path(settings.builddir) / sourceDir); _targetFile = target; for(const auto& object : objects) { std::filesystem::path objectFile = object; objectFiles.push_back(objectFile); dependsStr.push_back(objectFile.string()); } for(const auto& dep : config.depends) { depFiles.push_back(dep); } flagsFile = std::filesystem::path(settings.builddir) / cleanUp(sourceDir) / targetFile().stem(); flagsFile += ".flags"; target_type = ctor::target_type::static_library; source_language = ctor::language::c; for(const auto& source : config.sources) { std::filesystem::path sourceFile(source.file); // TODO: Use task languages instead if(sourceFile.extension().string() != ".c") { source_language = ctor::language::cpp; } } } bool TaskAR::dirtyInner() { if(!std::filesystem::exists(targetFile())) { return true; } if(!std::filesystem::exists(flagsFile)) { return true; } { auto lastFlags = readFile(flagsFile.string()); if(flagsString() != lastFlags) { //std::cout << "The compiler flags changed\n"; return true; } } return false; } int TaskAR::runInner() { auto toolchain = getToolChain(config.system); std::vector args; append(args, ar_option(toolchain, ctor::ar_opt::replace)); append(args, ar_option(toolchain, ctor::ar_opt::add_index)); append(args, ar_option(toolchain, ctor::ar_opt::create)); append(args, ar_option(toolchain, ctor::ar_opt::output, targetFile().string())); for(const auto& task : getDependsTasks()) { args.push_back(task->targetFile().string()); } { // Write flags to file. std::ofstream flagsStream(flagsFile); flagsStream << flagsString(); } if(settings.verbose == 0) { std::cout << "AR => " << targetFile().string() << std::endl; } const auto& c = ctor::get_configuration(); std::string tool; switch(outputSystem()) { case ctor::output_system::host: tool = c.get(ctor::cfg::host_ar, "/usr/bin/ar"); break; case ctor::output_system::build: tool = c.get(ctor::cfg::build_ar, "/usr/bin/ar"); break; } return execute(tool, args, settings.verbose > 0); } int TaskAR::clean() { if(std::filesystem::exists(targetFile())) { std::cout << "Removing " << targetFile().string() << "\n"; std::filesystem::remove(targetFile()); } if(std::filesystem::exists(flagsFile)) { std::cout << "Removing " << flagsFile.string() << "\n"; std::filesystem::remove(flagsFile); } return 0; } std::vector TaskAR::depends() const { std::vector deps; for(const auto& objectFile : objectFiles) { deps.push_back(objectFile.string()); } for(const auto& dep : config.depends) { deps.push_back(dep); } return deps; } std::string TaskAR::target() const { return _targetFile.string(); } std::filesystem::path TaskAR::targetFile() const { return std::filesystem::path(settings.builddir) / sourceDir / _targetFile; } bool TaskAR::derived() const { return false; } std::string TaskAR::flagsString() const { auto toolchain = getToolChain(config.system); std::string flagsStr; bool first{true}; for(const auto& flag : config.flags.ldflags) { for(const auto& str : to_strings(toolchain, flag)) { if(first) { flagsStr += " "; first = false; } flagsStr += str; } } flagsStr += "\n"; for(const auto& dep : config.depends) { if(dep != config.depends[0]) { flagsStr += " "; } flagsStr += dep; } auto deps = depends(); for(const auto& dep : deps) { flagsStr += " "; flagsStr += dep; } return flagsStr; }