Building a First Program
This page builds a one-file C program from an empty directory. Along the way it shows the parts every pekit build has: a workspace, a build file, a target, and a step. It takes about five minutes.
Before you start #
You need:
- pekit. To build it from its source, run
cargo install --git https://github.com/peios/pekit pekit --locked, orcargo install --path crates/pekit --lockedin a checkout. Building it needs a C compiler andmake, for the compression libraries and the memory allocator it links. - bubblewrap, the
bwrapprogram, which pekit runs every step in. - A C compiler from your distribution's packages, as
/usr/bin/cc. A step can see the host's/usr, but not/usr/local.
Create the workspace #
-
Make a directory for the workspace, and change into it:
-
Create an empty
workspace.gcsl. Its presence makes the directory a workspace; it can hold settings, but this one doesn't need any. -
Save this program as
hello.c:int
Write the build file #
Save this as build.gcsl, beside hello.c:
#[hermetic]
fn compile(source: File) -> File {
let compiled = exec(["cc", "-o", "hello", source.path])
= working_dir().file("hello")
}
def build hello() {
= compile(file("hello.c"))
}
The file declares two things:
compile, a function that runs the compiler on one source file and gives the program it made.#[hermetic]makes each call of it a step: pekit runs the call in a sandbox that holds onlysource, and keeps what it gives.working_dir()is the step's own directory, where the compiler writeshello.hello, a target.def builddeclares a definition you can name topekit build. Its body callscompilewith the filehello.c, found relative to the build file.
Build it #
-
Build the target:
pekit runs the one step, and shows what the target gave:
built /hello: 1 step run, 0 found in the store pekit-out/hello/compile/hello note: steps ran with the host's own tools, so this build is not hermeticThe note says that the step could use your machine's own tools, and this one did: it ran your
cc. That's fine to start with. Hermetic and Strict Builds explains how to build without them. -
Run the program:
It prints
Hello from pekit. -
Build again:
Nothing changed, so pekit takes the step's result from its store instead of running it:
built /hello: 0 steps run, 1 found in the store pekit-out/hello/compile/hello
Change the program #
-
In
hello.c, change the message toHello again. -
Build again. pekit sees that the step's input changed, and runs it:
built /hello: 1 step run, 0 found in the store pekit-out/hello/compile/hello -
Change the message back to
Hello from pekit, and build once more. pekit has run the step with that input before, so it finds the result in the store and runs nothing.
pekit tells a changed file by its contents, never by its modification time. Saving a file without changing it, or changing it and changing it back, rebuilds nothing.
Check the result #
pekit-out/hello/compile/hello is the program the last build made, and
./pekit-out/hello/compile/hello prints Hello from pekit. The path names
the target, hello, then the step that made the file, compile. Where
Results Go describes the rest of pekit-out/.
Next steps #
A real C build compiles and links many files, and uses a compiler pekit pins rather than the host's. The gcc library does both: see Building C. Targets and Steps explains what pekit did during each build.