diff options
Diffstat (limited to 'src/task_ld.cc')
-rw-r--r-- | src/task_ld.cc | 113 |
1 files changed, 46 insertions, 67 deletions
diff --git a/src/task_ld.cc b/src/task_ld.cc index 1b5665e..20e823d 100644 --- a/src/task_ld.cc +++ b/src/task_ld.cc @@ -7,32 +7,19 @@ #include <fstream> #include "libctor.h" -#include "settings.h" #include "execute.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 :: +#include "util.h" +#include "tools.h" TaskLD::TaskLD(const BuildConfiguration& config, const Settings& settings, const std::string& target, - const std::vector<std::string>& objects) - : Task(config) + const std::vector<std::string>& objects, + const std::string& sourceDir) + : Task(config, settings, sourceDir) , config(config) , settings(settings) + , sourceDir(sourceDir) { target_type = config.type; if(target_type == TargetType::Auto) @@ -40,8 +27,9 @@ TaskLD::TaskLD(const BuildConfiguration& config, target_type = TargetType::Executable; } - targetFile = settings.builddir; - targetFile /= target; + std::filesystem::create_directories(std::filesystem::path(settings.builddir) / sourceDir); + + _targetFile = target; for(const auto& object : objects) { std::filesystem::path objectFile = object; @@ -51,18 +39,16 @@ 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 = settings.builddir / targetFile.stem(); + flagsFile = std::filesystem::path(settings.builddir) / cleanUp(sourceDir) / targetFile().stem(); flagsFile += ".flags"; source_language = Language::C; for(const auto& source : config.sources) { - std::filesystem::path sourceFile(source); + std::filesystem::path sourceFile(source.file); if(sourceFile.extension().string() != ".c") { source_language = Language::Cpp; @@ -72,7 +58,7 @@ TaskLD::TaskLD(const BuildConfiguration& config, bool TaskLD::dirtyInner() { - if(!std::filesystem::exists(targetFile)) + if(!std::filesystem::exists(targetFile())) { return true; } @@ -82,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) @@ -105,42 +82,27 @@ bool TaskLD::dirtyInner() int TaskLD::runInner() { - std::string objectlist; - for(const auto& objectFile : objectFiles) - { - if(!objectlist.empty()) - { - objectlist += " "; - } - objectlist += objectFile.string(); - } + auto tool_chain = 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, getOption(tool_chain, 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, getOption(tool_chain, opt::link, lib)); } - else if(depFile.extension() == ".a") + else if(depFile.extension() == ".a" || depFile.extension() == ".o") { args.push_back(depFile.string()); } } - for(const auto& flag : config.ldflags) - { - args.push_back(flag); - } - args.push_back("-o"); - args.push_back(targetFile.string()); + append(args, config.flags.ldflags); + append(args, getOption(tool_chain, opt::output, targetFile().string())); { // Write flags to file. std::ofstream flagsStream(flagsFile); @@ -149,7 +111,7 @@ int TaskLD::runInner() if(settings.verbose == 0) { - std::cout << "LD => " << targetFile.string() << "\n"; + std::cout << "LD => " << targetFile().string() << "\n"; } auto tool = compiler(); @@ -158,10 +120,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)) @@ -191,15 +153,25 @@ 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 +{ + return false; } std::string TaskLD::flagsString() const { std::string flagsStr; - for(const auto& flag : config.ldflags) + for(const auto& flag : config.flags.ldflags) { - if(flag != config.ldflags[0]) + if(flag != config.flags.ldflags[0]) { flagsStr += " "; } @@ -216,5 +188,12 @@ std::string TaskLD::flagsString() const flagsStr += dep; } + auto deps = depends(); + for(const auto& dep : deps) + { + flagsStr += " "; + flagsStr += dep; + } + return flagsStr; } |