[M6] brute-force probe + INTERFACE IMPORTED target codegen

This commit is contained in:
2026-05-15 14:26:06 +00:00
parent 01b3c28d6c
commit 94e658fdf1
10 changed files with 250 additions and 57 deletions

View File

@@ -89,7 +89,34 @@ auto emit_find_packages(const std::vector<linkdb::Recipe>& recipes,
bool pkgconfig_emitted = false;
auto emit_one = [&](const linkdb::Recipe& r) {
if (r.pkg_config_module && !r.pkg_config_module->empty()) {
if (!r.brute_force_libs.empty() || !r.brute_force_includes.empty()) {
// Synthesize a single INTERFACE IMPORTED target named after
// the first entry in `targets` (e.g. `<pkg>::<pkg>`). No
// find_package — every artifact path is baked in.
if (r.targets.empty()) {
return;
}
const auto& target = r.targets.front();
out += std::format("add_library({} INTERFACE IMPORTED)\n", target);
if (!r.brute_force_libs.empty()) {
out += std::format("set_property(TARGET {} APPEND PROPERTY "
"INTERFACE_LINK_LIBRARIES",
target);
for (const auto& l : r.brute_force_libs) {
out += std::format("\n \"{}\"", l);
}
out += ")\n";
}
if (!r.brute_force_includes.empty()) {
out += std::format("set_property(TARGET {} APPEND PROPERTY "
"INTERFACE_INCLUDE_DIRECTORIES",
target);
for (const auto& i : r.brute_force_includes) {
out += std::format("\n \"{}\"", i);
}
out += ")\n";
}
} else if (r.pkg_config_module && !r.pkg_config_module->empty()) {
if (!pkgconfig_emitted) {
out += "find_package(PkgConfig REQUIRED)\n";
pkgconfig_emitted = true;

View File

@@ -65,6 +65,8 @@ auto Database::resolve(const std::string& package, const std::string& version,
.targets = row.targets,
.source = row.source,
.pkg_config_module = row.pkg_config_module,
.brute_force_libs = row.brute_force_libs,
.brute_force_includes = row.brute_force_includes,
};
}
return std::unexpected(util::Error{

View File

@@ -25,6 +25,14 @@ struct Recipe {
// overlay rows don't need a sentinel.
std::optional<std::string> pkg_config_module;
// Set by the brute-force probe (the last-resort discover stage).
// When non-empty, codegen skips `find_package(...)` and instead
// synthesizes an INTERFACE IMPORTED target named in `targets[0]`
// (which is `<pkg>::<pkg>`) with these absolute lib paths +
// include dirs.
std::vector<std::string> brute_force_libs;
std::vector<std::string> brute_force_includes;
bool operator==(const Recipe&) const = default;
};
@@ -40,6 +48,8 @@ struct OverlayRow {
std::string source;
std::int64_t verified_at = 0;
std::optional<std::string> pkg_config_module;
std::vector<std::string> brute_force_libs;
std::vector<std::string> brute_force_includes;
};
// RAII wrapper for an open sqlite3 connection used by the overlay database.

View File

@@ -90,24 +90,40 @@ auto overlay_open(const std::filesystem::path& path)
});
}
// Schema migration: legacy overlays predate pkg_config_module.
// SQLite ADD COLUMN errors when the column already exists; treat
// "duplicate column" as success.
constexpr const char* MIGRATE_PC =
"ALTER TABLE recipes ADD COLUMN pkg_config_module TEXT";
char* mig_err = nullptr;
if (sqlite3_exec(state->handle(), MIGRATE_PC, nullptr, nullptr, &mig_err) !=
SQLITE_OK) {
if (mig_err && std::string_view{mig_err}.find("duplicate column") ==
std::string_view::npos) {
std::string msg = std::format("cannot migrate overlay schema: {}",
mig_err ? mig_err : "?");
// Schema migrations. SQLite ADD COLUMN errors when the column
// already exists; treat "duplicate column" as success.
auto add_column = [&](const char* sql) -> util::Result<void> {
char* mig_err = nullptr;
if (sqlite3_exec(state->handle(), sql, nullptr, nullptr, &mig_err) !=
SQLITE_OK) {
if (mig_err && std::string_view{mig_err}.find("duplicate column") ==
std::string_view::npos) {
std::string msg = std::format("cannot migrate overlay schema: {}",
mig_err ? mig_err : "?");
sqlite3_free(mig_err);
return std::unexpected(util::Error{
util::ErrorCode::LinkdbCorrupt, std::move(msg), "", path,
std::nullopt,
});
}
sqlite3_free(mig_err);
return std::unexpected(util::Error{
util::ErrorCode::LinkdbCorrupt, std::move(msg), "", path, std::nullopt,
});
}
sqlite3_free(mig_err);
return {};
};
if (auto r = add_column(
"ALTER TABLE recipes ADD COLUMN pkg_config_module TEXT");
!r) {
return std::unexpected(r.error());
}
if (auto r = add_column(
"ALTER TABLE recipes ADD COLUMN brute_force_libs TEXT");
!r) {
return std::unexpected(r.error());
}
if (auto r = add_column(
"ALTER TABLE recipes ADD COLUMN brute_force_includes TEXT");
!r) {
return std::unexpected(r.error());
}
return state;
@@ -119,8 +135,8 @@ auto overlay_insert_manual(OverlayState& state, const std::string& package,
constexpr const char* SQL =
"INSERT OR REPLACE INTO recipes "
"(package, version_range, nixpkgs_attr, find_package, targets, components, source, "
" verified_at, pkg_config_module) "
"VALUES (?, ?, ?, ?, ?, NULL, 'manual', ?, ?)";
" verified_at, pkg_config_module, brute_force_libs, brute_force_includes) "
"VALUES (?, ?, ?, ?, ?, NULL, 'manual', ?, ?, ?, ?)";
sqlite3* db = state.handle();
sqlite3_stmt* stmt = nullptr;
@@ -129,6 +145,8 @@ auto overlay_insert_manual(OverlayState& state, const std::string& package,
}
auto targets_str = nlohmann::json(r.targets).dump();
auto libs_str = nlohmann::json(r.brute_force_libs).dump();
auto incs_str = nlohmann::json(r.brute_force_includes).dump();
auto now = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
@@ -144,6 +162,8 @@ auto overlay_insert_manual(OverlayState& state, const std::string& package,
} else {
sqlite3_bind_null(stmt, 7);
}
sqlite3_bind_text(stmt, 8, libs_str.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 9, incs_str.c_str(), -1, SQLITE_TRANSIENT);
auto rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
@@ -162,8 +182,8 @@ auto overlay_insert(OverlayState& state, const std::string& package,
constexpr const char* SQL =
"INSERT OR REPLACE INTO recipes "
"(package, version_range, nixpkgs_attr, find_package, targets, components, source, "
" verified_at, pkg_config_module) "
"VALUES (?, ?, ?, ?, ?, NULL, ?, ?, ?)";
" verified_at, pkg_config_module, brute_force_libs, brute_force_includes) "
"VALUES (?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?)";
sqlite3* db = state.handle();
sqlite3_stmt* stmt = nullptr;
@@ -172,6 +192,8 @@ auto overlay_insert(OverlayState& state, const std::string& package,
}
auto targets_str = nlohmann::json(r.targets).dump();
auto libs_str = nlohmann::json(r.brute_force_libs).dump();
auto incs_str = nlohmann::json(r.brute_force_includes).dump();
sqlite3_bind_text(stmt, 1, package.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, version_range.c_str(), -1, SQLITE_TRANSIENT);
@@ -185,6 +207,8 @@ auto overlay_insert(OverlayState& state, const std::string& package,
} else {
sqlite3_bind_null(stmt, 8);
}
sqlite3_bind_text(stmt, 9, libs_str.c_str(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 10, incs_str.c_str(), -1, SQLITE_TRANSIENT);
auto rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
@@ -276,7 +300,7 @@ auto overlay_query(OverlayState& state, const std::string& package)
-> util::Result<std::vector<OverlayRow>> {
constexpr const char* SQL =
"SELECT version_range, nixpkgs_attr, find_package, targets, source, verified_at, "
" pkg_config_module "
" pkg_config_module, brute_force_libs, brute_force_includes "
"FROM recipes WHERE package = ?";
sqlite3* db = state.handle();
@@ -310,6 +334,23 @@ auto overlay_query(OverlayState& state, const std::string& package)
if (sqlite3_column_type(stmt, 6) != SQLITE_NULL) {
row.pkg_config_module = column_text(stmt, 6);
}
auto parse_str_array = [&](int col, std::vector<std::string>& out_arr) {
if (sqlite3_column_type(stmt, col) == SQLITE_NULL) {
return;
}
try {
auto txt = column_text(stmt, col);
if (txt.empty()) {
return;
}
out_arr =
nlohmann::json::parse(txt).get<std::vector<std::string>>();
} catch (const nlohmann::json::exception&) {
// legacy/manual rows may have stored garbage; ignore
}
};
parse_str_array(7, row.brute_force_libs);
parse_str_array(8, row.brute_force_includes);
out.push_back(std::move(row));
}
sqlite3_finalize(stmt);

View File

@@ -0,0 +1,81 @@
module cargoxx.resolver;
import std;
import cargoxx.util;
namespace cargoxx::resolver {
namespace fs = std::filesystem;
namespace {
auto is_lib_filename(const fs::path& p) -> bool {
auto name = p.filename().string();
if (!name.starts_with("lib")) {
return false;
}
auto ext = p.extension().string();
if (ext == ".a") {
return true;
}
if (ext == ".so" || ext == ".dylib") {
return true;
}
// .so.<N>, .so.<N>.<M>, ... — common shared-lib versioning. Use a
// looser check: if the name contains ".so." or ".dylib." anywhere
// after the lib prefix, accept it.
return name.find(".so.") != std::string::npos ||
name.find(".dylib.") != std::string::npos;
}
} // namespace
auto brute_scan(const fs::path& store_path, const std::string& package_name)
-> util::Result<BruteCandidate> {
if (package_name.empty()) {
return std::unexpected(util::Error{
util::ErrorCode::ResolutionUnknownPackage,
"brute_scan: package name is empty",
"", std::nullopt, std::nullopt,
});
}
const auto lib_dir = store_path / "lib";
const auto include_dir = store_path / "include";
BruteCandidate out;
std::error_code ec;
if (fs::exists(lib_dir, ec) && !ec) {
for (const auto& entry : fs::directory_iterator{
lib_dir, fs::directory_options::skip_permission_denied, ec}) {
if (!entry.is_regular_file() && !entry.is_symlink()) {
continue;
}
if (!is_lib_filename(entry.path())) {
continue;
}
out.lib_files.push_back(entry.path().string());
}
std::ranges::sort(out.lib_files);
}
if (fs::exists(include_dir, ec) && !ec) {
// For include/, expose the top-level directory itself (e.g.
// `<store>/include`) — that's what `#include <pkg/foo.h>`
// expects. Adding every subdir would also work, but is noisier
// and provokes name collisions across deps.
out.include_dirs.push_back(include_dir.string());
}
if (out.lib_files.empty() && out.include_dirs.empty()) {
return std::unexpected(util::Error{
util::ErrorCode::ResolutionUnknownPackage,
std::format("no libs or headers under '{}'", store_path.string()),
"", store_path, std::nullopt,
});
}
return out;
}
} // namespace cargoxx::resolver

View File

@@ -89,6 +89,20 @@ auto recipe_from_findmodule(const FindModuleCandidate& fm,
};
}
auto recipe_from_brute(const BruteCandidate& b, const std::string& name,
const std::string& nixpkgs_attr, const std::string& source)
-> linkdb::Recipe {
return linkdb::Recipe{
.nixpkgs_attr = nixpkgs_attr,
// No find_package — codegen synthesizes the target directly.
.find_package = "",
.targets = {std::format("{}::{}", name, name)},
.source = source,
.brute_force_libs = b.lib_files,
.brute_force_includes = b.include_dirs,
};
}
struct Candidate {
std::string source;
linkdb::Recipe recipe;
@@ -183,6 +197,12 @@ auto discover(const std::string& name, const std::string& version_spec,
candidates.push_back(
{"pkg-config", recipe_from_pc(*pc_hit, name, "pkg-config")});
}
if (!realized_dev_path.empty()) {
if (auto b = brute_scan(fs::path{realized_dev_path}, name); b) {
candidates.push_back(
{"brute-force", recipe_from_brute(*b, name, name, "brute-force")});
}
}
if (candidates.empty()) {
return std::unexpected(error(

View File

@@ -110,6 +110,19 @@ auto pc_scan(const std::filesystem::path& store_path,
const std::string& package_name)
-> util::Result<PcCandidate>;
// Last-resort brute-force: every shared/static lib + every include
// directory under the store path is wrapped in a synthetic
// `<pkg>::<pkg>` INTERFACE IMPORTED target. Used when nothing more
// structured matched.
struct BruteCandidate {
std::vector<std::string> lib_files; // abs paths to lib*.{a,so,dylib}
std::vector<std::string> include_dirs; // abs paths under include/
};
auto brute_scan(const std::filesystem::path& store_path,
const std::string& package_name)
-> util::Result<BruteCandidate>;
// Output of a conan-center-index recipe scrape.
struct ConanRecipe {
std::string find_package; // e.g. "fmt CONFIG REQUIRED"