blob: 53d70f5ecacb505f458838275f31f50cd01af008 [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
Miguel Young de la Sotab2ef4832019-11-22 12:55:46 -060022ot_version = get_option('ot_version')
23if ot_version == 'undef'
24 error('ot_version option not set. Please run meson with a valid OpenTitan version option.')
25endif
26
Philipp Wagner525891d2020-10-30 19:01:04 +000027bin_dir = get_option('bin_dir')
28if bin_dir == 'undef'
29 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 -060030endif
Jon Flatleya863a282020-03-03 10:49:39 -050031tock_local = get_option('tock_local')
Miguel Young de la Sota3c8ab3b2019-11-21 13:59:09 -060032
Sam Elliott371c79f2020-06-23 20:06:19 +010033# See the comment in `./util/meson-purge-includes.sh` for why this is necessary.
34if not get_option('keep_includes')
35 meson.add_postconf_script(meson.source_root() + '/util/meson-purge-includes.sh')
36endif
37
Alphan Ulusoyd2bbbe12020-08-14 15:39:51 -040038coverage = get_option('coverage')
39# Coverage requires clang.
40if coverage and meson.get_compiler('c').get_id() != 'clang'
41 error('Coverage requires clang.')
42endif
Sam Elliott371c79f2020-06-23 20:06:19 +010043
Sam Elliottf928f2d2020-10-06 17:02:57 +010044# C compiler arguments to to catch common errors, used on all builds.
45extra_warning_args = [
Sam Elliott3eed4a72020-10-05 19:11:18 +010046 '-Wimplicit-fallthrough', # Error on implicit fallthrough
Sam Elliott499ad612020-10-05 19:27:55 +010047 '-Wswitch-default', # Ensure all switches have default statements
48 '-Wno-covered-switch-default', # We require `default:` always.
Sam Elliotta49d7042020-10-07 10:46:41 +010049 '-Wgnu', # We aim to be standards compliant, and avoid gnu extensions.
Philipp Wagner90610762020-11-04 17:34:58 +000050 '-Wno-error=unused-function', # Don't error out on unused functions, only warn.
Sam Elliott6fa87b12020-10-05 19:30:20 +010051 # 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 Elliotta49d7042020-10-07 10:46:41 +010056 '-Wno-gnu-zero-variadic-macro-arguments',
Sam Elliottf928f2d2020-10-06 17:02:57 +010057]
58
59# C compiler arguments to optimize for size, used on cross builds only.
Sam Elliottca894e02019-12-03 10:25:17 +000060optimize_size_args = [
61 '-Os', # General "Optimize for Size" Option
62 '-fvisibility=hidden', # Hide symbols by default
63]
64
Sam Elliottf928f2d2020-10-06 17:02:57 +010065native_c_compiler = meson.get_compiler('c', native: true)
66cross_c_compiler = meson.get_compiler('c', native: false)
67
68if 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 Marques1db34ad2020-05-27 13:41:57 +010075endif
76
Sam Elliott371c79f2020-06-23 20:06:19 +010077# The following flags are applied to *all* builds, both cross builds and native
78# builds.
79c_cpp_args = [
80 # We use absolute include paths as much as possible.
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -050081 '-I' + meson.source_root(),
82 '-I' + meson.build_root(),
Sam Elliott371c79f2020-06-23 20:06:19 +010083]
84add_project_arguments(c_cpp_args, language: ['c', 'cpp'], native: false)
85add_project_arguments(c_cpp_args, language: ['c', 'cpp'], native: true)
Miguel Osorio03f2e232019-09-17 19:44:37 -070086
Sam Elliott58d285f2019-11-29 11:58:29 +000087# The following flags are applied only to cross builds
Sam Elliott371c79f2020-06-23 20:06:19 +010088c_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 Elliott120c7372020-11-03 15:14:46 +000093 # Don't emit unwinding information
94 '-fno-asynchronous-unwind-tables',
Sam Elliott1d1ac6c2020-11-04 19:03:20 +000095 # Don't use COMMON sections for uninitialized globals
96 '-fno-common',
Sam Elliott371c79f2020-06-23 20:06:19 +010097]
Sam Elliottf928f2d2020-10-06 17:02:57 +010098
99# Add extra warning flags for cross builds, if they are supported.
100foreach warning_arg : extra_warning_args
101 if cross_c_compiler.has_argument(warning_arg)
102 c_cpp_cross_args += [warning_arg]
103 endif
104endforeach
105
106# Add the flags for cross builds.
Sam Elliott58d285f2019-11-29 11:58:29 +0000107add_project_arguments(
Sam Elliott371c79f2020-06-23 20:06:19 +0100108 c_cpp_cross_args,
Sam Elliottca894e02019-12-03 10:25:17 +0000109 optimize_size_args,
Sam Elliott371c79f2020-06-23 20:06:19 +0100110 language: ['c', 'cpp'], native: false)
Sam Elliott5e5a9dd2020-11-10 10:45:48 +0000111# Add a C-only flag for cross builds.
112add_project_arguments(
113 '-Wstrict-prototypes', # Ban K&R Declarations/Definitions.
114 language: ['c'], native: false)
Sam Elliott371c79f2020-06-23 20:06:19 +0100115
116# The following flags are applied only to cross builds
117c_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 Elliott120c7372020-11-03 15:14:46 +0000123 # 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 Elliott371c79f2020-06-23 20:06:19 +0100129]
Sam Elliott58d285f2019-11-29 11:58:29 +0000130add_project_link_arguments(
Sam Elliott371c79f2020-06-23 20:06:19 +0100131 c_cpp_cross_link_args,
132 language: ['c', 'cpp'], native: false)
133
Sam Elliott0ad19da2020-08-24 17:29:42 +0100134# The following flags are applied only to native builds
135c_cpp_native_args = [
136 # Use a define to exclude libc redefinitions.
137 '-DHOST_BUILD',
138]
Sam Elliottf928f2d2020-10-06 17:02:57 +0100139
140# Add Extra warning flags for native builds, if they are supported.
141foreach warning_arg : extra_warning_args
142 if native_c_compiler.has_argument(warning_arg)
143 c_cpp_native_args += [warning_arg]
144 endif
145endforeach
146
147# Add the flags for native builds.
Sam Elliott0ad19da2020-08-24 17:29:42 +0100148add_project_arguments(
149 c_cpp_native_args,
150 language: ['c', 'cpp'], native: true)
Sam Elliott5e5a9dd2020-11-10 10:45:48 +0000151# Add a C-only flag for native builds.
152add_project_arguments(
153 '-Wstrict-prototypes', # Ban K&R Declarations/Definitions.
154 language: ['c'], native: true)
Sam Elliott58d285f2019-11-29 11:58:29 +0000155
Miguel Osorio03f2e232019-09-17 19:44:37 -0700156# Common program references.
157prog_python = import('python').find_installation('python3')
Philipp Wagner2d149842020-11-19 22:56:18 +0000158prog_env = find_program('env')
159prog_srec_cat = find_program('srec_cat')
160
Philipp Wagner80cc8232020-10-29 16:26:09 +0000161prog_ld = find_program('ld')
162prog_as = find_program('as')
Timothy Chen5f7b9c42019-10-30 22:27:16 -0700163prog_objdump = find_program('objdump')
Miguel Osorio03f2e232019-09-17 19:44:37 -0700164prog_objcopy = find_program('objcopy')
Philipp Wagner2d149842020-11-19 22:56:18 +0000165
166prog_otbn_as = meson.source_root() / 'hw/ip/otbn/util/otbn-as'
167prog_otbn_ld = meson.source_root() / 'hw/ip/otbn/util/otbn-ld'
Miguel Osorio03f2e232019-09-17 19:44:37 -0700168
Miguel Young de la Sota3fbb28a2019-10-16 15:15:07 -0500169# Hardware register headers. These are generated from HJSON files, and accesible
170# in C via |#include "{IP_NAME}_regs.h"|.
Miguel Osorio03f2e232019-09-17 19:44:37 -0700171gen_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 Vogel84884772020-11-27 18:03:19 +0100181TOPNAME='top_earlgrey'
Pirmin Vogeldef712f2019-10-24 18:57:36 +0100182hw_ip_aes_reg_h = gen_hw_hdr.process('hw/ip/aes/data/aes.hjson')
Miguel Young de la Sota475887f2020-08-10 16:40:09 -0400183hw_ip_alert_handler_reg_h = gen_hw_hdr.process('hw/ip/alert_handler/data/alert_handler.hjson')
Silvestrs Timofejevs947a44a2020-12-04 16:42:29 +0000184hw_ip_aon_timer_reg_h = gen_hw_hdr.process('hw/ip/aon_timer/data/aon_timer.hjson')
Pirmin Vogel84884772020-11-27 18:03:19 +0100185hw_ip_clkgmr_reg_h = gen_hw_hdr.process('hw/' + TOPNAME + '/ip/clkmgr/data/autogen/clkmgr.hjson')
186hw_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 +0200187hw_ip_gpio_reg_h = gen_hw_hdr.process('hw/ip/gpio/data/gpio.hjson')
188hw_ip_hmac_reg_h = gen_hw_hdr.process('hw/ip/hmac/data/hmac.hjson')
Miguel Young de la Sota92bc4cc2020-04-09 12:32:43 -0400189hw_ip_i2c_reg_h = gen_hw_hdr.process('hw/ip/i2c/data/i2c.hjson')
Miguel Young de la Sota29a1e352020-11-18 10:49:59 -0500190hw_ip_lc_ctrl_reg_h = gen_hw_hdr.process('hw/ip/lc_ctrl/data/lc_ctrl.hjson')
Philipp Wagner5c2d2942020-06-22 11:34:00 +0100191hw_ip_otbn_reg_h = gen_hw_hdr.process('hw/ip/otbn/data/otbn.hjson')
Miguel Young de la Sota1fac7822020-09-18 13:47:28 -0400192hw_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 +0200193hw_ip_spi_device_reg_h = gen_hw_hdr.process('hw/ip/spi_device/data/spi_device.hjson')
194hw_ip_rv_timer_reg_h = gen_hw_hdr.process('hw/ip/rv_timer/data/rv_timer.hjson')
195hw_ip_uart_reg_h = gen_hw_hdr.process('hw/ip/uart/data/uart.hjson')
196hw_ip_usbdev_reg_h = gen_hw_hdr.process('hw/ip/usbdev/data/usbdev.hjson')
Pirmin Vogel84884772020-11-27 18:03:19 +0100197hw_ip_pwrmgr_reg_h = gen_hw_hdr.process('hw/' + TOPNAME + '/ip/pwrmgr/data/autogen/pwrmgr.hjson')
198hw_ip_rstmgr_reg_h = gen_hw_hdr.process('hw/' + TOPNAME + '/ip/rstmgr/data/autogen/rstmgr.hjson')
199hw_top_earlgrey_pinmux_reg_h = gen_hw_hdr.process('hw/' + TOPNAME + '/ip/pinmux/data/autogen/pinmux.hjson')
200hw_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 -0700201
Sam Elliott7e36bd72020-04-22 14:05:49 +0100202# Top Earlgrey library (top_earlgrey)
203# The sources for this are generated into the hw hierarchy.
204top_earlgrey = declare_dependency(
205 link_with: static_library(
206 'top_earlgrey_ot',
207 sources: [
Pirmin Vogel84884772020-11-27 18:03:19 +0100208 'hw/' + TOPNAME + '/sw/autogen/top_earlgrey.c',
Sam Elliott7e36bd72020-04-22 14:05:49 +0100209 ],
210 )
211)
212
Miguel Osorio03f2e232019-09-17 19:44:37 -0700213subdir('sw')
Philipp Wagner7f9a5bb2020-11-19 22:59:59 +0000214
215# Write environment file
216prog_meson_write_env = meson.source_root() / 'util/meson_write_env.py'
217r = run_command(
218 prog_python,
219 prog_meson_write_env,
220
221 '--out-file',
222 meson.current_build_dir() / '.env',
223
224 'PYTHON=@0@'.format(prog_python.path()),
225 'OTBN_AS=@0@'.format(prog_otbn_as),
226 'OTBN_LD=@0@'.format(prog_otbn_ld),
227 'RV32_TOOL_OBJCOPY=@0@'.format(prog_objcopy.path()),
228 'RV32_TOOL_AS=@0@'.format(prog_as.path()),
229 'RV32_TOOL_LD=@0@'.format(prog_ld.path()),
230 'RV32_TOOL_OBJDUMP=@0@'.format(prog_objdump.path()),
231 'RV32_TOOL_OBJCOPY=@0@'.format(prog_objcopy.path()),
232)
233assert(r.returncode() == 0, 'Error while calling meson_write_env.py.\n' +
234 'STDOUT:\n' + r.stdout().strip() + '\n' +
235 'STDERR:\n ' + r.stderr().strip())
236message(r.stdout().strip())