1# Build Instructions
2
3These instructions for building the Gunyah Hypervisor can be used for both local builds or building within a Docker container.
4
5## Build Environment
6
7Ensure your build environment is correctly setup. See [Setup Instructions](setup.md).
8
9Always ensure you have activated the `gunyah-venv` *before* running `configure` or building.
10```bash
11. gunyah-venv/bin/activate
12```
13
14## Download the source code
15
16The following repositories are needed to build a Gunyah Hypervisor image:
17
18These should all be cloned into the same top-level directory (this assumed in the Docker setup).
19
20- [Gunyah Hypervisor](https://github.com/quic/gunyah-hypervisor) — The Gunyah Hypervisor.
21 ```bash
22 git clone https://github.com/quic/gunyah-hypervisor.git hyp
23 ```
24- [Resource Manager](https://github.com/quic/gunyah-resource-manager) — The privileged root VM and VM manager supporting the Gunyah Hypervisor.
25 ```bash
26 git clone https://github.com/quic/gunyah-resource-manager.git resource-manager
27 ```
28- [Gunyah C Runtime](https://github.com/quic/gunyah-c-runtime) — A runtime for light-weight OS-less application VMs.
29 ```bash
30git clone https://github.com/quic/gunyah-c-runtime.git musl-c-runtime
31 ```
32
33## Build Configuration
34
35The build system has several configuration parameters that must be set:
36
37* `platform`: selects the target hardware platform
38* `featureset`:  selects a named hypervisor architecture configuration
39* `quality`: specifies the build quality, e.g. `debug`, `production` etc., which modify the build, such as including runtime assertions, compiler optimisations etc.
40
41These parameters must be set on the build system's command line; if one or more
42of them is left unset, the build system will print the known values for the
43missing parameter and abort. You may specify a comma-separated list to select
44multiple values for a parameter, or `all` to select every valid combination.
45You may also specify simply `all=true`, which is equivalent to specifying `all`
46for every parameter that is not otherwise specified. The when multiple options
47are selected, each combination (variant) will be built in separate output
48directories under the `build` directory.
49
50Each project may be built using `ninja` or `scons` and the process for build configuration depends on the selected build tool used. See the sections below.
51
52## Building
53
54The Gunyah Hypervisor, Resource Manager and C runtime are built separately,
55each following the similar build instructions below. These separate images need
56to be packaged together into a final boot image.
57
58> IMPORTANT! If making hypervisor public API changes, these changes will need to be updated in the Resource Manager and Runtime sources.
59
60### Building with Ninja
61
62To configure the build for use with *Ninja*, run `./configure.py <configuration>`
63in the top-level source repository of the component, specifying the desired
64configuration parameters.
65
66For example, in each of the Gunyah Hypervisor, Resource Manager and Gunyah C Runtime source directories, run:
67```sh
68./configure.py platform=qemu featureset=gunyah-rm-qemu quality=production
69```
70
71or to build all available configurations for the QEMU platform:
72```sh
73./configure.py platform=qemu all=true
74```
75
76This will create a `build` directory and Ninja build rules file for each enabled build variant. Generally, the `configure` step only needs to be run once.
77
78```sh
79ninja
80```
81
82Run `ninja` to build. There is usually no need to specify `-j` or similar, as
83Ninja will select this automatically. Ninja also will incrementally re-build if
84run again after making code changes.
85> Note, if configuration files are modified, Ninja will rerun the configuration tool with the previous parameters. However, you must manually rerun the configuration step if you rename or delete an existing module or configuration parameter, as Ninja will refuse to run if a build configuration file is missing.
86
87To build a specific file (for example, a single variant when multiple variants have been configured), specify its full name as the target on the `ninja` command line.
88
89To clean the build, run `ninja -t clean`. It should not be necessary to do this routinely.
90
91### Building with SCons
92
93To perform a standalone SCons build, run `scons`, specifying the configuration
94parameters. For example, to build debug builds of all available feature sets
95for the QEMU platform:
96
97```sh
98scons platform=qemu featureset=all quality=debug
99```
100
101Note, configuration parameters *must* be specified on every time you perform a SCons build; configuration is not cached.
102
103To clean the build, run `scons -c all=true`, or use configuration parameters to select a specific variant to clean. It should not be necessary to do this routinely.
104
105## Producing a Boot Image
106
107Once you have built the Gunyah Hypervisor, Resource Manager and C Runtime, a boot image needs be generated.
108
109To reduce the size of the boot image, the generated binaries of Resource Manager and C Runtime need to be stripped with the following commands:
110```bash
111$LLVM/bin/llvm-strip -o <path-to-resource-manager-src>/build/resource-manager.strip <path-to-resource-manager-src>/build/resource-manager
112$LLVM/bin/llvm-strip -o <path-to-c-runtime-src>/build/runtime.strip <path-to-c-runtime-src>/build/runtime
113```
114
115The individual executables generated by building [Gunyah Hypervisor](https://github.com/quic/gunyah-hypervisor), [Resource Manager](https://github.com/quic/gunyah-resource-manager), and [Gunyah C Runtime](https://github.com/quic/gunyah-c-runtime) need to be integrated into a single `hypvm.elf` boot image.
116
117You will need the [pyelftools](https://github.com/eliben/pyelftools) Python
118module. This should be installed in the gunyah python virtual-env.  However, it
119is available from its upstream project:
120```bash
121cd <tools-directory>
122git clone https://github.com/eliben/pyelftools.git
123```
124
125To generate `hypvm.elf` boot image run these steps (substituting `<path>`s for each tool / executable):
126```bash
127cd <path-to-gunyah-hypervisor-src>
128tools/elf/package_apps.py \
129 -a <path-to-resource-manager-src>/build/resource-manager.strip \
130 -r <path-to-c-runtime-src>/build/runtime.strip \
131 <path-to-gunyah-hypervisor-src>/build/qemu/gunyah-rm-qemu/production/hyp.elf \
132 -o <path-to-destination>/hypvm.elf
133```
134> Note, you may wish to pick a different hypervisor `hyp.elf` from a different build variant (i.e. `build/qemu/gunyah-rm-qemu/debug/`).
135