// -*- c++ -*- // Distributed under the BSD 2-Clause License. // See accompanying file LICENSE for details. #include "task_so.h" #include #include #include "ctor.h" #include "execute.h" #include "util.h" #include "tools.h" TaskSO::TaskSO(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::path base = sourceDir; target_type = ctor::target_type::dynamic_library; output_system = config.system; _targetFile = base / target; auto toolchain = getToolChain(config.system); _targetFile = extension(toolchain, target_type, config.system, _targetFile); 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.emplace_back(dep); } flagsFile = std::filesystem::path(settings.builddir) / cleanUp(sourceDir) / targetFile().stem(); flagsFile += ".flags"; 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; } } std::filesystem::create_directories(targetFile().parent_path()); } bool TaskSO::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 TaskSO::runInner() { auto toolchain = getToolChain(config.system); std::vector args; append(args, ld_option(toolchain, ctor::ld_opt::position_independent_code)); append(args, ld_option(toolchain, ctor::ld_opt::build_shared)); append(args, ld_option(toolchain, ctor::ld_opt::output, targetFile().string())); for(const auto& task : getDependsTasks()) { args.push_back(task->targetFile().string()); } for(const auto& flag : config.flags.ldflags) { append(args, to_strings(toolchain, flag)); } { // Write flags to file. std::ofstream flagsStream(flagsFile); flagsStream << flagsString(); } if(settings.verbose == 0) { std::cout << "LD => " << targetFile().string() << std::endl; } auto tool = compiler(); const auto& cfg = ctor::get_configuration(); return execute(settings, tool, args, cfg.env); } int TaskSO::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 TaskSO::depends() const { std::vector deps; for(const auto& objectFile : objectFiles) { deps.push_back(objectFile.string()); } for(const auto& depFile : depFiles) { deps.push_back(depFile.string()); } return deps; } std::string TaskSO::target() const { return _targetFile.string(); } std::filesystem::path TaskSO::targetFile() const { return std::filesystem::path(settings.builddir) / sourceDir / _targetFile; } bool TaskSO::derived() const { return false; } std::string TaskSO::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; }