From 8a7f76e7a1b404dc7f703d09d724defaee9e4238 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 14 Jan 2023 17:54:23 +0100 Subject: Extend target_type deduction based on filename extension and move to utils. --- src/ctor.h | 2 ++ src/tasks.cc | 23 ++++++++--------------- src/util.cc | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- src/util.h | 7 +++---- 4 files changed, 70 insertions(+), 23 deletions(-) diff --git a/src/ctor.h b/src/ctor.h index b8b3ae6..9738949 100644 --- a/src/ctor.h +++ b/src/ctor.h @@ -24,6 +24,8 @@ enum class target_type unit_test, unit_test_library, function, + + unknown, }; enum class language diff --git a/src/tasks.cc b/src/tasks.cc index 67bed2b..b44fce8 100644 --- a/src/tasks.cc +++ b/src/tasks.cc @@ -18,6 +18,7 @@ #include "task_fn.h" #include "rebuild.h" #include "configure.h" +#include "util.h" const std::deque& getTargets(const ctor::settings& settings, bool resolve_externals) @@ -92,23 +93,9 @@ std::set> taskFactory(const ctor::build_configuration& con { target_type = ctor::target_type::function; } - else if(targetFile.extension() == ".a") - { - target_type = ctor::target_type::static_library; - } - else if(targetFile.extension() == ".so") - { - target_type = ctor::target_type::dynamic_library; - } - else if(targetFile.extension() == "") - { - target_type = ctor::target_type::executable; - } else { - std::cerr << "Could not deduce target type from target " << - targetFile.string() << " please specify.\n"; - exit(1); + target_type = target_type_from_extension(targetFile); } } @@ -140,6 +127,12 @@ std::set> taskFactory(const ctor::build_configuration& con // The target_type cannot be Auto break; + case ctor::target_type::unknown: + std::cerr << "Could not deduce target type from target " << + targetFile.string() << " please specify.\n"; + exit(1); + break; + case ctor::target_type::static_library: case ctor::target_type::unit_test_library: tasks.insert(std::make_shared(config, settings, config.target, diff --git a/src/util.cc b/src/util.cc index ee56ede..601fd54 100644 --- a/src/util.cc +++ b/src/util.cc @@ -5,6 +5,15 @@ #include #include +#include +#include + +std::string to_lower(const std::string& str) +{ + std::string out{str}; + std::transform(out.begin(), out.end(), out.begin(), ::tolower); + return out; +} std::string readFile(const std::string& fileName) { @@ -83,15 +92,23 @@ std::vector readDeps(const std::string& depFile) ctor::language languageFromExtension(const std::filesystem::path& file) { auto ext = file.extension().string(); + + // First a few case sensitive comparisons if(ext == ".c") { return ctor::language::c; } - if(ext == ".C" || - ext == ".cc" || + if(ext == ".C") + { + return ctor::language::cpp; + } + + // The rest are compared in lowercase + ext = to_lower(ext); + + if(ext == ".cc" || ext == ".cpp" || - ext == ".CPP" || ext == ".c++" || ext == ".cp" || ext == ".cxx") @@ -100,7 +117,6 @@ ctor::language languageFromExtension(const std::filesystem::path& file) } if(ext == ".s" || - ext == ".S" || ext == ".asm") { return ctor::language::assembler; @@ -135,3 +151,40 @@ std::string cleanUp(const std::string& path) } return cleaned; } + +ctor::target_type target_type_from_extension(const std::filesystem::path& file) +{ + auto ext = to_lower(file.extension().string()); + // Loosely based on: + // https://en.wikipedia.org/wiki/List_of_file_formats#Object_code,_executable_files,_shared_and_dynamically_linked_libraries + if(ext == ".a" || + ext == ".lib") + { + return ctor::target_type::static_library; + } + + if(ext == ".so" || + ext == ".dll" || + ext == ".dylib") + { + return ctor::target_type::dynamic_library; + } + + if(ext == ".o" || + ext == ".obj") + { + return ctor::target_type::object; + } + + if(ext == "" || + ext == ".exe" || + ext == ".com" || + ext == ".bin" || + ext == ".run" || + ext == ".out") + { + return ctor::target_type::executable; + } + + return ctor::target_type::unknown; +} diff --git a/src/util.h b/src/util.h index c7318aa..45f69d4 100644 --- a/src/util.h +++ b/src/util.h @@ -8,6 +8,8 @@ #include #include +std::string to_lower(const std::string& str); + std::string readFile(const std::string& fileName); std::vector readDeps(const std::string& depFile); ctor::language languageFromExtension(const std::filesystem::path& file); @@ -19,7 +21,4 @@ void append(T& a, const T& b) a.insert(a.end(), b.begin(), b.end()); } -//using cxx_flags = std::vector; -//using c_flags = std::vector; -//using ld_flags= std::vector; -//using asm_flags = std::vector; +ctor::target_type target_type_from_extension(const std::filesystem::path& file); -- cgit v1.2.3