Tests stage their fixtures under fixed names in the temp directory, fs::temp_directory_path() / "crafter-build-<name>", and two runs at once staged into the same ones: natively every process shares the machine's /tmp, and every sandbox of crafter-build's Flatpak shares the one the app has, whatever checkout it runs in. A test of one run could find what it had staged removed or written over by the same test of the other run, which removes the directory as it starts and stages its copy anew. RunTests now makes a directory for the run in the temp directory, a PrivateDirectory, and runs each test with TMPDIR naming <run>/<name>, so that fs::temp_directory_path() in the test names it and the fixed names are the run's alone. The second test of one name in a run, as two configurations or two targets may each have one, gets <name>-2, so that removing one's directory does not take the other's. RunSingleTest takes the directory as a new last parameter and puts TMPDIR into the env prefix of the test's command, beside a Wayland test's WAYLAND_DISPLAY. env's command is sh, which execs the test's words, as for a Wayland test's: env takes a word with an '=' in it ahead of its command for an assignment, a binary's path among them, and with no command left it printed its environment and exited 0, a pass for a test that never ran. What a test that passes or skips leaves is removed when it ends. What one that fails, crashes or times out leaves is kept for a look, its log says where, as does what is printed of a test that fails or crashes, and the run's directory then outlives the run, unlocked, for a later run to remove a minute after, or, where the run was killed, a minute after its last change to it; PrivateDirectory gains Keep() for that. A test's logs, build/test-logs/<name>.log and <name>.compositor.log, are named as its TMPDIR is, <name>-2 for a later test of one name, so that two failing tests of one name each keep their own, and with it where what each left is kept: with one log for both, the second's was written over the first's. The README's line on the logs says so. The directory is dated as it is let go of, since the removal of abandoned directories goes by its date, which is that of what was last made or removed in it: for the run of a test that fails after a minute, as one that times out at the default 60 s does, a minute old already, and the next run would remove it at once. The removal now reads a directory's date only once it holds the directory's lock: a date read before could be one the holder replaced as it let go. The directory is the run's, not a pass's: every pass of a sweep over targets shares it, so it is made and its surroundings swept once, and let go of once the last pass is done. A pass that made one of its own would have made it once the passes before had let go of theirs, and making it would have removed any of those that had not changed for a minute: a kept one, after a pass of that long, while its test's output said where it was. RunTests becomes the wrapper that holds the run's directory around RunTestsWith, the passes it recurses into, and the directory is kept the moment a test keeps something in it, by the worker that ran the test, so that a later pass that throws, as one does for a target whose runner CRAFTER_BUILD_RUNNER_ gives wrong, or a worker that cannot be started does not take it with it; Keep() may be called from several workers at once. TMPDIR reaches the test where its runner hands it on, as running it directly and qemu-user do. Under wine a Windows test does not get TMPDIR, and takes the Wine prefix's TMP, TEMP and temp directory, and wasmtime gives a test no environment at all; RunSingleTest's comment says so. A test under `host` gets its TMPDIR as any test does, so that comment and HostDisplay's now say that `host` leaves the test the display, not the whole environment, as it is. In the runner rather than in each of the 74 tests whose main.cpp uses the temp directory at this commit: a project's own tests are covered the same way, where their runner hands them TMPDIR. Of the 74 only SysrootStdModule has to change. It builds against a sysroot it stages there, and the std module's cache directory is keyed on the sysroot's path, which is a new one every run now, so every run left one more directory, with its lock, in the real cache; it keeps its cache in its scratch directory now, as the tests that have crafter-build fetch a repository they stage do. AGENTS.template.md says what changes for the tests: HostFlatpak, whose /tmp is no longer the temp directory a test is given, is a second test that writes elsewhere, and under `crafter-build test` a staged copy goes with the test's TMPDIR rather than at the test's next run.
1429 lines
68 KiB
C++
1429 lines
68 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
module;
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <limits.h>
|
|
#include <unistd.h>
|
|
#include <dlfcn.h>
|
|
#include <sys/wait.h>
|
|
#include <sys/file.h>
|
|
#include <sys/prctl.h>
|
|
#include <signal.h>
|
|
#include <fcntl.h>
|
|
#include <errno.h>
|
|
module Crafter.Build:Platform_impl;
|
|
import std;
|
|
import :Platform;
|
|
import :Toolchain;
|
|
import :Clang;
|
|
import :Interface;
|
|
import :External;
|
|
import :Jobs;
|
|
import :Progress;
|
|
namespace fs = std::filesystem;
|
|
using namespace Crafter;
|
|
|
|
namespace {
|
|
// Exclusive advisory lock over one PCM cache dir (`<cache>/<target>-<march>/`),
|
|
// held for the duration of a (re)build. Concurrent crafter-build invocations —
|
|
// and the threads of a single invocation that build at once — that share a cache
|
|
// serialize their builds through it: the first builder to acquire the lock
|
|
// precompiles std.pcm / the Crafter.Build-*.pcm modules while everyone else
|
|
// blocks, then re-checks freshness and reuses the finished file instead of
|
|
// re-running clang and racing to overwrite the same path. Each builder must
|
|
// pass through the lock before it reaches the compile steps that *read* those
|
|
// PCMs, so a reader never observes a half-written file.
|
|
//
|
|
// A FileLock on a `.lock` file in the cache dir.
|
|
struct CacheLock {
|
|
FileLock lock;
|
|
explicit CacheLock(const fs::path& cacheDir) : lock(cacheDir / ".lock") {}
|
|
};
|
|
|
|
// The cached Crafter.Build PCMs are keyed by `<target>-<march>` alone, so
|
|
// every crafter-build on the machine writes to the same files regardless of
|
|
// which share/crafter-build its module sources came from. Freshness used to
|
|
// be a per-file mtime comparison, which cannot distinguish "this PCM is
|
|
// newer than my source" from "this PCM was built from *different* sources
|
|
// that happen to be newer" — so a second install or checkout silently
|
|
// compiled its project against the other's declarations. Same failure shape
|
|
// as issue #27: no error, a binary built against a layout nobody linked.
|
|
//
|
|
// A stamp over the bytes of every module source answers what the mtime
|
|
// can't. It covers the whole set rather than one file at a time because the
|
|
// PCMs import each other: a change to :Interface invalidates :Clang's PCM
|
|
// even though Crafter.Build-Clang.cppm is untouched.
|
|
std::string CrafterBuildSourceStamp(const fs::path& sourceDir, std::span<const std::string_view> moduleNames) {
|
|
std::string all;
|
|
auto append = [&all, &sourceDir](const fs::path& relative) {
|
|
std::ifstream in(sourceDir / relative, std::ios::binary);
|
|
std::ostringstream buffer;
|
|
buffer << in.rdbuf();
|
|
all += relative.string();
|
|
all += '\0';
|
|
all += buffer.str();
|
|
all += '\0';
|
|
};
|
|
for (std::string_view name : moduleNames) {
|
|
append(fs::path(std::format("{}.cppm", name)));
|
|
}
|
|
return std::format("{:016x}", std::hash<std::string>{}(all));
|
|
}
|
|
|
|
fs::path CacheStampPath(const fs::path& cacheDir) {
|
|
return cacheDir / "crafter-build-sources.stamp";
|
|
}
|
|
|
|
// A stamp is a one-line content hash kept next to the artifact it
|
|
// describes. Written last, after the artifact, so an interrupted build
|
|
// leaves a stamp that disagrees and the next run starts over.
|
|
std::string ReadStamp(const fs::path& file) {
|
|
std::ifstream in(file, std::ios::binary);
|
|
std::string stamp;
|
|
std::getline(in, stamp);
|
|
return stamp;
|
|
}
|
|
|
|
void WriteStamp(const fs::path& file, std::string_view stamp) {
|
|
std::ofstream(file, std::ios::binary | std::ios::trunc) << stamp << '\n';
|
|
}
|
|
|
|
fs::path StampPath(const fs::path& artifact) {
|
|
fs::path stamp = artifact;
|
|
stamp += ".stamp";
|
|
return stamp;
|
|
}
|
|
|
|
std::string HashStamp(std::string_view content) {
|
|
return std::format("{:016x}", std::hash<std::string_view>{}(content));
|
|
}
|
|
|
|
std::string FileBytes(const fs::path& file) {
|
|
std::ifstream in(file, std::ios::binary);
|
|
std::ostringstream buffer;
|
|
buffer << in.rdbuf();
|
|
return buffer.str();
|
|
}
|
|
|
|
// The string value of `"key": "..."` in `object`, if it has one.
|
|
std::optional<std::string> JsonString(std::string_view object, std::string_view key) {
|
|
const std::string quoted = std::format("\"{}\"", key);
|
|
std::size_t at = object.find(quoted);
|
|
if (at == std::string_view::npos) return std::nullopt;
|
|
at = object.find_first_not_of(" \t\r\n", at + quoted.size());
|
|
if (at == std::string_view::npos || object[at] != ':') return std::nullopt;
|
|
at = object.find_first_not_of(" \t\r\n", at + 1);
|
|
if (at == std::string_view::npos || object[at] != '"') return std::nullopt;
|
|
std::string value;
|
|
for (std::size_t i = at + 1; i < object.size(); ++i) {
|
|
if (object[i] == '"') return value;
|
|
if (object[i] == '\\' && i + 1 < object.size()) ++i;
|
|
value += object[i];
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
// The std module's "source-path" in a libstdc++.modules.json: the object
|
|
// in its "modules" array whose "logical-name" is "std". The manifest is
|
|
// a flat list of such objects, so each one is the text between its
|
|
// braces.
|
|
std::optional<std::string> StdModuleSourcePath(std::string_view manifest) {
|
|
for (std::size_t open = manifest.find('{', manifest.find("\"modules\"")); open != std::string_view::npos; open = manifest.find('{', open + 1)) {
|
|
const std::size_t close = manifest.find('}', open);
|
|
if (close == std::string_view::npos) break;
|
|
const std::string_view object = manifest.substr(open, close - open);
|
|
if (JsonString(object, "logical-name") == "std") return JsonString(object, "source-path");
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
|
|
Crafter::FileLock::FileLock(const fs::path& path) {
|
|
fd = ::open(path.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0644);
|
|
if (fd >= 0) {
|
|
while (::flock(fd, LOCK_EX) != 0 && errno == EINTR) {}
|
|
}
|
|
}
|
|
|
|
Crafter::FileLock::~FileLock() {
|
|
if (fd >= 0) {
|
|
::close(fd); // closing the descriptor drops the flock
|
|
}
|
|
}
|
|
|
|
fs::path Crafter::GetCrafterBuildHome() {
|
|
if (const char* envHome = std::getenv("CRAFTER_BUILD_HOME")) {
|
|
return fs::path(envHome);
|
|
}
|
|
fs::path hostExe = fs::read_symlink("/proc/self/exe");
|
|
|
|
// Walk up from the exe's directory looking for share/crafter-build with
|
|
// a known module source present as a sentinel. Handles:
|
|
// <prefix>/bin/crafter-build -> <prefix>/share/crafter-build (FHS install)
|
|
// <repo>/bin/crafter-build -> <repo>/share/crafter-build (build.sh)
|
|
// <repo>/bin/<arch>/crafter-build -> <repo>/share/crafter-build (self-host)
|
|
fs::path dir = hostExe.parent_path();
|
|
std::vector<fs::path> tried;
|
|
for (;;) {
|
|
fs::path candidate = dir / "share" / "crafter-build";
|
|
tried.push_back(candidate);
|
|
std::error_code ec;
|
|
if (fs::exists(candidate / "Crafter.Build.cppm", ec)) {
|
|
return candidate;
|
|
}
|
|
fs::path parent = dir.parent_path();
|
|
if (parent == dir) break;
|
|
dir = parent;
|
|
}
|
|
std::string msg = std::format("could not locate crafter-build runtime assets relative to {} (set CRAFTER_BUILD_HOME). Tried:", hostExe.string());
|
|
for (const auto& p : tried) {
|
|
msg += std::format("\n {}", p.string());
|
|
}
|
|
throw std::runtime_error(msg);
|
|
}
|
|
|
|
fs::file_time_type Crafter::NewestPrerequisite(const fs::path& depFile) {
|
|
std::ifstream in(depFile, std::ios::binary);
|
|
if (!in) return fs::file_time_type::max();
|
|
std::ostringstream buffer;
|
|
buffer << in.rdbuf();
|
|
const std::string text = std::move(buffer).str();
|
|
|
|
// Make-rule tokenizing, as clang writes it: `target: prereq prereq`, with a
|
|
// trailing '\' continuing the rule onto the next line (clang wraps at ~80
|
|
// columns, so any real depfile has several), '\ ' and '\#' escaping a
|
|
// character into a filename, and '$$' standing for a literal '$'. A
|
|
// backslash that is none of those is a Windows path separator and stays.
|
|
std::vector<std::string> tokens;
|
|
std::string token;
|
|
auto flush = [&tokens, &token]() {
|
|
if (!token.empty()) {
|
|
tokens.push_back(token);
|
|
token.clear();
|
|
}
|
|
};
|
|
for (std::size_t i = 0; i < text.size(); ++i) {
|
|
const char c = text[i];
|
|
const char next = i + 1 < text.size() ? text[i + 1] : '\0';
|
|
if (c == '\\' && (next == '\n' || next == '\r')) {
|
|
flush();
|
|
++i;
|
|
} else if (c == '\\' && (next == ' ' || next == '\t' || next == '#')) {
|
|
token += next;
|
|
++i;
|
|
} else if (c == '$' && next == '$') {
|
|
token += '$';
|
|
++i;
|
|
} else if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
|
|
flush();
|
|
} else {
|
|
token += c;
|
|
}
|
|
}
|
|
flush();
|
|
|
|
// A token ending in ':' names a rule target, not an input. The first one
|
|
// closes the target list (and a Windows `C:\build\x.o:` still ends in ':',
|
|
// where splitting on the first colon would not); a later one opens another
|
|
// rule, as `-MP` phony targets do, whose name is not a file to stat.
|
|
bool sawTarget = false;
|
|
fs::file_time_type newest = fs::file_time_type::min();
|
|
for (const std::string& prerequisite : tokens) {
|
|
if (prerequisite.ends_with(':')) {
|
|
sawTarget = true;
|
|
continue;
|
|
}
|
|
if (!sawTarget) continue;
|
|
std::error_code ec;
|
|
const fs::file_time_type stamp = fs::last_write_time(prerequisite, ec);
|
|
// A prerequisite that has since been deleted makes the recorded set
|
|
// unusable as evidence — the source that included it must have changed
|
|
// too, and rebuilding is what writes a depfile that no longer names it.
|
|
if (ec) return fs::file_time_type::max();
|
|
if (stamp > newest) newest = stamp;
|
|
}
|
|
// No rule at all: a truncated or empty depfile proves nothing.
|
|
if (!sawTarget) return fs::file_time_type::max();
|
|
return newest;
|
|
}
|
|
|
|
bool Crafter::MatchGlob(std::string_view glob, std::string_view name) {
|
|
std::size_t gi = 0;
|
|
std::size_t ni = 0;
|
|
std::size_t star = std::string_view::npos;
|
|
std::size_t mark = 0;
|
|
while (ni < name.size()) {
|
|
if (gi < glob.size() && (glob[gi] == '?' || glob[gi] == name[ni])) {
|
|
++gi; ++ni;
|
|
} else if (gi < glob.size() && glob[gi] == '*') {
|
|
star = gi++;
|
|
mark = ni;
|
|
} else if (star != std::string_view::npos) {
|
|
gi = star + 1;
|
|
ni = ++mark;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
while (gi < glob.size() && glob[gi] == '*') ++gi;
|
|
return gi == glob.size();
|
|
}
|
|
|
|
bool Crafter::MatchAny(std::span<const std::string> globs, std::string_view name) {
|
|
if (globs.empty()) return true;
|
|
for (const auto& g : globs) {
|
|
if (MatchGlob(g, name)) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
namespace {
|
|
// RunBuildCommand's command, run on the thread that calls. See the
|
|
// declaration in Crafter.Build-Platform.cppm for why this branches on the
|
|
// exit code rather than on whether the command said anything.
|
|
std::string RunBuildStep(std::string_view cmd) {
|
|
Progress::EchoCommand(cmd);
|
|
CommandResult r = RunCommandChecked(cmd);
|
|
// A mingw build against a system prefix sees mingw-w64 through a view in
|
|
// the cache, and clang names every header by the path it found it at.
|
|
r.output = MingwViewToPrefix(std::move(r.output));
|
|
|
|
if (r.exitCode == 0) {
|
|
// Warnings from a step that succeeded. Show them — until now they were
|
|
// invisible on an incremental build and fatal on a cold one.
|
|
if (!r.output.empty()) Progress::Diagnostic(r.output);
|
|
return "";
|
|
}
|
|
|
|
// A compiler killed outright (the OOM killer is the common one) prints
|
|
// nothing, so the old "output means failure" rule read it as success and
|
|
// let the build carry on with a missing object. Say what happened instead.
|
|
if (r.output.empty()) {
|
|
return r.crashed
|
|
? std::format("terminated by signal {}: {}", r.signal, cmd)
|
|
: std::format("exited with code {}: {}", r.exitCode, cmd);
|
|
}
|
|
if (r.crashed) {
|
|
return std::format("{}\nterminated by signal {}", r.output, r.signal);
|
|
}
|
|
return r.output;
|
|
}
|
|
}
|
|
|
|
std::string Crafter::RunBuildCommand(std::string_view cmd) {
|
|
// A job is on a worker already and runs the command where it is. A link,
|
|
// or the precompile of the std module, comes from the thread of a Build()
|
|
// or of a project file's load and waits here for its turn among the jobs.
|
|
std::string result;
|
|
JobPool::Process().Run([&result, cmd] { result = RunBuildStep(cmd); });
|
|
return result;
|
|
}
|
|
|
|
// --- Dependencies of project.cpp itself -------------------------------------
|
|
//
|
|
// #pragma crafter dependency "<source>" [branch "<b>" | commit "<sha>"] [project "<file>"]
|
|
//
|
|
// names a sibling crafter-build project that project.cpp imports a module
|
|
// from. Anything a file needs before it can compile cannot be C++ inside that
|
|
// file, so this is a directive next to the `import` it enables — where
|
|
// `#include` lives. Each one is fetched, built for the host, and put on
|
|
// project.so's module path and link line before project.cpp is compiled. The
|
|
// dependency's own project.cpp is loaded the same way, so its pragmas
|
|
// resolve recursively.
|
|
namespace {
|
|
struct PragmaDependency {
|
|
std::string source;
|
|
std::string branch;
|
|
std::string commit;
|
|
std::string project;
|
|
std::size_t line = 0;
|
|
};
|
|
|
|
[[noreturn]] void PragmaError(const fs::path& file, std::size_t line, std::string_view message) {
|
|
throw std::runtime_error(std::format("{}:{}: {}", file.string(), line, message));
|
|
}
|
|
|
|
// The tokens of one directive: bare words and double-quoted strings (no
|
|
// escapes — a path or URL never needs one). Reading stops at `//`.
|
|
class PragmaCursor {
|
|
public:
|
|
PragmaCursor(std::string_view text, const fs::path& file, std::size_t line) : text_(text), file_(file), line_(line) {}
|
|
|
|
bool AtEnd() {
|
|
SkipSpace();
|
|
return pos_ >= text_.size() || text_.substr(pos_).starts_with("//");
|
|
}
|
|
|
|
bool Take(char c) {
|
|
SkipSpace();
|
|
if (pos_ >= text_.size() || text_[pos_] != c) return false;
|
|
++pos_;
|
|
return true;
|
|
}
|
|
|
|
std::string_view Word() {
|
|
SkipSpace();
|
|
const std::size_t start = pos_;
|
|
while (pos_ < text_.size() && IsWordChar(text_[pos_])) ++pos_;
|
|
return text_.substr(start, pos_ - start);
|
|
}
|
|
|
|
std::optional<std::string_view> Quoted() {
|
|
SkipSpace();
|
|
if (pos_ >= text_.size() || text_[pos_] != '"') return std::nullopt;
|
|
const std::size_t close = text_.find('"', pos_ + 1);
|
|
if (close == std::string_view::npos) Fail("unterminated string");
|
|
const std::string_view inner = text_.substr(pos_ + 1, close - pos_ - 1);
|
|
pos_ = close + 1;
|
|
return inner;
|
|
}
|
|
|
|
[[noreturn]] void Fail(std::string_view message) const { PragmaError(file_, line_, message); }
|
|
|
|
private:
|
|
static bool IsWordChar(char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'; }
|
|
|
|
void SkipSpace() {
|
|
while (pos_ < text_.size() && (text_[pos_] == ' ' || text_[pos_] == '\t' || text_[pos_] == '\r')) ++pos_;
|
|
}
|
|
|
|
std::string_view text_;
|
|
const fs::path& file_;
|
|
std::size_t line_;
|
|
std::size_t pos_ = 0;
|
|
};
|
|
|
|
// One directive per line, anywhere in the file. Comments aren't parsed:
|
|
// a directive inside a block comment is honoured, and `//` lines fall out
|
|
// because they don't start with `#`.
|
|
std::vector<PragmaDependency> ScanProjectFilePragmas(const fs::path& absProject) {
|
|
std::vector<PragmaDependency> found;
|
|
std::ifstream in(absProject);
|
|
std::string raw;
|
|
for (std::size_t lineNo = 1; std::getline(in, raw); ++lineNo) {
|
|
PragmaCursor cur(raw, absProject, lineNo);
|
|
if (!cur.Take('#') || cur.Word() != "pragma" || cur.Word() != "crafter") continue;
|
|
const std::string_view verb = cur.Word();
|
|
if (verb != "dependency") cur.Fail(std::format("unknown crafter pragma '{}'", verb));
|
|
|
|
PragmaDependency dep;
|
|
dep.line = lineNo;
|
|
const std::optional<std::string_view> source = cur.Quoted();
|
|
if (!source) cur.Fail("expected a quoted source after 'dependency'");
|
|
dep.source = *source;
|
|
while (!cur.AtEnd()) {
|
|
const std::string_view key = cur.Word();
|
|
std::string* field = key == "branch" ? &dep.branch : key == "commit" ? &dep.commit : key == "project" ? &dep.project : nullptr;
|
|
if (!field) cur.Fail(key.empty() ? "unexpected text after the source" : std::format("unknown keyword '{}' (expected branch, commit or project)", key));
|
|
if (!field->empty()) cur.Fail(std::format("'{}' given twice", key));
|
|
const std::optional<std::string_view> value = cur.Quoted();
|
|
if (!value || value->empty()) cur.Fail(std::format("expected a quoted value after '{}'", key));
|
|
*field = *value;
|
|
}
|
|
if (dep.source.empty()) cur.Fail("the source is empty");
|
|
if (!dep.branch.empty() && !dep.commit.empty()) cur.Fail("'branch' and 'commit' are mutually exclusive");
|
|
found.push_back(std::move(dep));
|
|
}
|
|
return found;
|
|
}
|
|
|
|
struct ProjectFileFrame {
|
|
fs::path project;
|
|
// What the load was given, for ProjectArgs(), and only once the
|
|
// entry point is about to be called (see OpenProjectFile). A view
|
|
// is enough: the caller's storage outlives the load, and the frame
|
|
// is popped before the load returns.
|
|
std::span<const std::string_view> args;
|
|
std::vector<Configuration*> dependencies;
|
|
};
|
|
|
|
// Loads nest: a dependency's project.cpp loads inside its consumer's
|
|
// pre-pass, and GitProject inside an entry point nests again. Several
|
|
// threads may also load at once (ConcurrentCacheRace), so the stack is
|
|
// per thread. ProjectDependency() reads the top frame.
|
|
thread_local std::vector<ProjectFileFrame> LoadingFrames;
|
|
|
|
// A frame's dependencies, kept past the frame for PragmaDependencies().
|
|
// Process-wide rather than per thread: whoever asks need not be the
|
|
// thread that loaded the file. The Configurations live in the project
|
|
// cache, which outlives every load.
|
|
std::mutex PragmaDependenciesMutex;
|
|
std::unordered_map<fs::path, std::vector<Configuration*>> PragmaDependenciesByProject;
|
|
|
|
// Held for the whole of one load of a project file. Pushing one that is
|
|
// already on the stack is a cycle, which used to recurse forever.
|
|
class ProjectFileGuard {
|
|
public:
|
|
explicit ProjectFileGuard(const fs::path& absProject) {
|
|
for (const ProjectFileFrame& frame : LoadingFrames) {
|
|
if (frame.project != absProject) continue;
|
|
std::string chain;
|
|
for (const ProjectFileFrame& f : LoadingFrames) chain += std::format("{} -> ", f.project.string());
|
|
throw std::runtime_error(std::format("circular project dependency: {}{}", chain, absProject.string()));
|
|
}
|
|
index_ = LoadingFrames.size();
|
|
LoadingFrames.push_back({ absProject, {}, {} });
|
|
}
|
|
~ProjectFileGuard() { LoadingFrames.pop_back(); }
|
|
ProjectFileGuard(const ProjectFileGuard&) = delete;
|
|
ProjectFileGuard& operator=(const ProjectFileGuard&) = delete;
|
|
|
|
ProjectFileFrame& Frame() { return LoadingFrames[index_]; }
|
|
|
|
private:
|
|
std::size_t index_ = 0;
|
|
};
|
|
|
|
// How many `#pragma crafter dependency` loads this thread is inside. The
|
|
// tool's project file runs within its load, and so does every sibling it
|
|
// loads in turn, which is what LoadingHostTooling() reports to GitProject
|
|
// and LocalProject. Per thread like LoadingFrames: a sibling loaded on a
|
|
// thread of the tool's own goes uncounted, as it gets no ProjectArgs().
|
|
thread_local std::int32_t HostToolingLoads = 0;
|
|
|
|
class HostToolingScope {
|
|
public:
|
|
HostToolingScope() { ++HostToolingLoads; }
|
|
~HostToolingScope() { --HostToolingLoads; }
|
|
HostToolingScope(const HostToolingScope&) = delete;
|
|
HostToolingScope& operator=(const HostToolingScope&) = delete;
|
|
};
|
|
|
|
// A pre-dependency is host tooling: project.so is loaded into the running
|
|
// crafter-build, so the dependency is built for the host and against the
|
|
// same PCM cache a load populates — the hostConfig the load itself
|
|
// compiles project.so with, so the dependency's std/Crafter.Build PCMs
|
|
// land in the cache dir project.so is built against. Recursive, because
|
|
// the dependency's project.cpp is loaded with no arguments and so passes
|
|
// none on, while its own deps still read CRAFTER_BUILD_MARCH and
|
|
// CRAFTER_BUILD_CUDA_ARCH from the environment.
|
|
void ForceHostTooling(Configuration& cfg, const Configuration& host, std::unordered_set<Configuration*>& seen) {
|
|
if (!seen.insert(&cfg).second) return;
|
|
cfg.target = host.target;
|
|
cfg.march = host.march;
|
|
cfg.mtune = host.mtune;
|
|
cfg.cudaArch = host.cudaArch;
|
|
cfg.debug = false;
|
|
// project.so is a shared object, so the archive it links has to be
|
|
// position-independent; static libraries aren't by default. Part of
|
|
// VariantId, so this build never shares a bin dir with a normal one.
|
|
// Checked before pushing: the same Configuration comes back from the
|
|
// project cache for every consumer in this process. Linux hosts only,
|
|
// glibc or musl: Windows targets have no use for the flag.
|
|
const bool linuxHost = host.target.ends_with("-linux-gnu") || host.target.ends_with("-linux-musl");
|
|
if (linuxHost && !std::ranges::contains(cfg.compileFlags, "-fPIC")) {
|
|
cfg.compileFlags.emplace_back("-fPIC");
|
|
}
|
|
for (Configuration* dep : cfg.dependencies) ForceHostTooling(*dep, host, seen);
|
|
}
|
|
|
|
struct ProjectFileDependencies {
|
|
std::vector<Configuration*> configs;
|
|
std::string compileFlags; // " -fprebuilt-module-path=…" for each dependency and its transitive deps
|
|
// Every dependency's BuildResult.libs, verbatim, then its runtimeLibs,
|
|
// the CUDA runtime, then a run path per directory of its runPaths
|
|
// (RunPathFlags), as Build() links a program: the shared libraries a
|
|
// pragma dependency is built on are found there when the load
|
|
// dlopens project.so.
|
|
std::string linkFlags;
|
|
fs::file_time_type newestArtifact = fs::file_time_type::min();
|
|
};
|
|
|
|
bool IsGitSource(std::string_view source) {
|
|
return source.contains("://") || source.starts_with("git@");
|
|
}
|
|
|
|
ProjectFileDependencies ResolveProjectFileDependencies(const fs::path& absProject, const Configuration& host) {
|
|
ProjectFileDependencies out;
|
|
const std::vector<PragmaDependency> pragmas = ScanProjectFilePragmas(absProject);
|
|
if (pragmas.empty()) return out;
|
|
|
|
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
|
std::mutex depMutex;
|
|
std::unordered_set<Configuration*> forced;
|
|
std::unordered_set<Configuration*> walked;
|
|
std::set<std::string> libs;
|
|
std::set<std::string> runtimeLibs;
|
|
std::set<std::string> runPaths;
|
|
|
|
for (const PragmaDependency& pragma : pragmas) {
|
|
// Neither spec sets args, and the tool still gets none of the
|
|
// consumer's: this pre-pass runs before the load hands the
|
|
// frame its args, so ProjectArgs() is empty. Given the consumer's
|
|
// --target=, the tool's project.cpp would pick sources and flags
|
|
// for the wrong triple, and its flags would split the tool's
|
|
// variant from the one every other consumer shares. The scope
|
|
// keeps the tool and everything it loads in a project cache of
|
|
// their own: ForceHostTooling rewrites them in place, and a
|
|
// consumer loading the same sibling for itself got that host
|
|
// build back whenever the two cache keys matched.
|
|
Configuration* cfg = nullptr;
|
|
{
|
|
HostToolingScope hostTooling;
|
|
if (IsGitSource(pragma.source)) {
|
|
GitProjectSpec spec{ .source = { pragma.source, pragma.branch, pragma.commit } };
|
|
if (!pragma.project.empty()) spec.projectFile = pragma.project;
|
|
cfg = GitProject(spec);
|
|
} else {
|
|
if (!pragma.branch.empty() || !pragma.commit.empty()) PragmaError(absProject, pragma.line, "'branch' and 'commit' only apply to a git source");
|
|
// Relative to the project file, not the cwd: the load runs
|
|
// from wherever the consumer was invoked.
|
|
fs::path local = (absProject.parent_path() / pragma.source).lexically_normal();
|
|
if (fs::is_directory(local)) {
|
|
local /= pragma.project.empty() ? fs::path("project.cpp") : fs::path(pragma.project);
|
|
} else if (!pragma.project.empty()) {
|
|
PragmaError(absProject, pragma.line, "'project' only applies to a directory source");
|
|
}
|
|
if (!fs::exists(local)) PragmaError(absProject, pragma.line, std::format("no such project file: {}", local.string()));
|
|
cfg = LocalProject({ .projectFile = local });
|
|
}
|
|
}
|
|
if (std::ranges::contains(out.configs, cfg)) continue;
|
|
|
|
ForceHostTooling(*cfg, host, forced);
|
|
BuildResult built;
|
|
{
|
|
Progress::Task task(std::format("Building project dependency {}", cfg->name));
|
|
built = Build(*cfg, depResults, depMutex);
|
|
}
|
|
if (!built.result.empty()) PragmaError(absProject, pragma.line, std::format("project dependency '{}' failed to build: {}", cfg->name, built.result));
|
|
out.configs.push_back(cfg);
|
|
libs.insert(built.libs.begin(), built.libs.end());
|
|
runtimeLibs.insert(built.runtimeLibs.begin(), built.runtimeLibs.end());
|
|
runPaths.insert(built.runPaths.begin(), built.runPaths.end());
|
|
|
|
// The module path needs the dependency and everything it depends
|
|
// on, same as GetCompileCommand does for a consumer; the newest
|
|
// artifact over the same set is what project.so is stale against.
|
|
std::function<void(Configuration*)> walk = [&](Configuration* c) {
|
|
if (!walked.insert(c).second) return;
|
|
out.compileFlags += std::format(" -fprebuilt-module-path={}", c->PcmDir().string());
|
|
std::error_code ec;
|
|
const fs::file_time_type t = fs::last_write_time(ExpectedOutput(*c), ec);
|
|
if (!ec && t > out.newestArtifact) out.newestArtifact = t;
|
|
for (Configuration* sub : c->dependencies) walk(sub);
|
|
};
|
|
walk(cfg);
|
|
}
|
|
for (const std::string& lib : libs) out.linkFlags += std::format(" {}", lib);
|
|
for (const std::string& lib : runtimeLibs) out.linkFlags += std::format(" {}", lib);
|
|
for (const std::string& dir : runPaths) out.linkFlags += RunPathFlags(dir);
|
|
return out;
|
|
}
|
|
|
|
// project.so is stale when its stamp (crafter-build version, toolchain,
|
|
// module sources and compile command) disagrees, or when it predates its
|
|
// source or a pre-dependency artifact. The executable's mtime is not an
|
|
// input any more: an OSTree deployment dates every installed file at the
|
|
// epoch, so a comparison against it never fired after an update and the
|
|
// old project.so was loaded into a binary it did not match.
|
|
bool ProjectFileStale(const fs::path& artifact, const fs::path& project, std::string_view stamp, const ProjectFileDependencies& deps) {
|
|
if (!fs::exists(artifact)) return true;
|
|
if (ReadStamp(StampPath(artifact)) != stamp) return true;
|
|
const fs::file_time_type built = fs::last_write_time(artifact);
|
|
return built < fs::last_write_time(project) || built < deps.newestArtifact;
|
|
}
|
|
}
|
|
|
|
Configuration* Crafter::ProjectDependency(std::string_view name) {
|
|
if (LoadingFrames.empty()) throw std::runtime_error(std::format("ProjectDependency('{}'): called outside a project file", name));
|
|
const ProjectFileFrame& frame = LoadingFrames.back();
|
|
for (Configuration* cfg : frame.dependencies) {
|
|
if (cfg->name == name) return cfg;
|
|
for (const std::unique_ptr<Module>& iface : cfg->interfaces) {
|
|
if (iface->name == name) return cfg;
|
|
}
|
|
}
|
|
std::string declared;
|
|
for (Configuration* cfg : frame.dependencies) {
|
|
if (!declared.empty()) declared += ", ";
|
|
declared += cfg->name;
|
|
}
|
|
throw std::runtime_error(std::format("ProjectDependency('{}'): not declared by {} (declared: {})", name, frame.project.string(), declared.empty() ? "none" : declared));
|
|
}
|
|
|
|
bool Crafter::IsTopLevelProject() {
|
|
// One frame is the project file crafter-build was pointed at; a second
|
|
// is only ever pushed for a dependency's project file.
|
|
return LoadingFrames.size() <= 1;
|
|
}
|
|
|
|
std::vector<Configuration*> Crafter::PragmaDependencies(const fs::path& projectFile) {
|
|
std::error_code ec;
|
|
const fs::path absProject = fs::canonical(projectFile, ec);
|
|
if (ec) return {};
|
|
std::lock_guard lock(PragmaDependenciesMutex);
|
|
if (auto it = PragmaDependenciesByProject.find(absProject); it != PragmaDependenciesByProject.end()) return it->second;
|
|
return {};
|
|
}
|
|
|
|
std::vector<std::string> Crafter::ProjectArgs() {
|
|
if (LoadingFrames.empty()) return {};
|
|
const std::span<const std::string_view> args = LoadingFrames.back().args;
|
|
return std::vector<std::string>(args.begin(), args.end());
|
|
}
|
|
|
|
bool Crafter::LoadingHostTooling() {
|
|
return HostToolingLoads > 0;
|
|
}
|
|
|
|
namespace {
|
|
// The one reading of a wait status (pclose, system) for every helper
|
|
// that runs a command: a signal death is reported the way a shell
|
|
// reports it, as 128+signal.
|
|
void DecodeWaitStatus(std::int32_t status, CommandResult& result) {
|
|
if (WIFEXITED(status)) {
|
|
result.exitCode = WEXITSTATUS(status);
|
|
} else if (WIFSIGNALED(status)) {
|
|
result.signal = WTERMSIG(status);
|
|
result.exitCode = 128 + result.signal;
|
|
result.crashed = true;
|
|
} else {
|
|
result.exitCode = -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
std::string Crafter::RunPathFlags(std::string_view dir) {
|
|
return std::format(" -Xlinker -rpath -Xlinker {}", ShellQuote(dir));
|
|
}
|
|
|
|
std::string Crafter::ShellQuote(std::string_view s) {
|
|
std::string out;
|
|
out.reserve(s.size() + 2);
|
|
out.push_back('\'');
|
|
for (char c : s) {
|
|
if (c == '\'') out += "'\\''";
|
|
else out.push_back(c);
|
|
}
|
|
out.push_back('\'');
|
|
return out;
|
|
}
|
|
|
|
CommandResult Crafter::RunCommandChecked(std::string_view cmd) {
|
|
std::array<char, 128> buffer;
|
|
CommandResult result{};
|
|
|
|
std::string with = std::format("{} 2>&1", cmd);
|
|
FILE* pipe = popen(with.c_str(), "r");
|
|
if (!pipe) throw std::runtime_error("popen() failed!");
|
|
|
|
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
|
|
result.output += buffer.data();
|
|
}
|
|
|
|
DecodeWaitStatus(pclose(pipe), result);
|
|
return result;
|
|
}
|
|
|
|
std::string Crafter::JsonQuote(std::string_view s) {
|
|
std::string out = "\"";
|
|
for (const char c : s) {
|
|
if (c == '"') out += "\\\"";
|
|
else if (c == '\\') out += "\\\\";
|
|
else if (c == '\n') out += "\\n";
|
|
else if (c == '\t') out += "\\t";
|
|
else if (c == '\r') out += "\\r";
|
|
else if (static_cast<std::uint8_t>(c) < 0x20) out += std::format("\\u{:04x}", static_cast<std::uint8_t>(c));
|
|
else out.push_back(c);
|
|
}
|
|
out.push_back('"');
|
|
return out;
|
|
}
|
|
|
|
namespace {
|
|
bool InFlatpak() {
|
|
std::error_code ec;
|
|
return fs::exists("/.flatpak-info", ec);
|
|
}
|
|
|
|
// The command line that runs `program arguments` on the host: as it is
|
|
// natively, through the Flatpak session helper inside crafter-build's
|
|
// Flatpak, started there in `directory`. flatpak-spawn would start it in
|
|
// crafter-build's own working directory, which the host need not have,
|
|
// so it is / unless a caller asks for another: every host command names
|
|
// its paths absolutely. --watch-bus ends the host command when
|
|
// flatpak-spawn ends. A crafter-build killed outright leaves
|
|
// flatpak-spawn running, so the host command runs on until it finishes.
|
|
// Every host command of crafter-build's own is built here: the host's
|
|
// flatpak and flatpak-builder, for RunHostFlatpak, RunHostFlatpakBuilder
|
|
// and HostFlatpakUnavailable's probes.
|
|
std::string HostCommand(std::string_view program, std::string_view arguments, const fs::path& directory = "/") {
|
|
if (!InFlatpak()) return std::format("{} {}", program, arguments);
|
|
std::string env;
|
|
if (const char* userDir = std::getenv("FLATPAK_USER_DIR"); userDir && *userDir) {
|
|
env = std::format(" {}", ShellQuote(std::format("--env=FLATPAK_USER_DIR={}", userDir)));
|
|
}
|
|
return std::format("flatpak-spawn --host --watch-bus {}{} {} {}", ShellQuote(std::format("--directory={}", directory.string())), env, program, arguments);
|
|
}
|
|
|
|
std::int32_t RunOnHost(std::string_view program, std::string_view arguments, std::string_view shown) {
|
|
if (Progress::GetVerbosity() != Progress::Verbosity::Quiet) std::println("$ {} {}", shown, arguments);
|
|
return RunCommandAttached(HostCommand(program, arguments));
|
|
}
|
|
}
|
|
|
|
std::string Crafter::HostFlatpakUnavailable(const fs::path& projectDir) {
|
|
if (!InFlatpak()) {
|
|
// Through the shell: `command` is one of its builtins, which the
|
|
// `timeout` RunCommandWithTimeout wraps a command in cannot run.
|
|
if (RunCommandChecked("command -v flatpak").exitCode != 0) return "flatpak is not installed";
|
|
return "";
|
|
}
|
|
std::ifstream file("/.flatpak-info", std::ios::binary);
|
|
const std::string info((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
|
if (!info.contains("org.freedesktop.Flatpak=talk")) {
|
|
return "crafter-build's Flatpak lacks the org.freedesktop.Flatpak permission it runs the host's flatpak with: it is older than that, or runs in a build sandbox";
|
|
}
|
|
// The program install and bundle run, built as they build theirs, in
|
|
// `directory` on the host; and what it said, for a refusal.
|
|
const auto probe = [](const fs::path& directory) { return RunCommandWithTimeout(HostCommand("flatpak", "--version", directory), std::chrono::seconds(30)); };
|
|
const auto said = [](const CommandResult& result) {
|
|
std::string_view output = result.output;
|
|
while (!output.empty() && (output.back() == '\n' || output.back() == '\r')) output.remove_suffix(1);
|
|
return std::string(output);
|
|
};
|
|
if (const CommandResult reached = probe("/"); reached.exitCode != 0) {
|
|
return std::format("the host cannot be reached through flatpak-spawn --host: {}", said(reached));
|
|
}
|
|
// install and bundle hand the host's flatpak paths in the project's bin/
|
|
// and build/, so the host has to see those where crafter-build does,
|
|
// which it does not in the sandbox's own /tmp, /var, /usr, /etc and /app,
|
|
// nor where a link leads into one. The host answers for itself, by its
|
|
// own links and by whatever the sandbox shares, overrides included: the
|
|
// same program, started in a directory just made in each of them under a
|
|
// name no other has, starts only where the host sees that very directory,
|
|
// not merely a path it has as well, as it has /tmp. What this makes is
|
|
// removed again, bin/ and build/ too where they were not there before, so
|
|
// a refusal leaves nothing behind in the project's directory, which the
|
|
// caller's project.cpp is in.
|
|
std::random_device random;
|
|
const std::string mark = std::format(".crafter-build-host-probe-{:08x}{:08x}", random(), random());
|
|
for (const std::string_view sub : {"bin", "build"}) {
|
|
const fs::path dir = fs::absolute(projectDir / sub).lexically_normal();
|
|
std::error_code ec;
|
|
const bool existed = fs::exists(fs::symlink_status(dir, ec));
|
|
fs::create_directories(dir / mark, ec);
|
|
if (ec) return std::format("{} cannot be made, to see whether the host sees it: {}", (dir / mark).string(), ec.message());
|
|
const CommandResult seen = probe(dir / mark);
|
|
fs::remove(dir / mark, ec);
|
|
if (!existed) fs::remove(dir, ec);
|
|
if (seen.exitCode != 0) {
|
|
return std::format("the host does not see {} where crafter-build does ({}): crafter-build's sandbox has /tmp, /var, /usr, /etc and /app of its own, so keep the project, its bin/ and build/ and where their links lead in a directory the sandbox shares with the host, such as your home", dir.string(), said(seen));
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|
|
std::int32_t Crafter::RunHostFlatpak(std::string_view arguments) {
|
|
return RunOnHost("flatpak", arguments, "flatpak");
|
|
}
|
|
|
|
std::int32_t Crafter::RunHostFlatpakBuilder(std::string_view arguments) {
|
|
// flatpak-builder as a host package, else the org.flatpak.Builder app,
|
|
// as the self-host's `run flatpak` script finds it.
|
|
if (RunCommandWithTimeout(HostCommand("flatpak-builder", "--version"), std::chrono::seconds(30)).exitCode == 0) {
|
|
return RunOnHost("flatpak-builder", arguments, "flatpak-builder");
|
|
}
|
|
return RunOnHost("flatpak", std::format("run org.flatpak.Builder {}", arguments), "flatpak");
|
|
}
|
|
|
|
std::int32_t Crafter::RunCommandAttached(std::string_view cmd) {
|
|
// The child writes straight to the terminal. Anything of ours that is
|
|
// still pending — the in-place status line, buffered stdout when it is
|
|
// a pipe — has to be out before the child's first byte, or it lands in
|
|
// the middle of the child's output or after it.
|
|
Progress::Clear();
|
|
std::fflush(stdout);
|
|
std::fflush(stderr);
|
|
|
|
const std::string command(cmd);
|
|
const std::int32_t status = std::system(command.c_str());
|
|
if (status == -1) throw std::runtime_error(std::format("could not start a shell for: {}", cmd));
|
|
CommandResult result{};
|
|
DecodeWaitStatus(status, result);
|
|
return result.exitCode;
|
|
}
|
|
|
|
CommandResult Crafter::RunCommandWithTimeout(std::string_view cmd, std::chrono::seconds timeout) {
|
|
std::array<char, 128> buffer;
|
|
CommandResult result{};
|
|
|
|
// -k (not --kill-after=): the short form is what GNU coreutils and busybox
|
|
// (Alpine) have in common.
|
|
std::string wrapped = std::format("timeout -k 2 {} {} 2>&1", timeout.count(), cmd);
|
|
|
|
FILE* pipe = popen(wrapped.c_str(), "r");
|
|
if (!pipe) throw std::runtime_error("popen() failed!");
|
|
|
|
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
|
|
result.output += buffer.data();
|
|
}
|
|
|
|
DecodeWaitStatus(pclose(pipe), result);
|
|
// What comes back is `timeout`'s own exit, and it speaks through the
|
|
// code: 124 for a command it had to stop, 128+signal for one that died.
|
|
if (!result.crashed) {
|
|
if (result.exitCode == 124) {
|
|
result.timedOut = true;
|
|
} else if (result.exitCode >= 128) {
|
|
result.signal = result.exitCode - 128;
|
|
result.crashed = true;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
namespace {
|
|
// How a child that waitid saw end ended, read as DecodeWaitStatus reads
|
|
// a pclose: the exit code, or 128+signal when a signal ended it.
|
|
std::int32_t ChildExitCode(const siginfo_t& info) {
|
|
return info.si_code == CLD_EXITED ? info.si_status : 128 + info.si_status;
|
|
}
|
|
}
|
|
|
|
Crafter::BackgroundCommand::BackgroundCommand(std::string_view command, std::span<const EnvironmentChange> environment, const fs::path& log, std::chrono::seconds lifetime) {
|
|
// The death signal goes to the process this forks, so that has to be
|
|
// the timeout process: `exec` makes the sh it starts as become it.
|
|
// timeout passes a SIGTERM on to its whole group and ends what is still
|
|
// running 2 s later.
|
|
std::string script = std::format("exec timeout -k 2 {} sh -c {}", lifetime.count(), ShellQuote(command));
|
|
std::string shell = "sh";
|
|
std::string flag = "-c";
|
|
std::array<char*, 4> argv = { shell.data(), flag.data(), script.data(), nullptr };
|
|
|
|
std::vector<std::string> variables;
|
|
for (char** entry = environ; *entry != nullptr; ++entry) {
|
|
const std::string_view variable(*entry);
|
|
const std::string_view name = variable.substr(0, variable.find('='));
|
|
if (std::ranges::none_of(environment, [&](const EnvironmentChange& change) { return change.name == name; })) {
|
|
variables.emplace_back(variable);
|
|
}
|
|
}
|
|
for (const EnvironmentChange& change : environment) {
|
|
if (change.value) variables.push_back(std::format("{}={}", change.name, *change.value));
|
|
}
|
|
std::vector<char*> envp;
|
|
for (std::string& variable : variables) envp.push_back(variable.data());
|
|
envp.push_back(nullptr);
|
|
|
|
// Both close-on-exec: the child keeps them only as its 0, 1 and 2.
|
|
const std::int32_t output = ::open(log.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
|
|
if (output < 0) throw std::runtime_error(std::format("cannot write {}: {}", log.string(), std::strerror(errno)));
|
|
const std::int32_t input = ::open("/dev/null", O_RDONLY | O_CLOEXEC);
|
|
if (input < 0) {
|
|
const std::int32_t openError = errno;
|
|
::close(output);
|
|
throw std::runtime_error(std::format("cannot open /dev/null: {}", std::strerror(openError)));
|
|
}
|
|
|
|
const pid_t parent = ::getpid();
|
|
// Everything the child uses was made above: between fork and exec it
|
|
// makes async-signal-safe calls only, since another thread of this
|
|
// process may hold the allocator's lock at the moment of the fork.
|
|
pid = ::fork();
|
|
if (pid == 0) {
|
|
// The signal is sent when the thread that forked ends, not the
|
|
// process; the parent may also have ended before this line.
|
|
if (::prctl(PR_SET_PDEATHSIG, SIGTERM) != 0 || ::getppid() != parent) ::_exit(126);
|
|
// A signal mask outlives exec, and a SIGTERM blocked in it would
|
|
// keep both the death signal and Stop() from reaching timeout.
|
|
sigset_t none;
|
|
::sigemptyset(&none);
|
|
::sigprocmask(SIG_SETMASK, &none, nullptr);
|
|
::setpgid(0, 0);
|
|
::dup2(input, STDIN_FILENO);
|
|
::dup2(output, STDOUT_FILENO);
|
|
::dup2(output, STDERR_FILENO);
|
|
::execve("/bin/sh", argv.data(), envp.data());
|
|
::_exit(127);
|
|
}
|
|
const std::int32_t forkError = errno;
|
|
::close(input);
|
|
::close(output);
|
|
if (pid < 0) throw std::runtime_error(std::format("cannot start {}: {}", command, std::strerror(forkError)));
|
|
// From this side as well, so that the group exists before Stop() can
|
|
// signal it, whichever of the two runs first. Once the child has
|
|
// exec'd this fails, after the child's own call has made the group.
|
|
::setpgid(pid, pid);
|
|
}
|
|
|
|
Crafter::BackgroundCommand::~BackgroundCommand() {
|
|
Stop(std::chrono::seconds(5));
|
|
}
|
|
|
|
std::optional<std::int32_t> Crafter::BackgroundCommand::Exited() {
|
|
if (exitCode || pid < 0) return exitCode;
|
|
siginfo_t info{};
|
|
// WNOWAIT leaves an ended command unreaped: its pid, and with it the
|
|
// number of its process group, which Stop() signals, cannot be handed
|
|
// to another process before Stop() reaps it.
|
|
if (::waitid(P_PID, static_cast<id_t>(pid), &info, WEXITED | WNOHANG | WNOWAIT) == 0 && info.si_pid == pid) {
|
|
exitCode = ChildExitCode(info);
|
|
}
|
|
return exitCode;
|
|
}
|
|
|
|
void Crafter::BackgroundCommand::Stop(std::chrono::milliseconds grace) {
|
|
if (pid < 0 || reaped) return;
|
|
::kill(-pid, SIGTERM);
|
|
const auto deadline = std::chrono::steady_clock::now() + grace;
|
|
while (!Exited() && std::chrono::steady_clock::now() < deadline) {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
}
|
|
// What is left of the group: anything the command started that outlived
|
|
// it, and the command itself when it did not end within `grace`.
|
|
::kill(-pid, SIGKILL);
|
|
std::int32_t status = 0;
|
|
while (::waitpid(pid, &status, 0) < 0 && errno == EINTR) {}
|
|
reaped = true;
|
|
}
|
|
|
|
namespace {
|
|
// Removes what PrivateDirectory's destructor would have, for the
|
|
// processes that never ran it.
|
|
void RemoveAbandonedDirectories(const fs::path& parent, std::string_view prefix) {
|
|
std::error_code ec;
|
|
const auto now = fs::file_time_type::clock::now();
|
|
for (const fs::directory_entry& entry : fs::directory_iterator(parent, ec)) {
|
|
std::error_code entryError;
|
|
if (!entry.path().filename().string().starts_with(prefix) || !entry.is_directory(entryError) || entry.is_symlink(entryError)) continue;
|
|
// Another user's directory does not open: mkdtemp made it 0700.
|
|
const std::int32_t held = ::open(entry.path().c_str(), O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
|
if (held < 0) continue;
|
|
// Dated only once it is held: a directory that is kept is dated
|
|
// by its holder just before it lets go of it, so a date read
|
|
// before the lock can be one the holder has replaced since.
|
|
if (::flock(held, LOCK_EX | LOCK_NB) == 0) {
|
|
const auto changed = fs::last_write_time(entry.path(), entryError);
|
|
if (!entryError && now - changed >= std::chrono::minutes(1)) fs::remove_all(entry.path(), entryError);
|
|
}
|
|
::close(held);
|
|
}
|
|
}
|
|
|
|
fs::path MakeDirectory(const fs::path& parent, std::string_view prefix) {
|
|
RemoveAbandonedDirectories(parent, prefix);
|
|
std::string pattern = (parent / std::format("{}XXXXXX", prefix)).string();
|
|
if (::mkdtemp(pattern.data()) == nullptr) {
|
|
throw std::runtime_error(std::format("cannot make a directory in {}: {}", parent.string(), std::strerror(errno)));
|
|
}
|
|
return fs::path(pattern);
|
|
}
|
|
}
|
|
|
|
Crafter::PrivateDirectory::PrivateDirectory(const fs::path& parent, std::string_view prefix) : path(MakeDirectory(parent, prefix)) {
|
|
// Close-on-exec, so that the lock is this process's alone: a command it
|
|
// starts that outlives it does not keep the directory from being
|
|
// removed.
|
|
fd = ::open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
|
if (fd >= 0) {
|
|
while (::flock(fd, LOCK_EX) != 0 && errno == EINTR) {}
|
|
}
|
|
}
|
|
|
|
Crafter::PrivateDirectory::~PrivateDirectory() {
|
|
std::error_code ec;
|
|
if (kept) {
|
|
// Dated now, while it is still held: its date is that of what was
|
|
// last made or removed in it, which may be long before, as for the
|
|
// run of a test that failed after minutes, and a directory nobody
|
|
// holds is removed a minute after its date.
|
|
fs::last_write_time(path, fs::file_time_type::clock::now(), ec);
|
|
} else {
|
|
fs::remove_all(path, ec);
|
|
}
|
|
if (fd >= 0) ::close(fd);
|
|
}
|
|
|
|
std::string Crafter::BuildStdPcm(const Configuration& config, fs::path stdPcm) {
|
|
CacheLock lock(stdPcm.parent_path());
|
|
if(config.target == "x86_64-w64-mingw32") {
|
|
// libstdc++ 15 and newer install libstdc++.modules.json beside the
|
|
// library, naming the std module's source relative to itself. It is
|
|
// looked for in the library directories the compiler searches for
|
|
// this target, as it would look for libstdc++ itself, but only in
|
|
// those that belong to the mingw toolchain. The list also holds
|
|
// clang's resource directory, the sysroot's generic lib/, where a
|
|
// prefix given whole can keep a libstdc++ built for another target,
|
|
// and, when clang finds no gcc directory, an empty entry that would
|
|
// name a manifest in the working directory. A toolchain that isn't
|
|
// there is a build error, not an exception: Build() runs this with
|
|
// the threads of its external dependencies started.
|
|
const Toolchain& tc = GetToolchain();
|
|
const std::string where = tc.mingwPrefix.empty()
|
|
? std::string("where clang finds it by the x86_64-w64-mingw32 gcc on PATH")
|
|
: std::format("under {}", tc.mingwPrefix.string());
|
|
const std::string missing = std::format("no mingw-w64 libstdc++ with the std module (libstdc++ 15 or newer) {}. Install mingw-w64, or point CRAFTER_MINGW_DIR at its prefix, the directory holding x86_64-w64-mingw32/ and lib/gcc/", where);
|
|
// Asked once a run for each compiler and target: every mingw
|
|
// configuration comes through here, up to date or not, and the answer
|
|
// costs a clang.
|
|
static std::mutex probeMutex;
|
|
static std::unordered_map<std::string, CommandResult> probes;
|
|
const std::string probe = std::format("{} --target={} -print-search-dirs", GetBaseCommand(config), config.target);
|
|
CommandResult dirs;
|
|
{
|
|
std::lock_guard guard(probeMutex);
|
|
auto found = probes.find(probe);
|
|
if (found == probes.end()) found = probes.emplace(probe, RunCommandChecked(probe)).first;
|
|
dirs = found->second;
|
|
}
|
|
if (dirs.exitCode != 0) return std::format("{}: {}", missing, dirs.output);
|
|
fs::path manifestPath;
|
|
constexpr std::string_view librariesLine = "libraries: =";
|
|
if (const std::size_t at = dirs.output.find(librariesLine); at != std::string::npos) {
|
|
const std::size_t end = dirs.output.find('\n', at);
|
|
const std::string list = dirs.output.substr(at + librariesLine.size(), end == std::string::npos ? std::string::npos : end - at - librariesLine.size());
|
|
for (const auto part : std::views::split(list, ':')) {
|
|
const fs::path dir(std::string_view(part.begin(), part.end()));
|
|
const bool mingwOwn = std::ranges::contains(dir, fs::path("x86_64-w64-mingw32"));
|
|
if (mingwOwn && fs::exists(dir / "libstdc++.modules.json")) {
|
|
manifestPath = dir / "libstdc++.modules.json";
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (manifestPath.empty()) return missing;
|
|
// Found through the view MingwSysroot() may have made of the prefix,
|
|
// and named from here on by its place in the prefix, the directory
|
|
// the user knows and can go and look at.
|
|
manifestPath = fs::path(MingwViewToPrefix(manifestPath.string()));
|
|
const std::optional<std::string> source = StdModuleSourcePath(FileBytes(manifestPath));
|
|
if (!source) return std::format("{} names no std module", manifestPath.string());
|
|
const fs::path stdCc = (manifestPath.parent_path() / *source).lexically_normal();
|
|
if (!fs::exists(stdCc)) return std::format("{}, which {} names as the std module, is missing", stdCc.string(), manifestPath.string());
|
|
|
|
// The std.cc → std.cppm copy uses a plain filename: held under the
|
|
// cache lock, no other builder writes this path concurrently.
|
|
fs::path stdCppm = stdPcm.parent_path() / "std.cppm";
|
|
std::string command = std::format("{}{} --precompile {} -o {}", GetBaseCommand(config), StdPcmFlags(config, GetToolchain()), stdCppm.string(), stdPcm.string());
|
|
std::string stamp = HashStamp(command + '\0' + FileBytes(stdCc));
|
|
if (fs::exists(stdPcm) && ReadStamp(StampPath(stdPcm)) == stamp) return "";
|
|
std::error_code copyEc;
|
|
fs::copy_file(stdCc, stdCppm, fs::copy_options::overwrite_existing, copyEc);
|
|
if (copyEc) {
|
|
return std::format("copy {} -> {}: {}", stdCc.string(), stdCppm.string(), copyEc.message());
|
|
}
|
|
std::string result = RunBuildCommand(command);
|
|
if (result.empty()) WriteStamp(StampPath(stdPcm), stamp);
|
|
return result;
|
|
} else {
|
|
const bool isWasm = config.target.starts_with("wasm32");
|
|
// wasi-sdk drops std.cppm at <sysroot>/share/libc++/v1/, the rest of
|
|
// the libc++ ecosystem (e.g. /opt/aarch64-rootfs) follows FHS at
|
|
// <sysroot>/usr/share/libc++/v1/.
|
|
std::string stdCppm;
|
|
if (isWasm) {
|
|
stdCppm = std::format("{}/share/libc++/v1/std.cppm", config.ResolvedSysroot(GetToolchain()));
|
|
} else if (config.sysroot.empty()) {
|
|
stdCppm = GetToolchain().stdCppm.string();
|
|
} else {
|
|
// The source has to belong to the libc++ release whose headers
|
|
// the sysroot carries. Handing clang the toolchain's own copy
|
|
// regardless used to end in a page of "no member named ... in
|
|
// namespace 'std'" once the two releases differed.
|
|
StdModuleSource source = SysrootStdModule(GetToolchain(), config.sysroot);
|
|
if (!source.error.empty()) return source.error;
|
|
stdCppm = source.stdCppm.string();
|
|
}
|
|
// Keyed on the command and the module source's bytes, never on
|
|
// mtimes: an installed std.cppm can be dated at the epoch (OSTree),
|
|
// and a libc++ update under the same clang changes the bytes but
|
|
// not the path.
|
|
std::string command = std::format("{}{} --precompile {} -o {}", GetBaseCommand(config), StdPcmFlags(config, GetToolchain()), stdCppm, stdPcm.string());
|
|
std::string stamp = HashStamp(command + '\0' + FileBytes(stdCppm));
|
|
if (fs::exists(stdPcm) && ReadStamp(StampPath(stdPcm)) == stamp) return "";
|
|
std::string result = RunBuildCommand(command);
|
|
if (result.empty()) WriteStamp(StampPath(stdPcm), stamp);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
fs::path Crafter::GetCacheDir() {
|
|
if (const char* xdg = std::getenv("XDG_CACHE_HOME"); xdg && *xdg) {
|
|
return fs::path(xdg) / "crafter.build";
|
|
}
|
|
|
|
if (const char* home = std::getenv("HOME")) {
|
|
return fs::path(home) / ".cache" / "crafter.build";
|
|
}
|
|
|
|
throw std::runtime_error("Neither XDG_CACHE_HOME nor HOME set");
|
|
}
|
|
|
|
std::string Crafter::GetBaseCommand(const Configuration& config) {
|
|
const Toolchain& tc = GetToolchain();
|
|
std::string command = tc.clangxx.string();
|
|
if (config.target == "x86_64-w64-mingw32") {
|
|
// libstdc++ from the mingw toolchain, said outright: a clang built to
|
|
// default to libc++ (the Flatpak's) would otherwise reach for that.
|
|
// Found by clang's own mingw discovery unless there is a prefix.
|
|
command += " -stdlib=libstdc++";
|
|
if (const fs::path& sysroot = MingwSysroot(); !sysroot.empty()) command += std::format(" --sysroot={}", sysroot.string());
|
|
return command;
|
|
}
|
|
command += " -stdlib=libc++";
|
|
// A sysroot build (wasm, a cross rootfs) takes libc++ from the sysroot;
|
|
// only host builds can be pointed at an explicit libc++ install.
|
|
if (config.ResolvedSysroot(tc).empty()) command += tc.LibcxxCompileFlags();
|
|
return command;
|
|
}
|
|
|
|
namespace {
|
|
constexpr std::array<std::string_view, 16> CrafterBuildModules = {
|
|
"Crafter.Build-Toolchain",
|
|
"Crafter.Build-Shader",
|
|
"Crafter.Build-Platform",
|
|
"Crafter.Build-Flatpak",
|
|
"Crafter.Build-Jobs",
|
|
"Crafter.Build-Interface",
|
|
"Crafter.Build-Implementation",
|
|
"Crafter.Build-External",
|
|
"Crafter.Build-Agents",
|
|
"Crafter.Build-Clang",
|
|
"Crafter.Build-Test",
|
|
"Crafter.Build-Lint",
|
|
"Crafter.Build-Script",
|
|
"Crafter.Build-Progress",
|
|
"Crafter.Build-Asset",
|
|
"Crafter.Build",
|
|
};
|
|
|
|
// Returns the stamp of the module sources the cache was built from; it is
|
|
// one of project.so's inputs.
|
|
std::string EnsureCrafterBuildPcms(const fs::path& sourceDir, const fs::path& cacheDir, const Configuration& host) {
|
|
CacheLock lock(cacheDir);
|
|
// Re-derived under the lock: another builder may have refreshed the
|
|
// cache from its own sources while we waited.
|
|
// The std PCM is an input to every module PCM: its stamp is folded in
|
|
// so a rebuilt std.pcm (a libc++ update under the same clang) rebuilds
|
|
// them too, instead of leaving PCMs that import a std.pcm that is gone.
|
|
std::string stamp = HashStamp(CrafterBuildSourceStamp(sourceDir, CrafterBuildModules) + '\0' + ReadStamp(StampPath(cacheDir / "std.pcm")));
|
|
const bool upToDate = ReadStamp(CacheStampPath(cacheDir)) == stamp;
|
|
for (std::string_view name : CrafterBuildModules) {
|
|
fs::path cppmPath = sourceDir / std::format("{}.cppm", name);
|
|
fs::path pcmPath = cacheDir / std::format("{}.pcm", name);
|
|
if (!fs::exists(cppmPath)) {
|
|
throw std::runtime_error(std::format("module source {} not found in {} (set CRAFTER_BUILD_HOME)", name, sourceDir.string()));
|
|
}
|
|
if (upToDate && fs::exists(pcmPath)) {
|
|
continue;
|
|
}
|
|
std::string cmd = std::format(
|
|
"{} --target={} -march=native -mtune=native "
|
|
"-std=c++26 -O3 "
|
|
"-Wno-reserved-identifier -Wno-reserved-module-identifier "
|
|
"-fprebuilt-module-path={} "
|
|
"--precompile {} -o {}",
|
|
GetBaseCommand(host), host.target, cacheDir.string(), cppmPath.string(), pcmPath.string());
|
|
CommandResult r = Crafter::RunCommandChecked(cmd);
|
|
if (r.exitCode != 0) {
|
|
throw std::runtime_error(std::format("Failed to precompile {} (exit {}): {}", name, r.exitCode, r.output));
|
|
}
|
|
}
|
|
// Last, so an interrupted rebuild leaves the stamp disagreeing with the
|
|
// sources and the next run starts over rather than trusting a half-built
|
|
// set of PCMs.
|
|
if (!upToDate) WriteStamp(CacheStampPath(cacheDir), stamp);
|
|
return stamp;
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
using ProjectFn = Configuration (*)(std::span<const std::string_view>);
|
|
using ConfigurationsFn = std::vector<Configuration*> (*)(std::span<const std::string_view>);
|
|
|
|
// The entry point of a loaded project file: one of the two is set.
|
|
struct ProjectEntry {
|
|
ProjectFn one = nullptr;
|
|
ConfigurationsFn several = nullptr;
|
|
};
|
|
|
|
// Every Configuration a CrafterBuildProject returned to
|
|
// LoadConfigurations, kept for the rest of the process like the ones a
|
|
// CrafterBuildConfigurations returns, which its project file keeps:
|
|
// whoever holds a pointer LoadConfigurations gave out, the project cache
|
|
// or a consumer's `dependencies`, holds it that long.
|
|
std::mutex ReturnedConfigurationsMutex;
|
|
std::vector<std::unique_ptr<Configuration>> ReturnedConfigurations;
|
|
}
|
|
|
|
// Compiles the project file `guard` holds into project.so, unless the one
|
|
// there is current, loads it and finds its entry point. The frame holds
|
|
// `args` once this returns, for the call the caller makes next.
|
|
static ProjectEntry OpenProjectFile(ProjectFileGuard& guard, std::span<const std::string_view> args) {
|
|
// A copy: the loads of the file's dependencies push frames of their own,
|
|
// which can move this one.
|
|
const fs::path absProject = guard.Frame().project;
|
|
fs::path buildDir = absProject.parent_path() / "build";
|
|
if (!fs::exists(buildDir)) {
|
|
fs::create_directories(buildDir);
|
|
}
|
|
fs::path soPath = buildDir / std::format("{}.so", absProject.stem().string());
|
|
|
|
fs::path sourceDir = Crafter::GetCrafterBuildHome();
|
|
|
|
// The host toolchain's own triple (x86_64-pc-linux-gnu on glibc distros,
|
|
// x86_64-alpine-linux-musl on Alpine): project.cpp and the module PCMs
|
|
// it imports must be built for the libc crafter-build itself runs on.
|
|
Configuration hostConfig;
|
|
hostConfig.target = HostTarget();
|
|
hostConfig.march = "native";
|
|
hostConfig.mtune = "native";
|
|
// The same directory Build() resolves the std PCM from, toolchain key
|
|
// included (see StdPcmDir).
|
|
fs::path cacheDir = StdPcmDir(hostConfig);
|
|
if (!fs::exists(cacheDir)) fs::create_directories(cacheDir);
|
|
|
|
std::string stdResult = BuildStdPcm(hostConfig, cacheDir / "std.pcm");
|
|
if (!stdResult.empty()) {
|
|
throw std::runtime_error(std::format("Failed to build std.pcm: {}", stdResult));
|
|
}
|
|
|
|
const std::string sourcesStamp = EnsureCrafterBuildPcms(sourceDir, cacheDir, hostConfig);
|
|
|
|
// After the host PCMs exist (a dependency's build imports Crafter.Build
|
|
// from that cache) and before the staleness check (its artifacts are
|
|
// inputs to project.so). Built on every load, fresh or not: a dependency
|
|
// BMI whose own imports were rebuilt underneath it doesn't fail cleanly,
|
|
// clang crashes on it.
|
|
ProjectFileDependencies preDeps = ResolveProjectFileDependencies(absProject, hostConfig);
|
|
|
|
// -fuse-ld=lld like every other link the tool performs: lld is a hard
|
|
// dependency, binutils' ld is not (Alpine's clang would default to it),
|
|
// and a dependency archive built in release holds ThinLTO bitcode,
|
|
// which GNU ld can't read.
|
|
std::string compileCmd = std::format(
|
|
"{} --target={} -march=native -mtune=native "
|
|
"-std=c++26 -shared -fPIC -O3 -fuse-ld=lld "
|
|
"-Wno-return-type-c-linkage "
|
|
"-fprebuilt-module-path={}{} "
|
|
"{} -o {}{}{}",
|
|
GetBaseCommand(hostConfig), hostConfig.target, cacheDir.string(), preDeps.compileFlags,
|
|
absProject.string(), soPath.string(), preDeps.linkFlags, GetToolchain().LibcxxLinkFlags());
|
|
// What project.so was compiled against. The module source stamp stands
|
|
// in for the executable: project.so resolves Crafter:: symbols from the
|
|
// running binary, so only the declarations (the .cppm files) matter,
|
|
// and an implementation-only rebuild of crafter-build leaves it valid.
|
|
// The separators are appended, not written into a format string: a
|
|
// format string is read as a C string, so one with "\0" in it ends at the
|
|
// first NUL and everything after the version silently dropped out of the
|
|
// stamp. A project.so compiled against other declarations (a Shader with
|
|
// a different layout, say) was then loaded as current and corrupted the
|
|
// heap.
|
|
std::string projectStampInput(CrafterBuildVersion());
|
|
for (std::string_view part : {std::string_view(GetToolchain().Key()), std::string_view(sourcesStamp), std::string_view(compileCmd)}) {
|
|
projectStampInput += '\0';
|
|
projectStampInput += part;
|
|
}
|
|
const std::string projectStamp = HashStamp(projectStampInput);
|
|
|
|
if (ProjectFileStale(soPath, absProject, projectStamp, preDeps)) {
|
|
std::string result = RunBuildCommand(compileCmd);
|
|
if (!result.empty()) {
|
|
throw std::runtime_error(std::format("Failed to compile project {}: {}", absProject.string(), result));
|
|
}
|
|
WriteStamp(StampPath(soPath), projectStamp);
|
|
}
|
|
|
|
// Before dlopen, so the file's static initialisers can already call
|
|
// ProjectDependency(): its dependencies are the same on every load.
|
|
{
|
|
std::lock_guard lock(PragmaDependenciesMutex);
|
|
PragmaDependenciesByProject[absProject] = preDeps.configs;
|
|
}
|
|
guard.Frame().dependencies = std::move(preDeps.configs);
|
|
void* handle = dlopen(soPath.c_str(), RTLD_NOW | RTLD_GLOBAL);
|
|
if (!handle) {
|
|
throw std::runtime_error(std::format("Failed to load project {}: {}", soPath.string(), dlerror()));
|
|
}
|
|
|
|
// Looked up in project.so and the libraries it links, which is where
|
|
// dlsym searches for a handle: every project file this process has
|
|
// loaded exports an entry point under one of these two names, and none
|
|
// of the others is found here.
|
|
ProjectEntry entry;
|
|
entry.one = reinterpret_cast<ProjectFn>(dlsym(handle, "CrafterBuildProject"));
|
|
entry.several = reinterpret_cast<ConfigurationsFn>(dlsym(handle, "CrafterBuildConfigurations"));
|
|
if (!entry.one && !entry.several) {
|
|
throw std::runtime_error(std::format("{} defines neither CrafterBuildProject nor CrafterBuildConfigurations (looked in {})", absProject.string(), soPath.string()));
|
|
}
|
|
// Calling the first would leave what the second returns unbuilt, and
|
|
// the other way around, without a word.
|
|
if (entry.one && entry.several) {
|
|
throw std::runtime_error(std::format("{} defines both CrafterBuildProject and CrafterBuildConfigurations; a project file has one entry point", absProject.string()));
|
|
}
|
|
|
|
// The args only for the call itself, unlike the dependencies. A static
|
|
// initialiser runs once per process, on the first load of the file: a
|
|
// later load gets the same mapping back from dlopen, so a spec built at
|
|
// namespace scope kept the first load's args for every load after it.
|
|
// Now it gets none, whichever load ran it. The pragma pre-pass above
|
|
// also ran on this frame and relies on the args not being there yet.
|
|
guard.Frame().args = args;
|
|
return entry;
|
|
}
|
|
|
|
Configuration Crafter::LoadProject(const fs::path& projectFile, std::span<const std::string_view> args) {
|
|
RequireToolchain();
|
|
const fs::path absProject = fs::canonical(projectFile);
|
|
ProjectFileGuard guard(absProject);
|
|
const ProjectEntry entry = OpenProjectFile(guard, args);
|
|
if (!entry.one) {
|
|
throw std::runtime_error(std::format("{} defines CrafterBuildConfigurations, which LoadProject cannot return by value; load it with LoadConfigurations", absProject.string()));
|
|
}
|
|
return entry.one(args);
|
|
}
|
|
|
|
std::vector<Configuration*> Crafter::LoadConfigurations(const fs::path& projectFile, std::span<const std::string_view> args) {
|
|
RequireToolchain();
|
|
const fs::path absProject = fs::canonical(projectFile);
|
|
ProjectFileGuard guard(absProject);
|
|
const ProjectEntry entry = OpenProjectFile(guard, args);
|
|
if (entry.one) {
|
|
// The call's return value is the initialiser of the new-expression,
|
|
// so the Configuration is constructed where it stays and is never
|
|
// moved; std::make_unique would move a temporary into place. 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.
|
|
std::unique_ptr<Configuration> returned(new Configuration(entry.one(args)));
|
|
Configuration* const configuration = returned.get();
|
|
const std::lock_guard lock(ReturnedConfigurationsMutex);
|
|
ReturnedConfigurations.push_back(std::move(returned));
|
|
return { configuration };
|
|
}
|
|
|
|
std::vector<Configuration*> configurations = entry.several(args);
|
|
if (configurations.empty()) {
|
|
throw std::runtime_error(std::format("{}: CrafterBuildConfigurations returned no configuration", absProject.string()));
|
|
}
|
|
std::unordered_set<const Configuration*> seen;
|
|
for (const Configuration* configuration : configurations) {
|
|
if (!configuration) {
|
|
throw std::runtime_error(std::format("{}: CrafterBuildConfigurations returned a null pointer", absProject.string()));
|
|
}
|
|
// Most often a pasted `&lib` where `&tool` was meant, which would
|
|
// leave `tool` unbuilt without a word.
|
|
if (!seen.insert(configuration).second) {
|
|
throw std::runtime_error(std::format("{}: CrafterBuildConfigurations returned {} twice", absProject.string(), configuration->name));
|
|
}
|
|
// One that a project GitProject or LocalProject loaded keeps to
|
|
// itself, taken out of the `dependencies` or the `tests` of what
|
|
// that project's file returned. Returned from here it would count
|
|
// as one this file
|
|
// made: `test` would run its tests and `run` offer its scripts,
|
|
// which are that project's. And only the configurations a project
|
|
// file returns are rebased onto its directory, so this one would be
|
|
// built wherever its relative `path` leads from here.
|
|
if (const std::optional<LoadedProject> keeper = KeptBy(*configuration)) {
|
|
throw std::runtime_error(std::format("{}: CrafterBuildConfigurations returned {}, which {} keeps to itself: a project file returns the configurations it makes and the ones GitProject or LocalProject returned", absProject.string(), configuration->name, keeper->name));
|
|
}
|
|
}
|
|
return configurations;
|
|
}
|