blob: 81c0efe8bee2603d86c75d77cdbe02b88199b1b9 [file] [log] [blame]
Miguel Osorio03f2e232019-09-17 19:44:37 -07001# 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 Elliotteed65c62019-12-03 10:28:59 +00005project(
6 'opentitan', 'c', 'cpp',
7 version: '0.1',
Philipp Wagnere8ba3dc2020-10-30 16:49:31 +00008 meson_version: '>=0.53.0', # Matches version in python-requirements.txt
Miguel Young de la Sotafa12ce02019-11-26 14:53:55 -06009 default_options: [
10 'c_std=c11',
11 'build.c_std=c11',
Sam Elliott0c157c02019-12-02 19:30:59 +000012 'cpp_std=c++14',
13 'build.cpp_std=c++14',
Sam Elliott6fa87b12020-10-05 19:30:20 +010014 'warning_level=2',
Sam Elliotte9f25662019-12-02 19:33:19 +000015 'werror=true',
Sam Elliott5797c562019-12-02 19:40:34 +000016 'debug=true',
Sam Elliott6f30e932020-01-08 15:57:01 +000017 'b_staticpic=false', # Disable PIC for device static libraries
18 'b_pie=false', # Disable PIE for device executables
Sam Elliott0c157c02019-12-02 19:30:59 +000019 ],
Sam Elliotteed65c62019-12-03 10:28:59 +000020)
Miguel Osorio03f2e232019-09-17 19:44:37 -070021
Rupert Swarbrick7a574c32021-04-28 11:16:52 +010022# We allow both GCC and Clang, but these minimum versions.
Sam Elliott1e498992021-01-08 15:40:22 +000023required_gcc_version = '>=5'
24required_clang_version = '>=3.5'
25
Miguel Young de la Sotab2ef4832019-11-22 12:55:46 -060026ot_version = get_option('ot_version')
27if ot_version == 'undef'
28 error('ot_version option not set. Please run meson with a valid OpenTitan version option.')
29endif
30
Philipp Wagner525891d2020-10-30 19:01:04 +000031bin_dir = get_option('bin_dir')
32if bin_dir == 'undef'
33 error('bin_dir option not set. Please run meson with a valid binary directory option.')
Miguel Young de la Sota3c8ab3b2019-11-21 13:59:09 -060034endif
35
Sam Elliott371c79f2020-06-23 20:06:19 +010036# See the comment in `./util/meson-purge-includes.sh` for why this is necessary.
37if not get_option('keep_includes')
38 meson.add_postconf_script(meson.source_root() + '/util/meson-purge-includes.sh')
39endif
40
Alphan Ulusoyd2bbbe12020-08-14 15:39:51 -040041coverage = get_option('coverage')
42# Coverage requires clang.
43if coverage and meson.get_compiler('c').get_id() != 'clang'
44 error('Coverage requires clang.')
45endif
Sam Elliott371c79f2020-06-23 20:06:19 +010046
Sam Elliott1e498992021-01-08 15:40:22 +000047# Check C/C++ compiler version numbers.
48c_compilers = {
49 'C compiler for host': meson.get_compiler('c', native: true),
50 'C compiler for device': meson.get_compiler('c', native: false),
51 'C++ compiler for host': meson.get_compiler('cpp', native: true),
52}
53foreach kind, compiler : c_compilers
54 if compiler.get_id() == 'clang'
55 if not compiler.version().version_compare(required_clang_version)
56 error('@0@ version mismatch, @1@ required.'.format(kind, required_clang_version))
57 endif
58 elif compiler.get_id() == 'gcc'
59 if not compiler.version().version_compare(required_gcc_version)
60 error('@0@ version mismatch, @1@ required.'.format(kind, required_gcc_version))
61 endif
62 else
63 warning('Unsupported @0@: @1@'.format(kind, compiler.get_id()))
64 message('Please ensure this compiler satisfies the OpenTitan requirements in:')
65 message(' https://docs.opentitan.org/doc/ug/install_instructions/#system-requirements')
66 endif
67endforeach
68
69
Sam Elliottf928f2d2020-10-06 17:02:57 +010070# C compiler arguments to to catch common errors, used on all builds.
71extra_warning_args = [
Sam Elliott3eed4a72020-10-05 19:11:18 +010072 '-Wimplicit-fallthrough', # Error on implicit fallthrough
Sam Elliott499ad612020-10-05 19:27:55 +010073 '-Wswitch-default', # Ensure all switches have default statements
74 '-Wno-covered-switch-default', # We require `default:` always.
Sam Elliotta49d7042020-10-07 10:46:41 +010075 '-Wgnu', # We aim to be standards compliant, and avoid gnu extensions.
Philipp Wagner90610762020-11-04 17:34:58 +000076 '-Wno-error=unused-function', # Don't error out on unused functions, only warn.
Sam Elliott6fa87b12020-10-05 19:30:20 +010077 # Issues we intend to fix in the future but are currently ignored as there are
78 # many places they are triggered.
79 '-Wno-unused-parameter',
80 '-Wno-sign-compare',
81 '-Wno-missing-field-initializers',
Sam Elliotta49d7042020-10-07 10:46:41 +010082 '-Wno-gnu-zero-variadic-macro-arguments',
Sam Elliottf928f2d2020-10-06 17:02:57 +010083]
84
85# C compiler arguments to optimize for size, used on cross builds only.
Sam Elliottca894e02019-12-03 10:25:17 +000086optimize_size_args = [
87 '-Os', # General "Optimize for Size" Option
88 '-fvisibility=hidden', # Hide symbols by default
89]
90
Sam Elliottf928f2d2020-10-06 17:02:57 +010091native_c_compiler = meson.get_compiler('c', native: true)
92cross_c_compiler = meson.get_compiler('c', native: false)
93
94if cross_c_compiler.has_argument('-Wa,--no-pad-sections')
95 # Don't pad assembly sections. This was originally added to avoid sections
96 # being padded to the alignment size. Specifically, .vectors was being
97 # padded to 256 bytes when aligning to that value, when it only needed to be
98 # 128 bytes long. Clang doesn't do this padding, so restricting this option
99 # to GCC doesn't waste space when compiling with Clang.
100 optimize_size_args += '-Wa,--no-pad-sections'
Luís Marques1db34ad2020-05-27 13:41:57 +0100101endif
102
Sam Elliott371c79f2020-06-23 20:06:19 +0100103# The following flags are applied to *all* builds, both cross builds and native
104# builds.
105c_cpp_args = [
106 # We use absolute include paths as much as possible.
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -0500107 '-I' + meson.source_root(),
108 '-I' + meson.build_root(),
Sam Elliott371c79f2020-06-23 20:06:19 +0100109]
110add_project_arguments(c_cpp_args, language: ['c', 'cpp'], native: false)
111add_project_arguments(c_cpp_args, language: ['c', 'cpp'], native: true)
Miguel Osorio03f2e232019-09-17 19:44:37 -0700112
Sam Elliott58d285f2019-11-29 11:58:29 +0000113# The following flags are applied only to cross builds
Sam Elliott371c79f2020-06-23 20:06:19 +0100114c_cpp_cross_args = [
115 # Do not use standard system headers
116 '-nostdinc',
117 # Use OpenTitan's freestanding headers instead
118 '-isystem' + meson.source_root() / 'sw/device/lib/base/freestanding',
Sam Elliott120c7372020-11-03 15:14:46 +0000119 # Don't emit unwinding information
120 '-fno-asynchronous-unwind-tables',
Sam Elliott1d1ac6c2020-11-04 19:03:20 +0000121 # Don't use COMMON sections for uninitialized globals
122 '-fno-common',
Michael Mundaydcdadc72021-08-25 23:06:52 +0100123 # Place each function into its own section (used in conjunction with
124 # --gc-sections to remove unused functions from output files).
125 '-ffunction-sections',
Sam Elliott371c79f2020-06-23 20:06:19 +0100126]
Sam Elliottf928f2d2020-10-06 17:02:57 +0100127
128# Add extra warning flags for cross builds, if they are supported.
129foreach warning_arg : extra_warning_args
130 if cross_c_compiler.has_argument(warning_arg)
131 c_cpp_cross_args += [warning_arg]
132 endif
133endforeach
134
135# Add the flags for cross builds.
Sam Elliott58d285f2019-11-29 11:58:29 +0000136add_project_arguments(
Sam Elliott371c79f2020-06-23 20:06:19 +0100137 c_cpp_cross_args,
Sam Elliottca894e02019-12-03 10:25:17 +0000138 optimize_size_args,
Sam Elliott371c79f2020-06-23 20:06:19 +0100139 language: ['c', 'cpp'], native: false)
Sam Elliott5e5a9dd2020-11-10 10:45:48 +0000140# Add a C-only flag for cross builds.
141add_project_arguments(
142 '-Wstrict-prototypes', # Ban K&R Declarations/Definitions.
143 language: ['c'], native: false)
Sam Elliott371c79f2020-06-23 20:06:19 +0100144
145# The following flags are applied only to cross builds
146c_cpp_cross_link_args = [
147 # Do not use standard system startup files or libraries
148 '-nostartfiles',
149 '-nostdlib',
150 # Only link static files
151 '-static',
Sam Elliott120c7372020-11-03 15:14:46 +0000152 # Warn if we use COMMON
153 '-Wl,--warn-common',
154 # Warn if we include orphan sections
155 '-Wl,--orphan-handling=warn',
156 # Turn Linker Warnings into Errors
157 '-Wl,--fatal-warnings',
Michael Mundaydcdadc72021-08-25 23:06:52 +0100158 # Garbage collect unused sections.
159 '-Wl,--gc-sections',
Sam Elliott371c79f2020-06-23 20:06:19 +0100160]
Sam Elliott58d285f2019-11-29 11:58:29 +0000161add_project_link_arguments(
Sam Elliott371c79f2020-06-23 20:06:19 +0100162 c_cpp_cross_link_args,
163 language: ['c', 'cpp'], native: false)
164
Sam Elliott0ad19da2020-08-24 17:29:42 +0100165# The following flags are applied only to native builds
166c_cpp_native_args = [
167 # Use a define to exclude libc redefinitions.
168 '-DHOST_BUILD',
169]
Sam Elliottf928f2d2020-10-06 17:02:57 +0100170
171# Add Extra warning flags for native builds, if they are supported.
172foreach warning_arg : extra_warning_args
173 if native_c_compiler.has_argument(warning_arg)
174 c_cpp_native_args += [warning_arg]
175 endif
176endforeach
177
178# Add the flags for native builds.
Sam Elliott0ad19da2020-08-24 17:29:42 +0100179add_project_arguments(
180 c_cpp_native_args,
181 language: ['c', 'cpp'], native: true)
Sam Elliott5e5a9dd2020-11-10 10:45:48 +0000182# Add a C-only flag for native builds.
183add_project_arguments(
184 '-Wstrict-prototypes', # Ban K&R Declarations/Definitions.
185 language: ['c'], native: true)
Sam Elliott58d285f2019-11-29 11:58:29 +0000186
Miguel Osorio03f2e232019-09-17 19:44:37 -0700187# Common program references.
188prog_python = import('python').find_installation('python3')
Philipp Wagner2d149842020-11-19 22:56:18 +0000189prog_env = find_program('env')
190prog_srec_cat = find_program('srec_cat')
191
Philipp Wagner80cc8232020-10-29 16:26:09 +0000192prog_ld = find_program('ld')
193prog_as = find_program('as')
Timothy Chen5f7b9c42019-10-30 22:27:16 -0700194prog_objdump = find_program('objdump')
Miguel Osorio03f2e232019-09-17 19:44:37 -0700195prog_objcopy = find_program('objcopy')
Philipp Wagner2d149842020-11-19 22:56:18 +0000196
197prog_otbn_as = meson.source_root() / 'hw/ip/otbn/util/otbn-as'
198prog_otbn_ld = meson.source_root() / 'hw/ip/otbn/util/otbn-ld'
Miguel Osorio03f2e232019-09-17 19:44:37 -0700199
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -0500200# Hardware register headers. These are generated from HJSON files, and accesible
201# in C via |#include "{IP_NAME}_regs.h"|.
Miguel Osorio03f2e232019-09-17 19:44:37 -0700202gen_hw_hdr = generator(
203 prog_python,
204 output: '@BASENAME@_regs.h',
205 arguments: [
Alistair Francis38a4bc52021-05-25 09:58:47 +1000206 '@SOURCE_DIR@/util/regtool.py', '-D', '-o', '@OUTPUT@',
Miguel Osorio03f2e232019-09-17 19:44:37 -0700207 '@INPUT@',
208 ],
209)
210
211# TODO: Considering moving these into hw/ip directories.
Pirmin Vogel84884772020-11-27 18:03:19 +0100212TOPNAME='top_earlgrey'
Pirmin Vogeldef712f2019-10-24 18:57:36 +0100213hw_ip_aes_reg_h = gen_hw_hdr.process('hw/ip/aes/data/aes.hjson')
Philipp Wagnerc658d4e2021-03-24 14:43:11 +0000214hw_ip_alert_handler_reg_h = gen_hw_hdr.process('hw/' + TOPNAME + '/ip/alert_handler/data/autogen/alert_handler.hjson')
Silvestrs Timofejevs947a44a2020-12-04 16:42:29 +0000215hw_ip_aon_timer_reg_h = gen_hw_hdr.process('hw/ip/aon_timer/data/aon_timer.hjson')
Michael Munday5acd3e12021-01-22 13:36:25 +0000216hw_ip_clkmgr_reg_h = gen_hw_hdr.process('hw/' + TOPNAME + '/ip/clkmgr/data/autogen/clkmgr.hjson')
Timothy Trippele9c06242021-08-18 23:04:02 +0000217hw_ip_entropy_src_reg_h = gen_hw_hdr.process('hw/ip/entropy_src/data/entropy_src.hjson')
Pirmin Vogel84884772020-11-27 18:03:19 +0100218hw_ip_flash_ctrl_reg_h = gen_hw_hdr.process('hw/' + TOPNAME + '/ip/flash_ctrl/data/autogen/flash_ctrl.hjson')
Tobias Wölfel4c5fbec2019-10-23 12:43:17 +0200219hw_ip_gpio_reg_h = gen_hw_hdr.process('hw/ip/gpio/data/gpio.hjson')
220hw_ip_hmac_reg_h = gen_hw_hdr.process('hw/ip/hmac/data/hmac.hjson')
Michael Mundaya753d342021-03-22 14:08:15 +0000221hw_ip_kmac_reg_h = gen_hw_hdr.process('hw/ip/kmac/data/kmac.hjson')
Miguel Young de la Sota92bc4cc2020-04-09 12:32:43 -0400222hw_ip_i2c_reg_h = gen_hw_hdr.process('hw/ip/i2c/data/i2c.hjson')
Alphan Ulusoy910cd222020-11-16 15:04:02 -0500223hw_ip_keymgr_reg_h = gen_hw_hdr.process('hw/ip/keymgr/data/keymgr.hjson')
Miguel Young de la Sota29a1e352020-11-18 10:49:59 -0500224hw_ip_lc_ctrl_reg_h = gen_hw_hdr.process('hw/ip/lc_ctrl/data/lc_ctrl.hjson')
Philipp Wagner5c2d2942020-06-22 11:34:00 +0100225hw_ip_otbn_reg_h = gen_hw_hdr.process('hw/ip/otbn/data/otbn.hjson')
Miguel Young de la Sota1fac7822020-09-18 13:47:28 -0400226hw_ip_otp_ctrl_reg_h = gen_hw_hdr.process('hw/ip/otp_ctrl/data/otp_ctrl.hjson')
Tobias Wölfel4c5fbec2019-10-23 12:43:17 +0200227hw_ip_spi_device_reg_h = gen_hw_hdr.process('hw/ip/spi_device/data/spi_device.hjson')
Timothy Chen4ca4a2f2021-03-19 13:51:39 -0700228hw_ip_sram_ctrl_reg_h = gen_hw_hdr.process('hw/ip/sram_ctrl/data/sram_ctrl.hjson')
229hw_ip_entropy_src_reg_h = gen_hw_hdr.process('hw/ip/entropy_src/data/entropy_src.hjson')
230hw_ip_csrng_reg_h = gen_hw_hdr.process('hw/ip/csrng/data/csrng.hjson')
231hw_ip_edn_reg_h = gen_hw_hdr.process('hw/ip/edn/data/edn.hjson')
Tobias Wölfel4c5fbec2019-10-23 12:43:17 +0200232hw_ip_rv_timer_reg_h = gen_hw_hdr.process('hw/ip/rv_timer/data/rv_timer.hjson')
233hw_ip_uart_reg_h = gen_hw_hdr.process('hw/ip/uart/data/uart.hjson')
234hw_ip_usbdev_reg_h = gen_hw_hdr.process('hw/ip/usbdev/data/usbdev.hjson')
Pirmin Vogel84884772020-11-27 18:03:19 +0100235hw_ip_pwrmgr_reg_h = gen_hw_hdr.process('hw/' + TOPNAME + '/ip/pwrmgr/data/autogen/pwrmgr.hjson')
236hw_ip_rstmgr_reg_h = gen_hw_hdr.process('hw/' + TOPNAME + '/ip/rstmgr/data/autogen/rstmgr.hjson')
Timothy Chen1d8b9602021-07-08 19:58:08 -0700237hw_ip_ibex_reg_h = gen_hw_hdr.process('hw//ip/rv_core_ibex/data/rv_core_ibex.hjson')
Pirmin Vogel84884772020-11-27 18:03:19 +0100238hw_top_earlgrey_pinmux_reg_h = gen_hw_hdr.process('hw/' + TOPNAME + '/ip/pinmux/data/autogen/pinmux.hjson')
239hw_top_earlgrey_rv_plic_reg_h = gen_hw_hdr.process('hw/' + TOPNAME + '/ip/rv_plic/data/autogen/rv_plic.hjson')
Miguel Osorio03f2e232019-09-17 19:44:37 -0700240
Sam Elliott7e36bd72020-04-22 14:05:49 +0100241# Top Earlgrey library (top_earlgrey)
242# The sources for this are generated into the hw hierarchy.
243top_earlgrey = declare_dependency(
244 link_with: static_library(
245 'top_earlgrey_ot',
246 sources: [
Pirmin Vogel84884772020-11-27 18:03:19 +0100247 'hw/' + TOPNAME + '/sw/autogen/top_earlgrey.c',
Sam Elliott7e36bd72020-04-22 14:05:49 +0100248 ],
249 )
250)
251
Miguel Osorio03f2e232019-09-17 19:44:37 -0700252subdir('sw')
Philipp Wagner7f9a5bb2020-11-19 22:59:59 +0000253
254# Write environment file
255prog_meson_write_env = meson.source_root() / 'util/meson_write_env.py'
256r = run_command(
257 prog_python,
258 prog_meson_write_env,
259
260 '--out-file',
261 meson.current_build_dir() / '.env',
262
263 'PYTHON=@0@'.format(prog_python.path()),
264 'OTBN_AS=@0@'.format(prog_otbn_as),
265 'OTBN_LD=@0@'.format(prog_otbn_ld),
266 'RV32_TOOL_OBJCOPY=@0@'.format(prog_objcopy.path()),
267 'RV32_TOOL_AS=@0@'.format(prog_as.path()),
268 'RV32_TOOL_LD=@0@'.format(prog_ld.path()),
269 'RV32_TOOL_OBJDUMP=@0@'.format(prog_objdump.path()),
270 'RV32_TOOL_OBJCOPY=@0@'.format(prog_objcopy.path()),
271)
272assert(r.returncode() == 0, 'Error while calling meson_write_env.py.\n' +
273 'STDOUT:\n' + r.stdout().strip() + '\n' +
274 'STDERR:\n ' + r.stderr().strip())
275message(r.stdout().strip())