blob: 4ed1f79546316bbfe6077a7e39005da35e476d96 [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
Sam Elliott1e498992021-01-08 15:40:22 +000022# We allow both GCC and Clang, but these minimum versions.
23required_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
Jon Flatleya863a282020-03-03 10:49:39 -050035tock_local = get_option('tock_local')
Miguel Young de la Sota3c8ab3b2019-11-21 13:59:09 -060036
Sam Elliott371c79f2020-06-23 20:06:19 +010037# See the comment in `./util/meson-purge-includes.sh` for why this is necessary.
38if not get_option('keep_includes')
39 meson.add_postconf_script(meson.source_root() + '/util/meson-purge-includes.sh')
40endif
41
Alphan Ulusoyd2bbbe12020-08-14 15:39:51 -040042coverage = get_option('coverage')
43# Coverage requires clang.
44if coverage and meson.get_compiler('c').get_id() != 'clang'
45 error('Coverage requires clang.')
46endif
Sam Elliott371c79f2020-06-23 20:06:19 +010047
Sam Elliott1e498992021-01-08 15:40:22 +000048# Check C/C++ compiler version numbers.
49c_compilers = {
50 'C compiler for host': meson.get_compiler('c', native: true),
51 'C compiler for device': meson.get_compiler('c', native: false),
52 'C++ compiler for host': meson.get_compiler('cpp', native: true),
53}
54foreach kind, compiler : c_compilers
55 if compiler.get_id() == 'clang'
56 if not compiler.version().version_compare(required_clang_version)
57 error('@0@ version mismatch, @1@ required.'.format(kind, required_clang_version))
58 endif
59 elif compiler.get_id() == 'gcc'
60 if not compiler.version().version_compare(required_gcc_version)
61 error('@0@ version mismatch, @1@ required.'.format(kind, required_gcc_version))
62 endif
63 else
64 warning('Unsupported @0@: @1@'.format(kind, compiler.get_id()))
65 message('Please ensure this compiler satisfies the OpenTitan requirements in:')
66 message(' https://docs.opentitan.org/doc/ug/install_instructions/#system-requirements')
67 endif
68endforeach
69
70
Sam Elliottf928f2d2020-10-06 17:02:57 +010071# C compiler arguments to to catch common errors, used on all builds.
72extra_warning_args = [
Sam Elliott3eed4a72020-10-05 19:11:18 +010073 '-Wimplicit-fallthrough', # Error on implicit fallthrough
Sam Elliott499ad612020-10-05 19:27:55 +010074 '-Wswitch-default', # Ensure all switches have default statements
75 '-Wno-covered-switch-default', # We require `default:` always.
Sam Elliotta49d7042020-10-07 10:46:41 +010076 '-Wgnu', # We aim to be standards compliant, and avoid gnu extensions.
Philipp Wagner90610762020-11-04 17:34:58 +000077 '-Wno-error=unused-function', # Don't error out on unused functions, only warn.
Sam Elliott6fa87b12020-10-05 19:30:20 +010078 # Issues we intend to fix in the future but are currently ignored as there are
79 # many places they are triggered.
80 '-Wno-unused-parameter',
81 '-Wno-sign-compare',
82 '-Wno-missing-field-initializers',
Sam Elliotta49d7042020-10-07 10:46:41 +010083 '-Wno-gnu-zero-variadic-macro-arguments',
Sam Elliottf928f2d2020-10-06 17:02:57 +010084]
85
86# C compiler arguments to optimize for size, used on cross builds only.
Sam Elliottca894e02019-12-03 10:25:17 +000087optimize_size_args = [
88 '-Os', # General "Optimize for Size" Option
89 '-fvisibility=hidden', # Hide symbols by default
90]
91
Sam Elliottf928f2d2020-10-06 17:02:57 +010092native_c_compiler = meson.get_compiler('c', native: true)
93cross_c_compiler = meson.get_compiler('c', native: false)
94
95if cross_c_compiler.has_argument('-Wa,--no-pad-sections')
96 # Don't pad assembly sections. This was originally added to avoid sections
97 # being padded to the alignment size. Specifically, .vectors was being
98 # padded to 256 bytes when aligning to that value, when it only needed to be
99 # 128 bytes long. Clang doesn't do this padding, so restricting this option
100 # to GCC doesn't waste space when compiling with Clang.
101 optimize_size_args += '-Wa,--no-pad-sections'
Luís Marques1db34ad2020-05-27 13:41:57 +0100102endif
103
Sam Elliott371c79f2020-06-23 20:06:19 +0100104# The following flags are applied to *all* builds, both cross builds and native
105# builds.
106c_cpp_args = [
107 # We use absolute include paths as much as possible.
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -0500108 '-I' + meson.source_root(),
109 '-I' + meson.build_root(),
Sam Elliott371c79f2020-06-23 20:06:19 +0100110]
111add_project_arguments(c_cpp_args, language: ['c', 'cpp'], native: false)
112add_project_arguments(c_cpp_args, language: ['c', 'cpp'], native: true)
Miguel Osorio03f2e232019-09-17 19:44:37 -0700113
Sam Elliott58d285f2019-11-29 11:58:29 +0000114# The following flags are applied only to cross builds
Sam Elliott371c79f2020-06-23 20:06:19 +0100115c_cpp_cross_args = [
116 # Do not use standard system headers
117 '-nostdinc',
118 # Use OpenTitan's freestanding headers instead
119 '-isystem' + meson.source_root() / 'sw/device/lib/base/freestanding',
Sam Elliott120c7372020-11-03 15:14:46 +0000120 # Don't emit unwinding information
121 '-fno-asynchronous-unwind-tables',
Sam Elliott1d1ac6c2020-11-04 19:03:20 +0000122 # Don't use COMMON sections for uninitialized globals
123 '-fno-common',
Sam Elliott371c79f2020-06-23 20:06:19 +0100124]
Sam Elliottf928f2d2020-10-06 17:02:57 +0100125
126# Add extra warning flags for cross builds, if they are supported.
127foreach warning_arg : extra_warning_args
128 if cross_c_compiler.has_argument(warning_arg)
129 c_cpp_cross_args += [warning_arg]
130 endif
131endforeach
132
133# Add the flags for cross builds.
Sam Elliott58d285f2019-11-29 11:58:29 +0000134add_project_arguments(
Sam Elliott371c79f2020-06-23 20:06:19 +0100135 c_cpp_cross_args,
Sam Elliottca894e02019-12-03 10:25:17 +0000136 optimize_size_args,
Sam Elliott371c79f2020-06-23 20:06:19 +0100137 language: ['c', 'cpp'], native: false)
Sam Elliott5e5a9dd2020-11-10 10:45:48 +0000138# Add a C-only flag for cross builds.
139add_project_arguments(
140 '-Wstrict-prototypes', # Ban K&R Declarations/Definitions.
141 language: ['c'], native: false)
Sam Elliott371c79f2020-06-23 20:06:19 +0100142
143# The following flags are applied only to cross builds
144c_cpp_cross_link_args = [
145 # Do not use standard system startup files or libraries
146 '-nostartfiles',
147 '-nostdlib',
148 # Only link static files
149 '-static',
Sam Elliott120c7372020-11-03 15:14:46 +0000150 # Warn if we use COMMON
151 '-Wl,--warn-common',
152 # Warn if we include orphan sections
153 '-Wl,--orphan-handling=warn',
154 # Turn Linker Warnings into Errors
155 '-Wl,--fatal-warnings',
Sam Elliott371c79f2020-06-23 20:06:19 +0100156]
Sam Elliott58d285f2019-11-29 11:58:29 +0000157add_project_link_arguments(
Sam Elliott371c79f2020-06-23 20:06:19 +0100158 c_cpp_cross_link_args,
159 language: ['c', 'cpp'], native: false)
160
Sam Elliott0ad19da2020-08-24 17:29:42 +0100161# The following flags are applied only to native builds
162c_cpp_native_args = [
163 # Use a define to exclude libc redefinitions.
164 '-DHOST_BUILD',
165]
Sam Elliottf928f2d2020-10-06 17:02:57 +0100166
167# Add Extra warning flags for native builds, if they are supported.
168foreach warning_arg : extra_warning_args
169 if native_c_compiler.has_argument(warning_arg)
170 c_cpp_native_args += [warning_arg]
171 endif
172endforeach
173
174# Add the flags for native builds.
Sam Elliott0ad19da2020-08-24 17:29:42 +0100175add_project_arguments(
176 c_cpp_native_args,
177 language: ['c', 'cpp'], native: true)
Sam Elliott5e5a9dd2020-11-10 10:45:48 +0000178# Add a C-only flag for native builds.
179add_project_arguments(
180 '-Wstrict-prototypes', # Ban K&R Declarations/Definitions.
181 language: ['c'], native: true)
Sam Elliott58d285f2019-11-29 11:58:29 +0000182
Miguel Osorio03f2e232019-09-17 19:44:37 -0700183# Common program references.
184prog_python = import('python').find_installation('python3')
Philipp Wagner2d149842020-11-19 22:56:18 +0000185prog_env = find_program('env')
186prog_srec_cat = find_program('srec_cat')
187
Philipp Wagner80cc8232020-10-29 16:26:09 +0000188prog_ld = find_program('ld')
189prog_as = find_program('as')
Timothy Chen5f7b9c42019-10-30 22:27:16 -0700190prog_objdump = find_program('objdump')
Miguel Osorio03f2e232019-09-17 19:44:37 -0700191prog_objcopy = find_program('objcopy')
Philipp Wagner2d149842020-11-19 22:56:18 +0000192
193prog_otbn_as = meson.source_root() / 'hw/ip/otbn/util/otbn-as'
194prog_otbn_ld = meson.source_root() / 'hw/ip/otbn/util/otbn-ld'
Miguel Osorio03f2e232019-09-17 19:44:37 -0700195
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -0500196# Hardware register headers. These are generated from HJSON files, and accesible
197# in C via |#include "{IP_NAME}_regs.h"|.
Miguel Osorio03f2e232019-09-17 19:44:37 -0700198gen_hw_hdr = generator(
199 prog_python,
200 output: '@BASENAME@_regs.h',
201 arguments: [
202 '@SOURCE_DIR@/util/regtool.py', '-D', '-o', '@BUILD_DIR@/@BASENAME@_regs.h',
203 '@INPUT@',
204 ],
205)
206
207# TODO: Considering moving these into hw/ip directories.
Pirmin Vogel84884772020-11-27 18:03:19 +0100208TOPNAME='top_earlgrey'
Pirmin Vogeldef712f2019-10-24 18:57:36 +0100209hw_ip_aes_reg_h = gen_hw_hdr.process('hw/ip/aes/data/aes.hjson')
Philipp Wagnerc658d4e2021-03-24 14:43:11 +0000210hw_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 +0000211hw_ip_aon_timer_reg_h = gen_hw_hdr.process('hw/ip/aon_timer/data/aon_timer.hjson')
Michael Munday5acd3e12021-01-22 13:36:25 +0000212hw_ip_clkmgr_reg_h = gen_hw_hdr.process('hw/' + TOPNAME + '/ip/clkmgr/data/autogen/clkmgr.hjson')
Miguel Young de la Sota48771f32021-02-10 13:30:47 -0500213hw_ip_entropy_reg_h = gen_hw_hdr.process('hw/ip/entropy_src/data/entropy_src.hjson')
Pirmin Vogel84884772020-11-27 18:03:19 +0100214hw_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 +0200215hw_ip_gpio_reg_h = gen_hw_hdr.process('hw/ip/gpio/data/gpio.hjson')
216hw_ip_hmac_reg_h = gen_hw_hdr.process('hw/ip/hmac/data/hmac.hjson')
Michael Mundaya753d342021-03-22 14:08:15 +0000217hw_ip_kmac_reg_h = gen_hw_hdr.process('hw/ip/kmac/data/kmac.hjson')
Miguel Young de la Sota92bc4cc2020-04-09 12:32:43 -0400218hw_ip_i2c_reg_h = gen_hw_hdr.process('hw/ip/i2c/data/i2c.hjson')
Alphan Ulusoy910cd222020-11-16 15:04:02 -0500219hw_ip_keymgr_reg_h = gen_hw_hdr.process('hw/ip/keymgr/data/keymgr.hjson')
Miguel Young de la Sota29a1e352020-11-18 10:49:59 -0500220hw_ip_lc_ctrl_reg_h = gen_hw_hdr.process('hw/ip/lc_ctrl/data/lc_ctrl.hjson')
Philipp Wagner5c2d2942020-06-22 11:34:00 +0100221hw_ip_otbn_reg_h = gen_hw_hdr.process('hw/ip/otbn/data/otbn.hjson')
Miguel Young de la Sota1fac7822020-09-18 13:47:28 -0400222hw_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 +0200223hw_ip_spi_device_reg_h = gen_hw_hdr.process('hw/ip/spi_device/data/spi_device.hjson')
Timothy Chen4ca4a2f2021-03-19 13:51:39 -0700224hw_ip_sram_ctrl_reg_h = gen_hw_hdr.process('hw/ip/sram_ctrl/data/sram_ctrl.hjson')
225hw_ip_entropy_src_reg_h = gen_hw_hdr.process('hw/ip/entropy_src/data/entropy_src.hjson')
226hw_ip_csrng_reg_h = gen_hw_hdr.process('hw/ip/csrng/data/csrng.hjson')
227hw_ip_edn_reg_h = gen_hw_hdr.process('hw/ip/edn/data/edn.hjson')
Tobias Wölfel4c5fbec2019-10-23 12:43:17 +0200228hw_ip_rv_timer_reg_h = gen_hw_hdr.process('hw/ip/rv_timer/data/rv_timer.hjson')
229hw_ip_uart_reg_h = gen_hw_hdr.process('hw/ip/uart/data/uart.hjson')
230hw_ip_usbdev_reg_h = gen_hw_hdr.process('hw/ip/usbdev/data/usbdev.hjson')
Pirmin Vogel84884772020-11-27 18:03:19 +0100231hw_ip_pwrmgr_reg_h = gen_hw_hdr.process('hw/' + TOPNAME + '/ip/pwrmgr/data/autogen/pwrmgr.hjson')
232hw_ip_rstmgr_reg_h = gen_hw_hdr.process('hw/' + TOPNAME + '/ip/rstmgr/data/autogen/rstmgr.hjson')
233hw_top_earlgrey_pinmux_reg_h = gen_hw_hdr.process('hw/' + TOPNAME + '/ip/pinmux/data/autogen/pinmux.hjson')
234hw_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 -0700235
Sam Elliott7e36bd72020-04-22 14:05:49 +0100236# Top Earlgrey library (top_earlgrey)
237# The sources for this are generated into the hw hierarchy.
238top_earlgrey = declare_dependency(
239 link_with: static_library(
240 'top_earlgrey_ot',
241 sources: [
Pirmin Vogel84884772020-11-27 18:03:19 +0100242 'hw/' + TOPNAME + '/sw/autogen/top_earlgrey.c',
Sam Elliott7e36bd72020-04-22 14:05:49 +0100243 ],
244 )
245)
246
Miguel Osorio03f2e232019-09-17 19:44:37 -0700247subdir('sw')
Philipp Wagner7f9a5bb2020-11-19 22:59:59 +0000248
249# Write environment file
250prog_meson_write_env = meson.source_root() / 'util/meson_write_env.py'
251r = run_command(
252 prog_python,
253 prog_meson_write_env,
254
255 '--out-file',
256 meson.current_build_dir() / '.env',
257
258 'PYTHON=@0@'.format(prog_python.path()),
259 'OTBN_AS=@0@'.format(prog_otbn_as),
260 'OTBN_LD=@0@'.format(prog_otbn_ld),
261 'RV32_TOOL_OBJCOPY=@0@'.format(prog_objcopy.path()),
262 'RV32_TOOL_AS=@0@'.format(prog_as.path()),
263 'RV32_TOOL_LD=@0@'.format(prog_ld.path()),
264 'RV32_TOOL_OBJDUMP=@0@'.format(prog_objdump.path()),
265 'RV32_TOOL_OBJCOPY=@0@'.format(prog_objcopy.path()),
266)
267assert(r.returncode() == 0, 'Error while calling meson_write_env.py.\n' +
268 'STDOUT:\n' + r.stdout().strip() + '\n' +
269 'STDERR:\n ' + r.stderr().strip())
270message(r.stdout().strip())