summaryrefslogtreecommitdiff
path: root/src/util.cc
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2023-01-14 17:54:23 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2023-01-14 18:01:12 +0100
commit8a7f76e7a1b404dc7f703d09d724defaee9e4238 (patch)
tree6bafc5ca77048bb5ab2acf4089c38be7e32e42bd /src/util.cc
parentca439c62d35d587c90230894ec0d1b002ba8c639 (diff)
Extend target_type deduction based on filename extension and move to utils.
Diffstat (limited to 'src/util.cc')
-rw-r--r--src/util.cc61
1 files changed, 57 insertions, 4 deletions
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 <iostream>
#include <fstream>
+#include <algorithm>
+#include <cctype>
+
+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<std::string> 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;
+}