blob: aee1444c3baa0e3c4cd12e69ffadc321d0a9c626 [file] [log] [blame] [view]
David Brazdil5ecf75f2019-07-21 10:39:47 +02001# Hermetic build
2
3Hafnium build is not hermetic as it uses some system tools and libraries, e.g.
4`bison` and `libssl`. To ensure consistency and repeatability, the team
5maintains and periodically publishes a container image as the reference build
6environment. The image is hosted on Google Cloud Platform as
7`eu.gcr.io/hafnium-build/hafnium_ci`.
8
9Building inside a container is always enabled only for Kokoro pre-submit tests
10but can be enabled for local builds too. It is disabled by default as it
Andrew Walbran6f8fd4c2019-08-05 13:28:17 +010011requires the use of Docker which currently supports rootless containers only in
12nightly builds. As rootless container tools mature, Hafnium may change the
David Brazdil5ecf75f2019-07-21 10:39:47 +020013default settings. For now, running the hermetic build locally is intended
14primarily to reproduce issues in pre-submit tests.
15
Andrew Walbranb7849972019-11-15 15:23:43 +000016[TOC]
17
David Brazdil5ecf75f2019-07-21 10:39:47 +020018## Installing Docker
19
20### Stable
21
22If you don't mind running a Docker daemon with root privileges on your system,
23you can follow the [official guide](https://docs.docker.com/install/) to install
24Docker, or [go/installdocker](https://goto.google.com/installdocker) if you are
25a Googler.
26
27Because the daemon runs as root, files generated by the container are owned by
28root as well. To work around this, the build will automatically derive a local
29container image from the base container, adding user `hafnium` with the same
30UID/GID as the local user.
31
32### Nightly with rootless
33
34The latest nightly version of Docker has support for running containers with
Andrew Walbran6f8fd4c2019-08-05 13:28:17 +010035user namespaces, thus eliminating the need for a daemon with root privileges. It
36can be installed into the local user's `bin` directory with a script:
37
38```shell
David Brazdil5ecf75f2019-07-21 10:39:47 +020039curl -fsSL https://get.docker.com/rootless -o get-docker.sh
40sh get-docker.sh
41```
42
Andrew Walbran6f8fd4c2019-08-05 13:28:17 +010043The script will also walk you through the installation of dependencies, changes
44to system configuration files and environment variable values needed by the
45client to discover the rootless daemon.
David Brazdil5ecf75f2019-07-21 10:39:47 +020046
47## Enabling for local builds
48
49Hermetic builds are controlled by the `HAFNIUM_HERMETIC_BUILD` environment
50variable. Setting it to `true` instructs the build to run commands inside the
51container. Any other value disables the feature.
52
53To always enable hermetic builds, put this line in your `~/.bashrc`:
Andrew Walbran6f8fd4c2019-08-05 13:28:17 +010054
55```shell
David Brazdil5ecf75f2019-07-21 10:39:47 +020056export HAFNIUM_HERMETIC_BUILD=true
57```
58
59When you now run `make`, you should see the following line:
Andrew Walbran6f8fd4c2019-08-05 13:28:17 +010060
61```shell
David Brazdil5ecf75f2019-07-21 10:39:47 +020062$ make
63Running in container: make all
64...
65```
Andrew Walbran6f8fd4c2019-08-05 13:28:17 +010066
David Brazdil5ecf75f2019-07-21 10:39:47 +020067## Running commands inside the container
68
69An arbitrary command can be executed inside the container with
Andrew Walbran6f8fd4c2019-08-05 13:28:17 +010070`build/run_in_container.sh [-i] <command> ...`. This is done automatically
Andrew Walbran219ceaf2020-01-10 15:44:29 +000071inside `Makefile` and `kokoro/build.sh` which detect whether they are already
72running inside the container and respawn themselves using `run_in_container.sh`
73if not.
David Brazdil5ecf75f2019-07-21 10:39:47 +020074
75For example, you can spawn a shell with:
Andrew Walbran6f8fd4c2019-08-05 13:28:17 +010076
77```shell
David Brazdil5ecf75f2019-07-21 10:39:47 +020078./build/run_in_container.sh -i bash
79```
80
81## Building container image
82
83The container image is defined in `build/docker/Dockerfile` and can be built
84locally:
Andrew Walbran6f8fd4c2019-08-05 13:28:17 +010085
86```shell
David Brazdil5ecf75f2019-07-21 10:39:47 +020087./build/docker/build.sh
88```
89
Andrew Walbran6f8fd4c2019-08-05 13:28:17 +010090Owners of the `hafnium-build` GCP repository can publish the new image (requires
91[go/cloud-sdk](https://goto.google.com/cloud-sdk) installed and authenticated):
92
93```shell
David Brazdil5ecf75f2019-07-21 10:39:47 +020094./build/docker/publish.sh
95```