Philipp Wagner | c120b18 | 2020-09-17 11:53:03 +0100 | [diff] [blame] | 1 | --- |
| 2 | title: Build Software |
| 3 | --- |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 4 | |
| 5 | ## Prerequisites |
| 6 | |
Garret Kelly | 9eebde0 | 2019-10-22 15:36:49 -0400 | [diff] [blame] | 7 | _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 Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 8 | |
| 9 | ## Building software |
| 10 | |
Miguel Young de la Sota | 8ff30b8 | 2019-11-25 12:58:34 -0600 | [diff] [blame] | 11 | OpenTitan software is built using [Meson](https://mesonbuild.com). |
| 12 | However, 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 | |
| 14 | For example, the following commands build the `boot_rom` and `hello_world` binaries for FPGA: |
Miguel Osorio | 39fc9a4 | 2019-10-31 15:05:40 -0700 | [diff] [blame] | 15 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 16 | ```console |
Miguel Young de la Sota | 8ff30b8 | 2019-11-25 12:58:34 -0600 | [diff] [blame] | 17 | # Configure the Meson environment. |
Miguel Osorio | 39fc9a4 | 2019-10-31 15:05:40 -0700 | [diff] [blame] | 18 | $ cd $REPO_TOP |
Miguel Young de la Sota | 8ff30b8 | 2019-11-25 12:58:34 -0600 | [diff] [blame] | 19 | $ ./meson_init.sh |
| 20 | |
| 21 | # Build the two targets we care about, specifically. |
Miguel Young de la Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 22 | $ 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 Sota | 8ff30b8 | 2019-11-25 12:58:34 -0600 | [diff] [blame] | 24 | |
Miguel Young de la Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 25 | # Build *everything*, including targets for other devices. |
| 26 | $ ninja -C build-out all |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 27 | ``` |
| 28 | |
Miguel Young de la Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 29 | Note that specific targets are followed by the device they are built for. |
| 30 | OpenTitan 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 Sota | 8ff30b8 | 2019-11-25 12:58:34 -0600 | [diff] [blame] | 32 | In general, `clean` rules are unnecessary, and Meson will set up `ninja` such that it reruns `meson.build` files which have changed. |
Greg Chadwick | 567cfd6 | 2019-10-31 16:15:33 +0000 | [diff] [blame] | 33 | |
Miguel Young de la Sota | 8ff30b8 | 2019-11-25 12:58:34 -0600 | [diff] [blame] | 34 | Build 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`. |
| 35 | As 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. |
| 36 | Complete details of these semantics are documented in `util/build_consts.sh`. |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 37 | |
Miguel Young de la Sota | 8ff30b8 | 2019-11-25 12:58:34 -0600 | [diff] [blame] | 38 | The locations of `build-{out,bin}` can be controled by setting the `$BUILD_ROOT` enviromnent variable, which defaults to `$REPO_TOP`. |
Srikrishna Iyer | 4523cb2 | 2019-10-02 16:41:05 -0700 | [diff] [blame] | 39 | |
Miguel Young de la Sota | 8ff30b8 | 2019-11-25 12:58:34 -0600 | [diff] [blame] | 40 | `./meson_init.sh` itself is idempotent, but this behavior can be changed with additional flags; see `./meson_init.sh` for more information. |
| 41 | For 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 Sota | 76526c3 | 2020-01-28 10:24:41 -0500 | [diff] [blame] | 43 | Building 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 | |
| 49 | In general, this executable is built by building the `foo_export_$DEVICE` target. |
Miguel Young de la Sota | 8ff30b8 | 2019-11-25 12:58:34 -0600 | [diff] [blame] | 50 | |
| 51 | Building 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 Singh | 0caa6e0 | 2020-04-01 15:24:27 -0700 | [diff] [blame] | 52 | |
Philipp Wagner | c36275a | 2021-01-21 11:57:06 +0000 | [diff] [blame] | 53 | ## Troubleshooting the build system |
| 54 | |
| 55 | If 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. |
| 56 | This sledgehammer is only intended to be used as a last resort when the existing configuration is seriously broken. |
| 57 | |
Satnam Singh | 0caa6e0 | 2020-04-01 15:24:27 -0700 | [diff] [blame] | 58 | ```console |
| 59 | $ ./meson_init.sh -f |
| 60 | ``` |
Philipp Wagner | c36275a | 2021-01-21 11:57:06 +0000 | [diff] [blame] | 61 | |
Satnam Singh | 0caa6e0 | 2020-04-01 15:24:27 -0700 | [diff] [blame] | 62 | If any `meson.build` files are changed the configuration can be regenerated by passing the `-r` flag to `./meson_init.sh` |
Philipp Wagner | c36275a | 2021-01-21 11:57:06 +0000 | [diff] [blame] | 63 | |
Satnam Singh | 0caa6e0 | 2020-04-01 15:24:27 -0700 | [diff] [blame] | 64 | ```console |
| 65 | $ ./meson_init.sh -r |
Sam Elliott | 4cac9ec | 2020-06-03 11:53:04 +0100 | [diff] [blame] | 66 | ``` |
| 67 | |
Philipp Wagner | c36275a | 2021-01-21 11:57:06 +0000 | [diff] [blame] | 68 | ## Bringing your own toolchain |
Sam Elliott | 4cac9ec | 2020-06-03 11:53:04 +0100 | [diff] [blame] | 69 | |
| 70 | `./meson_init.sh` needs to know where the toolchain you are using is, and which tools from it should be used. |
| 71 | |
| 72 | If 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 | |
| 74 | If 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 | |
| 80 | If 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. |
| 81 | These are called `meson-<triple>-<compiler>.txt`, and are present in toolchains since version 20200602-1. |
| 82 | You can still use `TOOLCHAIN_PATH` to point to the toolchain location if you have updated the paths within these files. |
| 83 | |
| 84 | If 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 Wagner | c36275a | 2021-01-21 11:57:06 +0000 | [diff] [blame] | 85 | |
Sam Elliott | 4cac9ec | 2020-06-03 11:53:04 +0100 | [diff] [blame] | 86 | ```console |
| 87 | $ ./meson_init -t /path/to/toolchain/file |
| 88 | ``` |
| 89 | |
| 90 | If 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 Wagner | c120b18 | 2020-09-17 11:53:03 +0100 | [diff] [blame] | 91 | If `TOOLCHAIN_PATH` is set, this will be used to update any paths within the legacy configuration. |
Philipp Wagner | a1b7214 | 2021-01-21 12:04:59 +0000 | [diff] [blame] | 92 | |
| 93 | ## Debugging device software |
| 94 | |
| 95 | ### Attaching a debugger |
| 96 | |
| 97 | GDB can be used to debug device software running on an FPGA or in a Verilator simulation. |
| 98 | Refer 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 | |
| 102 | A disassembly of all executable sections is produced by the build system by default. |
| 103 | It can be found by looking for files with the `.dis` extension next to the corresponding ELF file. |
| 104 | |
| 105 | To get a different type of disassembly, e.g. one which includes data sections in addition to executable sections, objdump can be called manually. |
| 106 | For 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 | |
| 112 | Refer to the output of `riscv32-unknown-elf-objdump --help` for a full list of options. |