Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 1 | # Copyright lowRISC contributors. |
| 2 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | |
Sam Elliott | eed65c6 | 2019-12-03 10:28:59 +0000 | [diff] [blame] | 5 | project( |
| 6 | 'opentitan', 'c', 'cpp', |
| 7 | version: '0.1', |
Philipp Wagner | e8ba3dc | 2020-10-30 16:49:31 +0000 | [diff] [blame] | 8 | meson_version: '>=0.53.0', # Matches version in python-requirements.txt |
Miguel Young de la Sota | fa12ce0 | 2019-11-26 14:53:55 -0600 | [diff] [blame] | 9 | default_options: [ |
| 10 | 'c_std=c11', |
| 11 | 'build.c_std=c11', |
Sam Elliott | 0c157c0 | 2019-12-02 19:30:59 +0000 | [diff] [blame] | 12 | 'cpp_std=c++14', |
| 13 | 'build.cpp_std=c++14', |
Sam Elliott | 6fa87b1 | 2020-10-05 19:30:20 +0100 | [diff] [blame] | 14 | 'warning_level=2', |
Sam Elliott | e9f2566 | 2019-12-02 19:33:19 +0000 | [diff] [blame] | 15 | 'werror=true', |
Sam Elliott | 5797c56 | 2019-12-02 19:40:34 +0000 | [diff] [blame] | 16 | 'debug=true', |
Sam Elliott | 6f30e93 | 2020-01-08 15:57:01 +0000 | [diff] [blame] | 17 | 'b_staticpic=false', # Disable PIC for device static libraries |
| 18 | 'b_pie=false', # Disable PIE for device executables |
Sam Elliott | 0c157c0 | 2019-12-02 19:30:59 +0000 | [diff] [blame] | 19 | ], |
Sam Elliott | eed65c6 | 2019-12-03 10:28:59 +0000 | [diff] [blame] | 20 | ) |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 21 | |
Miguel Young de la Sota | b2ef483 | 2019-11-22 12:55:46 -0600 | [diff] [blame] | 22 | ot_version = get_option('ot_version') |
| 23 | if ot_version == 'undef' |
| 24 | error('ot_version option not set. Please run meson with a valid OpenTitan version option.') |
| 25 | endif |
| 26 | |
Philipp Wagner | 525891d | 2020-10-30 19:01:04 +0000 | [diff] [blame] | 27 | bin_dir = get_option('bin_dir') |
| 28 | if bin_dir == 'undef' |
| 29 | error('bin_dir option not set. Please run meson with a valid binary directory option.') |
Miguel Young de la Sota | 3c8ab3b | 2019-11-21 13:59:09 -0600 | [diff] [blame] | 30 | endif |
Jon Flatley | a863a28 | 2020-03-03 10:49:39 -0500 | [diff] [blame] | 31 | tock_local = get_option('tock_local') |
Miguel Young de la Sota | 3c8ab3b | 2019-11-21 13:59:09 -0600 | [diff] [blame] | 32 | |
Sam Elliott | 371c79f | 2020-06-23 20:06:19 +0100 | [diff] [blame] | 33 | # See the comment in `./util/meson-purge-includes.sh` for why this is necessary. |
| 34 | if not get_option('keep_includes') |
| 35 | meson.add_postconf_script(meson.source_root() + '/util/meson-purge-includes.sh') |
| 36 | endif |
| 37 | |
Alphan Ulusoy | d2bbbe1 | 2020-08-14 15:39:51 -0400 | [diff] [blame] | 38 | coverage = get_option('coverage') |
| 39 | # Coverage requires clang. |
| 40 | if coverage and meson.get_compiler('c').get_id() != 'clang' |
| 41 | error('Coverage requires clang.') |
| 42 | endif |
Sam Elliott | 371c79f | 2020-06-23 20:06:19 +0100 | [diff] [blame] | 43 | |
Sam Elliott | f928f2d | 2020-10-06 17:02:57 +0100 | [diff] [blame] | 44 | # C compiler arguments to to catch common errors, used on all builds. |
| 45 | extra_warning_args = [ |
Sam Elliott | 3eed4a7 | 2020-10-05 19:11:18 +0100 | [diff] [blame] | 46 | '-Wimplicit-fallthrough', # Error on implicit fallthrough |
Sam Elliott | 499ad61 | 2020-10-05 19:27:55 +0100 | [diff] [blame] | 47 | '-Wswitch-default', # Ensure all switches have default statements |
| 48 | '-Wno-covered-switch-default', # We require `default:` always. |
Sam Elliott | a49d704 | 2020-10-07 10:46:41 +0100 | [diff] [blame] | 49 | '-Wgnu', # We aim to be standards compliant, and avoid gnu extensions. |
Philipp Wagner | 9061076 | 2020-11-04 17:34:58 +0000 | [diff] [blame] | 50 | '-Wno-error=unused-function', # Don't error out on unused functions, only warn. |
Sam Elliott | 6fa87b1 | 2020-10-05 19:30:20 +0100 | [diff] [blame] | 51 | # Issues we intend to fix in the future but are currently ignored as there are |
| 52 | # many places they are triggered. |
| 53 | '-Wno-unused-parameter', |
| 54 | '-Wno-sign-compare', |
| 55 | '-Wno-missing-field-initializers', |
Sam Elliott | a49d704 | 2020-10-07 10:46:41 +0100 | [diff] [blame] | 56 | '-Wno-gnu-zero-variadic-macro-arguments', |
Sam Elliott | f928f2d | 2020-10-06 17:02:57 +0100 | [diff] [blame] | 57 | ] |
| 58 | |
| 59 | # C compiler arguments to optimize for size, used on cross builds only. |
Sam Elliott | ca894e0 | 2019-12-03 10:25:17 +0000 | [diff] [blame] | 60 | optimize_size_args = [ |
| 61 | '-Os', # General "Optimize for Size" Option |
| 62 | '-fvisibility=hidden', # Hide symbols by default |
| 63 | ] |
| 64 | |
Sam Elliott | f928f2d | 2020-10-06 17:02:57 +0100 | [diff] [blame] | 65 | native_c_compiler = meson.get_compiler('c', native: true) |
| 66 | cross_c_compiler = meson.get_compiler('c', native: false) |
| 67 | |
| 68 | if cross_c_compiler.has_argument('-Wa,--no-pad-sections') |
| 69 | # Don't pad assembly sections. This was originally added to avoid sections |
| 70 | # being padded to the alignment size. Specifically, .vectors was being |
| 71 | # padded to 256 bytes when aligning to that value, when it only needed to be |
| 72 | # 128 bytes long. Clang doesn't do this padding, so restricting this option |
| 73 | # to GCC doesn't waste space when compiling with Clang. |
| 74 | optimize_size_args += '-Wa,--no-pad-sections' |
Luís Marques | 1db34ad | 2020-05-27 13:41:57 +0100 | [diff] [blame] | 75 | endif |
| 76 | |
Sam Elliott | 371c79f | 2020-06-23 20:06:19 +0100 | [diff] [blame] | 77 | # The following flags are applied to *all* builds, both cross builds and native |
| 78 | # builds. |
| 79 | c_cpp_args = [ |
| 80 | # We use absolute include paths as much as possible. |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 81 | '-I' + meson.source_root(), |
| 82 | '-I' + meson.build_root(), |
Sam Elliott | 371c79f | 2020-06-23 20:06:19 +0100 | [diff] [blame] | 83 | ] |
| 84 | add_project_arguments(c_cpp_args, language: ['c', 'cpp'], native: false) |
| 85 | add_project_arguments(c_cpp_args, language: ['c', 'cpp'], native: true) |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 86 | |
Sam Elliott | 58d285f | 2019-11-29 11:58:29 +0000 | [diff] [blame] | 87 | # The following flags are applied only to cross builds |
Sam Elliott | 371c79f | 2020-06-23 20:06:19 +0100 | [diff] [blame] | 88 | c_cpp_cross_args = [ |
| 89 | # Do not use standard system headers |
| 90 | '-nostdinc', |
| 91 | # Use OpenTitan's freestanding headers instead |
| 92 | '-isystem' + meson.source_root() / 'sw/device/lib/base/freestanding', |
Sam Elliott | 120c737 | 2020-11-03 15:14:46 +0000 | [diff] [blame] | 93 | # Don't emit unwinding information |
| 94 | '-fno-asynchronous-unwind-tables', |
Sam Elliott | 1d1ac6c | 2020-11-04 19:03:20 +0000 | [diff] [blame] | 95 | # Don't use COMMON sections for uninitialized globals |
| 96 | '-fno-common', |
Sam Elliott | 371c79f | 2020-06-23 20:06:19 +0100 | [diff] [blame] | 97 | ] |
Sam Elliott | f928f2d | 2020-10-06 17:02:57 +0100 | [diff] [blame] | 98 | |
| 99 | # Add extra warning flags for cross builds, if they are supported. |
| 100 | foreach warning_arg : extra_warning_args |
| 101 | if cross_c_compiler.has_argument(warning_arg) |
| 102 | c_cpp_cross_args += [warning_arg] |
| 103 | endif |
| 104 | endforeach |
| 105 | |
| 106 | # Add the flags for cross builds. |
Sam Elliott | 58d285f | 2019-11-29 11:58:29 +0000 | [diff] [blame] | 107 | add_project_arguments( |
Sam Elliott | 371c79f | 2020-06-23 20:06:19 +0100 | [diff] [blame] | 108 | c_cpp_cross_args, |
Sam Elliott | ca894e0 | 2019-12-03 10:25:17 +0000 | [diff] [blame] | 109 | optimize_size_args, |
Sam Elliott | 371c79f | 2020-06-23 20:06:19 +0100 | [diff] [blame] | 110 | language: ['c', 'cpp'], native: false) |
Sam Elliott | 5e5a9dd | 2020-11-10 10:45:48 +0000 | [diff] [blame] | 111 | # Add a C-only flag for cross builds. |
| 112 | add_project_arguments( |
| 113 | '-Wstrict-prototypes', # Ban K&R Declarations/Definitions. |
| 114 | language: ['c'], native: false) |
Sam Elliott | 371c79f | 2020-06-23 20:06:19 +0100 | [diff] [blame] | 115 | |
| 116 | # The following flags are applied only to cross builds |
| 117 | c_cpp_cross_link_args = [ |
| 118 | # Do not use standard system startup files or libraries |
| 119 | '-nostartfiles', |
| 120 | '-nostdlib', |
| 121 | # Only link static files |
| 122 | '-static', |
Sam Elliott | 120c737 | 2020-11-03 15:14:46 +0000 | [diff] [blame] | 123 | # Warn if we use COMMON |
| 124 | '-Wl,--warn-common', |
| 125 | # Warn if we include orphan sections |
| 126 | '-Wl,--orphan-handling=warn', |
| 127 | # Turn Linker Warnings into Errors |
| 128 | '-Wl,--fatal-warnings', |
Sam Elliott | 371c79f | 2020-06-23 20:06:19 +0100 | [diff] [blame] | 129 | ] |
Sam Elliott | 58d285f | 2019-11-29 11:58:29 +0000 | [diff] [blame] | 130 | add_project_link_arguments( |
Sam Elliott | 371c79f | 2020-06-23 20:06:19 +0100 | [diff] [blame] | 131 | c_cpp_cross_link_args, |
| 132 | language: ['c', 'cpp'], native: false) |
| 133 | |
Sam Elliott | 0ad19da | 2020-08-24 17:29:42 +0100 | [diff] [blame] | 134 | # The following flags are applied only to native builds |
| 135 | c_cpp_native_args = [ |
| 136 | # Use a define to exclude libc redefinitions. |
| 137 | '-DHOST_BUILD', |
| 138 | ] |
Sam Elliott | f928f2d | 2020-10-06 17:02:57 +0100 | [diff] [blame] | 139 | |
| 140 | # Add Extra warning flags for native builds, if they are supported. |
| 141 | foreach warning_arg : extra_warning_args |
| 142 | if native_c_compiler.has_argument(warning_arg) |
| 143 | c_cpp_native_args += [warning_arg] |
| 144 | endif |
| 145 | endforeach |
| 146 | |
| 147 | # Add the flags for native builds. |
Sam Elliott | 0ad19da | 2020-08-24 17:29:42 +0100 | [diff] [blame] | 148 | add_project_arguments( |
| 149 | c_cpp_native_args, |
| 150 | language: ['c', 'cpp'], native: true) |
Sam Elliott | 5e5a9dd | 2020-11-10 10:45:48 +0000 | [diff] [blame] | 151 | # Add a C-only flag for native builds. |
| 152 | add_project_arguments( |
| 153 | '-Wstrict-prototypes', # Ban K&R Declarations/Definitions. |
| 154 | language: ['c'], native: true) |
Sam Elliott | 58d285f | 2019-11-29 11:58:29 +0000 | [diff] [blame] | 155 | |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 156 | # Common program references. |
| 157 | prog_python = import('python').find_installation('python3') |
Philipp Wagner | 2d14984 | 2020-11-19 22:56:18 +0000 | [diff] [blame] | 158 | prog_env = find_program('env') |
| 159 | prog_srec_cat = find_program('srec_cat') |
| 160 | |
Philipp Wagner | 80cc823 | 2020-10-29 16:26:09 +0000 | [diff] [blame] | 161 | prog_ld = find_program('ld') |
| 162 | prog_as = find_program('as') |
Timothy Chen | 5f7b9c4 | 2019-10-30 22:27:16 -0700 | [diff] [blame] | 163 | prog_objdump = find_program('objdump') |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 164 | prog_objcopy = find_program('objcopy') |
Philipp Wagner | 2d14984 | 2020-11-19 22:56:18 +0000 | [diff] [blame] | 165 | |
| 166 | prog_otbn_as = meson.source_root() / 'hw/ip/otbn/util/otbn-as' |
| 167 | prog_otbn_ld = meson.source_root() / 'hw/ip/otbn/util/otbn-ld' |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 168 | |
Miguel Young de la Sota | 3fbb28a | 2019-10-16 15:15:07 -0500 | [diff] [blame] | 169 | # Hardware register headers. These are generated from HJSON files, and accesible |
| 170 | # in C via |#include "{IP_NAME}_regs.h"|. |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 171 | gen_hw_hdr = generator( |
| 172 | prog_python, |
| 173 | output: '@BASENAME@_regs.h', |
| 174 | arguments: [ |
| 175 | '@SOURCE_DIR@/util/regtool.py', '-D', '-o', '@BUILD_DIR@/@BASENAME@_regs.h', |
| 176 | '@INPUT@', |
| 177 | ], |
| 178 | ) |
| 179 | |
| 180 | # TODO: Considering moving these into hw/ip directories. |
Pirmin Vogel | def712f | 2019-10-24 18:57:36 +0100 | [diff] [blame] | 181 | hw_ip_aes_reg_h = gen_hw_hdr.process('hw/ip/aes/data/aes.hjson') |
Miguel Young de la Sota | 475887f | 2020-08-10 16:40:09 -0400 | [diff] [blame] | 182 | hw_ip_alert_handler_reg_h = gen_hw_hdr.process('hw/ip/alert_handler/data/alert_handler.hjson') |
Sam Elliott | 91df54a | 2020-10-27 13:12:21 +0000 | [diff] [blame] | 183 | hw_ip_clkgmr_reg_h = gen_hw_hdr.process('hw/top_earlgrey/ip/clkmgr/data/autogen/clkmgr.hjson') |
Timothy Chen | 20706b0 | 2020-10-29 18:41:17 -0700 | [diff] [blame] | 184 | hw_ip_flash_ctrl_reg_h = gen_hw_hdr.process('hw/top_earlgrey/ip/flash_ctrl/data/autogen/flash_ctrl.hjson') |
Tobias Wölfel | 4c5fbec | 2019-10-23 12:43:17 +0200 | [diff] [blame] | 185 | hw_ip_gpio_reg_h = gen_hw_hdr.process('hw/ip/gpio/data/gpio.hjson') |
| 186 | hw_ip_hmac_reg_h = gen_hw_hdr.process('hw/ip/hmac/data/hmac.hjson') |
Miguel Young de la Sota | 92bc4cc | 2020-04-09 12:32:43 -0400 | [diff] [blame] | 187 | hw_ip_i2c_reg_h = gen_hw_hdr.process('hw/ip/i2c/data/i2c.hjson') |
Philipp Wagner | 5c2d294 | 2020-06-22 11:34:00 +0100 | [diff] [blame] | 188 | hw_ip_otbn_reg_h = gen_hw_hdr.process('hw/ip/otbn/data/otbn.hjson') |
Miguel Young de la Sota | 1fac782 | 2020-09-18 13:47:28 -0400 | [diff] [blame] | 189 | hw_ip_otp_ctrl_reg_h = gen_hw_hdr.process('hw/ip/otp_ctrl/data/otp_ctrl.hjson') |
Tobias Wölfel | 4c5fbec | 2019-10-23 12:43:17 +0200 | [diff] [blame] | 190 | hw_ip_spi_device_reg_h = gen_hw_hdr.process('hw/ip/spi_device/data/spi_device.hjson') |
| 191 | hw_ip_rv_timer_reg_h = gen_hw_hdr.process('hw/ip/rv_timer/data/rv_timer.hjson') |
| 192 | hw_ip_uart_reg_h = gen_hw_hdr.process('hw/ip/uart/data/uart.hjson') |
| 193 | hw_ip_usbdev_reg_h = gen_hw_hdr.process('hw/ip/usbdev/data/usbdev.hjson') |
Alphan Ulusoy | bb457d0 | 2020-08-11 23:44:38 -0400 | [diff] [blame] | 194 | hw_ip_pwrmgr_reg_h = gen_hw_hdr.process('hw/top_earlgrey/ip/pwrmgr/data/autogen/pwrmgr.hjson') |
Timothy Chen | f609628 | 2020-09-25 17:54:29 -0700 | [diff] [blame] | 195 | hw_ip_rstmgr_reg_h = gen_hw_hdr.process('hw/top_earlgrey/ip/rstmgr/data/autogen/rstmgr.hjson') |
Sam Elliott | 390e470 | 2020-04-03 15:29:01 +0100 | [diff] [blame] | 196 | hw_top_earlgrey_pinmux_reg_h = gen_hw_hdr.process('hw/top_earlgrey/ip/pinmux/data/autogen/pinmux.hjson') |
| 197 | hw_top_earlgrey_rv_plic_reg_h = gen_hw_hdr.process('hw/top_earlgrey/ip/rv_plic/data/autogen/rv_plic.hjson') |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 198 | |
Sam Elliott | 7e36bd7 | 2020-04-22 14:05:49 +0100 | [diff] [blame] | 199 | # Top Earlgrey library (top_earlgrey) |
| 200 | # The sources for this are generated into the hw hierarchy. |
| 201 | top_earlgrey = declare_dependency( |
| 202 | link_with: static_library( |
| 203 | 'top_earlgrey_ot', |
| 204 | sources: [ |
| 205 | 'hw/top_earlgrey/sw/autogen/top_earlgrey.c', |
| 206 | ], |
| 207 | ) |
| 208 | ) |
| 209 | |
Miguel Osorio | 03f2e23 | 2019-09-17 19:44:37 -0700 | [diff] [blame] | 210 | subdir('sw') |
Philipp Wagner | 7f9a5bb | 2020-11-19 22:59:59 +0000 | [diff] [blame] | 211 | |
| 212 | # Write environment file |
| 213 | prog_meson_write_env = meson.source_root() / 'util/meson_write_env.py' |
| 214 | r = run_command( |
| 215 | prog_python, |
| 216 | prog_meson_write_env, |
| 217 | |
| 218 | '--out-file', |
| 219 | meson.current_build_dir() / '.env', |
| 220 | |
| 221 | 'PYTHON=@0@'.format(prog_python.path()), |
| 222 | 'OTBN_AS=@0@'.format(prog_otbn_as), |
| 223 | 'OTBN_LD=@0@'.format(prog_otbn_ld), |
| 224 | 'RV32_TOOL_OBJCOPY=@0@'.format(prog_objcopy.path()), |
| 225 | 'RV32_TOOL_AS=@0@'.format(prog_as.path()), |
| 226 | 'RV32_TOOL_LD=@0@'.format(prog_ld.path()), |
| 227 | 'RV32_TOOL_OBJDUMP=@0@'.format(prog_objdump.path()), |
| 228 | 'RV32_TOOL_OBJCOPY=@0@'.format(prog_objcopy.path()), |
| 229 | ) |
| 230 | assert(r.returncode() == 0, 'Error while calling meson_write_env.py.\n' + |
| 231 | 'STDOUT:\n' + r.stdout().strip() + '\n' + |
| 232 | 'STDERR:\n ' + r.stderr().strip()) |
| 233 | message(r.stdout().strip()) |