Skip to content
Permalink
Browse files

[sw] Add Meson as software build system

Overview:

This commit adds the following changes:

* Meson build files for sw/ targets. There are two supported build
  configurations:
  * build-fpga: Nexys FPGA
  * build-sim: Verilator
* RISCV crosstool support via toolchain.txt
* Add util/embedded_target.py to generate additional objects per
  embedded_target.
* Breakdown sw/lib/ot_lib.a intp per IP libraries. Each target now
  includes the dependencies it needs, e.g. sw_lib_uart_ot can be used to
  include the UART device library.

New build system requirements:

```console
$ pip3 install --user meson
$ apt-get install ninja-build
```

Steps for using Meson:

```console
$ cd ${REPO_TOP}
$ ./meson_init.sh
$ ninja -C build-fpga
$ ninja -C build-verilator
```

To build an individual target:

In the following example, just build the flash_test.bin
target:

```console
$ ninja -C build-fpga sw/tests/flash_ctrl/flash_test.bin
```

meson_init.sh configuration options:

* `-f`: Delete build directories before calling meson.
* `-r`: Call Meson with `--reconfigure` flag. Requires valid build
  directories.
  • Loading branch information
moidx committed Sep 18, 2019
1 parent 4ba02d0 commit 03f2e237748fc4f0ebfa20d09eefef6899546e48
@@ -0,0 +1,62 @@
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

project('opentitan', 'c', 'cpp', version: '0.1',)

target = get_option('target')
if target == 'undef'
error('target option not set. Please run meson with a valid build target option.')
endif

if target == 'verilator'
# TODO: Consider using extra args array if using this flag globally is no
# longer OK.
add_project_arguments('-DSIMULATION', language: 'c')
endif

# The following flags are applied to all native builds.
add_project_arguments('-Wall', '-Werror', 'cpp_std=c++14', language: 'cpp')
add_project_arguments('-std=gnu99', '-Wall', '-Werror', language: 'c')

# Common program references.
prog_python = import('python').find_installation('python3')
prog_objcopy = find_program('objcopy')
prog_srec_cat = find_program('srec_cat')
prog_git = find_program('git')

# RISCV linker parameters.
riscv_linkfile = files(['sw/exts/common/link.ld'])
riscv_link_args = ['-Wl,-T,@0@/@1@'.format('..', riscv_linkfile[0])]
riscv_link_deps = [riscv_linkfile]

# RISCV CRT parameters
startup_files = files(['sw/exts/common/_crt.c'])

# Additional arguments for utility in charge of generating bin and vmem outputs
# These variables are expected to be used in custom_target rules.
embedded_target_output = ['@BASENAME@.bin', '@BASENAME@.vmem']
embedded_target_args = [prog_python, '../util/embedded_target.py',
'--objcopy', prog_objcopy, '--srec_cat', prog_srec_cat,
'--input', '@INPUT@', '--basename', '@BASENAME@', '--outdir', '@OUTDIR@']

# HW headers. Placed here to avoid adding meson build files in the hw directory.
gen_hw_hdr = generator(
prog_python,
output: '@BASENAME@_regs.h',
arguments: [
'@SOURCE_DIR@/util/regtool.py', '-D', '-o', '@BUILD_DIR@/@BASENAME@_regs.h',
'@INPUT@',
],
)

# TODO: Considering moving these into hw/ip directories.
hw_ip_flash_ctrl_regs_h = gen_hw_hdr.process('hw/ip/flash_ctrl/doc/flash_ctrl.hjson')
hw_ip_gpio_reg_h = gen_hw_hdr.process('hw/ip/gpio/doc/gpio.hjson')
hw_ip_hmac_reg_h = gen_hw_hdr.process('hw/ip/hmac/doc/hmac.hjson')
hw_ip_spi_device_reg_h = gen_hw_hdr.process('hw/ip/spi_device/doc/spi_device.hjson')
hw_ip_rv_timer_reg_h = gen_hw_hdr.process('hw/ip/rv_timer/doc/rv_timer.hjson')
hw_ip_uart_reg_h = gen_hw_hdr.process('hw/ip/uart/doc/uart.hjson')
hw_ip_usbdev_reg_h = gen_hw_hdr.process('hw/ip/usbdev/doc/usbdev.hjson')

subdir('sw')
@@ -0,0 +1,76 @@
#!/bin/bash
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

set -o errexit
set -o pipefail
set -o nounset

readonly BUILD_DIR_PREFIX="build"
readonly TARGET_VERILATOR="verilator"
readonly TARGET_FPGA="fpga"

function usage() {
cat << USAGE
Configure Meson build targets.
Usage: $0 [-r|-f]
-f: Remove build directories before running Meson.
-r: Force reconfiguration of build directories.
USAGE
}

FLAGS_force=false
FLAGS_reconfigure=""
while getopts 'r?:f?' flag; do
case "${flag}" in
f) FLAGS_force=true;;
r) FLAGS_reconfigure="--reconfigure";;
?) usage && exit 1;;
*) usage
error "Unexpected option ${flag}"
;;
esac
done

if [[ "${FLAGS_force}" == true && -n "${FLAGS_reconfigure}" ]]; then
usage >&2
echo "Error: -r and -f cannont be used at the same time." >&2
exit 1
fi

if [[ ! -n "$(command -v meson)" ]]; then
echo "Unable to find meson. Please install meson before running this command." >&2
exit 1
fi

if [[ ! -n "$(command -v ninja)" ]]; then
echo "Unable to find ninja. Please install ninja before running this command." >&2
exit 1
fi

if [[ "${FLAGS_force}" == true ]]; then
for target_suffix in "${TARGET_VERILATOR}" "${TARGET_FPGA}"; do
rm -rf "${BUILD_DIR_PREFIX}-${target_suffix}"
done
fi

if [[ ! -n "${FLAGS_reconfigure}" ]] ; then
for target_suffix in "${TARGET_VERILATOR}" "${TARGET_FPGA}"; do
if [[ -d "${BUILD_DIR_PREFIX}-${target_suffix}" ]]; then
usage >&2
echo "Error: ${BUILD_DIR_PREFIX}-${target_suffix} already exists. " \
"Remove directory, or rerun $0 with the -r option" >&2
exit 1
fi
done
fi

meson ${FLAGS_reconfigure} "-Dtarget=${TARGET_VERILATOR}" --cross-file=toolchain.txt \
--buildtype=plain "${BUILD_DIR_PREFIX}-${TARGET_VERILATOR}"

meson ${FLAGS_reconfigure} "-Dtarget=${TARGET_FPGA}" --cross-file=toolchain.txt \
--buildtype=plain "${BUILD_DIR_PREFIX}-${TARGET_FPGA}"
@@ -0,0 +1 @@
option('target', type : 'combo', choices: ['verilator', 'fpga', 'undef'], value : 'undef')
@@ -0,0 +1,42 @@
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

# Generate header file with chip_info information.
chip_info_h = custom_target(
'chip_info_h',
output: 'chip_info.h',
command: [
prog_python, '@SOURCE_DIR@/util/rom_chip_info.py', '--outdir', '@OUTDIR@']
)

# ROM linker parameters.
rom_linkfile = files(['rom_link.ld'])
rom_link_args = ['-Wl,-T,@0@/@1@'.format('..', rom_linkfile[0])]
rom_link_deps = [rom_linkfile]

custom_target(
'boot_rom',
output: embedded_target_output,
input: executable(
'boot_rom',
[
chip_info_h,
'boot_rom.c',
'bootstrap.c',
'crt0.S'
],
name_suffix: 'elf',
link_args: rom_link_args,
link_depends: rom_link_deps,
dependencies: [
sw_lib_flash_ctrl,
sw_lib_gpio,
sw_lib_hmac,
sw_lib_spi_device,
sw_lib_uart,
],
),
command: embedded_target_args,
build_by_default: true,
)
@@ -0,0 +1,23 @@
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

custom_target(
'hello_usbdev',
output: embedded_target_output,
input: executable(
'hello_usbdev',
['hello_usbdev.c', startup_files],
name_suffix: 'elf',
link_args: riscv_link_args,
link_depends: riscv_link_deps,
dependencies: [
sw_lib_gpio,
sw_lib_irq,
sw_lib_uart,
sw_lib_usb,
],
),
command: embedded_target_args,
build_by_default: true,
)
@@ -0,0 +1,23 @@
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

custom_target(
'hello_world',
output: embedded_target_output,
input: executable(
'hello_world',
['hello_world.c', startup_files],
name_suffix: 'elf',
link_args: riscv_link_args,
link_depends: riscv_link_deps,
dependencies: [
sw_lib_gpio,
sw_lib_irq,
sw_lib_spi_device,
sw_lib_uart,
],
),
command: embedded_target_args,
build_by_default: true,
)
@@ -0,0 +1,6 @@
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

subdir('hello_usbdev')
subdir('hello_world')
@@ -0,0 +1,6 @@
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

subdir('vendor')
subdir('spiflash')
@@ -0,0 +1,18 @@
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

executable(
'spiflash',
[
'ftdi_spi_interface.cc',
'spiflash.cc',
'updater.cc',
'verilator_spi_interface.cc',
],
dependencies: [
dependency('libcrypto', native: true),
libmpsse
],
native: true,
)
@@ -0,0 +1,5 @@
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

subdir('mpsse')
@@ -0,0 +1,19 @@
# Copyright lowRISC contributors.
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

libmpsse = declare_dependency(
include_directories : include_directories('.'),
link_with: static_library(
'mpsse',
sources: [
'mpsse.c',
'support.c'
],
dependencies: [
dependency('libftdi1', native: true),
dependency('libusb-1.0', native: true),
],
native: true,
),
)

0 comments on commit 03f2e23

Please sign in to comment.
You can’t perform that action at this time.