summaryrefslogtreecommitdiffhomepage
path: root/LanguageSettings.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2024-05-10 21:15:30 +0200
committerRoland Reichwein <mail@reichwein.it>2024-05-10 21:15:30 +0200
commita7e016c2c633667b561a0f26ebde88cb26571d1c (patch)
tree7c66727de7022d5d962448be365dd059bb0ecac1 /LanguageSettings.cpp
parentbfdb4e9d2cfc7890c5f194e670039fa76c391330 (diff)
Build static and dynamic libs
Diffstat (limited to 'LanguageSettings.cpp')
-rw-r--r--LanguageSettings.cpp95
1 files changed, 52 insertions, 43 deletions
diff --git a/LanguageSettings.cpp b/LanguageSettings.cpp
index 1d243d5..8da144f 100644
--- a/LanguageSettings.cpp
+++ b/LanguageSettings.cpp
@@ -12,45 +12,23 @@
#include <libreichwein/stringhelper.h>
+#include "env.h"
+#include "file.h"
+
namespace fs = std::filesystem;
using namespace std::string_literals;
-namespace {
- std::string env_value(const std::string& key) {
- char* value_ptr{std::getenv(key.c_str())};
- if (value_ptr == nullptr) {
- return {};
- } else {
- return value_ptr;
- }
- }
-
- // returns "" if not found
- std::string find_executable(const std::vector<std::string>& list) {
- std::vector<std::string> paths {Reichwein::Stringhelper::split(env_value("PATH"), ":")};
-
- for (const auto& i: list) {
- for (const auto& j: paths) {
- if (fs::exists(j + "/" + i)) {
- return i;
- }
- }
- }
-
- return {};
- }
-
-}
-
LanguageSettings::LanguageSettings():
// C++
CXX{env_value("CXX")},
CXXFLAGS{env_value("CXXFLAGS")},
LDFLAGS{env_value("LDFLAGS")},
LDLIBS{env_value("LDLIBS")},
- LIBS{env_value("LIBS")}
+ LIBS{env_value("LIBS")},
// C
// CFLAGS
+
+ AR{env_value("AR")}
{
std::string _default_compiler(find_executable({"g++", "clang++"}));
@@ -64,21 +42,29 @@ LanguageSettings::LanguageSettings():
}
if (CXXFLAGS.empty()) {
- CXXFLAGS = "-std=c++17";
+ CXXFLAGS = "-std=c++17 -Wall -O2";
+ }
+
+ if (AR.empty()) {
+ AR = "ar";
}
}
std::string LanguageSettings::getCompileCommand(const std::filesystem::path& target,
- const std::filesystem::path &source,
+ const std::filesystem::path& source,
+ const std::filesystem::path& build,
const std::vector<std::filesystem::path>& includepaths) const
{
std::string includes{std::accumulate(includepaths.begin(), includepaths.end(), std::string{},
[](const std::string& sum, const fs::path& p){ return sum + " -I" + p.string();})};
+ std::string CXXFLAGS_add{is_dynamic_lib(build) ? " -fvisibility=hidden -fPIC"s : ""s};
+
// compile: $(CXX) $(CXXFLAGS) -c $< -o $@
- return fmt::format("{}{}{} -c {} -o {}",
+ return fmt::format("{}{}{}{} -c {} -o {}",
CXX,
CXXFLAGS.empty() ? ""s : (" "s + CXXFLAGS),
+ CXXFLAGS_add,
includes,
source.string(),
target.string());
@@ -88,22 +74,36 @@ std::string LanguageSettings::getLinkCommand(const std::filesystem::path& target
const std::vector<std::filesystem::path> &inputs,
const std::vector<std::string>& link_libs) const
{
- // link: $(CXX) $(LDFLAGS) $^ $(LDLIBS) $(LIBS) -o $@
std::string input_string{std::accumulate(inputs.begin(), inputs.end(), std::string{},
[](const std::string& sum, const fs::path& p){ return sum + " " + p.string(); })};
- std::string link_libs_string{std::accumulate(link_libs.begin(), link_libs.end(), std::string(),
- [](const std::string& sum, const std::string& i){ return sum + " -l" + i; })};
-
- return fmt::format("{}{}{}{}{}{} -o {}",
- CXX,
- CXXFLAGS.empty() ? ""s : (" "s + CXXFLAGS),
- input_string,
- LDLIBS.empty() ? ""s : (" "s + LDLIBS),
- LIBS.empty() ? ""s : (" "s + LIBS),
- link_libs_string,
- target.string());
+ if (is_static_lib(target)) {
+ // link: $(AR) rcs libxyz.a x.o y.o z.o
+ return fmt::format("{} rcs {}{}",
+ AR,
+ target.string(),
+ input_string);
+ } else {
+ // dynamic link: -shared -Wl,-soname,libXXX.so.N -o libXXX.so.N.M.O
+ std::string LDFLAGS_add{is_dynamic_lib(target) ? fmt::format(" -shared -Wl,-soname,{} -o {}",
+ soname_shorter(target.string()).string(),
+ target.string()) : ""s};
+
+ std::string link_libs_string{std::accumulate(link_libs.begin(), link_libs.end(), std::string(),
+ [](const std::string& sum, const std::string& i){ return sum + " -l" + i; })};
+
+ // link: $(CXX) $(LDFLAGS) $^ $(LDLIBS) $(LIBS) -o $@
+ return fmt::format("{}{}{}{}{}{}{} -o {}",
+ CXX,
+ LDFLAGS.empty() ? ""s : (" "s + LDFLAGS),
+ LDFLAGS_add,
+ input_string,
+ LDLIBS.empty() ? ""s : (" "s + LDLIBS),
+ LIBS.empty() ? ""s : (" "s + LIBS),
+ link_libs_string,
+ target.string());
+ }
}
std::string LanguageSettings::getDepCommand(const std::filesystem::path& target, const std::filesystem::path &source) const
@@ -112,3 +112,12 @@ std::string LanguageSettings::getDepCommand(const std::filesystem::path& target,
return fmt::format("{} -MM -MF {} -c {}", CXX, target.string(), source.string());
}
+// TODO:
+// variables:
+// CC
+// CFLAGS
+// AR
+// ARFLAGS
+// AS
+// ASFLAGS
+// dynamic lib: