build: make dependency detection and pthread flags target-aware

- Skip host system roots and CPATH/LIBRARY_PATH when cross-compiling, and
  search the target sysroot instead.
- Guard -pthread on Windows for Net and the System/Async/CryptoAsync/
  SecurityAsync targets (MinGW has no -lpthread).
- Compare the target OS case-insensitively.

Fixes #67
Fixes #68
This commit is contained in:
faumaray committed 2026-12-06 14:06:16 +03:00
1 parent 8440770035
commit 1b1947340f
1 file changed
+73 -24
+73 -24
View File
@@ -130,6 +130,32 @@ std::string upper_case(std::string_view text)
return out;
}
std::string lower_case(std::string_view text)
{
std::string out{text};
for (char &c : out) {
if (c >= 'A' && c <= 'Z') {
c = static_cast<char>(c - 'A' + 'a');
}
}
return out;
}
// The OS this description is evaluated on, as a canonical lower-case name.
// Cross builds must not pick up the host's system headers or libraries.
std::string_view host_os()
{
#if defined(_WIN32)
return "windows";
#elif defined(__APPLE__)
return "macos";
#elif defined(__linux__)
return "linux";
#else
return "unknown";
#endif
}
std::optional<std::string> env_value(const std::string &name)
{
const char *value = std::getenv(name.c_str());
@@ -483,7 +509,8 @@ struct ExternalDep
ExternalDep detect_dependency(std::string_view label,
std::initializer_list<std::string_view> headers,
std::initializer_list<std::string_view> libraries,
const std::vector<std::string> &fallback_roots = {})
const std::vector<std::string> &fallback_roots = {},
bool search_host_environment = true)
{
ExternalDep dep;
const std::string prefix = upper_case(label);
@@ -501,17 +528,22 @@ ExternalDep detect_dependency(std::string_view label,
include_roots.emplace_back(*root);
library_roots.emplace_back(*root);
}
for (const char *variable : {"CPATH", "CPLUS_INCLUDE_PATH", "C_INCLUDE_PATH"}) {
if (auto paths = env_value(variable)) {
for (auto &path : split_list(*paths, ":")) {
include_roots.emplace_back(path);
// CPATH/LIBRARY_PATH describe the host toolchain, so they are only useful
// when the target matches the host. Cross builds rely on <NAME>_* roots or
// the target sysroot instead.
if (search_host_environment) {
for (const char *variable : {"CPATH", "CPLUS_INCLUDE_PATH", "C_INCLUDE_PATH"}) {
if (auto paths = env_value(variable)) {
for (auto &path : split_list(*paths, ":")) {
include_roots.emplace_back(path);
}
}
}
}
for (const char *variable : {"LIBRARY_PATH", "LD_LIBRARY_PATH"}) {
if (auto paths = env_value(variable)) {
for (auto &path : split_list(*paths, ":")) {
library_roots.emplace_back(path);
for (const char *variable : {"LIBRARY_PATH", "LD_LIBRARY_PATH"}) {
if (auto paths = env_value(variable)) {
for (auto &path : split_list(*paths, ":")) {
library_roots.emplace_back(path);
}
}
}
}
@@ -645,27 +677,39 @@ void build(Void::Build::Project &project)
{
const ProfileContext &context = project.context();
const fs::path root = fs::current_path();
const bool windows = context.os == "windows";
const std::string target_os = lower_case(context.os);
const bool windows = target_os == "windows";
// A target OS that differs from the host means every host system root is
// wrong for this build: MinGW would mix glibc headers into the Windows
// build. Only explicitly configured target roots (and the sysroot) apply.
const bool cross_compiling = !target_os.empty() && target_os != host_os();
const bool search_host = !cross_compiling;
const bool gcc = context.compiler == CompilerFamily::Gcc;
// ── External dependencies ──────────────────────────────────────────────
const std::vector<std::string> conventional_roots{"/usr", "/usr/local",
"/opt/homebrew"};
std::vector<std::string> conventional_roots;
if (search_host) {
conventional_roots = {"/usr", "/usr/local", "/opt/homebrew"};
}
if (!context.sysroot.empty()) {
conventional_roots.push_back(context.sysroot);
}
const auto boost = detect_dependency(
"Boost", {"boost/beast.hpp", "boost/asio.hpp", "boost/url.hpp",
"boost/stacktrace.hpp"},
{}, conventional_roots);
{}, conventional_roots, search_host);
const auto openssl =
detect_dependency("OpenSSL", {"openssl/ssl.h", "openssl/evp.h"},
{"ssl", "crypto"}, conventional_roots);
{"ssl", "crypto"}, conventional_roots, search_host);
const auto sqlite3 = detect_dependency("SQLite3", {"sqlite3.h"}, {"sqlite3"},
conventional_roots);
const auto sodium =
detect_dependency("Sodium", {"sodium.h"}, {"sodium"}, conventional_roots);
conventional_roots, search_host);
const auto sodium = detect_dependency("Sodium", {"sodium.h"}, {"sodium"},
conventional_roots, search_host);
const auto libarchive = detect_dependency("LibArchive", {"archive.h"},
{"archive"}, conventional_roots);
{"archive"}, conventional_roots,
search_host);
const auto lua = detect_dependency("Lua", {"lua.h"}, {"lua5.4"},
conventional_roots);
conventional_roots, search_host);
// ── Module targets ─────────────────────────────────────────────────────
const std::vector<std::string> modules = selected_modules_in_order();
@@ -707,8 +751,10 @@ void build(Void::Build::Project &project)
apply_external(target, openssl);
// Boost.URL ships compiled symbols.
target.link_option("-lboost_url", Visibility::Public);
target.compile_option("-pthread", Visibility::Public);
target.link_option("-pthread", Visibility::Public);
if (!windows) {
target.compile_option("-pthread", Visibility::Public);
target.link_option("-pthread", Visibility::Public);
}
} else if (module == "Stacktrace") {
if (!boost.found) {
throw std::runtime_error{
@@ -728,8 +774,11 @@ void build(Void::Build::Project &project)
}
} else if (module == "CryptoAsync" || module == "SecurityAsync" ||
module == "System" || module == "SystemAsync") {
target.compile_option("-pthread", Visibility::Public);
target.link_option("-pthread", Visibility::Public);
// MinGW has no -lpthread; the Windows runtime provides threading.
if (!windows) {
target.compile_option("-pthread", Visibility::Public);
target.link_option("-pthread", Visibility::Public);
}
} else if (module == "Archive") {
if (libarchive.found) {
apply_external(target, libarchive);