# Libraries

---

# Libraries

_Pekit / Libraries_

> How a pekit build imports libraries, where pekit finds them, how a library's steps are shared between workspaces, how a library builds from files of its own, and which libraries gcslib provides.

A library is GCSL code kept apart from any workspace, such as the gcc
library, which compiles and links C. A build file imports a library by a
path in quotes, and calls its functions, which are usually steps:

```gcsl
import "github.com/gcslib/gcc"

def build hello() {
    = gcc.link([gcc.compile(file("hello.c"))], output = "hello")
}
```

A library sees only its own files, the standard library, and the libraries
it imports, never anything of the workspace that uses it. So it means the
same in every workspace. GCSL's [Workspaces](/gcsl/working-with-files/workspaces.md) page
describes imports in full.

## Where pekit finds a library

pekit looks for a library's directory, by its import path, in two places,
and uses the first it finds:

1. Each directory of `$GCSL_LIBRARY_PATH`, in order.
2. `.gcsl/libraries/` at the workspace's root, where `pekit lock` puts the
   libraries it pins.

`$GCSL_LIBRARY_PATH` is for working on a library: point it at your
checkout, and every workspace on your machine builds with your copy. For
everything else, pin libraries with `pekit lock`, so that every checkout
builds with the same ones. See [Pinning
Libraries](/pekit/libraries/pinning-libraries.md).

## Steps from a library are shared

pekit places every library at `/w/.gcsl/libraries/<path>` in the layout
steps see, wherever it found the library. A step a library makes is keyed
by the library's path and contents, never by where it was on disk. So a
library's steps give the same results from the store whether the library
came from `$GCSL_LIBRARY_PATH` or from `pekit lock`, and in every workspace
that imports the same version.

## Libraries with files of their own

A library can keep other files beside its `.gcsl` files: sources to build,
templates, or tools to run. A relative path in a library's code names its
own files, such as `file("templates/greeting.txt")`, wherever the library
is used. The Peios SDK's library builds libpeios this way, from the
sources in its own repository.

Because every library is at the same path in every workspace, a step that
builds from a library's files is the same step everywhere, and its results
are shared through the store.

## What the lock tells a build

A build's code can read what `pekit.lock` pins, through the GCSL standard
library's `libraries` module: each pinned library's path, repository,
directory, and commit. A library that takes something the same repository also
gives another way can check that the two agree. The gcslib rust library uses
it to check `Cargo.lock`. A build fails if `Cargo.lock` takes a git crate from
a repository at another commit than the one `pekit.lock` pins a library of
that repository at.

## The gcslib libraries

gcslib is a set of libraries for building common kinds of software with
pekit:

| Library | Builds |
|---|---|
| `github.com/gcslib/gcc` | C programs and static libraries, with Bootlin's pinned compilers. See [Building C](/pekit/languages/building-c.md). |
| `github.com/gcslib/buildenv` | Software with its own build system, such as `./configure` and `make`, in a pinned environment. See [Building Upstream Software](/pekit/languages/building-upstream-software.md). |
| `github.com/gcslib/python` | Python programs, bundled into one archive. See [Building Python](/pekit/languages/building-python.md). |
| `github.com/gcslib/rust` | Rust programs, one crate at a time or with Cargo. See [Building Rust](/pekit/languages/building-rust.md). |
| `github.com/gcslib/go` | Go programs, one package at a time. See [Building Go](/pekit/languages/building-go.md). |
| `github.com/gcslib/clang` | A pinned libclang, for tools such as bindgen. |
| `github.com/gcslib/modsig` | Linux kernel modules signed with a key of the keychain. See [Signing Kernel Modules](/pekit/shipping/signing-kernel-modules.md). |
| `github.com/gcslib/pipsig` | Firmware, and other files that aren't ELF, signed for the Peios kernel with a key of the keychain. See [Signing Firmware](/pekit/shipping/signing-firmware.md). |
| `github.com/gcslib/ppk` | Peios packages. See [Packaging](/pekit/shipping/packaging.md). |
| `github.com/gcslib/peiso` | Bootable Peios images. See [Building and Booting an Image](/pekit/shipping/building-an-image.md). |
| `github.com/gcslib/qemu` | Programs and tests that boot an image in QEMU. |

The first eight are in repositories of their own, which `pekit lock` finds
by their paths. ppk, peiso and qemu are in pekit's own repository, under
`gcslib/`. To pin them, give their paths a source in `pekit.lock`, as
[Pinning Libraries](/pekit/libraries/pinning-libraries.md#take-libraries-from-elsewhere)
shows.

---

# Pinning Libraries

_Pekit / Libraries_

> Pin every library a workspace imports at a git commit with pekit lock, update the pins, take libraries from other repositories, and build offline and strictly from the pins.

`pekit lock` pins each library a workspace imports, and each library those
import, at a commit of the git repository it comes from. It records the
pins in `pekit.lock`, at the workspace's root. Commit that file, and every
checkout builds with exactly the same libraries.

## Before you start

- `git`, which pekit runs to fetch libraries.
- A workspace whose build files import at least one library.

## Pin the workspace's libraries

1. From anywhere in the workspace, run:

   ```sh
   pekit lock
   ```

   pekit finds every library the workspace imports, fetches each from its
   repository, and pins it at the commit the repository's `HEAD` names:

   ```text
   pinned github.com/gcslib/gcc at 72dae8c75eabc34c63615380a9197e1021a39952
   ```

   It exports each library into `.gcsl/libraries/<path>/`, and records its
   commit and tree digest in `.gcsl/pekit-exports.json`, outside the
   exported tree.

2. Commit `pekit.lock`. Don't commit `.gcsl/`: any command that builds puts
   back a pinned library that's missing, from the commit it's pinned at.
   It also refreshes an untouched recorded export when the lock moves.

## Check the result

`pekit.lock` lists each library under `libraries`, with its repository and
commit. Run `pekit build --strict` on a target: a strict build fails unless
every library it imports is pinned, so a successful one confirms the pins
are complete.

## Update the pins

`pekit lock` pins only libraries that aren't pinned yet. To move every
library to what its repository holds now, run:

```sh
pekit lock --update
```

pekit prints a line for each library it pins again. A library that nothing
imports any more is dropped from the lock, and its directory with it:

```text
dropped example.org/greeting, no longer imported
```

If the dropped library's directory is inside a library that's still pinned,
the directory belongs to that library and stays.

## Pin a library inside another

A library's directory can be inside another library's, as
`github.com/peios/peios-sdk/tools` is inside `github.com/peios/peios-sdk`.
The outer library's export holds the inner one's directory, so pekit pins
both at one commit of one repository. It refuses to pin them at two
commits, because each export would overwrite the other's files.

The usual way to meet this is to import the inner library after the outer
one is pinned and its repository has moved on. `pekit lock` would pin the
inner library at the repository's new commit, so it refuses, naming both:

```text
error: `github.com/peios/peios-sdk/tools`'s directory is in `github.com/peios/peios-sdk`'s, so both must be pinned at one commit, and are not:
  `github.com/peios/peios-sdk` is the root of `https://github.com/peios/peios-sdk` at 3f1c0a9e2b7d45c8a6e0f9b18d2c47e5a1b3c6d8
  `github.com/peios/peios-sdk/tools` is `tools` of `https://github.com/peios/peios-sdk` at 9b2e4d71c0a85f36e1d7c9a2b4f08e6d3c5a17b9
pin both at one commit: give their sources in `pekit.lock` one repository and revision, and run `pekit lock --update`
```

To pin both at one commit, run `pekit lock --update`. When both libraries
come from one source, as they do by default, it pins them at the same
commit. If you gave them sources of their own, make the sources name one
repository and revision first.

`pekit.lock` is unchanged by the refusal, but the inner library might
already be exported at the new commit. Until you run `pekit lock --update`,
builds then warn that the outer library differs from its pin.

Every command that builds refuses a `pekit.lock` that pins two such
libraries at two commits in the same way, before it exports either.

## When a library differs from its pin

After pulling a changed `pekit.lock`, the next command that builds refreshes
an older export automatically, before checking strictness. This applies to
non-strict builds too. pekit replaces it only if its tree still matches the
digest it recorded when exporting it.

If a library under `.gcsl/libraries/` differs from the commit it's pinned
at, and you edited it there or it has no export record from pekit, it is
left alone. A non-strict build warns, and a strict build fails:

```text
warning: `.gcsl/libraries/github.com/gcslib/gcc` differs from the commit `pekit.lock` pins, 72dae8c75eabc34c63615380a9197e1021a39952; remove `.gcsl/libraries/github.com/gcslib/gcc` and pekit will restore it from the pin
```

Preserve any edits you need, then remove the named library directory. The
next command that builds exports the pinned commit and records it. Exports
made by an older pekit have no record. One that still matches its pin is
recorded the next time pekit runs, and refreshes like any other from then
on. One that already differs needs this one-time recovery.

To work on a library, use `$GCSL_LIBRARY_PATH` instead of editing the
exported copy. See [Writing a Library](/pekit/libraries/writing-a-library.md).

## Take libraries from elsewhere

By default, a library's repository follows the convention of Go's modules:
the library `github.com/gcslib/gcc` is the repository
`https://github.com/gcslib/gcc`, at its `HEAD`, with the library at the
repository's root. `pekit lock` records this as a source in `pekit.lock`.

To take libraries from another repository, a mirror, a branch, or a
directory inside a repository, edit the sources. Each source has a prefix of
import paths, a git repository, the directory in it that holds the
prefix's libraries, and the branch, tag, or commit to follow:

```json
{
  "sources": [
    {"prefix": "github.com/gcslib/ppk", "git": "<pekit-repository>", "dir": "gcslib/github.com/gcslib/ppk", "rev": "main"}
  ]
}
```

Replace *pekit-repository* with the URL or path of pekit's repository,
which holds the ppk library under `gcslib/`.

A library uses the first source whose prefix its path is under. Run
`pekit lock --update` after changing a source, so that the libraries it
covers are pinned from it. `git` can be any repository git can fetch,
including a path on your machine.

## Build offline

pekit keeps a bare clone of each repository in its cache directory, and
fetches only when it needs a commit it doesn't have. A workspace whose
libraries are pinned and already fetched builds without the network.

## Build strictly from the pins

A strict build, `--strict`, fails unless its libraries are exactly those
`pekit.lock` pins. That means:

- Every library it imports is pinned.
- No library differs from its pin.
- `$GCSL_LIBRARY_PATH` is unset.

```text
error: a strict build uses the libraries `pekit.lock` pins, and only them:
  the workspace has no `pekit.lock`: run `pekit lock`
  `$GCSL_LIBRARY_PATH` is set, which would find libraries elsewhere: unset it
```

`pekit.lock(5)` describes every field of `pekit.lock`.

---

# Writing a Library

_Pekit / Libraries_

> Write a pekit library of steps in a git repository, use files of its own, try it from a workspace with GCSL_LIBRARY_PATH, and publish it for other workspaces to pin.

A library packages steps for other workspaces to use. It's a directory of
GCSL files in a git repository, named by an import path. This page writes a
small library that greets someone using a template file of its own, tries
it from a workspace, and pins it there.

## Before you start

- `git`.
- A workspace to try the library from. This page uses one at `uselib`,
  with an empty `workspace.gcsl`.

## Write the library

1. Make a directory for the library, and a git repository in it. This page
   names the library `example.org/greeting`, and keeps its checkout at
   `libsrc/example.org/greeting`:

   ```sh
   mkdir -p libsrc/example.org/greeting/templates
   cd libsrc/example.org/greeting
   git init -b main
   ```

   A library published at `https://example.org/greeting` is found there by
   default, as the [Go modules](https://go.dev/ref/mod) convention has it.

2. Save the template as `templates/greeting.txt`:

   ```text
   Hello, NAME, from a library
   ```

3. Save the library's code as `greeting.gcsl`:

   ```gcsl
   # Greets someone with the library's own template.

   #[track_caller]
   #[hermetic]
   def greet(name: String, template: File = file("templates/greeting.txt")) -> File {
       let made = exec(["/bin/sh", "-c", "sed 's/NAME/{name}/' {template.path} > greeting.txt"])
       = working_dir().file("greeting.txt")
   }
   ```

   A library's public names are its `def` functions, declared types, and
   fields; `fn` and `let` stay private to their file. `#[track_caller]`
   reports an error in the step at the caller's line, where the reader can
   act on it. The relative path in the default names the library's own
   file, wherever the library is used.

4. Commit it:

   ```sh
   git add -A
   git commit -m "Add the greeting library"
   ```

## Try it from a workspace

1. In `uselib/build.gcsl`, import the library and call it:

   ```gcsl
   import "example.org/greeting"

   def build hello() {
       = greeting.greet("world")
   }
   ```

2. Build with `$GCSL_LIBRARY_PATH` naming the directory that holds
   `example.org/`:

   ```sh
   GCSL_LIBRARY_PATH=/path/to/libsrc pekit build hello
   ```

   pekit finds the library in your checkout. Edit it and build again, and
   pekit uses your edit.

## Pin it

1. Tell `pekit lock` where the library's repository is. In
   `uselib/pekit.lock`, add a source:

   ```json
   {"sources": [{"prefix": "example.org/greeting", "git": "/path/to/libsrc/example.org/greeting", "dir": "", "rev": "main"}]}
   ```

   A library published at the URL its path names needs no source.

2. Pin it, without `$GCSL_LIBRARY_PATH`:

   ```sh
   pekit lock
   pekit build hello
   ```

## Check the result

`pekit lock` prints the commit it pinned:

```text
pinned example.org/greeting at 1b4e27f7c836186a4bfab3f74521ef642b953632
```

The build after it reports the step found in the store, `0 steps run, 1
found in the store`. The step is keyed by the library's path and contents,
not by where pekit found it, so the result from your checkout is reused.
`pekit-out/hello/greet/greeting.txt` holds `Hello, world, from a library`.

## Ask for a tool

A library that runs a compiler or another tool should ask for it as a
toolchain, so that each workspace chooses which one. Use an existing kind,
such as `Toolchain::C`, or declare a kind in the library, and resolve it in
a step's parameter default. [Registering
Toolchains](/pekit/building/registering-toolchains.md#define-a-toolchain-of-your-own)
shows both sides.

## Keep libraries apart from the workspace

A library sees nothing of the workspace that imports it: not its
declarations, and not its files. Take anything from the workspace as an
argument. A library can import other libraries, which `pekit lock` pins
along with it.
