2026-10-03 16:37:00 +02:00
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
fix(test): run each test's command with a TMPDIR of its own
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.
2026-11-27 09:17:28 +01:00
# include <stdlib.h>
2026-10-03 16:37:00 +02:00
import std ;
import Crafter . Build ;
namespace fs = std : : filesystem ;
using namespace Crafter ;
// A sysroot build precompiles the std module from a module source that has to
// belong to the libc++ release whose headers the sysroot carries. Not every
// distro packages that source with its libc++ (Alpine does not), and the
// fallback used to be the toolchain's own copy whatever its release: with a
// libc++ 22 toolchain and a libc++ 23 sysroot the build died on a page of "no
// member named 'runtime_format' in namespace 'std'" that named neither
// release nor what to do about it.
//
// Staged, compiler-free layouts cover the order SysrootStdModule picks in: the
// sysroot's own source, the toolchain's for the same major release, the copy
// crafter-build ships for another release, and otherwise one line naming both
// releases and where the matching source belongs. The last block drives
// Build() against a sysroot nothing matches and expects that line back in
// place of compiler output.
namespace {
std : : int32_t Failures = 0 ;
void Check ( bool cond , std : : string_view msg ) {
if ( ! cond ) {
std : : println ( std : : cerr , " FAIL: {} " , msg ) ;
+ + Failures ;
}
}
void Write ( const fs : : path & file , std : : string_view content ) {
fs : : create_directories ( file . parent_path ( ) ) ;
std : : ofstream ( file ) < < content ;
}
// What libc++ installs as include/c++/v1/__config, reduced to the line
// that states the release.
void StageHeaders ( const fs : : path & includeDir , std : : int32_t version ) {
Write ( includeDir / " __config " , std : : format ( " #ifndef _LIBCPP___CONFIG \n # define _LIBCPP___CONFIG \n # define _LIBCPP_VERSION {} \n #endif \n " , version ) ) ;
}
fs : : path StageSysroot ( const fs : : path & sysroot , std : : int32_t version ) {
StageHeaders ( sysroot / " usr " / " include " / " c++ " / " v1 " , version ) ;
return sysroot ;
}
}
int main ( ) {
const fs : : path scratch = fs : : temp_directory_path ( ) / " crafter-build-sysroot-std-module " ;
std : : error_code ec ;
fs : : remove_all ( scratch , ec ) ;
fs : : create_directories ( scratch ) ;
fix(test): run each test's command with a TMPDIR of its own
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.
2026-11-27 09:17:28 +01:00
// The cache the build below makes its std module's directory in, which
// is keyed on the sysroot's path: one under the temp directory, which
// is a new one every run of the suite, so the real cache would keep one
// more directory each run. Set while this is the only thread.
setenv ( " XDG_CACHE_HOME " , ( scratch / " cache " ) . c_str ( ) , 1 ) ;
2026-10-03 16:37:00 +02:00
{
StageHeaders ( scratch / " headers " , 230102 ) ;
Check ( LibcxxVersion ( scratch / " headers " ) = = 230102 , " the release is read from __config " ) ;
Check ( LibcxxVersion ( scratch / " no-such-dir " ) = = 0 , " missing headers read as an unknown release " ) ;
Write ( scratch / " other " / " __config " , " #define _LIBCPP_ABI_VERSION 1 \n " ) ;
Check ( LibcxxVersion ( scratch / " other " ) = = 0 , " a __config without the version line reads as an unknown release " ) ;
Check ( LibcxxVersionString ( 230102 ) = = " 23.1.2 " & & LibcxxVersionString ( 220108 ) = = " 22.1.8 " , " XXYYZZ is spelled as a dotted release " ) ;
}
// A toolchain on libc++ 22.1.8 that also ships the sources for 23.
Toolchain tc ;
tc . stdCppm = scratch / " toolchain " / " share " / " libc++ " / " v1 " / " std.cppm " ;
tc . libcxxVersion = 220108 ;
tc . stdModulesDir = scratch / " shipped " ;
Write ( tc . stdCppm , " " ) ;
Write ( tc . stdModulesDir / " 23 " / " std.cppm " , " " ) ;
{
// The sysroot's own source matches its headers by construction, so
// it wins whatever the releases are.
const fs : : path sysroot = StageSysroot ( scratch / " sysroot-own " , 240100 ) ;
const fs : : path own = sysroot / " usr " / " share " / " libc++ " / " v1 " / " std.cppm " ;
Write ( own , " " ) ;
StdModuleSource source = SysrootStdModule ( tc , sysroot ) ;
Check ( source . error . empty ( ) & & source . stdCppm = = own , " a sysroot that ships the module source uses its own " ) ;
}
{
const fs : : path sysroot = StageSysroot ( scratch / " sysroot-same-major " , 220103 ) ;
StdModuleSource source = SysrootStdModule ( tc , sysroot ) ;
Check ( source . error . empty ( ) & & source . stdCppm = = tc . stdCppm , " a point release of the toolchain's libc++ uses the toolchain's source " ) ;
}
{
// The reported case: libc++ 23 headers, a libc++ 22 toolchain.
const fs : : path sysroot = StageSysroot ( scratch / " sysroot-next-major " , 230102 ) ;
StdModuleSource source = SysrootStdModule ( tc , sysroot ) ;
Check ( source . error . empty ( ) & & source . stdCppm = = tc . stdModulesDir / " 23 " / " std.cppm " , " another release uses the source shipped for it, not the toolchain's " ) ;
}
{
const fs : : path sysroot = StageSysroot ( scratch / " sysroot-unshipped " , 210108 ) ;
StdModuleSource source = SysrootStdModule ( tc , sysroot ) ;
Check ( source . stdCppm . empty ( ) & & ! source . error . empty ( ) , " a release nothing was shipped for is an error, not a compile against the wrong source " ) ;
Check ( source . error . contains ( " 21.1.8 " ) & & source . error . contains ( " 22.1.8 " ) , std : : format ( " the error names both releases, got '{}' " , source . error ) ) ;
Check ( source . error . contains ( ( sysroot / " usr " / " share " / " libc++ " / " v1 " ) . string ( ) ) , std : : format ( " the error says where the matching source belongs, got '{}' " , source . error ) ) ;
Check ( ! source . error . contains ( ' \n ' ) , " the error is one line " ) ;
}
{
const fs : : path sysroot = scratch / " sysroot-no-libcxx " ;
fs : : create_directories ( sysroot / " usr " / " include " ) ;
StdModuleSource source = SysrootStdModule ( tc , sysroot ) ;
Check ( source . stdCppm . empty ( ) & & source . error . contains ( " no libc++ headers " ) , std : : format ( " a sysroot without libc++ says so, got '{}' " , source . error ) ) ;
}
{
// Nothing to compare when either release is unknown: the toolchain's
// source goes to the compiler, as it did before releases were read.
const fs : : path sysroot = scratch / " sysroot-unreadable " ;
Write ( sysroot / " usr " / " include " / " c++ " / " v1 " / " __config " , " #define _LIBCPP_ABI_VERSION 1 \n " ) ;
Check ( SysrootStdModule ( tc , sysroot ) . stdCppm = = tc . stdCppm , " an unreadable sysroot release falls back to the toolchain's source " ) ;
Toolchain unknown = tc ;
unknown . libcxxVersion = 0 ;
unknown . stdModulesDir . clear ( ) ;
Check ( SysrootStdModule ( unknown , StageSysroot ( scratch / " sysroot-vs-unknown " , 230102 ) ) . stdCppm = = tc . stdCppm , " an unreadable toolchain release falls back to the toolchain's source " ) ;
}
{
Toolchain bare ;
bare . stdCppm = scratch / " nowhere " / " std.cppm " ;
bare . libcxxVersion = 220108 ;
StdModuleSource source = SysrootStdModule ( bare , StageSysroot ( scratch / " sysroot-vs-bare " , 220108 ) ) ;
Check ( source . stdCppm . empty ( ) & & source . error . contains ( " no std module source " ) , std : : format ( " a toolchain without a module source is an error too, got '{}' " , source . error ) ) ;
}
{
// End to end with the real toolchain: a sysroot on a libc++ release
// that does not exist fails the build with the one-line explanation
fix(test): run each test's command with a TMPDIR of its own
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.
2026-11-27 09:17:28 +01:00
// before clang is run, for the host's target. The std module's cache
// directory is made in the scratch cache (above).
2026-10-03 16:37:00 +02:00
const Toolchain & real = GetToolchain ( ) ;
Check ( real . libcxxVersion ! = 0 , " the running toolchain's libc++ release is read from its headers " ) ;
const fs : : path project = scratch / " project " ;
Write ( project / " main.cpp " , " int main() { return 0; } \n " ) ;
Configuration app ;
app . path = project ;
app . name = " sysroot-std-module " ;
app . outputName = " sysroot-std-module " ;
app . target = HostTarget ( ) ;
app . type = ConfigurationType : : Executable ;
app . sysroot = StageSysroot ( scratch / " sysroot-future " , 990000 ) . string ( ) ;
std : : array < fs : : path , 0 > ifaces = { } ;
std : : array < fs : : path , 1 > impls = { " main " } ;
app . GetInterfacesAndImplementations ( ifaces , impls ) ;
std : : unordered_map < fs : : path , std : : shared_future < BuildResult > > depResults ;
std : : mutex depMutex ;
BuildResult r = Build ( app , depResults , depMutex ) ;
Check ( r . result . contains ( " libc++ 99.0.0 " ) , std : : format ( " the build fails naming the sysroot's release, got '{}' " , r . result ) ) ;
Check ( r . result . contains ( LibcxxVersionString ( real . libcxxVersion ) ) , std : : format ( " the build fails naming the toolchain's release, got '{}' " , r . result ) ) ;
Check ( ! r . result . contains ( ' \n ' ) & & ! r . result . contains ( " error: " ) , std : : format ( " the failure is one line, not compiler output, got '{}' " , r . result ) ) ;
}
return Failures = = 0 ? 0 : 1 ;
}