diff --git a/implementations/Crafter.Build-External.cpp b/implementations/Crafter.Build-External.cpp index 3fb8fe9..0a0c7a2 100644 --- a/implementations/Crafter.Build-External.cpp +++ b/implementations/Crafter.Build-External.cpp @@ -348,17 +348,28 @@ namespace { // absolute so Build() can run from any cwd without misresolving them. // The mutex is NOT held across LoadProject — the dep may itself call // GitProject/LocalProject, and a non-recursive lock would deadlock. - Configuration LoadProjectFromRoot(const fs::path& projectPath, const fs::path& depRoot, std::span args) { + // + // The Configuration is made on the heap with the call as its + // initialiser, so it is constructed where the cache keeps it and is + // never moved. A project file takes pointers to the Configuration it + // returns, for a test that links the library it describes + // (cfg.AddTest(...).Dependencies({ &cfg })), and they are pointers to + // this object when the compiler constructed the local the file returns + // in place of the return value, as clang does for a function that + // returns one local. Assigned to a local here and moved into the cache + // from there, the Configuration left every such pointer behind. + std::unique_ptr LoadProjectFromRoot(const fs::path& projectPath, const fs::path& depRoot, std::span args) { fs::path savedCwd = fs::current_path(); - Configuration cfg; + std::unique_ptr made; fs::current_path(depRoot); try { - cfg = LoadProject(projectPath, args); + made.reset(new Configuration(LoadProject(projectPath, args))); } catch (...) { fs::current_path(savedCwd); throw; } fs::current_path(savedCwd); + Configuration& cfg = *made; cfg.path = fs::absolute(depRoot / cfg.path).lexically_normal(); auto abs = [&](const fs::path& p) -> fs::path { return p.is_absolute() ? p : fs::absolute(depRoot / p).lexically_normal(); @@ -378,20 +389,19 @@ namespace { for (auto& entry : cfg.flatpak.share) { if (!entry.source.empty()) entry.source = abs(entry.source); } - return cfg; + return made; } // Into ProjectCache under `fullHash`, with `loaded` recorded beside it. // When another thread stored the same spec first, its Configuration // wins and `cfg` is dropped. - Configuration* StoreProject(std::string fullHash, Configuration cfg, LoadedProject loaded) { + Configuration* StoreProject(std::string fullHash, std::unique_ptr cfg, LoadedProject loaded) { std::lock_guard lock(ProjectCacheMutex); if (auto it = ProjectCache.find(fullHash); it != ProjectCache.end()) { return it->second.get(); } - auto stored = std::make_unique(std::move(cfg)); - Configuration* ptr = stored.get(); - ProjectCache.emplace(std::move(fullHash), std::move(stored)); + Configuration* ptr = cfg.get(); + ProjectCache.emplace(std::move(fullHash), std::move(cfg)); LoadedProjects.emplace(ptr, std::move(loaded)); return ptr; } @@ -461,7 +471,7 @@ Configuration* Crafter::GitProject(const GitProjectSpec& spec) { } std::vector argViews(spec.args.begin(), spec.args.end()); - Configuration cfg = LoadProjectFromRoot(projectPath, cloneDir, argViews); + std::unique_ptr cfg = LoadProjectFromRoot(projectPath, cloneDir, argViews); const fs::path projectInRepo = spec.projectFile.lexically_normal(); const fs::path below = projectInRepo.parent_path(); @@ -522,7 +532,7 @@ Configuration* Crafter::LocalProject(const LocalProjectSpec& spec) { fs::path depRoot = canonical.parent_path(); std::vector argViews(spec.args.begin(), spec.args.end()); - Configuration cfg = LoadProjectFromRoot(canonical, depRoot, argViews); + std::unique_ptr cfg = LoadProjectFromRoot(canonical, depRoot, argViews); return StoreProject(std::move(fullHash), std::move(cfg), { .projectFile = canonical,