#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Void::Build::Bridge { namespace { using JsonVal = Void::Json::Strict::Value; Void::Result::Error make_error(std::string msg) { return Void::System::make_system_error(Void::System::SystemErrorCode::invalid_argument, std::move(msg)); } const JsonVal *find_key(const JsonVal::Object &obj, const std::string &k) { const auto it = obj.find(k); return it == obj.end() ? nullptr : &it->second; } std::string get_str(const JsonVal::Object &obj, const std::string &k) { const auto *v = find_key(obj, k); if (!v) { return {}; } const auto *s = std::get_if(&v->value); return s ? *s : std::string{}; } bool get_bool(const JsonVal::Object &obj, const std::string &k, bool def = false) { const auto *v = find_key(obj, k); if (!v) { return def; } const auto *b = std::get_if(&v->value); return b ? *b : def; } std::string escape_cmake(std::string_view s) { std::string out; for (const char c : s) { if (c == '\\' || c == '"' || c == '$' || c == ';') { out.push_back('\\'); } out.push_back(c); } return out; } std::string emit_imported_target_cmake(const std::string &cmake_name, const Graph::ImportedTargetInterface &iface) { std::string cm_type = "INTERFACE"; std::filesystem::path loc; if (!iface.static_libraries.empty()) { cm_type = "STATIC"; loc = iface.static_libraries.front(); } else if (!iface.shared_libraries.empty()) { cm_type = "SHARED"; loc = iface.shared_libraries.front(); } std::string out; out += "if(NOT TARGET " + cmake_name + ")\n"; out += " add_library(" + cmake_name + " " + cm_type + " IMPORTED GLOBAL)\n"; if (!loc.empty()) { out += " set_property(TARGET " + cmake_name + " PROPERTY IMPORTED_LOCATION \"" + escape_cmake(loc.string()) + "\")\n"; } if (!iface.include_dirs.empty()) { out += " set_property(TARGET " + cmake_name + " PROPERTY INTERFACE_INCLUDE_DIRECTORIES"; for (const auto &[inc, is_sys] : iface.include_dirs) { (void)is_sys; out += " \"" + escape_cmake(inc.string()) + "\""; } out += ")\n"; } if (!iface.defines.empty()) { out += " set_property(TARGET " + cmake_name + " PROPERTY INTERFACE_COMPILE_DEFINITIONS"; for (const auto &def : iface.defines) { out += " \"" + escape_cmake(def) + "\""; } out += ")\n"; } if (!iface.compile_options.empty()) { out += " set_property(TARGET " + cmake_name + " PROPERTY INTERFACE_COMPILE_OPTIONS"; for (const auto &opt : iface.compile_options) { out += " \"" + escape_cmake(opt) + "\""; } out += ")\n"; } // Preserve extra transitive static/shared libraries after the primary location! std::vector extra_libs; for (std::size_t i = (cm_type == "STATIC" ? 1 : 0); i < iface.static_libraries.size(); ++i) { extra_libs.push_back(iface.static_libraries[i].string()); } for (std::size_t i = (cm_type == "SHARED" ? 1 : 0); i < iface.shared_libraries.size(); ++i) { extra_libs.push_back(iface.shared_libraries[i].string()); } for (const auto &lopt : iface.link_options) { extra_libs.push_back(lopt); } if (!extra_libs.empty()) { out += " set_property(TARGET " + cmake_name + " PROPERTY INTERFACE_LINK_LIBRARIES"; for (const auto &lib : extra_libs) { out += " \"" + escape_cmake(lib) + "\""; } out += ")\n"; } out += "endif()\n"; return out; } } // namespace Void::Result::Result execute_cmake_bridge(const std::filesystem::path &request_json_path, const std::filesystem::path &output_cmake_path) { auto text = Void::FileSystem::read_file(request_json_path); if (!text) { return std::unexpected{text.error()}; } auto parsed = Void::Json::Strict::parse(*text); if (!parsed) { return std::unexpected{make_error("bridge request JSON is malformed")}; } const auto *root = std::get_if(&parsed->value); if (!root) { return std::unexpected{make_error("bridge request root must be a JSON object")}; } const std::filesystem::path project_root = get_str(*root, "project_root"); const std::filesystem::path build_root = get_str(*root, "build_root"); const std::string profile_name = get_str(*root, "profile"); const std::string compiler_str = get_str(*root, "compiler"); const std::string compiler_path = get_str(*root, "compiler_path"); const std::string os_name = get_str(*root, "os"); const std::string arch_name = get_str(*root, "architecture"); const bool offline = get_bool(*root, "offline", false); // Reentry cycle guard (Plan 14: prevent infinite nested bridge recursion / lock deadlock). std::string active_stack; if (const char *env = std::getenv("VBS_BRIDGE_STACK")) { active_stack = env; } const std::string self_marker = "|" + project_root.string() + "|"; if (active_stack.find(self_marker) != std::string::npos) { return std::unexpected{make_error(std::format( "reentrant VbsDeps bridge cycle detected for project '{}'", project_root.string()))}; } ProfileContext ctx = default_profiles().resolve( profile_name.empty() ? "debug" : profile_name, parse_compiler(compiler_str.empty() ? "gcc" : compiler_str), os_name.empty() ? Toolchain::host_os_name() : os_name, arch_name.empty() ? Toolchain::host_architecture_name() : arch_name); ctx.compiler_path = compiler_path; auto tc = Toolchain::discover_and_probe(ctx, build_root / ".vbs" / "probes"); if (!tc) { return std::unexpected{tc.error()}; } std::string generated_cmake = "# Generated by VoidBuildSystem CMake bridge (Plan 14)\n"; std::set emitted_names; const auto *deps_v = find_key(*root, "dependencies"); const auto *deps_arr = deps_v ? std::get_if(&deps_v->value) : nullptr; if (!deps_arr) { return std::unexpected{make_error("bridge request missing 'dependencies' array")}; } for (const auto &dv : *deps_arr) { const auto *dobj = std::get_if(&dv.value); if (!dobj) { continue; } DependencyRecord dep; dep.name = get_str(*dobj, "alias"); dep.builder = get_str(*dobj, "builder"); dep.registry_authority = get_str(*dobj, "registry"); if (const auto git = get_str(*dobj, "git"); !git.empty()) { dep.source_kind = DependencySourceKind::Git; dep.location = git; dep.revision = get_str(*dobj, "rev"); } else if (const auto pth = get_str(*dobj, "path"); !pth.empty()) { dep.source_kind = DependencySourceKind::Path; dep.location = pth; } else if (const auto arc = get_str(*dobj, "archive"); !arc.empty()) { dep.source_kind = DependencySourceKind::Archive; dep.location = arc; dep.sha256 = get_str(*dobj, "sha256"); } else if (const auto pkg = get_str(*dobj, "package"); !pkg.empty()) { dep.source_kind = DependencySourceKind::Package; dep.package = pkg; dep.version = get_str(*dobj, "version"); } if (const auto *ov = find_key(*dobj, "options")) { if (const auto *oo = std::get_if(&ov->value)) { for (const auto &[k, val] : *oo) { if (const auto *s = std::get_if(&val.value)) { if (*s == "ON" || *s == "TRUE" || *s == "true") { dep.options.emplace(k, OptionValue{true}); } else if (*s == "OFF" || *s == "FALSE" || *s == "false") { dep.options.emplace(k, OptionValue{false}); } else { dep.options.emplace(k, OptionValue{*s}); } } } } } Package::FetchOptions fopts; fopts.cache_dir = build_root / ".vbs" / "cache"; fopts.project_root = project_root; fopts.offline = offline; auto mat = Package::materialize_dependency_source(dep, fopts); if (!mat) { return std::unexpected{mat.error()}; } // Auto-detect builder if unspecified. if (dep.builder.empty()) { std::error_code ec; if (std::filesystem::is_regular_file(mat->source_dir / "build.cpp", ec)) { dep.builder = "native"; } else if (std::filesystem::is_regular_file(mat->source_dir / "CMakeLists.txt", ec)) { dep.builder = "cmake"; } else if (std::filesystem::is_regular_file(mat->source_dir / "meson.build", ec)) { dep.builder = "meson"; } } if (dep.builder == "cmake" || dep.builder == "meson" || dep.builder == "conan") { Provider::ProviderRequest preq; preq.dependency = dep; preq.source = *mat; preq.profile = ctx; preq.toolchain = *tc; preq.provider_build_root = build_root / ".vbs" / "providers"; preq.mode = Provider::ProviderMode::FullBuild; auto exp = Provider::resolve_foreign_provider(preq); if (!exp) { return std::unexpected{exp.error()}; } for (const auto &[tname, iface] : exp->targets) { if (emitted_names.insert(tname).second) { generated_cmake += emit_imported_target_cmake(tname, iface); } } } else { // Native VBS package source: check exported package.json or include/ layout. Graph::ImportedTargetInterface iface; iface.dependency_name = dep.name; iface.target_name = dep.name; std::error_code ec; if (std::filesystem::is_directory(mat->source_dir / "include", ec)) { iface.include_dirs.emplace_back(mat->source_dir / "include", false); } const std::string alias_tgt = dep.name + "::" + dep.name; if (emitted_names.insert(alias_tgt).second) { generated_cmake += emit_imported_target_cmake(alias_tgt, iface); } } } return Platform::atomic_write_file(output_cmake_path, generated_cmake, true); } } // namespace Void::Build::Bridge