An executable packaged as a Flatpak came out as the files/ of one, which
only flatpak build-finish makes an app of, and getting it installed or
onto another machine took flatpak commands on the host, typed by hand.
Two verbs do it where the host's flatpak can be reached:
`crafter-build install` builds the project and installs its app for this
user, and `crafter-build bundle` builds it into bin/<variant>.flatpak, a
single file for `flatpak install --user` that names Flathub's repository
for the runtime. Inside crafter-build's Flatpak they need the
org.freedesktop.Flatpak permission, which its manifest does not grant in
this commit. Both put the app into the project's repo first, in
build/flatpak-apps/<app-id>/ (FlatpakAppDir), one per app rather than
per variant, since flatpak-builder's state there is its cache. Every
variant exports into it, so the export and the install or bundle made
from it hold a lock there, and another crafter-build exporting meanwhile
waits rather than leave this one installing or bundling its program. The
user remote an install re-points is every checkout's that builds the
app-id, so re-pointing it and installing from it hold a second lock,
which every crafter-build installing there takes, natively or in the
Flatpak, whatever its XDG_CACHE_HOME: in
~/.cache/crafter.build/flatpak-remotes/, found by HOME, for the user's
own installation, which flatpak hides from the sandbox behind a tmpfs of
the sandbox's own, and in the directory of a private one,
FLATPAK_USER_DIR, one lock for all of them where the sandbox shares that
directory with the host. Without modules, the host's flatpak
build-finish finishes build/flatpak-apps/<app-id>/build-dir, made afresh
each time from a metadata naming the app, its runtime and the Sdk and a
tree of hard links to the build's files/, copies where a link cannot be
made, across filesystems or on one without links, vfat or exFAT: it
writes the command, the sandbox's permissions and export/ by flatpak's
own rules, those of whichever flatpak the host runs, and refuses a
permission it does not know, and flatpak build-export puts the result
into the repo. When cfg.flatpak.modules names flatpak-builder module
files for what the app needs in /app besides the program,
flatpak-builder does both, from a manifest that lists them and copies
the built program in last, so nothing is compiled twice. That manifest
keeps share/locale in the app ("separate-locales": false):
flatpak-builder would split it into a .Locale extension by default,
which a bundle, holding the app alone, leaves out. The loader rebases a
dependency's module files as it does its cfg.files. install uses a user
remote named after the app-id, re-pointed at the repo each time, and
says how to remove both. The app's tree in
build/flatpak-apps/<app-id>/build-dir, where flatpak-builder installs a
module's files too, is left out of the include walk with every other
build/, so the headers a module installed reach no compile.
cfg.flatpak gets the runtime, the Platform or the Sdk at the branch
crafter-build runs on, and the sandbox's permissions, finish-args as
Flatpak spells them, which any configuration may declare: an executable
gets its dependencies' first and its own last, so it can take one back.
The build reads neither, and a change to either links nothing. install
and bundle print the permissions, with where each came from, and hand
them to build-finish, the one judge of what they mean, so a refusal can
be traced to the configuration that declared it. FlatpakPermissions
checks the order everywhere, and that a change to one neither moves the
build to another directory nor links anything, and, where the host's
flatpak runs, what build-finish makes of them and that it refuses a
socket it does not have; FlatpakBundle that build-finish exports the
desktop entry and the icon, and that the build's own files/ is left as
it was; VariantId's test has the runtime, permissions and modules move
no directory.
The host's flatpak and flatpak-builder are reached only through
HostCommand in :Platform, which builds the command lines of
RunHostFlatpak, RunHostFlatpakBuilder and HostFlatpakUnavailable's
probes: flatpak-spawn --host --watch-bus inside crafter-build's Flatpak,
started in the host's / and passing FLATPAK_USER_DIR on, and the command
itself natively. flatpak-spawn would otherwise start a command in
crafter-build's own working directory, which the host need not have, and
from /app, say, every host command would fail. HostFlatpakUnavailable
says why the host cannot be reached: no org.freedesktop.Flatpak
permission, no session bus (CI's build sandbox has neither), a host that
does not see the project's bin/ and build/ where crafter-build does, as
it does not in the sandbox's own /tmp, /var, /usr, /etc and /app or
where a link leads into one, or no flatpak natively. That, and an
executable that is not packaged as a Flatpak, install and bundle refuse
as soon as the project is loaded, before anything is built;
FlatpakVerbRefusal shows the second. The host is asked rather than the
sandbox's paths modelled, which an override sharing a directory inside
the sandbox's own would defeat: its flatpak is started in a directory
just made in each of bin/ and build/ under a name no other has, which it
enters only where it sees that very directory, and the directory is
removed again. --help gets a section on both, and its Clean section says
the repo an install points at goes with build/. FlatpakModules runs
flatpak-builder only with a flatpak-builder program on the host,
crafter-build falling back to the org.flatpak.Builder app, whose
`flatpak run` would write ~/.var/app in the real home, and with the
Platform and Sdk it builds the app with. The library's install function
is InstallFlatpakApp because project.cpp files define an InstallFlatpak
script of their own under `using namespace Crafter`, this repository's
included, and a library InstallFlatpak would make the name an overload
set, which the scripts table's std::function cannot be built from;
BundleFlatpakApp is named to match. JsonQuote writes the manifest's
strings, and variants.json's, which escaped quotes and backslashes only.
FlatpakExport, FlatpakManifestText, ExportFlatpak and FlatpakPermission,
which only :Clang uses, stay out of the API a project.cpp compiles
against, as RecordFlatpakPlaced does, so their names cannot meet a
project's own under `using namespace Crafter`; ExportFlatpak's comment
names the app's lock, which its callers hold, beside
HostFlatpakUnavailable.
535 lines
25 KiB
C++
535 lines
25 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-only
|
||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
||
module Crafter.Build:External_impl;
|
||
import std;
|
||
import :External;
|
||
import :Toolchain;
|
||
import :Platform;
|
||
import :Progress;
|
||
import :Clang;
|
||
namespace fs = std::filesystem;
|
||
using namespace Crafter;
|
||
|
||
namespace {
|
||
|
||
std::string DeriveName(const GitSource& source) {
|
||
std::string url = source.url;
|
||
while (!url.empty() && (url.back() == '/' || url.back() == '\\')) url.pop_back();
|
||
if (url.ends_with(".git")) url.resize(url.size() - 4);
|
||
auto slash = url.find_last_of("/\\");
|
||
return slash == std::string::npos ? url : url.substr(slash + 1);
|
||
}
|
||
|
||
std::string JoinOptions(std::span<const std::string> options) {
|
||
std::string out;
|
||
for (const std::string& opt : options) {
|
||
if (!out.empty()) out += ' ';
|
||
out += ShellQuote(opt);
|
||
}
|
||
return out;
|
||
}
|
||
|
||
std::string FetchGit(const GitSource& source, const fs::path& cloneDir) {
|
||
auto runGit = [](std::string_view cmd) -> std::optional<std::string> {
|
||
CommandResult r = RunCommandChecked(cmd);
|
||
if (r.exitCode != 0) return std::format("`{}` failed (exit {}): {}", cmd, r.exitCode, r.output);
|
||
return std::nullopt;
|
||
};
|
||
// Submodules follow the checkout only when asked to: after every clone,
|
||
// switch, checkout or pull, so they match the revision that was fetched
|
||
// instead of whichever one was checked out before, from the URLs that
|
||
// revision's .gitmodules gives (sync). A no-op, and offline, when they
|
||
// already match, which puts back submodules moved by hand; and no git at
|
||
// all without any.
|
||
auto updateSubmodules = [&runGit](const fs::path& dir) -> std::optional<std::string> {
|
||
if (!fs::exists(dir / ".gitmodules")) return std::nullopt;
|
||
const std::string quoted = ShellQuote(dir.string());
|
||
if (auto err = runGit(std::format("git -C {} submodule sync --recursive", quoted))) return err;
|
||
return runGit(std::format("git -C {} submodule update --init --recursive", quoted));
|
||
};
|
||
|
||
if (fs::exists(cloneDir)) {
|
||
CommandResult remote = RunCommandChecked(std::format("git -C {} remote get-url origin", ShellQuote(cloneDir.string())));
|
||
std::string currentUrl = remote.output;
|
||
while (!currentUrl.empty() && (currentUrl.back() == '\n' || currentUrl.back() == '\r')) currentUrl.pop_back();
|
||
if (remote.exitCode != 0 || currentUrl != source.url) {
|
||
fs::remove_all(cloneDir);
|
||
} else if (!source.commit.empty()) {
|
||
CommandResult head = RunCommandChecked(std::format("git -C {} rev-parse HEAD", ShellQuote(cloneDir.string())));
|
||
std::string currentHead = head.output;
|
||
while (!currentHead.empty() && (currentHead.back() == '\n' || currentHead.back() == '\r')) currentHead.pop_back();
|
||
if (head.exitCode == 0 && currentHead != source.commit) {
|
||
if (auto err = runGit(std::format("git -C {} fetch origin", ShellQuote(cloneDir.string())))) return *err;
|
||
if (auto err = runGit(std::format("git -C {} checkout {}", ShellQuote(cloneDir.string()), ShellQuote(source.commit)))) return *err;
|
||
}
|
||
// Also when the commit already matched: a clone made before
|
||
// submodules were updated has them at the default branch's.
|
||
if (auto err = updateSubmodules(cloneDir)) return *err;
|
||
return "";
|
||
} else {
|
||
// No commit pinned — pull latest. The user opted into "track latest"
|
||
// by not pinning; the cost is one network round-trip per build.
|
||
// Pin source.commit to a SHA for reproducible, offline-capable builds.
|
||
if (!source.branch.empty()) {
|
||
if (auto err = runGit(std::format("git -C {} pull origin {}", ShellQuote(cloneDir.string()), ShellQuote(source.branch)))) return *err;
|
||
} else {
|
||
if (auto err = runGit(std::format("git -C {} pull", ShellQuote(cloneDir.string())))) return *err;
|
||
}
|
||
if (auto err = updateSubmodules(cloneDir)) return *err;
|
||
return "";
|
||
}
|
||
}
|
||
|
||
// Random per-invocation tmp suffix so concurrent crafter-build processes
|
||
// resolving to the same cache key don't share an in-flight `.tmp`
|
||
// directory — git's pack tempfiles would get yanked when one process
|
||
// removes or renames the dir out from under the other.
|
||
std::random_device rd;
|
||
fs::path tmpDir = cloneDir;
|
||
tmpDir += std::format(".tmp.{:08x}{:08x}", rd(), rd());
|
||
|
||
// Not --recursive: that checks out the default branch's submodules, and
|
||
// the requested revision's come once it is checked out.
|
||
if (auto err = runGit(std::format("git clone {} {}", ShellQuote(source.url), ShellQuote(tmpDir.string())))) {
|
||
if (fs::exists(tmpDir)) fs::remove_all(tmpDir);
|
||
return *err;
|
||
}
|
||
if (!source.branch.empty()) {
|
||
if (auto err = runGit(std::format("git -C {} switch {}", ShellQuote(tmpDir.string()), ShellQuote(source.branch)))) {
|
||
fs::remove_all(tmpDir);
|
||
return *err;
|
||
}
|
||
}
|
||
if (!source.commit.empty()) {
|
||
if (auto err = runGit(std::format("git -C {} checkout {}", ShellQuote(tmpDir.string()), ShellQuote(source.commit)))) {
|
||
fs::remove_all(tmpDir);
|
||
return *err;
|
||
}
|
||
}
|
||
if (auto err = updateSubmodules(tmpDir)) {
|
||
fs::remove_all(tmpDir);
|
||
return *err;
|
||
}
|
||
|
||
std::error_code ec;
|
||
fs::rename(tmpDir, cloneDir, ec);
|
||
if (ec) {
|
||
fs::remove_all(tmpDir);
|
||
// If a concurrent build won the race, cloneDir now exists with the
|
||
// same source revision (cache key includes url+branch+commit), so
|
||
// discard our clone and reuse theirs.
|
||
if (fs::exists(cloneDir)) return "";
|
||
return std::format("rename {} -> {} failed: {}", tmpDir.string(), cloneDir.string(), ec.message());
|
||
}
|
||
return "";
|
||
}
|
||
|
||
std::string BuildInjectedCMakeFlags(const fs::path& cmakeBuildDir, std::string_view target) {
|
||
std::string out;
|
||
// Without an explicit build type, some packages (glslang) append a 'd'
|
||
// suffix to library names assuming a debug build, breaking -l<name>
|
||
// linking. Pin to Release.
|
||
out += " -DCMAKE_BUILD_TYPE=Release";
|
||
const Toolchain& tc = GetToolchain();
|
||
out += std::format(" -DCMAKE_C_COMPILER={}", ShellQuote(tc.clang.string()));
|
||
out += std::format(" -DCMAKE_CXX_COMPILER={}", ShellQuote(tc.clangxx.string()));
|
||
// Stdlib choice: libc++ for Linux targets (matches how crafter-build
|
||
// itself is built with -stdlib=libc++); the mingw cross-compile uses the
|
||
// libstdc++ shipped by the mingw-w64 toolchain, so it keeps the default.
|
||
if (target.ends_with("-linux-gnu") || target.ends_with("-linux-musl")) {
|
||
out += std::format(" -DCMAKE_CXX_FLAGS={}", ShellQuote(std::format("-stdlib=libc++{}", tc.LibcxxCompileFlags())));
|
||
out += std::format(" -DCMAKE_EXE_LINKER_FLAGS={}", ShellQuote(std::format("-stdlib=libc++{}", tc.LibcxxLinkFlags())));
|
||
out += std::format(" -DCMAKE_SHARED_LINKER_FLAGS={}", ShellQuote(std::format("-stdlib=libc++{}", tc.LibcxxLinkFlags())));
|
||
}
|
||
if (target == "x86_64-w64-mingw32") {
|
||
out += " -DCMAKE_SYSTEM_NAME=Windows";
|
||
out += std::format(" -DCMAKE_C_COMPILER_TARGET={}", target);
|
||
out += std::format(" -DCMAKE_CXX_COMPILER_TARGET={}", target);
|
||
// The libstdc++ and the mingw-w64 install the project's own compiles
|
||
// use (GetBaseCommand).
|
||
out += " -DCMAKE_CXX_FLAGS=-stdlib=libstdc++";
|
||
if (const fs::path& sysroot = MingwSysroot(); !sysroot.empty()) out += std::format(" -DCMAKE_SYSROOT={}", ShellQuote(sysroot.string()));
|
||
}
|
||
fs::path absBuildDir = fs::absolute(cmakeBuildDir);
|
||
out += std::format(" -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY={}", ShellQuote(absBuildDir.string()));
|
||
out += std::format(" -DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}", ShellQuote(absBuildDir.string()));
|
||
return out;
|
||
}
|
||
|
||
std::string ConfigureCMake(const fs::path& cloneDir, const fs::path& cmakeBuildDir, std::span<const std::string> options, std::string_view target) {
|
||
fs::path optionsFile = cmakeBuildDir / ".crafter-options";
|
||
std::string optionsKey = JoinOptions(options);
|
||
// The injected flags are inputs too: a different compiler or libc++
|
||
// must reconfigure rather than reuse a CMakeCache made with the old one.
|
||
const std::string injected = BuildInjectedCMakeFlags(cmakeBuildDir, target);
|
||
const std::string configureKey = injected + '\n' + optionsKey;
|
||
|
||
bool needsConfigure = !fs::exists(cmakeBuildDir / "CMakeCache.txt");
|
||
if (!needsConfigure) {
|
||
std::ifstream existing(optionsFile);
|
||
if (!existing) {
|
||
needsConfigure = true;
|
||
} else {
|
||
std::stringstream buf;
|
||
buf << existing.rdbuf();
|
||
if (buf.str() != configureKey) needsConfigure = true;
|
||
}
|
||
}
|
||
|
||
if (!needsConfigure) return "";
|
||
|
||
// A build directory configured with other inputs starts over instead of
|
||
// being reconfigured. CMake reconfigures in place without recompiling
|
||
// what an old compiler or --sysroot compiled (the Makefiles generator
|
||
// writes --sysroot into build.make, which make does not compare) and
|
||
// keeps the check results the old configure cached. A mingw external
|
||
// built against the host's /usr as the sysroot (issue #43) kept its
|
||
// objects reading __libc_single_threaded after the fix that way. The
|
||
// directory keeps its name, which projects point generated headers at.
|
||
if (fs::exists(cmakeBuildDir / "CMakeCache.txt")) {
|
||
std::error_code ec;
|
||
fs::remove_all(cmakeBuildDir, ec);
|
||
if (ec) return std::format("cannot clear {} for a fresh configure: {}", cmakeBuildDir.string(), ec.message());
|
||
}
|
||
if (!fs::exists(cmakeBuildDir)) fs::create_directories(cmakeBuildDir);
|
||
|
||
std::string cmd = std::format("cmake -S {} -B {}{}", ShellQuote(fs::absolute(cloneDir).string()), ShellQuote(fs::absolute(cmakeBuildDir).string()), injected);
|
||
if (!options.empty()) {
|
||
cmd += " ";
|
||
cmd += optionsKey;
|
||
}
|
||
|
||
CommandResult r = RunCommandChecked(cmd);
|
||
if (r.exitCode != 0) {
|
||
return std::format("cmake configure failed (exit {}): {}", r.exitCode, r.output);
|
||
}
|
||
|
||
std::ofstream(optionsFile) << configureKey;
|
||
return "";
|
||
}
|
||
|
||
std::string BuildCMake(const fs::path& cmakeBuildDir) {
|
||
// Without --parallel, the Unix Makefiles generator builds one translation
|
||
// unit at a time, leaving every core but one idle. Pass an explicit job
|
||
// count (a bare --parallel maps to an unbounded `make -j` fork bomb on the
|
||
// Makefiles generator) so deps like DPP/msquic/glslang compile ~N× faster.
|
||
std::uint32_t jobs = std::max(1u, std::thread::hardware_concurrency());
|
||
std::string cmd = std::format("cmake --build {} --parallel {}", ShellQuote(fs::absolute(cmakeBuildDir).string()), jobs);
|
||
CommandResult r = RunCommandChecked(cmd);
|
||
if (r.exitCode != 0) {
|
||
return std::format("cmake --build failed (exit {}): {}", r.exitCode, r.output);
|
||
}
|
||
return "";
|
||
}
|
||
|
||
} // namespace
|
||
|
||
fs::path Crafter::ExternalCloneDir(const ExternalDependency& dep, std::string_view target) {
|
||
std::string name = dep.name.empty() ? DeriveName(dep.source) : dep.name;
|
||
if (name.empty()) return {};
|
||
std::string keyMaterial = std::format("{}|{}|{}|{}|{}", dep.source.url, dep.source.branch, dep.source.commit, JoinOptions(dep.options), target);
|
||
std::size_t key = std::hash<std::string>{}(keyMaterial);
|
||
return GetCacheDir() / "external" / std::format("{}-{:016x}", name, key);
|
||
}
|
||
|
||
std::vector<std::string> Crafter::ExternalIncludeFlags(const ExternalDependency& dep, std::string_view target) {
|
||
std::vector<std::string> flags;
|
||
fs::path cloneDir = ExternalCloneDir(dep, target);
|
||
if (cloneDir.empty()) return flags;
|
||
for (const fs::path& include : dep.includeDirs) {
|
||
fs::path full = include.empty() ? cloneDir : cloneDir / include;
|
||
flags.push_back(std::format("-I{}", fs::absolute(full).string()));
|
||
}
|
||
return flags;
|
||
}
|
||
|
||
fs::path Crafter::ExternalBuildDir(const ExternalDependency& dep, std::string_view target) {
|
||
fs::path cloneDir = ExternalCloneDir(dep, target);
|
||
if (cloneDir.empty() || dep.builder != ExternalBuilder::CMake) return cloneDir;
|
||
return cloneDir / "build";
|
||
}
|
||
|
||
std::string Crafter::BuildExternal(const ExternalDependency& dep, std::string_view target) {
|
||
std::string name = dep.name.empty() ? DeriveName(dep.source) : dep.name;
|
||
if (name.empty()) {
|
||
return std::format("Could not derive name for external dependency from URL '{}'", dep.source.url);
|
||
}
|
||
|
||
fs::path externalRoot = GetCacheDir() / "external";
|
||
if (!fs::exists(externalRoot)) {
|
||
std::error_code ec;
|
||
fs::create_directories(externalRoot, ec);
|
||
if (ec) {
|
||
return std::format("Failed to create {}: {}", externalRoot.string(), ec.message());
|
||
}
|
||
}
|
||
|
||
fs::path cloneDir = ExternalCloneDir(dep, target);
|
||
// Build() makes one call for all the configurations of a pass, but two
|
||
// crafter-builds, or two passes, can still reach the same clone at once,
|
||
// and two pulls in one clone race on its .git/FETCH_HEAD.
|
||
FileLock lock(fs::path(std::format("{}.lock", cloneDir.string())));
|
||
|
||
if (std::string err = FetchGit(dep.source, cloneDir); !err.empty()) {
|
||
return std::format("git fetch for '{}': {}", name, err);
|
||
}
|
||
if (dep.builder != ExternalBuilder::CMake) return "";
|
||
|
||
const fs::path cmakeBuildDir = ExternalBuildDir(dep, target);
|
||
if (std::string err = ConfigureCMake(cloneDir, cmakeBuildDir, dep.options, target); !err.empty()) {
|
||
return std::format("cmake configure for '{}': {}", name, err);
|
||
}
|
||
if (std::string err = BuildCMake(cmakeBuildDir); !err.empty()) {
|
||
return std::format("cmake build for '{}': {}", name, err);
|
||
}
|
||
return "";
|
||
}
|
||
|
||
ExternalBuildResult Crafter::ExternalOutputs(const ExternalDependency& dep, std::string_view target) {
|
||
ExternalBuildResult result;
|
||
result.compileFlags = ExternalIncludeFlags(dep, target);
|
||
|
||
const fs::path cmakeBuildDir = ExternalBuildDir(dep, target);
|
||
if (dep.builder != ExternalBuilder::CMake || cmakeBuildDir.empty()) return result;
|
||
|
||
// Each search path is both a -L (link time) and a run path (the
|
||
// loader's), so the program picks its shared dependencies up from the
|
||
// cache without any LD_LIBRARY_PATH gymnastics; static ones (.a) need
|
||
// the run path not at all and are not hurt by it. The PE/COFF target
|
||
// (mingw) resolves DLLs through PATH or the program's own directory
|
||
// rather than a run path, and lld warns if it is given one.
|
||
const bool isPe = target == "x86_64-w64-mingw32";
|
||
std::string buildDirAbs = fs::absolute(cmakeBuildDir).string();
|
||
result.linkFlags.push_back(std::format("-L{}", buildDirAbs));
|
||
if (!isPe) result.runPaths.push_back(buildDirAbs);
|
||
for (const fs::path& libDir : dep.libDirs) {
|
||
fs::path full = libDir.is_absolute() ? libDir : cmakeBuildDir / libDir;
|
||
std::string fullAbs = fs::absolute(full).string();
|
||
result.linkFlags.push_back(std::format("-L{}", fullAbs));
|
||
if (!isPe) result.runPaths.push_back(fullAbs);
|
||
}
|
||
for (const std::string& lib : dep.libs) {
|
||
result.linkFlags.push_back(std::format("-l{}", lib));
|
||
}
|
||
|
||
// The newest library in every directory the link searches: the build
|
||
// directory and the libDirs, where projects like msquic put theirs,
|
||
// versioned names (libmsquic.so.2) included.
|
||
auto isLibrary = [](const std::string& name) {
|
||
return name.ends_with(".a") || name.ends_with(".so") || name.contains(".so.") || name.ends_with(".dll");
|
||
};
|
||
std::vector<fs::path> searched = { cmakeBuildDir };
|
||
for (const fs::path& libDir : dep.libDirs) searched.push_back(libDir.is_absolute() ? libDir : cmakeBuildDir / libDir);
|
||
for (const fs::path& dir : searched) {
|
||
std::error_code listEc;
|
||
for (fs::directory_iterator it(dir, listEc), end; !listEc && it != end; it.increment(listEc)) {
|
||
std::error_code ec;
|
||
if (!it->is_regular_file(ec) || !isLibrary(it->path().filename().string())) continue;
|
||
fs::file_time_type t = it->last_write_time(ec);
|
||
if (!ec && t > result.latestArtifact) result.latestArtifact = t;
|
||
}
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
namespace {
|
||
std::mutex ProjectCacheMutex;
|
||
std::unordered_map<std::string, std::unique_ptr<Configuration>> ProjectCache;
|
||
// Keyed by the Configurations ProjectCache owns, which live as long as
|
||
// the process does, so the pointers never dangle. Guarded by
|
||
// ProjectCacheMutex as well.
|
||
std::unordered_map<const Configuration*, LoadedProject> LoadedProjects;
|
||
|
||
// Run the dep's project.cpp from its own directory so its relative paths
|
||
// (cfg.path = "./", interfaces/, lib/, ...) resolve against the dep
|
||
// root, then rebase cfg.path and all other relative path collections to
|
||
// 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<const std::string_view> args) {
|
||
fs::path savedCwd = fs::current_path();
|
||
Configuration cfg;
|
||
fs::current_path(depRoot);
|
||
try {
|
||
cfg = LoadProject(projectPath, args);
|
||
} catch (...) {
|
||
fs::current_path(savedCwd);
|
||
throw;
|
||
}
|
||
fs::current_path(savedCwd);
|
||
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();
|
||
};
|
||
for (fs::path& p : cfg.cFiles) p = abs(p);
|
||
for (fs::path& p : cfg.cuda) p = abs(p);
|
||
for (fs::path& p : cfg.files) p = abs(p);
|
||
for (fs::path& p : cfg.buildFiles) p = abs(p);
|
||
for (Shader& s : cfg.shaders) s.path = abs(s.path);
|
||
// A dependency that packages its own executable as a Flatpak names
|
||
// the files it is installed with as it names its cfg.files. An
|
||
// empty one is none, and stays empty.
|
||
if (!cfg.flatpak.desktopFile.empty()) cfg.flatpak.desktopFile = abs(cfg.flatpak.desktopFile);
|
||
if (!cfg.flatpak.metainfo.empty()) cfg.flatpak.metainfo = abs(cfg.flatpak.metainfo);
|
||
for (fs::path& p : cfg.flatpak.icons) p = abs(p);
|
||
for (fs::path& p : cfg.flatpak.modules) p = abs(p);
|
||
return cfg;
|
||
}
|
||
|
||
// 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) {
|
||
std::lock_guard lock(ProjectCacheMutex);
|
||
if (auto it = ProjectCache.find(fullHash); it != ProjectCache.end()) {
|
||
return it->second.get();
|
||
}
|
||
auto stored = std::make_unique<Configuration>(std::move(cfg));
|
||
Configuration* ptr = stored.get();
|
||
ProjectCache.emplace(std::move(fullHash), std::move(stored));
|
||
LoadedProjects.emplace(ptr, std::move(loaded));
|
||
return ptr;
|
||
}
|
||
|
||
// `url` as it names a repository: without user info, which is where a
|
||
// token goes, and without trailing slashes or ".git", so specs that only
|
||
// spell the repository differently name the same one.
|
||
std::string RepositoryKey(std::string_view url) {
|
||
std::string key(url);
|
||
if (const std::size_t scheme = key.find("://"); scheme != std::string::npos) {
|
||
const std::size_t host = scheme + 3;
|
||
const std::size_t at = key.rfind('@', key.find('/', host));
|
||
if (at != std::string::npos && at >= host) key.erase(host, at + 1 - host);
|
||
}
|
||
while (key.ends_with('/')) key.pop_back();
|
||
if (key.ends_with(".git")) key.resize(key.size() - 4);
|
||
return key;
|
||
}
|
||
}
|
||
|
||
Configuration* Crafter::GitProject(const GitProjectSpec& spec) {
|
||
// Two hashes: the clone is shared across specs that point at the same
|
||
// source revision (forwarded args don't change what gets cloned, just
|
||
// how the dep's project.cpp interprets them at runtime), while the
|
||
// in-memory Configuration is keyed by the full spec so each (args)
|
||
// permutation gets its own Configuration object, and host tooling one
|
||
// apart from the target's even for the same args.
|
||
std::string srcKey = std::format("git|{}|{}|{}|{}", spec.source.url, spec.source.branch, spec.source.commit, spec.projectFile.string());
|
||
std::string srcHash = std::format("{:016x}", std::hash<std::string>{}(srcKey));
|
||
|
||
std::string fullKey = std::format("{}{}", LoadingHostTooling() ? "host|" : "", srcKey);
|
||
for (const std::string& a : spec.args) {
|
||
fullKey += '|';
|
||
fullKey += a;
|
||
}
|
||
std::string fullHash = std::format("{:016x}", std::hash<std::string>{}(fullKey));
|
||
|
||
{
|
||
std::lock_guard lock(ProjectCacheMutex);
|
||
if (auto it = ProjectCache.find(fullHash); it != ProjectCache.end()) {
|
||
return it->second.get();
|
||
}
|
||
}
|
||
|
||
std::string name = DeriveName(spec.source);
|
||
if (name.empty()) {
|
||
throw std::runtime_error(std::format("GitProject: could not derive name from URL '{}'", spec.source.url));
|
||
}
|
||
|
||
fs::path externalRoot = GetCacheDir() / "external";
|
||
fs::create_directories(externalRoot);
|
||
fs::path cloneDir = externalRoot / std::format("{}-{}", name, srcHash);
|
||
|
||
{
|
||
// One fetch of a clone at a time, as in BuildExternal.
|
||
FileLock lock(fs::path(std::format("{}.lock", cloneDir.string())));
|
||
const bool exists = fs::exists(cloneDir);
|
||
Progress::Task task(std::format("{} {}", exists ? "Updating" : "Cloning", name));
|
||
if (std::string err = FetchGit(spec.source, cloneDir); !err.empty()) {
|
||
throw std::runtime_error(std::format("GitProject({}): {}", spec.source.url, err));
|
||
}
|
||
}
|
||
|
||
fs::path projectPath = cloneDir / spec.projectFile;
|
||
if (!fs::exists(projectPath)) {
|
||
throw std::runtime_error(std::format("GitProject({}): {} not found in clone", spec.source.url, spec.projectFile.string()));
|
||
}
|
||
|
||
std::vector<std::string_view> argViews(spec.args.begin(), spec.args.end());
|
||
Configuration cfg = LoadProjectFromRoot(projectPath, cloneDir, argViews);
|
||
|
||
const fs::path projectInRepo = spec.projectFile.lexically_normal();
|
||
const fs::path below = projectInRepo.parent_path();
|
||
return StoreProject(std::move(fullHash), std::move(cfg), {
|
||
.projectFile = projectPath.lexically_normal(),
|
||
.name = below.empty() ? name : std::format("{}/{}", name, below.generic_string()),
|
||
.identity = std::format("git|{}|{}", RepositoryKey(spec.source.url), projectInRepo.generic_string()),
|
||
});
|
||
}
|
||
|
||
fs::path Crafter::GitFetch(const GitSource& source) {
|
||
std::string name = DeriveName(source);
|
||
if (name.empty()) {
|
||
throw std::runtime_error(std::format("GitFetch: could not derive name from URL '{}'", source.url));
|
||
}
|
||
fs::path externalRoot = GetCacheDir() / "external";
|
||
std::error_code ec;
|
||
fs::create_directories(externalRoot, ec);
|
||
if (ec) {
|
||
throw std::runtime_error(std::format("GitFetch: failed to create {}: {}", externalRoot.string(), ec.message()));
|
||
}
|
||
std::string keyMaterial = std::format("git|{}|{}|{}", source.url, source.branch, source.commit);
|
||
std::size_t key = std::hash<std::string>{}(keyMaterial);
|
||
fs::path cloneDir = externalRoot / std::format("{}-{:016x}", name, key);
|
||
|
||
{
|
||
// One fetch of a clone at a time, as in BuildExternal.
|
||
FileLock lock(fs::path(std::format("{}.lock", cloneDir.string())));
|
||
const bool exists = fs::exists(cloneDir);
|
||
Progress::Task task(std::format("{} {}", exists ? "Updating" : "Cloning", name));
|
||
if (std::string err = FetchGit(source, cloneDir); !err.empty()) {
|
||
throw std::runtime_error(std::format("GitFetch({}): {}", source.url, err));
|
||
}
|
||
}
|
||
return cloneDir;
|
||
}
|
||
|
||
Configuration* Crafter::LocalProject(const LocalProjectSpec& spec) {
|
||
fs::path projectPath = fs::absolute(spec.projectFile).lexically_normal();
|
||
if (!fs::exists(projectPath)) {
|
||
throw std::runtime_error(std::format("LocalProject: {} not found", projectPath.string()));
|
||
}
|
||
fs::path canonical = fs::canonical(projectPath);
|
||
|
||
std::string fullKey = std::format("{}local|{}", LoadingHostTooling() ? "host|" : "", canonical.string());
|
||
for (const std::string& a : spec.args) {
|
||
fullKey += '|';
|
||
fullKey += a;
|
||
}
|
||
std::string fullHash = std::format("{:016x}", std::hash<std::string>{}(fullKey));
|
||
|
||
{
|
||
std::lock_guard lock(ProjectCacheMutex);
|
||
if (auto it = ProjectCache.find(fullHash); it != ProjectCache.end()) {
|
||
return it->second.get();
|
||
}
|
||
}
|
||
|
||
fs::path depRoot = canonical.parent_path();
|
||
std::vector<std::string_view> argViews(spec.args.begin(), spec.args.end());
|
||
Configuration cfg = LoadProjectFromRoot(canonical, depRoot, argViews);
|
||
|
||
return StoreProject(std::move(fullHash), std::move(cfg), {
|
||
.projectFile = canonical,
|
||
.name = depRoot.filename().string(),
|
||
.identity = std::format("local|{}", canonical.string()),
|
||
});
|
||
}
|
||
|
||
std::optional<LoadedProject> Crafter::LoadedProjectOf(const Configuration& cfg) {
|
||
std::lock_guard lock(ProjectCacheMutex);
|
||
if (auto it = LoadedProjects.find(&cfg); it != LoadedProjects.end()) return it->second;
|
||
return std::nullopt;
|
||
}
|