Registering Toolchains
A workspace chooses the compilers and tools its steps use by registering
toolchains in pekitconf. Register pinned toolchains to make a build
independent of the machine it runs on, or define your own for a tool that
no library provides.
Before you start #
- A workspace that imports a library whose steps ask for a toolchain, such as the gcc library. See Libraries for how to make one available.
Register a library's toolchains #
-
In
workspace.gcsl, import the toolchains the library defines, and theToolchaintype:import Toolchain::* from "github.com/gcslib/gcc" import Toolchain from toolchainimport Toolchain::* froma library brings in every toolchain it defines, and nothing else of it. To bring in one, name it, asimport Toolchain::C gcc_x86_64 from "github.com/gcslib/gcc". -
List them in
pekitconf.toolchains, in order of preference:pekitconf = {toolchains = [ Toolchain::C gcc_x86_64, Toolchain::C gcc_x86_64_to_aarch64, ]}Each entry names a toolchain's kind and its definition. For each step, pekit uses the first toolchain of the kind it asks for that runs on the machine and builds for the target platform.
-
Build a target that uses the library:
Check the result #
The steps that ask for a C toolchain run with the one you registered.
pekit build --platform aarch64 hello builds the same target with
gcc_x86_64_to_aarch64, and file pekit-out/hello/link/hello reports an
ARM aarch64 executable. A get that finds no toolchain fails with an
error naming the kind and both platforms.
Keep the host's compiler #
Registering any toolchain replaces the default list, which holds only
system_c, the host's own C compiler. To keep it as well, register it
yourself. It's defined in the toolchain module:
import * from toolchain
pekitconf = {toolchains = [Toolchain::C system_c]}
A step that uses system_c runs the host's cc, so its result depends on
what's installed. A strict build refuses it.
Define a toolchain of your own #
A toolchain can hold any tool, not only a compiler. This workspace keeps a
tool called greet in tools/greeter/bin/, and defines a toolchain kind
for it.
-
In
workspace.gcsl, declare the kind, with the layout of files and directories it provides:import * from toolchain enum Toolchain::Greeter<{root: Dir, greet: File}>A kind's layout is a record whose every field is a file or a directory, so that a toolchain can be passed to a step.
-
Define a toolchain of that kind. Its
host_platformandplatformsay where it runs and what it builds for; a field you leave out matches any platform:def Toolchain::Greeter workspace_greeter() { let tools = dir("tools/greeter") host_platform = {os = "linux"} platform = {} provides = {root = tools; greet = tools.file("bin/greet")} }A toolchain fetched from the internet would fetch and unpack its files here, as the gcc library's do.
-
Register it, beside any others:
pekitconf = {toolchains = [Toolchain::C system_c, Toolchain::Greeter workspace_greeter]} -
In a build file, ask for it in a step's parameter default:
import toolchain import Toolchain from toolchain #[hermetic] fn greeting(name: String, greeter: {root: Dir, greet: File} = toolchain.get(Toolchain::Greeter)) -> File { let greeted = exec([greeter.greet.path, name, "greeting.txt"]) = working_dir().file("greeting.txt") } def build hello() { = greeting("world") }
pekit build hello runs the tool in the step, and
pekit-out/hello/greeting/greeting.txt holds Hello, world. Because the
tool is an argument, changing it reruns every step that uses it.