feat: project.cpp declares its own dependencies with #pragma crafter dependency

A project.cpp could only import std and Crafter.Build, so anything shared
between build descriptions had to be a header every consumer copies. Now

    #pragma crafter dependency "<source>" [branch "<b>" | commit "<sha>"] [project "<file>"]

names a sibling crafter-build project that project.cpp itself imports a
module from. LoadProject scans for the directive, resolves it through
GitProject (a URL) or LocalProject (a path relative to the project file),
forces the result into host-tooling mode (the host triple LoadProject's
own cache is keyed on, native march, no debug, -fPIC so the archive links
into a shared object), builds it, and puts its BMIs and archive on the
project.so command line. The dependency's own project.cpp goes through
the same loader, so its directives resolve recursively; a cycle between
project files is an error instead of unbounded recursion.

The dependency is built on every load, fresh or not: clang crashes rather
than erroring when it imports a BMI whose own imported PCM was rebuilt
underneath it, and the module depfile — which Module::Check already reads —
is what notices the host PCM cache was regenerated. Its artifact mtime is
an input to project.so's staleness check, so editing the dependency
recompiles project.cpp without touching it. The Linux link gains
-fuse-ld=lld because release archives hold ThinLTO bitcode.

ProjectDependency(name) hands the same Configuration* back from inside
CrafterBuildProject, by configuration name or exported module name, so a
test that links the module needn't spell the source twice. It reads a
thread_local frame stack that an RAII guard keeps for the whole of
LoadProject, which also provides the cycle check.

ExpectedOutput() is the link gate's artifact-path lambda hoisted into the
:Clang partition, so the staleness check and the -r runner share the one
rule. Windows branches are wired identically but unverified.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Jorijn van der Graaf 2026-10-02 20:03:22 +02:00
commit b94139b67c
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
15 changed files with 618 additions and 45 deletions

View file

@ -265,6 +265,21 @@ fs::path Crafter::StdPcmDir(const Configuration& config) {
return GetCacheDir()/stdPcmKey;
}
fs::path Crafter::ExpectedOutput(const Configuration& config) {
const fs::path dir = config.BinDir();
const bool windowsTarget = config.target == "x86_64-w64-mingw32" || config.target == "x86_64-pc-windows-msvc";
if (config.type == ConfigurationType::Executable) {
if (config.target.starts_with("wasm32")) return dir / std::format("{}.wasm", config.outputName);
return config.target == "x86_64-w64-mingw32" ? dir / std::format("{}.exe", config.outputName) : dir / config.outputName;
}
if (config.type == ConfigurationType::LibraryStatic) {
return windowsTarget ? dir / std::format("{}.lib", config.outputName) : dir / std::format("lib{}.a", config.outputName);
}
// LibraryDynamic — the .dll on Windows targets so an mtime check sees the
// freshly linked library; on Unix the .so is that file.
return windowsTarget ? dir / std::format("{}.dll", config.outputName) : dir / std::format("lib{}.so", config.outputName);
}
CompileCommand Crafter::GetCompileCommand(const Configuration& config) {
CompileCommand out;
out.stdPcmDir = StdPcmDir(config);
@ -1042,34 +1057,13 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
// was rebuilt by an earlier run (so dep.repack is false this time around)
// but the consumer was never relinked against the new dep.
{
auto expectedOutputFor = [](const Configuration& c) -> fs::path {
fs::path dir = c.BinDir();
if (c.type == ConfigurationType::Executable) {
if (c.target.starts_with("wasm32")) return dir / (std::format("{}.wasm", c.outputName));
return c.target == "x86_64-w64-mingw32"
? dir / (std::format("{}.exe", c.outputName))
: dir / c.outputName;
}
if (c.type == ConfigurationType::LibraryStatic) {
return c.target == "x86_64-w64-mingw32" || c.target == "x86_64-pc-windows-msvc"
? dir / std::format("{}.lib", c.outputName)
: dir / std::format("lib{}.a", c.outputName);
}
// LibraryDynamic — point at the .dll on Windows targets so the
// mtime check sees the newly-built DLL; on Unix the .so suffices.
if (c.target == "x86_64-w64-mingw32" || c.target == "x86_64-pc-windows-msvc") {
return dir / std::format("{}.dll", c.outputName);
}
return dir / std::format("lib{}.so", c.outputName);
};
fs::path expected = expectedOutputFor(config);
fs::path expected = ExpectedOutput(config);
if (!fs::exists(expected)) {
buildResult.repack = true;
} else {
auto consumerMtime = fs::last_write_time(expected);
for (Configuration* dep : config.dependencies) {
fs::path depArtifact = expectedOutputFor(*dep);
fs::path depArtifact = ExpectedOutput(*dep);
if (fs::exists(depArtifact) && fs::last_write_time(depArtifact) > consumerMtime) {
buildResult.repack = true;
break;
@ -1786,13 +1780,7 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) {
return 1;
}
fs::path dir = config.BinDir();
fs::path artifact = dir / config.outputName;
if (config.target.starts_with("wasm32")) {
artifact += ".wasm";
} else if (config.target == "x86_64-w64-mingw32" || config.target == "x86_64-pc-windows-msvc") {
artifact += ".exe";
}
artifact = fs::absolute(artifact);
fs::path artifact = fs::absolute(ExpectedOutput(config));
fs::path absDir = fs::absolute(dir);
// wasm targets need either a wasm runtime (wasi-cli) or an HTTP