diff options
Diffstat (limited to 'src/task_ld.cc')
-rw-r--r-- | src/task_ld.cc | 119 |
1 files changed, 46 insertions, 73 deletions
diff --git a/src/task_ld.cc b/src/task_ld.cc index 82c26a9..3d917e4 100644 --- a/src/task_ld.cc +++ b/src/task_ld.cc @@ -6,46 +6,30 @@ #include <iostream> #include <fstream> -#include "libctor.h" -#include "settings.h" +#include "ctor.h" #include "execute.h" +#include "util.h" +#include "tools.h" -namespace -{ -std::string readFile(const std::string &fileName) -{ - std::ifstream ifs(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate); - - std::ifstream::pos_type fileSize = ifs.tellg(); - ifs.seekg(0, std::ios::beg); - - std::vector<char> bytes(fileSize); - ifs.read(bytes.data(), fileSize); - - return std::string(bytes.data(), fileSize); -} -} // namespace :: - -TaskLD::TaskLD(const BuildConfiguration& config, - const Settings& settings, +TaskLD::TaskLD(const ctor::build_configuration& config, + const ctor::settings& settings, const std::string& target, const std::vector<std::string>& objects, - const std::string& sourcePath) - : Task(config) + const std::string& sourceDir) + : Task(config, settings, sourceDir) , config(config) , settings(settings) + , sourceDir(sourceDir) { target_type = config.type; - if(target_type == TargetType::Auto) + if(target_type == ctor::target_type::automatic) { - target_type = TargetType::Executable; + target_type = ctor::target_type::executable; } - std::filesystem::path base = settings.builddir; - base /= sourcePath; - std::filesystem::create_directories(base); + std::filesystem::create_directories(std::filesystem::path(settings.builddir) / sourceDir); - targetFile = base / target; + _targetFile = target; for(const auto& object : objects) { std::filesystem::path objectFile = object; @@ -55,28 +39,26 @@ TaskLD::TaskLD(const BuildConfiguration& config, for(const auto& dep : config.depends) { - std::filesystem::path depFile = settings.builddir; - depFile /= dep; - depFiles.push_back(depFile); + depFiles.push_back(dep); } - flagsFile = base / targetFile.stem(); + flagsFile = std::filesystem::path(settings.builddir) / cleanUp(sourceDir) / targetFile().stem(); flagsFile += ".flags"; - source_language = Language::C; + source_language = ctor::language::c; for(const auto& source : config.sources) { std::filesystem::path sourceFile(source.file); if(sourceFile.extension().string() != ".c") { - source_language = Language::Cpp; + source_language = ctor::language::cpp; } } } bool TaskLD::dirtyInner() { - if(!std::filesystem::exists(targetFile)) + if(!std::filesystem::exists(targetFile())) { return true; } @@ -86,15 +68,6 @@ bool TaskLD::dirtyInner() return true; } - for(const auto& objectFile : objectFiles) - { - if(std::filesystem::last_write_time(targetFile) <= - std::filesystem::last_write_time(objectFile)) - { - return true; - } - } - { auto lastFlags = readFile(flagsFile.string()); if(flagsString() != lastFlags) @@ -109,31 +82,20 @@ bool TaskLD::dirtyInner() int TaskLD::runInner() { - std::string objectlist; - for(const auto& objectFile : objectFiles) - { - if(!objectlist.empty()) - { - objectlist += " "; - } - objectlist += objectFile.string(); - } + auto toolchain = getToolChain(config.system); std::vector<std::string> args; - for(const auto& objectFile : objectFiles) - { - args.push_back(objectFile.string()); - } - - for(const auto& depFile : depFiles) + for(const auto& dep : getDependsTasks()) { + auto depFile = dep->targetFile(); if(depFile.extension() == ".so") { - args.push_back(std::string("-L") + settings.builddir); + append(args, ld_option(toolchain, ctor::ld_opt::library_path, + targetFile().parent_path().string())); auto lib = depFile.stem().string().substr(3); // strip 'lib' prefix - args.push_back(std::string("-l") + lib); + append(args, ld_option(toolchain, ctor::ld_opt::link, lib)); } - else if(depFile.extension() == ".a") + else if(depFile.extension() == ".a" || depFile.extension() == ".o") { args.push_back(depFile.string()); } @@ -141,10 +103,10 @@ int TaskLD::runInner() for(const auto& flag : config.flags.ldflags) { - args.push_back(flag); + append(args, to_strings(toolchain, flag)); } - args.push_back("-o"); - args.push_back(targetFile.string()); + + append(args, ld_option(toolchain, ctor::ld_opt::output, targetFile().string())); { // Write flags to file. std::ofstream flagsStream(flagsFile); @@ -153,7 +115,7 @@ int TaskLD::runInner() if(settings.verbose == 0) { - std::cout << "LD => " << targetFile.string() << "\n"; + std::cout << "LD => " << targetFile().string() << std::endl; } auto tool = compiler(); @@ -162,10 +124,10 @@ int TaskLD::runInner() int TaskLD::clean() { - if(std::filesystem::exists(targetFile)) + if(std::filesystem::exists(targetFile())) { - std::cout << "Removing " << targetFile.string() << "\n"; - std::filesystem::remove(targetFile); + std::cout << "Removing " << targetFile().string() << "\n"; + std::filesystem::remove(targetFile()); } if(std::filesystem::exists(flagsFile)) @@ -195,7 +157,12 @@ std::vector<std::string> TaskLD::depends() const std::string TaskLD::target() const { - return targetFile.string(); + return _targetFile.string(); +} + +std::filesystem::path TaskLD::targetFile() const +{ + return std::filesystem::path(settings.builddir) / sourceDir / _targetFile; } bool TaskLD::derived() const @@ -205,14 +172,20 @@ bool TaskLD::derived() const std::string TaskLD::flagsString() const { + auto toolchain = getToolChain(config.system); std::string flagsStr; + bool first{true}; for(const auto& flag : config.flags.ldflags) { - if(flag != config.flags.ldflags[0]) + for(const auto& str : to_strings(toolchain, flag)) { - flagsStr += " "; + if(first) + { + flagsStr += " "; + first = false; + } + flagsStr += str; } - flagsStr += flag; } flagsStr += "\n"; |