summaryrefslogtreecommitdiff
path: root/src/task_ld.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/task_ld.cc')
-rw-r--r--src/task_ld.cc86
1 files changed, 58 insertions, 28 deletions
diff --git a/src/task_ld.cc b/src/task_ld.cc
index 3e05fb1..03745be 100644
--- a/src/task_ld.cc
+++ b/src/task_ld.cc
@@ -6,29 +6,34 @@
#include <iostream>
#include <fstream>
-#include "libctor.h"
+#include "ctor.h"
#include "execute.h"
#include "util.h"
+#include "tools.h"
-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& sourceDir)
- : Task(config, settings, sourceDir)
- , config(config)
- , settings(settings)
- , sourceDir(sourceDir)
+ const std::string& sourceDir_,
+ bool is_self_)
+ : Task(config_, settings_, sourceDir_)
+ , config(config_)
+ , settings(settings_)
+ , sourceDir(sourceDir_)
+ , is_self(is_self_)
{
target_type = config.type;
- if(target_type == TargetType::Auto)
+ output_system = config.system;
+
+ if(target_type == ctor::target_type::automatic)
{
- target_type = TargetType::Executable;
+ target_type = ctor::target_type::executable;
}
- std::filesystem::create_directories(std::filesystem::path(settings.builddir) / sourceDir);
-
_targetFile = target;
+ auto toolchain = getToolChain(config.system);
+ _targetFile = extension(toolchain, target_type, config.system, _targetFile);
for(const auto& object : objects)
{
std::filesystem::path objectFile = object;
@@ -38,21 +43,23 @@ TaskLD::TaskLD(const BuildConfiguration& config,
for(const auto& dep : config.depends)
{
- depFiles.push_back(dep);
+ depFiles.emplace_back(dep);
}
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;
}
}
+
+ std::filesystem::create_directories(targetFile().parent_path());
}
bool TaskLD::dirtyInner()
@@ -81,17 +88,22 @@ bool TaskLD::dirtyInner()
int TaskLD::runInner()
{
+ auto toolchain = getToolChain(config.system);
+
std::vector<std::string> args;
for(const auto& dep : getDependsTasks())
{
auto depFile = dep->targetFile();
- if(depFile.extension() == ".so")
+ auto dep_type = target_type_from_extension(toolchain, depFile);
+ if(dep_type == ctor::target_type::dynamic_library)
{
- 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" || depFile.extension() == ".o")
+ else if(dep_type == ctor::target_type::static_library ||
+ dep_type == ctor::target_type::object)
{
args.push_back(depFile.string());
}
@@ -99,10 +111,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);
@@ -111,11 +123,24 @@ int TaskLD::runInner()
if(settings.verbose == 0)
{
- std::cout << "LD => " << targetFile().string() << "\n";
+ std::string output = "LD => " + targetFile().string() + '\n';
+ std::cout << output << std::flush;
}
auto tool = compiler();
- return execute(tool, args, settings.verbose > 0);
+ const auto& cfg = ctor::get_configuration();
+ auto ldflags = cfg.getenv("LDFLAGS");
+ if(!ldflags.empty())
+ {
+ append(args, ld_option(toolchain, ctor::ld_opt::custom, ldflags));
+ }
+ auto res = execute(settings, tool, args, cfg.env, is_self);
+ if(res != 0)
+ {
+ std::filesystem::remove(targetFile());
+ }
+
+ return res;
}
int TaskLD::clean()
@@ -168,16 +193,21 @@ 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";
for(const auto& dep : config.depends)
{