From 019ee9f3026a419cd25916974d32fe1ebcff9578 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 25 Sep 2021 11:06:50 +0200 Subject: Add support for (optionally) explicitly declaring source language type in build configurations. --- src/libctor.h | 18 ++++++++++++++-- src/task_ar.cc | 2 +- src/task_cc.cc | 66 +++++++++++++++++++++++++++++++++++++++++++++------------- src/task_cc.h | 2 +- src/task_ld.cc | 2 +- src/task_so.cc | 2 +- 6 files changed, 71 insertions(+), 21 deletions(-) diff --git a/src/libctor.h b/src/libctor.h index 6903c6b..6026ffb 100644 --- a/src/libctor.h +++ b/src/libctor.h @@ -33,13 +33,27 @@ enum class OutputSystem Build, // Internal tool during cross-compilation }; +struct Source +{ + Source(const char* file) + : file(file) {} + Source(const std::string& file) + : file(file) {} + Source(const char* file, Language language) + : file(file), language(language) {} + Source(const std::string& file, Language language) + : file(file), language(language) {} + + std::string file; + Language language{Language::Auto}; +}; + struct BuildConfiguration { TargetType type{TargetType::Auto}; - Language language{Language::Auto}; OutputSystem system{OutputSystem::Host}; std::string target; - std::vector sources; // source list + std::vector sources; // source list std::vector depends; // internal dependencies std::vector cxxflags; // flags for c++ compiler std::vector cflags; // flags for c compiler diff --git a/src/task_ar.cc b/src/task_ar.cc index e920571..d767912 100644 --- a/src/task_ar.cc +++ b/src/task_ar.cc @@ -58,7 +58,7 @@ TaskAR::TaskAR(const BuildConfiguration& config, 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; diff --git a/src/task_cc.cc b/src/task_cc.cc index 8256c70..eb361cb 100644 --- a/src/task_cc.cc +++ b/src/task_cc.cc @@ -5,6 +5,7 @@ #include #include +#include #include "libctor.h" #include "settings.h" @@ -84,30 +85,75 @@ std::vector readDeps(const std::string& depFile) return output; } + +Language languageFromExtension(const std::filesystem::path& file) +{ + auto ext = file.extension().string(); + if(ext == ".c") + { + return Language::C; + } + + if(ext == ".C" || + ext == ".cc" || + ext == ".cpp" || + ext == ".CPP" || + ext == ".c++" || + ext == ".cp" || + ext == ".cxx") + { + return Language::Cpp; + } + + if(ext == ".s" || + ext == ".S" || + ext == ".asm") + { + return Language::Asm; + } + + std::cerr << "Could not deduce language from " << file.string() << "\n"; + exit(1); + return {}; +} } // namespace :: TaskCC::TaskCC(const BuildConfiguration& config, const Settings& settings, - const std::string& sourceDir, const std::string& source) + const std::string& sourceDir, const Source& source) : Task(config) , config(config) , settings(settings) , sourceDir(sourceDir) { sourceFile = sourceDir; - sourceFile /= source; + sourceFile /= source.file; std::filesystem::path base = settings.builddir; base /= config.target; base += "-"; base += sourceFile.stem(); - if(sourceFile.extension().string() == ".c") + target_type = TargetType::Object; + source_language = source.language; + if(source_language == Language::Auto) { - base += "_c"; + source_language = languageFromExtension(sourceFile); } - else + + switch(source_language) { + case Language::C: + base += "_c"; + break; + case Language::Cpp: base += "_cc"; + break; + case Language::Asm: + base += "_asm"; + break; + case Language::Auto: + assert(0 && "This should never happen"); + break; } targetFile = base; @@ -116,16 +162,6 @@ TaskCC::TaskCC(const BuildConfiguration& config, const Settings& settings, depsFile += ".d"; flagsFile = base; flagsFile += ".flags"; - - target_type = TargetType::Object; - if(sourceFile.extension().string() == ".c") - { - source_language = Language::C; - } - else - { - source_language = Language::Cpp; - } } std::string TaskCC::name() const diff --git a/src/task_cc.h b/src/task_cc.h index 10813a7..67d8d45 100644 --- a/src/task_cc.h +++ b/src/task_cc.h @@ -19,7 +19,7 @@ class TaskCC public: TaskCC(const BuildConfiguration& config, const Settings& settings, - const std::string& sourceDir, const std::string& source); + const std::string& sourceDir, const Source& source); std::string name() const override; bool dirtyInner() override; diff --git a/src/task_ld.cc b/src/task_ld.cc index 1b5665e..7710bb0 100644 --- a/src/task_ld.cc +++ b/src/task_ld.cc @@ -62,7 +62,7 @@ TaskLD::TaskLD(const BuildConfiguration& config, 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; diff --git a/src/task_so.cc b/src/task_so.cc index 5623bcf..863b80a 100644 --- a/src/task_so.cc +++ b/src/task_so.cc @@ -57,7 +57,7 @@ TaskSO::TaskSO(const BuildConfiguration& config, 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; -- cgit v1.2.3