#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { namespace Modules = vls::modules; namespace Build = Modules::Build; namespace Protocol = Void::LanguageServer::Protocol; namespace fs = std::filesystem; using Json = Protocol::Json; constexpr std::size_t max_manifest_bytes = 2 * 1024 * 1024; const Json::Object &object(const Json &value) { if (const auto *fields = Protocol::as_object(&value)) return *fields; throw std::runtime_error("expected an object"); } const Json &field(const Json::Object &object, std::string_view name) { if (const auto *value = Protocol::find_member(object, name)) return *value; throw std::runtime_error("missing field " + std::string(name)); } const std::string &string(const Json &value, std::size_t limit = 4096) { if (const auto *text = Protocol::as_string(&value); text && text->size() <= limit) return *text; throw std::runtime_error("expected a bounded string"); } std::vector strings(const Json &value, std::size_t limit) { const auto *items = Protocol::as_array(&value); if (!items || items->size() > limit) throw std::runtime_error("expected a bounded array"); std::vector result; for (const auto &item : *items) result.push_back(string(item)); return result; } struct Unit { std::string alternative; Modules::ModuleCommand command; Modules::UnitInputs inputs; }; Json run(const Json::Object &manifest) { if (const auto *mode = Protocol::as_string(Protocol::find_member(manifest, "mode")); mode && *mode == "scan") { namespace Sched = vls::scheduler; const fs::path directory = string(field(manifest, "directory")); const fs::path working = string(field(manifest, "working_directory")); const auto command = strings(field(manifest, "command"), 1024); const auto &output_fields = object(field(manifest, "output")); const Modules::ExpectedOutput output{string(field(output_fields, "working_directory")), string(field(output_fields, "spelling"))}; auto product = std::make_shared(); Sched::Scheduler pool{Sched::Budget{.foreground = 1, .background = 1, .artifact = 1, .queued = 4, .memory_mib = 64, .watchdog = std::chrono::seconds{120}}}; Modules::ModuleWorkQueue queue{pool}; std::string failure; auto work = Modules::gcc_module_scan_work( [&](const Build::ContextKey &) -> std::optional { return Modules::ScanCommand{working, command, output}; }, Build::ContextKey{{}, {}, Build::ContextRole::target, "scan", working / "unit.cpp"}, product, Modules::ScanOptions{directory, std::chrono::milliseconds{120000}, Modules::max_scan_bytes}); if (!queue.submit("scan-document", Modules::ModuleWorkKind::scan, 1, std::move(work), [&](Modules::ModuleTaskResult result) { if (result.outcome != Sched::Outcome::completed) failure = result.detail; })) throw std::runtime_error("scan admission was refused"); pool.drain(); if (!failure.empty()) throw std::runtime_error(failure); Json::Array provides, imports; for (const auto &module : product->rule->provides()) provides.push_back(Json{module.logical_name}); for (const auto &module : product->rule->requirements()) imports.push_back(Json{module.logical_name}); std::string generation; if (!product->rule->provides().empty()) { Modules::ArtifactCache cache{directory / "artifacts"}; Modules::ModulePlan plan; plan.domain.compatibility = "gcc-corpus/x86_64"; plan.claims_complete = true; const Build::ContextKey context{{}, {}, Build::ContextRole::target, "artifact", working / "artifact.cpp"}; const Modules::UnitInputs inputs{"artifact-command", "artifact-source", "artifact-closure", {}}; plan.units.push_back({{context, {working, "artifact.o"}, inputs, "artifact-scan", {}}, Modules::UnitKind::primary_interface, {}}); plan.order.push_back(context); Modules::ModuleCommand artifact_command{ working, {command.front(), "-std=c++26", "-fmodules-ts", "-c", command.back(), "-o", (directory / "module.o").native()}}; auto produced = Modules::produce_module_artifacts_scheduled( plan, cache, directory / "artifact-staging", Modules::gcc_module_artifact_work( [&](const Build::ContextKey &) -> std::optional { return artifact_command; }), pool); if (!produced) throw std::runtime_error(produced.error()); generation = (*produced)->id(); } return Json{Json::Object{{"p1689", Json{product->p1689}}, {"provides", Json{std::move(provides)}}, {"requires", Json{std::move(imports)}}, {"generation", Json{std::move(generation)}}}}; } const fs::path staging = string(field(manifest, "staging")); const fs::path cache_path = string(field(manifest, "cache")); const std::string &compatibility = string(field(manifest, "compatibility")); std::vector units; for (const auto &item : *Protocol::as_array(&field(manifest, "units"))) { const auto &entry = object(item); Unit unit; unit.alternative = string(field(entry, "alternative")); unit.command.working_directory = string(field(entry, "working_directory")); unit.command.arguments = strings(field(entry, "command"), 1024); unit.inputs = {string(field(entry, "command_id")), string(field(entry, "source_id")), string(field(entry, "textual_closure")), std::nullopt}; units.push_back(std::move(unit)); } if (units.empty() || units.size() > 64) throw std::runtime_error("expected a bounded unit list"); Modules::ModulePlan plan; plan.domain.compatibility = compatibility; plan.claims_complete = true; std::map commands; for (const auto &unit : units) { Build::ContextKey context{{}, {}, Build::ContextRole::target, unit.alternative, fs::path{"/fixture"} / (unit.alternative + ".cpp")}; Modules::CensusMember member{context, {"/fixture", unit.alternative + ".o"}, unit.inputs, "scan-" + unit.alternative, {}}; plan.units.push_back({member, Modules::UnitKind::primary_interface, {}}); plan.order.push_back(context); commands.emplace(unit.alternative, unit.command); } Modules::ArtifactCache cache{cache_path}; const Modules::ModuleCommandLookup lookup = [&](const Build::ContextKey &context) -> std::optional { const auto command = commands.find(context.alternative); if (command == commands.end()) return std::nullopt; return command->second; }; if (Protocol::as_bool(Protocol::find_member(manifest, "supersede")).value_or(false)) { namespace Sched = vls::scheduler; Sched::Scheduler pool{Sched::Budget{.foreground = 1, .background = 1, .artifact = 1, .queued = 8, .memory_mib = 1024, .watchdog = std::chrono::seconds{120}}}; std::latch started{1}; std::mutex mutex; std::condition_variable changed; bool release{}; const Modules::ArtifactWork real = Modules::gcc_module_artifact_work(lookup); std::expected, std::string> superseded = std::unexpected("not run"); std::thread producer{[&] { superseded = Modules::produce_module_artifacts_scheduled( plan, cache, staging, [&](std::stop_token stop, const Build::ContextKey &context, const fs::path &output) -> std::expected { started.count_down(); std::unique_lock lock{mutex}; changed.wait(lock, [&] { return release; }); if (stop.stop_requested()) return std::unexpected("cancelled"); return real(stop, context, output); }, pool); }}; started.wait(); if (!pool.submit( {.priority = Sched::Priority::artifact, .key = units.front().alternative, .revision = 2, .memory_mib = 0}, [](std::stop_token) { return std::string{}; }, [](Sched::Result) {})) { release = true; changed.notify_all(); producer.join(); throw std::runtime_error("superseding submission was refused"); } { std::lock_guard lock{mutex}; release = true; changed.notify_all(); } producer.join(); if (superseded) throw std::runtime_error("a superseded module build published a generation"); if (cache.current() || cache.generations() != 0) throw std::runtime_error("a superseded module build left artifacts behind"); auto clean = Modules::produce_module_artifacts_scheduled( plan, cache, staging, Modules::gcc_module_artifact_work(lookup), pool); if (!clean) throw std::runtime_error(clean.error()); return Json{Json::Object{{"superseded", Json{true}}, {"published", Json{false}}, {"clean_generation", Json{(*clean)->id()}}}}; } auto produced = Modules::produce_module_artifacts(plan, cache, staging, Modules::gcc_module_artifact_work(lookup)); if (!produced) throw std::runtime_error(produced.error()); Json::Array artifacts; for (const auto &unit : units) { const Modules::ArtifactKey key{compatibility, unit.alternative, unit.inputs.command + "|" + unit.inputs.source + "|" + unit.inputs.textual_closure + "|" + unit.inputs.system_closure.value_or(""), "cmi"}; const auto entry = (*produced)->find(key); if (!entry) throw std::runtime_error("published generation is missing " + unit.alternative); artifacts.push_back(Json{Json::Object{{"alternative", Json{unit.alternative}}, {"path", Json{entry->path.native()}}, {"sha256", Json{entry->sha256}}}}); } return Json{Json::Object{{"generation", Json{(*produced)->id()}}, {"artifacts", Json{std::move(artifacts)}}}}; } } // namespace int main() { try { std::string bytes; char buffer[4096]; while (std::cin) { std::cin.read(buffer, sizeof buffer); bytes.append(buffer, static_cast(std::cin.gcount())); if (bytes.size() > max_manifest_bytes) throw std::runtime_error("fixture manifest too large"); } if (std::cin.bad()) throw std::runtime_error("fixture manifest read failed"); const auto input = Protocol::parse_json(bytes, 8); if (!input) throw std::runtime_error(input.error()); const auto output = Protocol::serialize_json(run(object(*input))); if (!output) throw std::runtime_error(output.error()); std::cout << *output << '\n'; return std::cout ? 0 : 2; } catch (const std::exception &error) { std::cerr << "vls_module_artifact_fixture: " << error.what() << '\n'; return 2; } }