feat(script): scripts in project.cpp, run with crafter-build run

A project.cpp could describe how a project is built, tested and linted,
but not the chores around it: packaging, installing, publishing. Those
stayed shell lines in a README or in someone's history.

Configuration gains `scripts`, a plain list like `defines`. A Script is
a name and a C++ callback, as a lint rule is; there is no script
language and no config file. The callback gets a ScriptContext (the
project, the project file's directory, the arguments) and returns the
exit status. ctx.Run runs a shell command with the terminal attached
and returns its status. It is only stored when project.cpp loads, which
happens for every verb and for every project that depends on this one.

`crafter-build run <name> [args...]` runs one, a bare `run` lists them.
Only the scripts of the project crafter-build was pointed at are
runnable, never a dependency's.

Everything after the script's name is handed to the script untouched.
The two branches that do this sit ahead of every other branch of the
parse loop, --version and --help included. Below the verbs,
`run package clean` would delete build/, and any other word would land
in the project's args and so in the variant hash, moving the build to
another directory. Options before the name keep their meaning, so
project args go there.

The driver is its own partition, :Script. It refuses a project with no
scripts, a name that is empty or starts with '-' (it could never be
typed), a name declared twice (running either would be a guess) and an
unknown name, each with exit 1 and without running anything. A script
that throws exits 1. A return value outside 0..255 becomes 1: an exit
status is eight bits, and a raw wait status such as 256 would otherwise
wrap to success.

Two helpers in :Platform:

- RunCommandAttached runs a command with inherited stdio. Every helper
  so far captured through popen, which is right for a compile step and
  useless for a command that runs for half an hour or asks a question.
  It reads the wait status through DecodeWaitStatus, which
  RunCommandChecked and RunCommandWithTimeout now share.
- ShellQuote is exported. It existed twice, as ShellQuote in :External
  and ShellQuoteSh in :Test, with the same behaviour; both are gone. A
  script that puts a path into a command needs it too.

Not included: a string shorthand for one-command scripts (a one-line
lambda covers it) and a ctx.Build() helper (Build() is already
exported).

A new partition and new exported symbols: run ./build.sh.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Jorijn van der Graaf 2026-10-06 18:16:00 +02:00
commit 561398013c
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
12 changed files with 313 additions and 56 deletions

View file

@ -12,6 +12,7 @@ import :Platform;
import :Toolchain;
import :Test;
import :Lint;
import :Script;
import :Progress;
import :Asset;
namespace fs = std::filesystem;
@ -1594,6 +1595,7 @@ R"(Usage:
{0} test [test-options] [globs...] Build and run the project's tests
{0} lint [lint-options] [globs...] Run the project's lint rules over its sources
{0} format [format-options] [globs...] Apply the project's transform rules (rewrites files)
{0} run [script [args...]] Run a script the project defines, or list them
{0} clean Delete the project's bin/ and build/ trees
{0} help | -h | --help Show this help
{0} --version Print the version and the compiler in use
@ -1646,6 +1648,23 @@ Format options (after the `format` subcommand):
transform. `format` rewrites changed files in place; `lint` reports the
same transforms as would-reformat findings without writing.
Run (after the `run` subcommand):
<script> [args...] Run the named script. Everything after the name is
handed to the script untouched, including what
looks like an option of crafter-build itself, so
-v, -q, --project= and project args go before it.
--list List the project's scripts. A bare `run` does too.
Scripts are defined in project.cpp, as entries of cfg.scripts:
cfg.scripts.push_back({{"docs", [](Crafter::ScriptContext& ctx) {{
return ctx.Run("python3 tools/gen-docs.py");
}}}});
The callback returns the exit status. ctx.Run runs a shell command with the
terminal attached and returns its status; ctx.args holds the arguments and
ctx.projectDir the project's directory. Commands run in the directory
crafter-build was started from. Only the scripts of the project being run
are available, never a dependency's.
Clean (after the `clean` subcommand):
Removes bin/ and build/ next to the project file. Does not load project.cpp,
so it works when the project itself no longer compiles. Every target, variant
@ -1677,6 +1696,8 @@ Exit status:
0 success / all non-skipped tests passed / lint clean / format applied
1 build failure, one or more tests failed, lint findings, no rules defined,
format write errors, or `format --check` found files that would change
`run` exits with the status its script returned, and with 1 when there is no
such script, when the script throws, or when it returns a value outside 0..255
)", argv0);
}
@ -1692,14 +1713,29 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) {
bool runLint = false;
bool runFormat = false;
bool runClean = false;
bool runScript = false;
bool runAfterBuild = false;
RunTestsOptions testOpts;
RunLintOptions lintOpts;
RunScriptOptions scriptOpts;
Progress::Verbosity verbosity = Progress::Verbosity::Default;
for (std::int32_t i = 1; i < argc; ++i) {
std::string_view arg = argv[i];
if (arg == "--version") {
// The two `run` branches come before everything else on purpose.
// What follows a script's name belongs to the script, whatever
// it looks like: `run package --help` must not print this
// tool's help, `run package clean` must not delete build/, and
// none of it may reach projectArgs, where it would change the
// variant hash and so the directory the project builds into.
if (scriptOpts.named) {
scriptOpts.args.emplace_back(arg);
} else if (runScript && !arg.starts_with("-")) {
// The first word after `run`, even one that is a verb
// elsewhere. Options before it keep their usual meaning.
scriptOpts.name = arg;
scriptOpts.named = true;
} else if (arg == "--version") {
// Answered here, before the project file is looked for, so
// it works in any directory and is never forwarded to
// project.cpp as an unknown argument.
@ -1717,6 +1753,10 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) {
} else if (!runTests && !runLint && !runClean && arg == "format") {
runFormat = true;
lintOpts.mode = LintMode::Apply;
} else if (!runTests && !runLint && !runFormat && !runClean && arg == "run") {
runScript = true;
} else if (runScript && arg == "--list") {
scriptOpts.listOnly = true;
} else if (runFormat && arg == "--check") {
lintOpts.mode = LintMode::Check;
} else if (arg == "-r") {
@ -1778,6 +1818,11 @@ std::int32_t Crafter::Run(std::int32_t argc, char** argv) {
Configuration config = LoadProject(projectFile, projectArgs);
SetParentProject(&config);
if (runScript) {
scriptOpts.projectFile = fs::absolute(projectFile).lexically_normal();
return RunScript(config, scriptOpts);
}
if (runLint || runFormat) {
// lexically_normal: fs::absolute keeps a "./" component, which
// would leak into displayed paths and the project-root derivation.

View file

@ -21,16 +21,6 @@ std::string DeriveName(const GitSource& source) {
return slash == std::string::npos ? url : url.substr(slash + 1);
}
std::string ShellQuote(std::string_view s) {
std::string out = "'";
for (char c : s) {
if (c == '\'') out += "'\\''";
else out += c;
}
out += "'";
return out;
}
std::string JoinOptions(std::span<const std::string> options) {
std::string out;
for (const std::string& opt : options) {

View file

@ -552,6 +552,35 @@ bool Crafter::IsTopLevelProject() {
return LoadingFrames.size() <= 1;
}
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::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{};
@ -564,19 +593,27 @@ CommandResult Crafter::RunCommandChecked(std::string_view cmd) {
result.output += buffer.data();
}
std::int32_t status = pclose(pipe);
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;
}
DecodeWaitStatus(pclose(pipe), result);
return result;
}
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{};
@ -592,25 +629,16 @@ CommandResult Crafter::RunCommandWithTimeout(std::string_view cmd, std::chrono::
result.output += buffer.data();
}
std::int32_t status = pclose(pipe);
if (WIFEXITED(status)) {
std::int32_t code = WEXITSTATUS(status);
if (code == 124) {
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;
result.exitCode = 124;
} else if (code >= 128) {
result.signal = code - 128;
result.exitCode = code;
} else if (result.exitCode >= 128) {
result.signal = result.exitCode - 128;
result.crashed = true;
} else {
result.exitCode = code;
}
} else if (WIFSIGNALED(status)) {
result.signal = WTERMSIG(status);
result.exitCode = 128 + result.signal;
result.crashed = true;
} else {
result.exitCode = -1;
}
return result;
}
@ -736,7 +764,7 @@ std::string Crafter::GetBaseCommand(const Configuration& config) {
}
namespace {
constexpr std::array<std::string_view, 12> CrafterBuildModules = {
constexpr std::array<std::string_view, 13> CrafterBuildModules = {
"Crafter.Build-Toolchain",
"Crafter.Build-Shader",
"Crafter.Build-Platform",
@ -746,6 +774,7 @@ namespace {
"Crafter.Build-Clang",
"Crafter.Build-Test",
"Crafter.Build-Lint",
"Crafter.Build-Script",
"Crafter.Build-Progress",
"Crafter.Build-Asset",
"Crafter.Build",

View file

@ -0,0 +1,100 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
module Crafter.Build:Script_impl;
import std;
import :Script;
import :Clang;
import :Platform;
import :Progress;
namespace fs = std::filesystem;
using namespace Crafter;
namespace {
std::string JoinNames(std::span<const Script> scripts) {
std::string out;
for (const Script& script : scripts) {
if (!out.empty()) out += ", ";
out += script.name;
}
return out;
}
}
std::int32_t ScriptContext::Run(std::string_view command) const {
// Echoed by default, unlike a compile step: a script's commands are the
// only account of what it is doing to the machine.
if (Progress::GetVerbosity() != Progress::Verbosity::Quiet) std::println("$ {}", command);
return RunCommandAttached(command);
}
std::int32_t Crafter::RunScript(Configuration& projectCfg, const RunScriptOptions& opts) {
// Loading the project may have left an in-place status line behind.
Progress::Clear();
const std::span<const Script> scripts = projectCfg.scripts;
if (scripts.empty()) {
std::println(std::cerr,
R"msg(No scripts defined.
Add them in project.cpp before returning the Configuration:
cfg.scripts.push_back({{"docs", [](Crafter::ScriptContext& ctx) {{
return ctx.Run("python3 tools/gen-docs.py");
}}}});
`crafter-build run docs [args...]` then runs it and exits with what it
returned; ctx.args holds the arguments.)msg");
return 1;
}
// Checked for every script on every run, the list included, so a bad
// declaration is reported the first time anyone uses `run` and not only
// when that one name is asked for.
std::unordered_set<std::string_view> seen;
for (const Script& script : scripts) {
if (script.name.empty() || script.name.starts_with('-')) {
std::println(std::cerr, "script name '{}' cannot be run: a name must not be empty or start with '-'", script.name);
return 1;
}
// Running the first or the last of two would both be a guess, and
// the usual cause is a pasted line whose name was never changed.
if (!seen.insert(script.name).second) {
std::println(std::cerr, "script '{}' is defined twice", script.name);
return 1;
}
if (!script.run) {
std::println(std::cerr, "script '{}' has no callback", script.name);
return 1;
}
}
if (opts.listOnly || !opts.named) {
for (const Script& script : scripts) std::println("{}", script.name);
return 0;
}
const auto found = std::ranges::find(scripts, opts.name, &Script::name);
if (found == scripts.end()) {
std::println(std::cerr, "No script named '{}'. Scripts: {}", opts.name, JoinNames(scripts));
return 1;
}
ScriptContext ctx{
.project = projectCfg,
// lexically_normal: fs::absolute keeps a "./" component.
.projectDir = opts.projectFile.empty() ? fs::absolute(projectCfg.path).lexically_normal() : opts.projectFile.parent_path(),
.args = opts.args,
};
try {
const std::int32_t code = found->run(ctx);
if (code == 0 || (code >= 1 && code <= 255)) return code;
std::println(std::cerr, "script '{}' returned {}, which is not an exit status (0..255); exiting with 1", found->name, code);
return 1;
} catch (const std::exception& e) {
// Never let a script's exception unwind across the project.so
// boundary and out of Run: report it as this script's failure.
Progress::Clear();
std::println(std::cerr, "script '{}' failed: {}", found->name, e.what());
return 1;
}
}

View file

@ -23,23 +23,11 @@ namespace {
// MatchGlob/MatchAny live in :Platform — shared with the lint verb.
std::string ShellQuoteSh(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;
}
std::string JoinAndQuoteArgs(std::span<const std::string> args) {
std::string out;
for (const auto& a : args) {
if (!out.empty()) out.push_back(' ');
out += ShellQuoteSh(a);
out += ShellQuote(a);
}
return out;
}
@ -273,7 +261,7 @@ TestResult Crafter::RunSingleTest(const Test& test, const fs::path& binary, std:
if (test.runner.exec.empty()) {
// Local runner: spawn the binary directly through the host shell.
std::string cmd = std::format("{} {}", ShellQuoteSh(binary.string()), ph["{args}"]);
std::string cmd = std::format("{} {}", ShellQuote(binary.string()), ph["{args}"]);
r = RunCommandWithTimeout(cmd, timeout);
} else {
// Prefix runner (qemu-user, wasmtime, wine, ...): templated exec