Build Context

The build context is a set of five intrinsics the compiler fills in for each build. Every one is declared by the standard Rux package, so you import the ones you use:

import Core::{ #target, #build, #compiler, #source, #config };
IntrinsicDescribes
#targetThe machine and ABI the program is being built for
#buildThe build profile, mode, and timestamps
#compilerThe Rux compiler itself
#sourceThe current source location
#configKey/value definitions supplied by the build

Read a field with .; the field names are lowerCamelCase, and the query methods are PascalCase. Every value is fixed at compile time, so it can drive a when condition or stand in as an ordinary literal in normal code.

import Core::{ #build };

const BuildDate: Slice<char8> = #build.date;   // baked in as a literal

#target

Facts about the machine, operating system, and ABI the current build targets.

Field / methodTypeExample value
osOperatingSystem.Linux
archArchitecture.X86_64
abiApplicationBinaryInterface.SystemV
endianEndianness.Little
pointerBitsinteger64
dataModelDataModel.LP64
objectFormatObjectFormat.ELF
tripleSlice<char8>"x86_64-linux-gnu"
HasFeature(.AVX2)boolwhether a CPU feature is enabled
import Core::{ #target };

when #target.arch == .X86_64 && #target.HasFeature(.AVX2) {
    UseAvx2Kernel();
} else {
    UseScalarKernel();
}

#build

The profile and options the build was invoked with, plus a single timestamp shared across the whole compilation.

Field / methodTypeNotes
profileSlice<char8>Profile name, e.g. "Debug" or "Release"
modeBuildMode.Debug or .Release
optimizationOptimizationMode.None, .Size, or .Speed
debugAssertionsboolWhether DebugAssert checks are compiled in
debugInfoboolWhether debug information is emitted
isTestbooltrue under rux test
outputKindOutputKind.Executable, .SharedLibrary, .StaticLibrary, or .SourceLibrary
timestampintegerUnix seconds; honors SOURCE_DATE_EPOCH
dateSlice<char8>"YYYY-MM-DD" from timestamp
timeSlice<char8>"HH:MM:SS" from timestamp

#compiler

The compiler building the program.

Field / methodTypeNotes
versionSemanticVersionHas .major, .minor, .patch; orderable
HasFeature("link-attribute")boolWhether the compiler supports a named feature

version compares against another SemanticVersion, so code can require a minimum compiler:

import Core::{ #compiler };

when #compiler.version >= SemanticVersion::New(0, 3, 0) {
    UseNewIntrinsic();
}

Feature names accepted by #compiler.HasFeature include "conditional-compilation", "namespaced-intrinsics", "target-intrinsics", "build-intrinsics", "compiler-feature-detection", "source-location-defaults", "extern-symbol-names", "link-attribute", and "no-return-attribute".

#source

The location in the source where the intrinsic is written. Reading one costs nothing at run time — it is already a literal.

FieldTypeExpands to
lineintegerCurrent line number
columnintegerCurrent column number
fileSlice<char8>File name, e.g. "Main.rux"
fileNameSlice<char8>Alias of file
filePathSlice<char8>Stable package-relative path
functionSlice<char8>Enclosing function name
moduleSlice<char8>Enclosing module path

Source Location as a Default Argument

The most important use of #source is as a default argument. A default expands at the call site, not where the function is defined, so a function can learn where it was called from without the caller passing anything. This is how the standard library's Assert and Panic capture a location:

import Core::{ #source };

func Log(message: Slice<char8>,
         file: Slice<char8> = #source.file,
         line: uint32 = #source.line,
         function: Slice<char8> = #source.function) {
    Print(file, ":", line, " (", function, ") ", message);
}

Log("starting up");   // file, line, and function filled in from this call site

Callers normally omit these arguments; pass them explicitly only to forward a location from elsewhere.

#config

Key/value strings supplied by the build — from a [Build.Defines] table in Rux.toml and overridden by --define NAME[=VALUE] on the command line.

MethodTypeResult
#config.Get("NAME")Slice<char8>The value, or an empty string when undefined
#config.Has("NAME")boolWhether the key is defined
import Core::{ #config };

when #config.Has("TELEMETRY") {
    EnableTelemetry(#config.Get("TELEMETRY"));
}

Enum Variant Reference

The enum-valued fields draw from these variants. Only buildable operating systems can actually be produced; naming any other in a when is allowed but warns.

EnumVariants
OperatingSystemWindows, Linux, MacOS, FreeBSD, OpenBSD, NetBSD, DragonFlyBSD, Illumos, Solaris (buildable); AIX, Android, Fuchsia, Haiku, IOS, QNX, Redox (nameable only)
ArchitectureX86_64, X86Bit32, AArch64, ARM32, RISCV64, RISCV32
ApplicationBinaryInterfaceSystemV, WindowsX64, WindowsX86, AAPCS, AAPCS64, RiscvIlp32, RiscvLp64
EndiannessLittle, Big
DataModelILP32, LP64, LLP64
ObjectFormatCOFF, ELF, MachO, Wasm
BuildModeDebug, Release
OptimizationModeNone, Size, Speed
OutputKindExecutable, SharedLibrary, StaticLibrary, SourceLibrary

CPU features for #target.HasFeature are SSE2, SSE3, SSSE3, SSE41, SSE42, AVX, AVX2, AVX512, NEON, SVE, and RVV.

A standalone SourceLibrary check observes SourceLibrary. When that source is compiled as a dependency, it observes the consuming package's output kind.

See Also