David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 1 | # Hermetic build |
| 2 | |
| 3 | Hafnium 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 |
| 5 | maintains and periodically publishes a container image as the reference build |
| 6 | environment. The image is hosted on Google Cloud Platform as |
| 7 | `eu.gcr.io/hafnium-build/hafnium_ci`. |
| 8 | |
| 9 | Building inside a container is always enabled only for Kokoro pre-submit tests |
| 10 | but can be enabled for local builds too. It is disabled by default as it |
| 11 | requires the use of Docker which currently supports rootless containers only |
| 12 | in nightly builds. As rootless container tools mature, Hafnium may change the |
| 13 | default settings. For now, running the hermetic build locally is intended |
| 14 | primarily to reproduce issues in pre-submit tests. |
| 15 | |
| 16 | ## Installing Docker |
| 17 | |
| 18 | ### Stable |
| 19 | |
| 20 | If you don't mind running a Docker daemon with root privileges on your system, |
| 21 | you can follow the [official guide](https://docs.docker.com/install/) to install |
| 22 | Docker, or [go/installdocker](https://goto.google.com/installdocker) if you are |
| 23 | a Googler. |
| 24 | |
| 25 | Because the daemon runs as root, files generated by the container are owned by |
| 26 | root as well. To work around this, the build will automatically derive a local |
| 27 | container image from the base container, adding user `hafnium` with the same |
| 28 | UID/GID as the local user. |
| 29 | |
| 30 | ### Nightly with rootless |
| 31 | |
| 32 | The latest nightly version of Docker has support for running containers with |
| 33 | user namespaces, thus eliminating the need for a daemon with root privileges. |
| 34 | It can be installed into the local user's `bin` directory with a script: |
| 35 | ``` shell |
| 36 | curl -fsSL https://get.docker.com/rootless -o get-docker.sh |
| 37 | sh get-docker.sh |
| 38 | ``` |
| 39 | |
| 40 | The script will also walk you through the installation of dependencies, |
| 41 | changes to system configuration files and environment variable values needed |
| 42 | by the client to discover the rootless daemon. |
| 43 | |
| 44 | ## Enabling for local builds |
| 45 | |
| 46 | Hermetic builds are controlled by the `HAFNIUM_HERMETIC_BUILD` environment |
| 47 | variable. Setting it to `true` instructs the build to run commands inside the |
| 48 | container. Any other value disables the feature. |
| 49 | |
| 50 | To always enable hermetic builds, put this line in your `~/.bashrc`: |
| 51 | ``` shell |
| 52 | export HAFNIUM_HERMETIC_BUILD=true |
| 53 | ``` |
| 54 | |
| 55 | When you now run `make`, you should see the following line: |
| 56 | ``` shell |
| 57 | $ make |
| 58 | Running in container: make all |
| 59 | ... |
| 60 | ``` |
| 61 | ## Running commands inside the container |
| 62 | |
| 63 | An arbitrary command can be executed inside the container with |
| 64 | `build/run_in_container.sh [-i] <command> ...`. This is done |
| 65 | automatically inside `Makefile` and `kokoro/ubuntu/build.sh` which |
| 66 | detect whether they are already running inside the container and respawn |
| 67 | themselves using `run_in_container.sh` if not. |
| 68 | |
| 69 | For example, you can spawn a shell with: |
| 70 | ``` shell |
| 71 | ./build/run_in_container.sh -i bash |
| 72 | ``` |
| 73 | |
| 74 | ## Building container image |
| 75 | |
| 76 | The container image is defined in `build/docker/Dockerfile` and can be built |
| 77 | locally: |
| 78 | ``` shell |
| 79 | ./build/docker/build.sh |
| 80 | ``` |
| 81 | |
| 82 | Owners of the `hafnium-build` GCP repository can publish the new image |
| 83 | (requires [go/cloud-sdk](https://goto.google.com/cloud-sdk) installed and |
| 84 | authenticated): |
| 85 | ``` shell |
| 86 | ./build/docker/publish.sh |
| 87 | ``` |