#include #include #include #include #include #include #include #include #include #include #include #include namespace Void::Build::Provider { namespace { Void::Result::Error make_error(std::string msg) { return Void::System::make_system_error(Void::System::SystemErrorCode::invalid_argument, std::move(msg)); } bool is_space(char c) { return std::isspace(static_cast(c)) != 0; } std::string trim(std::string_view text) { return std::string{Void::Str::trim_ascii(text)}; } std::vector split_ws(std::string_view text) { std::vector out; std::size_t i = 0; while (i < text.size()) { while (i < text.size() && is_space(text[i])) { ++i; } const std::size_t start = i; while (i < text.size() && !is_space(text[i])) { ++i; } if (i > start) { out.emplace_back(text.substr(start, i - start)); } } return out; } std::vector split_colon_list(std::string_view text) { std::vector out; std::size_t start = 0; while (start <= text.size()) { const auto end = text.find(':', start); const auto piece = text.substr(start, end == std::string_view::npos ? std::string_view::npos : end - start); if (!piece.empty()) { out.emplace_back(piece); } if (end == std::string_view::npos) { break; } start = end + 1; } return out; } struct PcFile { std::filesystem::path path; std::map> variables; std::map> fields; }; std::optional load_pc(const std::filesystem::path &path) { auto text = Void::FileSystem::read_file(path); if (!text) { return std::nullopt; } PcFile pc; pc.path = path; const std::string_view text_view{*text}; std::size_t start = 0; while (start < text_view.size()) { auto end = text_view.find('\n', start); if (end == std::string_view::npos) { end = text_view.size(); } const std::string line = trim(text_view.substr(start, end - start)); start = end + 1; if (line.empty() || line.starts_with('#')) { continue; } const auto eq = line.find('='); const auto colon = line.find(':'); if (eq != std::string::npos && (colon == std::string::npos || eq < colon)) { pc.variables[trim(std::string_view{line}.substr(0, eq))] = trim(std::string_view{line}.substr(eq + 1)); } else if (colon != std::string::npos) { pc.fields[trim(std::string_view{line}.substr(0, colon))] = trim(std::string_view{line}.substr(colon + 1)); } } return pc; } // Expands ${var} references using the file's variables (plus the builtin pcfiledir). std::string expand(const PcFile &pc, std::string_view value, int depth = 0) { if (depth > 16) { return std::string{value}; } std::string out; std::size_t i = 0; while (i < value.size()) { const auto dollar = value.find("${", i); if (dollar == std::string_view::npos) { out += value.substr(i); break; } out += value.substr(i, dollar - i); const auto close = value.find('}', dollar + 2); if (close == std::string_view::npos) { out += value.substr(dollar); break; } const std::string name{value.substr(dollar + 2, close - dollar - 2)}; if (name == "pcfiledir") { out += pc.path.parent_path().string(); } else if (const auto it = pc.variables.find(name); it != pc.variables.end()) { out += expand(pc, it->second, depth + 1); } i = close + 1; } return out; } std::string field(const PcFile &pc, std::string_view name) { const auto it = pc.fields.find(std::string{name}); return it == pc.fields.end() ? std::string{} : expand(pc, it->second); } struct ModuleReq { std::string name; std::string op; std::string version; }; std::vector parse_module_list(std::string_view text) { std::vector out; for (const auto &part : [&] { std::vector pieces; std::size_t start = 0; while (start <= text.size()) { const auto comma = text.find(',', start); pieces.emplace_back(text.substr(start, comma == std::string_view::npos ? std::string_view::npos : comma - start)); if (comma == std::string_view::npos) { break; } start = comma + 1; } return pieces; }()) { const auto tokens = split_ws(part); if (tokens.empty()) { continue; } ModuleReq req; req.name = tokens[0]; if (tokens.size() >= 2) { req.op = tokens[1]; if (req.op == "==") { req.op = "="; } if (tokens.size() >= 3) { req.version = tokens[2]; } } out.push_back(std::move(req)); } return out; } int compare_versions(std::string_view a, std::string_view b) { // rpmvercmp-style comparison: alternating numeric and alphabetic segments. std::size_t i = 0; std::size_t j = 0; const auto is_alnum = [](char c) { return std::isalnum(static_cast(c)) != 0; }; while (i < a.size() || j < b.size()) { while (i < a.size() && !is_alnum(a[i])) { ++i; } while (j < b.size() && !is_alnum(b[j])) { ++j; } if (i >= a.size() || j >= b.size()) { break; } const bool a_digit = std::isdigit(static_cast(a[i])) != 0; const bool b_digit = std::isdigit(static_cast(b[j])) != 0; std::size_t a_end = i; std::size_t b_end = j; if (a_digit) { while (a_end < a.size() && std::isdigit(static_cast(a[a_end]))) { ++a_end; } } else { while (a_end < a.size() && std::isalpha(static_cast(a[a_end]))) { ++a_end; } } if (b_digit) { while (b_end < b.size() && std::isdigit(static_cast(b[b_end]))) { ++b_end; } } else { while (b_end < b.size() && std::isalpha(static_cast(b[b_end]))) { ++b_end; } } if (a_digit != b_digit) { return a_digit ? 1 : -1; // Numeric segments sort newer than alphabetic ones. } auto a_seg = a.substr(i, a_end - i); auto b_seg = b.substr(j, b_end - j); if (a_digit) { while (a_seg.size() > 1 && a_seg.front() == '0') { a_seg.remove_prefix(1); } while (b_seg.size() > 1 && b_seg.front() == '0') { b_seg.remove_prefix(1); } if (a_seg.size() != b_seg.size()) { return a_seg.size() < b_seg.size() ? -1 : 1; } } if (const int cmp = a_seg.compare(b_seg); cmp != 0) { return cmp < 0 ? -1 : 1; } i = a_end; j = b_end; } if (i < a.size()) { return 1; } if (j < b.size()) { return -1; } return 0; } bool satisfies(std::string_view version, const ModuleReq &req) { if (req.op.empty() || req.version.empty()) { return true; } const int cmp = compare_versions(version, req.version); if (req.op == ">=") { return cmp >= 0; } if (req.op == "<=") { return cmp <= 0; } if (req.op == ">") { return cmp > 0; } if (req.op == "<") { return cmp < 0; } if (req.op == "=") { return cmp == 0; } if (req.op == "!=") { return cmp != 0; } return false; } std::optional find_pc(std::string_view module, const std::vector &search_paths) { for (const auto &dir : search_paths) { std::error_code ec; const auto candidate = dir / (std::string{module} + ".pc"); if (std::filesystem::is_regular_file(candidate, ec)) { return candidate; } } return std::nullopt; } struct ResolvedModule { std::string name; std::string version; std::vector cflags; std::vector libs; }; Void::Result::Result resolve_module(const ModuleReq &req, const std::vector &search_paths, std::map> &resolved, std::set &visiting) { if (const auto it = resolved.find(req.name); it != resolved.end()) { if (!satisfies(it->second.version, req)) { return std::unexpected{make_error( std::format("pkg-config module '{}' {} {} is required, but version {} was found", req.name, req.op, req.version, it->second.version))}; } return {}; } if (!visiting.insert(req.name).second) { return {}; // Dependency cycle: already being resolved. } const auto path = find_pc(req.name, search_paths); if (!path) { return std::unexpected{ make_error(std::format("pkg-config module '{}' was not found in {}", req.name, [&] { std::string dirs; for (const auto &dir : search_paths) { dirs += (dirs.empty() ? "" : ":") + dir.string(); } return dirs.empty() ? std::string{"(no search paths)"} : dirs; }()))}; } const auto pc = load_pc(*path); if (!pc) { return std::unexpected{ make_error(std::format("cannot read pkg-config file '{}'", path->string()))}; } ResolvedModule module; module.name = req.name; module.version = field(*pc, "Version"); if (!satisfies(module.version, req)) { return std::unexpected{make_error( std::format("pkg-config module '{}' {} {} is required, but version {} is installed", req.name, req.op, req.version, module.version))}; } // Required modules first (their flags come before this module's own). for (const auto &child : parse_module_list(field(*pc, "Requires"))) { if (auto ok = resolve_module(child, search_paths, resolved, visiting); !ok) { return ok; } const auto &child_module = resolved.at(child.name); module.cflags.insert(module.cflags.end(), child_module.cflags.begin(), child_module.cflags.end()); module.libs.insert(module.libs.end(), child_module.libs.begin(), child_module.libs.end()); } const auto cflags = split_ws(field(*pc, "Cflags")); module.cflags.insert(module.cflags.end(), cflags.begin(), cflags.end()); const auto libs = split_ws(field(*pc, "Libs")); module.libs.insert(module.libs.end(), libs.begin(), libs.end()); visiting.erase(req.name); resolved[req.name] = std::move(module); return {}; } } // namespace std::vector pkgconfig_search_paths() { std::vector paths; const std::string libdir = Void::Configuration::get_env_or("PKG_CONFIG_LIBDIR", ""); const std::string path_env = Void::Configuration::get_env_or("PKG_CONFIG_PATH", ""); for (const auto &entry : split_colon_list(path_env)) { paths.emplace_back(entry); } if (!libdir.empty()) { for (const auto &entry : split_colon_list(libdir)) { paths.emplace_back(entry); } return paths; } for (const char *fallback : {"/usr/local/lib/pkgconfig", "/usr/local/share/pkgconfig", "/usr/lib/pkgconfig", "/usr/lib/x86_64-linux-gnu/pkgconfig", "/usr/lib64/pkgconfig", "/usr/share/pkgconfig"}) { paths.emplace_back(fallback); } return paths; } Void::Result::Result resolve_pkgconfig(std::string_view dependency_name, std::string_view spec, const std::vector &search_paths) { const auto requirements = parse_module_list(spec); if (requirements.empty()) { return std::unexpected{make_error("empty pkg-config module spec")}; } std::map> resolved; std::set visiting; for (const auto &req : requirements) { if (auto ok = resolve_module(req, search_paths, resolved, visiting); !ok) { return std::unexpected{ok.error()}; } } Graph::ImportedTargetInterface iface; iface.dependency_name = std::string{dependency_name}; iface.target_name = requirements.front().name; for (const auto &[name, module] : resolved) { (void)name; for (const auto &flag : module.cflags) { if (flag.starts_with("-I") && flag.size() > 2) { iface.include_dirs.emplace_back(flag.substr(2), true); } else if (flag.starts_with("-isystem") && flag.size() > 8) { iface.include_dirs.emplace_back(flag.substr(8), true); } else if (flag.starts_with("-D") && flag.size() > 2) { iface.defines.push_back(flag.substr(2)); } else { iface.compile_options.push_back(flag); } } for (const auto &flag : module.libs) { if (flag.starts_with("-l") || flag.starts_with("-L") || flag.starts_with("-Wl,") || flag == "-pthread" || flag == "-ldl") { iface.link_options.push_back(flag); if (flag == "-pthread") { iface.compile_options.push_back(flag); } } else if (flag.ends_with(".a")) { iface.static_libraries.emplace_back(flag); } else if (flag.ends_with(".so") || flag.contains(".so.") || flag.ends_with(".dylib")) { iface.shared_libraries.emplace_back(flag); } else { iface.link_options.push_back(flag); } } } ProviderExport exported; exported.dependency_name = std::string{dependency_name}; exported.builder = "pkgconfig"; exported.targets[iface.target_name] = iface; exported.targets[std::string{dependency_name} + "::" + iface.target_name] = iface; return exported; } } // namespace Void::Build::Provider