HeadlessCompositor drives RunSingleTest with itself in every part. The harness starts it again as the compositor, which listens on a wayland-7 socket in the XDG_RUNTIME_DIR it is given and records its environment, and as the test, which records its own and connects. A test that sets wayland passes against its compositor and runs with WAYLAND_DISPLAY naming the compositor's socket, without DISPLAY and WAYLAND_SOCKET, and with the XDG_RUNTIME_DIR of the process that runs it. The compositor gets a directory of its own in that one, or in /tmp where that process has none, and none of the WAYLAND_DISPLAY, WAYLAND_SOCKET and DISPLAY that process has, each given a value of the session's in the driver, and afterwards it has been sent SIGTERM, is gone and so is its directory. The compositor's timeout(1) is the test's, the wait for its socket and 30 s more, and none for a test with no time limit; and a test whose binary's path has an '=' in it runs as any other. A test that does not set wayland, and one run under `host`, keep that display. Without an override CRAFTER_BUILD_COMPOSITOR names the compositor, and an override wins over it. A compositor sh cannot find fails the test with status 127, named, and a line on how a test that may run without it is skipped; one that ends before it listens fails it with its status and what it printed, checked on a word the command does not hold, without running the test; a failing test keeps what its compositor printed; a compositor killed while the test runs is reported as ended. A stand-in crafter-build killed in the middle of a test has its compositor sent SIGTERM and gone, and the directory it leaves is removed when the next is made, once dated past the minute, while one changed a moment ago and one that is held stay. Without PR_SET_PDEATHSIG the two checks on the killed crafter-build fail, and without the removal of abandoned directories the one on its directory does; with the test's own words as env's command, the check on the binary whose path has an '=' fails, with a limit of its own for the compositor of a test with none the one on that limit does, without what the compositor printed in the failure the one on that, with WAYLAND_SOCKET left to the compositor and the test the ones on it, and without the command in the failure of a compositor sh cannot find the one on that, which looks for the harness's words, not sh's. No weston is needed, so the suite runs it in the native loop and in the Flatpak's build sandbox alike. The driver makes its compositors' directories in a directory of its own in $XDG_RUNTIME_DIR, or in /tmp where that is not a directory, rather than in the temp directory, since a socket's path holds 107 bytes and a temp directory's path can use them up; its check without an XDG_RUNTIME_DIR has the harness make that compositor's directory in /tmp itself, as crafter-build does where there is none. The driver's directory is a PrivateDirectory, now exported for it, so that one a killed driver left goes when a later one is made, once it has not changed for a minute, and AGENTS.template.md's line on where tests write now says that what a test runs writes caches of its own, crafter-build's builds and wasmtime the modules the wasm tests run, and names this test as the one that writes anywhere else.
237 lines
14 KiB
C++
237 lines
14 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
export module Crafter.Build:Platform;
|
|
import std;
|
|
namespace fs = std::filesystem;
|
|
|
|
namespace Crafter {
|
|
struct Configuration;
|
|
struct CommandResult {
|
|
std::int32_t exitCode = 0;
|
|
std::string output;
|
|
bool crashed = false;
|
|
bool timedOut = false;
|
|
std::int32_t signal = 0;
|
|
};
|
|
// An exclusive lock on the file `path` (created if missing), held from
|
|
// construction to destruction. An flock, so it keeps out the other
|
|
// threads of this process (each opens the file anew) and other
|
|
// crafter-build processes alike. The OS drops it when the descriptor
|
|
// closes, process death included, so an interrupted build never leaves a
|
|
// stale lock; the descriptor is close-on-exec, so the tools a holder
|
|
// spawns don't keep it past its release.
|
|
class FileLock {
|
|
std::int32_t fd = -1;
|
|
public:
|
|
explicit FileLock(const fs::path& path);
|
|
~FileLock();
|
|
FileLock(const FileLock&) = delete;
|
|
FileLock& operator=(const FileLock&) = delete;
|
|
};
|
|
std::string BuildStdPcm(const Configuration& config, fs::path stdPcm);
|
|
fs::path GetCacheDir();
|
|
CommandResult RunCommandChecked(std::string_view command);
|
|
// A compile / link / archive step. Success is the command's exit status,
|
|
// never whether it printed anything: a warning does not stop the compiler
|
|
// from writing a valid object, and warnings are only emitted when a
|
|
// translation unit is actually recompiled — so keying failure off the
|
|
// output made the same unchanged source pass or fail depending on whether
|
|
// its object happened to be up to date.
|
|
//
|
|
// Returns an empty string on success (any warnings go to the user via
|
|
// Progress::Diagnostic), otherwise the compiler's diagnostics. Callers
|
|
// therefore keep the `if (!result.empty())` shape they already had.
|
|
//
|
|
// The command runs on a worker of JobPool::Process(), so no more of these
|
|
// steps run at once than that pool has workers: called on one of its
|
|
// workers it runs there, called from any other thread it waits for one.
|
|
// A step that runs a compiler any other way is not counted.
|
|
std::string RunBuildCommand(std::string_view command);
|
|
export CommandResult RunCommandWithTimeout(std::string_view command, std::chrono::seconds timeout);
|
|
// Runs `command` through sh with this process's own stdin, stdout and
|
|
// stderr, so its output appears as it is produced and it can prompt.
|
|
// Every helper above captures instead, which suits a compile step and
|
|
// not a command that runs for half an hour. Returns the exit code, or
|
|
// 128+signal when the command was killed; a nonzero exit is the caller's
|
|
// to judge and never throws. While it runs, Ctrl-C goes to the command
|
|
// and not to crafter-build, which then sees it as exit code 130.
|
|
// Project scripts reach this through ScriptContext::Run.
|
|
std::int32_t RunCommandAttached(std::string_view command);
|
|
// One change to the environment a command inherits from this process:
|
|
// the variable set to `value`, or taken out when there is none.
|
|
struct EnvironmentChange {
|
|
std::string name;
|
|
std::optional<std::string> value;
|
|
};
|
|
// A command that runs beside its caller, through sh, until Stop() or the
|
|
// destructor ends it: the compositor of a test that sets Test::wayland.
|
|
// Its stdin is /dev/null and its stdout and stderr go to `log`. It is
|
|
// the leader of a process group of its own, so that Stop() reaches what
|
|
// it starts in turn, as long as that stays in the group, and runs under `timeout`, which ends that group
|
|
// after `lifetime` if nothing else has, a `lifetime` of 0 being none,
|
|
// as for timeout(1). The timeout process is also sent
|
|
// SIGTERM, which it passes on to the group, when the thread that
|
|
// constructed this ends, so a crafter-build that is killed, by Ctrl-C
|
|
// or otherwise, leaves no command of this kind running. That is why the
|
|
// object has to stay on the thread that made it. Throws when the command
|
|
// cannot be started.
|
|
class BackgroundCommand {
|
|
std::int32_t pid = -1;
|
|
std::optional<std::int32_t> exitCode;
|
|
bool reaped = false;
|
|
public:
|
|
BackgroundCommand(std::string_view command, std::span<const EnvironmentChange> environment, const fs::path& log, std::chrono::seconds lifetime);
|
|
~BackgroundCommand();
|
|
BackgroundCommand(const BackgroundCommand&) = delete;
|
|
BackgroundCommand& operator=(const BackgroundCommand&) = delete;
|
|
// The command's exit code once it has ended on its own, 128+signal
|
|
// when a signal ended it; nullopt while it runs. Does not wait.
|
|
std::optional<std::int32_t> Exited();
|
|
// Sends the group SIGTERM, then SIGKILL to whatever of it is left
|
|
// once the command has ended or `grace` has passed, and waits for the
|
|
// command. Further calls do nothing.
|
|
void Stop(std::chrono::milliseconds grace);
|
|
};
|
|
// A new directory in `parent`, named `prefix` and six random characters,
|
|
// that only this user may enter (mkdtemp), removed with everything in it
|
|
// when this ends. A process that is killed runs no destructor, so this
|
|
// also holds an flock on the directory, which the OS drops with the
|
|
// process, and making one removes the directories of `prefix` in
|
|
// `parent` that nobody holds and that have not changed for a minute:
|
|
// left behind by a process that is gone. The minute covers one that is
|
|
// being made at that moment, between mkdtemp and its lock. Throws when
|
|
// the directory cannot be made.
|
|
// Exported for the tests that make a directory of their own outside the
|
|
// temp directory.
|
|
export class PrivateDirectory {
|
|
std::int32_t fd = -1;
|
|
public:
|
|
const fs::path path;
|
|
PrivateDirectory(const fs::path& parent, std::string_view prefix);
|
|
~PrivateDirectory();
|
|
PrivateDirectory(const PrivateDirectory&) = delete;
|
|
PrivateDirectory& operator=(const PrivateDirectory&) = delete;
|
|
};
|
|
// The link flags that record `dir` as a run path, with -Xlinker rather
|
|
// than -Wl,: clang splits a -Wl, value at every comma, and `dir` may be a
|
|
// project's own directory, named whatever its owner chose. Quoted for
|
|
// the shell the link runs in.
|
|
std::string RunPathFlags(std::string_view dir);
|
|
// `s` as one single-quoted sh word, safe to splice into a command line
|
|
// whatever it contains: paths with spaces, arguments a user typed.
|
|
export std::string ShellQuote(std::string_view s);
|
|
// `s` as a JSON string, quotes included, whatever it contains: what a
|
|
// generated manifest or variants.json writes for a path or an argument.
|
|
std::string JsonQuote(std::string_view s);
|
|
// Why crafter-build cannot run the host's flatpak for a project in
|
|
// `projectDir`; empty when it can. Natively that is the flatpak on PATH.
|
|
// Inside crafter-build's Flatpak it takes the org.freedesktop.Flatpak
|
|
// permission, a session bus to reach the host's Flatpak session helper
|
|
// over (flatpak-builder's build sandbox, where CI runs, has neither), and
|
|
// a host that sees the project's bin/ and build/ where crafter-build
|
|
// does, which it does not in the sandbox's own /tmp, /var, /usr, /etc or
|
|
// /app: the host's flatpak is started in a directory made in each for
|
|
// it, so bin/ and build/ are made where they are missing.
|
|
export std::string HostFlatpakUnavailable(const fs::path& projectDir);
|
|
// Runs `flatpak <arguments>` on the host with the terminal attached,
|
|
// after echoing it as ScriptContext::Run does, and returns its exit
|
|
// code. Inside crafter-build's Flatpak it goes through flatpak-spawn
|
|
// --host --watch-bus, so the host command ends when flatpak-spawn does,
|
|
// starts it in the host's /, whatever crafter-build's working directory,
|
|
// and passes FLATPAK_USER_DIR on when it is set. crafter-build's own code
|
|
// reaches the host only through HostCommand, the builder of host command
|
|
// lines this shares with RunHostFlatpakBuilder and HostFlatpakUnavailable's
|
|
// probes, for the host's flatpak and flatpak-builder; add no other host
|
|
// command (AGENTS.md, Security considerations). The permission is the
|
|
// sandbox's, not this function's: anything else that runs there, a
|
|
// project.cpp among them, can use it through flatpak-spawn --host
|
|
// without this. Exported for the tests. Host commands have no terminal
|
|
// of their own, so `arguments` must not need one to answer a prompt.
|
|
export std::int32_t RunHostFlatpak(std::string_view arguments);
|
|
// `flatpak-builder <arguments>` the same way: the host's flatpak-builder,
|
|
// or the org.flatpak.Builder app where the host has none.
|
|
std::int32_t RunHostFlatpakBuilder(std::string_view arguments);
|
|
std::string GetBaseCommand(const Configuration& config);
|
|
// The configurations `projectFile` describes, in the order it gives them.
|
|
// Compiles the file into a shared object in the build/ beside it, unless
|
|
// the one there is current, loads that into this process and calls the
|
|
// file's entry point with `args`. A project file defines one of two:
|
|
// extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> args);
|
|
// extern "C" std::vector<Configuration*> CrafterBuildConfigurations(std::span<const std::string_view> args);
|
|
// The first is a project of one configuration, which comes back as a
|
|
// list of one. The second is a project of several that need not have
|
|
// anything to do with each other, such as a library and a tool, or the
|
|
// programs of one repository; they come back as the file returned them.
|
|
// The two names are how a loaded file says which it is: both are C
|
|
// symbols, which carry no return type, so one name with two return types
|
|
// would be called as whichever the loader guessed. Throws for a file
|
|
// that defines neither or both, and for a CrafterBuildConfigurations
|
|
// that returns nothing, a null pointer, one configuration twice, or one
|
|
// that a project GitProject or LocalProject loaded keeps to itself
|
|
// (KeptBy): every configuration in the list is one the file made or one
|
|
// those two returned.
|
|
//
|
|
// The pointers are used for as long as the process runs. What a
|
|
// CrafterBuildConfigurations returns is its project file's to keep that
|
|
// long, at addresses that do not change: in a std::deque the file only
|
|
// adds to, one new set per call, or what GitProject and LocalProject
|
|
// gave it. What a CrafterBuildProject returns is kept here, constructed
|
|
// by that call and never moved.
|
|
export std::vector<Configuration*> LoadConfigurations(const fs::path& projectFile, std::span<const std::string_view> args);
|
|
// What the CrafterBuildProject of `projectFile` returns, loaded as
|
|
// LoadConfigurations loads it. Throws for a project file that defines
|
|
// CrafterBuildConfigurations: the configurations that one returns stay
|
|
// where the file keeps them and cannot be returned by value.
|
|
export Configuration LoadProject(const fs::path& projectFile, std::span<const std::string_view> args);
|
|
// A dependency the project file currently running its entry point
|
|
// declared with `#pragma crafter dependency`, so it can also be wired
|
|
// into the build graph (a test that links the same module, say) without
|
|
// spelling the source twice. `name` is the dependency's Configuration
|
|
// name or any module it exports — whatever the project file `import`s.
|
|
// Throws when no such dependency was declared, or when called from
|
|
// anywhere other than inside a project file's load.
|
|
export Configuration* ProjectDependency(std::string_view name);
|
|
// True while the project file crafter-build was pointed at runs its
|
|
// entry point, and when no project file is running at all; false
|
|
// inside a dependency's project file (a `#pragma crafter dependency`,
|
|
// GitProject or LocalProject target). Wire anything only the project's
|
|
// own build or test run needs behind it. The case that forced it: a
|
|
// library's test links crafter-build itself, so when crafter-build loads
|
|
// that library as a dependency, the test's GitProject loads crafter-build's
|
|
// project file in turn and the two recurse until the cycle guard throws.
|
|
export bool IsTopLevelProject();
|
|
// What the `#pragma crafter dependency` lines of `projectFile` resolved
|
|
// to the last time it was loaded in this process, in file order;
|
|
// empty when it has none or was never loaded. Unlike ProjectDependency
|
|
// this still answers once the load has returned, which is when the
|
|
// dependency notes of the project crafter-build was pointed at are
|
|
// gathered (Crafter.Build:Agents).
|
|
std::vector<Configuration*> PragmaDependencies(const fs::path& projectFile);
|
|
// True while this thread loads a `#pragma crafter dependency`: the tool's
|
|
// project file and every sibling it loads in turn. GitProject and
|
|
// LocalProject key the project cache on it, so host tooling, which
|
|
// ForceHostTooling rewrites in place, never shares a Configuration with a
|
|
// load for the target.
|
|
bool LoadingHostTooling();
|
|
// Resolves the directory holding distributed runtime assets (Crafter.Build
|
|
// module sources, wasi-runtime/, etc). Honors CRAFTER_BUILD_HOME; otherwise
|
|
// derives <prefix>/share/crafter-build from the running executable's path.
|
|
export fs::path GetCrafterBuildHome();
|
|
// Newest mtime among the prerequisites a compiler recorded in `depFile`
|
|
// (clang's `-MD -MF <artifact>.d`), or clang-scan-deps for the scan of
|
|
// a C++ source. This is how a build step learns which headers its
|
|
// source pulled in: only the tool that preprocessed the source knows
|
|
// which files its #includes opened.
|
|
//
|
|
// Returns file_time_type::max() when freshness cannot be proven — no
|
|
// depfile (nothing has compiled this artifact since depfiles were emitted,
|
|
// including every object left over from an older crafter-build) or a
|
|
// prerequisite that no longer exists. Callers compare the result against
|
|
// their artifact's mtime, so max() reads as "rebuild".
|
|
fs::file_time_type NewestPrerequisite(const fs::path& depFile);
|
|
// Wildcard name matching ('*', '?') shared by the test and lint verbs.
|
|
bool MatchGlob(std::string_view glob, std::string_view name);
|
|
// Empty `globs` matches everything.
|
|
bool MatchAny(std::span<const std::string> globs, std::string_view name);
|
|
}
|