fix(external): keep a sibling project's Configuration where it was built
A project file may take pointers to the Configuration it returns. The
usual one belongs to a test that links the library the file describes,
cfg.AddTest("T").Dependencies({ &cfg }): where the compiler constructs
the local the file returns in place of the return value, &cfg is the
address of what CrafterBuildProject returns.
For the project crafter-build is pointed at, that address stays good:
Run() initialises its Configuration with the call. For a project loaded
through GitProject or LocalProject it did not. LoadProjectFromRoot
assigned the call's result to a local, and from there the Configuration
was moved into the project cache, so what a consumer got was not the
object the project file had built, and every pointer the file had taken
to that one pointed at an object that no longer existed. RunLint follows
such a pointer: for a sibling whose path lies inside the project's
directory it works out the compile command of each of the sibling's
tests when a rule needs an AST, and GetCompileCommand walks the
dependencies of the configuration it is given.
LoadProjectFromRoot now makes the Configuration on the heap with the
call as its initialiser, and StoreProject takes the unique_ptr, so the
Configuration is constructed where the cache keeps it and is never
moved.
This commit is contained in:
parent
12e8b908f6
commit
8baf14068e
1 changed files with 20 additions and 10 deletions
|
|
@ -348,17 +348,28 @@ namespace {
|
||||||
// absolute so Build() can run from any cwd without misresolving them.
|
// absolute so Build() can run from any cwd without misresolving them.
|
||||||
// The mutex is NOT held across LoadProject — the dep may itself call
|
// The mutex is NOT held across LoadProject — the dep may itself call
|
||||||
// GitProject/LocalProject, and a non-recursive lock would deadlock.
|
// GitProject/LocalProject, and a non-recursive lock would deadlock.
|
||||||
Configuration LoadProjectFromRoot(const fs::path& projectPath, const fs::path& depRoot, std::span<const std::string_view> 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<Configuration> LoadProjectFromRoot(const fs::path& projectPath, const fs::path& depRoot, std::span<const std::string_view> args) {
|
||||||
fs::path savedCwd = fs::current_path();
|
fs::path savedCwd = fs::current_path();
|
||||||
Configuration cfg;
|
std::unique_ptr<Configuration> made;
|
||||||
fs::current_path(depRoot);
|
fs::current_path(depRoot);
|
||||||
try {
|
try {
|
||||||
cfg = LoadProject(projectPath, args);
|
made.reset(new Configuration(LoadProject(projectPath, args)));
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
fs::current_path(savedCwd);
|
fs::current_path(savedCwd);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
fs::current_path(savedCwd);
|
fs::current_path(savedCwd);
|
||||||
|
Configuration& cfg = *made;
|
||||||
cfg.path = fs::absolute(depRoot / cfg.path).lexically_normal();
|
cfg.path = fs::absolute(depRoot / cfg.path).lexically_normal();
|
||||||
auto abs = [&](const fs::path& p) -> fs::path {
|
auto abs = [&](const fs::path& p) -> fs::path {
|
||||||
return p.is_absolute() ? p : fs::absolute(depRoot / p).lexically_normal();
|
return p.is_absolute() ? p : fs::absolute(depRoot / p).lexically_normal();
|
||||||
|
|
@ -378,20 +389,19 @@ namespace {
|
||||||
for (auto& entry : cfg.flatpak.share) {
|
for (auto& entry : cfg.flatpak.share) {
|
||||||
if (!entry.source.empty()) entry.source = abs(entry.source);
|
if (!entry.source.empty()) entry.source = abs(entry.source);
|
||||||
}
|
}
|
||||||
return cfg;
|
return made;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Into ProjectCache under `fullHash`, with `loaded` recorded beside it.
|
// Into ProjectCache under `fullHash`, with `loaded` recorded beside it.
|
||||||
// When another thread stored the same spec first, its Configuration
|
// When another thread stored the same spec first, its Configuration
|
||||||
// wins and `cfg` is dropped.
|
// wins and `cfg` is dropped.
|
||||||
Configuration* StoreProject(std::string fullHash, Configuration cfg, LoadedProject loaded) {
|
Configuration* StoreProject(std::string fullHash, std::unique_ptr<Configuration> cfg, LoadedProject loaded) {
|
||||||
std::lock_guard lock(ProjectCacheMutex);
|
std::lock_guard lock(ProjectCacheMutex);
|
||||||
if (auto it = ProjectCache.find(fullHash); it != ProjectCache.end()) {
|
if (auto it = ProjectCache.find(fullHash); it != ProjectCache.end()) {
|
||||||
return it->second.get();
|
return it->second.get();
|
||||||
}
|
}
|
||||||
auto stored = std::make_unique<Configuration>(std::move(cfg));
|
Configuration* ptr = cfg.get();
|
||||||
Configuration* ptr = stored.get();
|
ProjectCache.emplace(std::move(fullHash), std::move(cfg));
|
||||||
ProjectCache.emplace(std::move(fullHash), std::move(stored));
|
|
||||||
LoadedProjects.emplace(ptr, std::move(loaded));
|
LoadedProjects.emplace(ptr, std::move(loaded));
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
@ -461,7 +471,7 @@ Configuration* Crafter::GitProject(const GitProjectSpec& spec) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string_view> argViews(spec.args.begin(), spec.args.end());
|
std::vector<std::string_view> argViews(spec.args.begin(), spec.args.end());
|
||||||
Configuration cfg = LoadProjectFromRoot(projectPath, cloneDir, argViews);
|
std::unique_ptr<Configuration> cfg = LoadProjectFromRoot(projectPath, cloneDir, argViews);
|
||||||
|
|
||||||
const fs::path projectInRepo = spec.projectFile.lexically_normal();
|
const fs::path projectInRepo = spec.projectFile.lexically_normal();
|
||||||
const fs::path below = projectInRepo.parent_path();
|
const fs::path below = projectInRepo.parent_path();
|
||||||
|
|
@ -522,7 +532,7 @@ Configuration* Crafter::LocalProject(const LocalProjectSpec& spec) {
|
||||||
|
|
||||||
fs::path depRoot = canonical.parent_path();
|
fs::path depRoot = canonical.parent_path();
|
||||||
std::vector<std::string_view> argViews(spec.args.begin(), spec.args.end());
|
std::vector<std::string_view> argViews(spec.args.begin(), spec.args.end());
|
||||||
Configuration cfg = LoadProjectFromRoot(canonical, depRoot, argViews);
|
std::unique_ptr<Configuration> cfg = LoadProjectFromRoot(canonical, depRoot, argViews);
|
||||||
|
|
||||||
return StoreProject(std::move(fullHash), std::move(cfg), {
|
return StoreProject(std::move(fullHash), std::move(cfg), {
|
||||||
.projectFile = canonical,
|
.projectFile = canonical,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue