blob: f435f91e9dc61a8466aca4c6d2fc102bb9d19c5d [file] [log] [blame] [view]
Philipp Wagnerc120b182020-09-17 11:53:03 +01001---
2title: Build Software
3---
lowRISC Contributors802543a2019-08-31 12:12:56 +01004
5## Prerequisites
6
Garret Kelly9eebde02019-10-22 15:36:49 -04007_Make sure you followed the install instructions to [prepare the system]({{< relref "install_instructions#system-preparation" >}}) and install the [compiler toolchain]({{< relref "install_instructions#compiler-toolchain" >}})._
lowRISC Contributors802543a2019-08-31 12:12:56 +01008
9## Building software
10
Miguel Young de la Sota8ff30b82019-11-25 12:58:34 -060011OpenTitan software is built using [Meson](https://mesonbuild.com).
12However, Meson is not an exact fit for a lot of things OpenTitan does (such as distinguishing between FPGA, ASIC, and simulations), so the setup is a little bit different.
13
14For example, the following commands build the `boot_rom` and `hello_world` binaries for FPGA:
Miguel Osorio39fc9a42019-10-31 15:05:40 -070015
lowRISC Contributors802543a2019-08-31 12:12:56 +010016```console
Miguel Young de la Sota8ff30b82019-11-25 12:58:34 -060017# Configure the Meson environment.
Miguel Osorio39fc9a42019-10-31 15:05:40 -070018$ cd $REPO_TOP
Miguel Young de la Sota8ff30b82019-11-25 12:58:34 -060019$ ./meson_init.sh
20
21# Build the two targets we care about, specifically.
Miguel Young de la Sota76526c32020-01-28 10:24:41 -050022$ ninja -C build-out sw/device/boot_rom/boot_rom_export_fpga_nexysvideo
23$ ninja -C build-out sw/device/examples/hello_world/hello_world_export_fpga_nexysvideo
Miguel Young de la Sota8ff30b82019-11-25 12:58:34 -060024
Miguel Young de la Sota76526c32020-01-28 10:24:41 -050025# Build *everything*, including targets for other devices.
26$ ninja -C build-out all
lowRISC Contributors802543a2019-08-31 12:12:56 +010027```
28
Miguel Young de la Sota76526c32020-01-28 10:24:41 -050029Note that specific targets are followed by the device they are built for.
30OpenTitan needs to link the same device executable for multiple devices, so each executable target is duplicated one for each device we support.
31
Miguel Young de la Sota8ff30b82019-11-25 12:58:34 -060032In general, `clean` rules are unnecessary, and Meson will set up `ninja` such that it reruns `meson.build` files which have changed.
Greg Chadwick567cfd62019-10-31 16:15:33 +000033
Miguel Young de la Sota8ff30b82019-11-25 12:58:34 -060034Build intermediates will show up in `$REPO_TOP/build-out`, including unlinked object files and libraries, while completed executables are exported to `$REPO_TOP/build-bin`.
35As a rule, you should only ever need to refer to artifacts inside of `build-bin`; the exact structure of `build-out` is subject to change.
36Complete details of these semantics are documented in `util/build_consts.sh`.
lowRISC Contributors802543a2019-08-31 12:12:56 +010037
Miguel Young de la Sota8ff30b82019-11-25 12:58:34 -060038The locations of `build-{out,bin}` can be controled by setting the `$BUILD_ROOT` enviromnent variable, which defaults to `$REPO_TOP`.
Srikrishna Iyer4523cb22019-10-02 16:41:05 -070039
Miguel Young de la Sota8ff30b82019-11-25 12:58:34 -060040`./meson_init.sh` itself is idempotent, but this behavior can be changed with additional flags; see `./meson_init.sh` for more information.
41For this reason, most examples involving Meson will include a call to `./meson_init.sh`, but you will rarely need to run it more than once per checkout.
42
Miguel Young de la Sota76526c32020-01-28 10:24:41 -050043Building an executable `foo` destined to run on the OpenTitan device `$DEVICE` will output the following files under `build-bin/sw/device`:
44* `foo_$DEVICE.elf`: the linked program, in ELF format.
45* `foo_$DEVICE.bin`: the linked program, as a plain binary with ELF debug information removed.
46* `foo_$DEVICE.dis`: the disassembled program with inline source code.
47* `foo_$DEVICE.vmem`: a Verilog memory file which can be read by `$readmemh()` in Verilog code.
48
49In general, this executable is built by building the `foo_export_$DEVICE` target.
Miguel Young de la Sota8ff30b82019-11-25 12:58:34 -060050
51Building an executable destined to run on a host machine (i.e., under `sw/host`) will output a host excecutable under `build-bin/sw/host`, which can be run directly.
Satnam Singh0caa6e02020-04-01 15:24:27 -070052
Philipp Wagnerc36275a2021-01-21 11:57:06 +000053## Troubleshooting the build system
54
55If you encounter an error running `./meson_init.sh` you could re-run using the `-f` flag which will erase any existing building directories to yield a clean build.
56This sledgehammer is only intended to be used as a last resort when the existing configuration is seriously broken.
57
Satnam Singh0caa6e02020-04-01 15:24:27 -070058```console
59$ ./meson_init.sh -f
60```
Philipp Wagnerc36275a2021-01-21 11:57:06 +000061
Satnam Singh0caa6e02020-04-01 15:24:27 -070062If any `meson.build` files are changed the configuration can be regenerated by passing the `-r` flag to `./meson_init.sh`
Philipp Wagnerc36275a2021-01-21 11:57:06 +000063
Satnam Singh0caa6e02020-04-01 15:24:27 -070064```console
65$ ./meson_init.sh -r
Sam Elliott4cac9ec2020-06-03 11:53:04 +010066```
67
Philipp Wagnerc36275a2021-01-21 11:57:06 +000068## Bringing your own toolchain
Sam Elliott4cac9ec2020-06-03 11:53:04 +010069
70`./meson_init.sh` needs to know where the toolchain you are using is, and which tools from it should be used.
71
72If you are using the lowrisc-provided toolchain (obtained with `get-toolchain.py`), and it is installed in the default location (`/tools/riscv`), then `./meson_init.sh` does not need additional configuration.
73
74If you are using the lowrisc-provided toolchain, but have located it in a non-default location (using `get-toolchain.py -t /path/to/lowrisc/toolchain`), you can use the environment variable `TOOLCHAIN_PATH` to point to your toolchain location, like so:
75```console
76$ export TOOLCHAIN_PATH=/path/to/lowrisc/toolchain
77$ ./meson_init.sh
78```
79
80If you have moved a lowrisc-provided toolchain (obtained with `get-toolchain.py`), you will need to update paths within the meson toolchain configuration files within the toolchain installation.
81These are called `meson-<triple>-<compiler>.txt`, and are present in toolchains since version 20200602-1.
82You can still use `TOOLCHAIN_PATH` to point to the toolchain location if you have updated the paths within these files.
83
84If you have built your own toolchain by following option 2 under [Installing Software Build Requirements]({{< relref "doc/ug/install_instructions/index#device-compiler-toolchain-rv32imc" >}}), then you need to point `./meson_init.sh` to your custom toolchain file using `-t FILE`:
Philipp Wagnerc36275a2021-01-21 11:57:06 +000085
Sam Elliott4cac9ec2020-06-03 11:53:04 +010086```console
87$ ./meson_init -t /path/to/toolchain/file
88```
89
90If you do not specify your own toolchain configuration file (using `./meson_init.sh -t`), and `meson_init.sh` cannot find the default configuration in your toolchain, the legacy `toolchain.txt` from the main OpenTitan repository will be used.
Philipp Wagnerc120b182020-09-17 11:53:03 +010091If `TOOLCHAIN_PATH` is set, this will be used to update any paths within the legacy configuration.
Philipp Wagnera1b72142021-01-21 12:04:59 +000092
93## Debugging device software
94
95### Attaching a debugger
96
97GDB can be used to debug device software running on an FPGA or in a Verilator simulation.
98Refer to the [Getting started on FPGAs]({{<relref getting_started_fpga >}}) and [Verilator]({{<relref getting_started_verilator >}}) guides for more details.
99
100### Disassembling device code
101
102A disassembly of all executable sections is produced by the build system by default.
103It can be found by looking for files with the `.dis` extension next to the corresponding ELF file.
104
105To get a different type of disassembly, e.g. one which includes data sections in addition to executable sections, objdump can be called manually.
106For example the following command shows how to disassemble all sections of the UART DIF smoke test interleaved with the actual source code.
107
108```console
109$ riscv32-unknown-elf-objdump --disassemble-all --headers --line-numbers --source build-bin/sw/device/tests/dif_uart_smoketest_sim_verilator.elf
110```
111
112Refer to the output of `riscv32-unknown-elf-objdump --help` for a full list of options.