-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeson.build
312 lines (267 loc) · 7.92 KB
/
meson.build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
project('demo', 'c',
version: '0.1',
license: 'GPL-3.0',
license_files: 'LICENSE',
default_options: {
'warning_level': '3',
'c_std': 'gnu17',
}
)
# Check cross compile
assert(meson.is_cross_build(),
'This project must be cross compile!\n' +
'add --cross-file= to switch to corss compile'
)
# Check compile for STM32
target_mcu = meson.get_external_property('target_mcu', 'null')
assert(target_mcu.contains('stm32f10'),
'This project is only for STM32 F10x series(your selection: @0@)\n'.format(target_mcu) +
'Please select your MCU cross file in cross_files subdir'
)
# Check specific device info
capacity = meson.get_external_property('capacity', 'null')
allowed_capacity_list = ['LD', 'LD_VL', 'MD', 'MD_VL', 'HD', 'HD_VL', 'XL', 'CL']
if false == allowed_capacity_list.contains(capacity)
error('Unsupport capacity (your selection: @0@)\r\n'.format(capacity),
'supported capacities are: ', allowed_capacity_list)
endif
# Project informations
proj_name = meson.project_name()
src_dir = meson.current_source_dir()
build_dir = meson.current_build_dir()
exe_sufix = meson.get_external_property('exe_sufix', 'elf')
# Get cross tools from cross file
objcopy_tool = find_program('objcopy')
objdump_tool = find_program('objdump')
size_tool = find_program('size')
gdb_tool = find_program('gdb')
ls_tool = find_program(
'list_files.py',
dirs: [src_dir / 'tools'],
required: true
)
# Import modules
fs = import('fs')
ssmod = import('sourceset')
# Global informations
project_ss = ssmod.source_set()
global_config = {}
global_c_flags = [
'-mcpu=' + host_machine.cpu(),
'-DSTM32F10X_' + capacity,
'-DUSE_STDPERIPH_DRIVER',
'-DPROJECT_VERSION=' + '"' + meson.project_version() + '"',
'-fdata-sections',
'-ffunction-sections',
]
global_link_flags = [
'-mcpu=' + host_machine.cpu(),
'-nostartfiles',
'-Wl,--gc-sections',
'-lm',
]
# Read .config and generate marcos
if fs.exists('.config')
config_lines = fs.read('.config')
foreach line: config_lines.splitlines()
if line.contains('=') and not line.startswith('#')
line = line.strip()
part = line.split('=')
global_config += {part[0]: part[1]}
global_c_flags += '-DCONFIG_' + line
endif
endforeach
endif
# Add all flags, srcs, incs to meson
add_global_arguments(global_c_flags, language: 'c')
add_global_link_arguments(global_link_flags, language: 'c')
# toolchain_path = fs.parent(fs.parent(find_program('c').full_path()))
# toolchain_inc = toolchain_path / 'arm-none-eabi/include'
# message(toolchain_inc)
# project_ss.add(declare_dependency(include_directories: toolchain_inc))
# Process subdirs and subprojects
subdir('src')
util_dep = dependency('embed-utils-full',
default_options: {
'buildtype': 'release',
'default_library': 'static',
'warning_level': '0',
'embed-utils-tests': true
},
method: 'builtin',
required: true
)
project_ss.add(util_dep)
stm32_dep = dependency('STM32_StdLib',
version: '3.5.0',
default_options: {
'buildtype': 'release',
'default_library': 'static',
'warning_level': '0',
},
method: 'builtin',
required: true
)
project_ss.add(stm32_dep)
# Get memory configuration from cross file
mem_cfg = {
'flash_base': meson.get_external_property('flash_base'),
'flash_size': meson.get_external_property('flash_size'),
'ram_base': meson.get_external_property('ram_base'),
'ram_size': meson.get_external_property('ram_size'),
'stack_size': meson.get_external_property('stack_size', 0x400),
}
# all target description
target_dict = {
'dbg': {
'c_args': [
'-Og',
'-DVECT_TAB_SRAM',
'-g3',
],
'link_args': [
'--specs=nano.specs'
],
'linker_script': 'linker_ram_new.ld',
'debug': true,
},
'rel': {
'c_args': ['-O2'],
'link_args': [
'--specs=nano.specs'
],
'linker_script': 'linker_new.ld',
'download': true,
}
}
top_objs = []
dbg_target = ''
dl_target = ''
# generate all target build setup
foreach target_name, options : target_dict
# Specify target informations
exe_name = proj_name + '_@0@'.format(target_name)
lds_name = target_name + '_' + options['linker_script']
target_c_args = options.get('c_args', [])
target_link_args = options.get('link_args', [])
# Handle configs
target_configs = global_config + options.get('configs', {})
# Handle source set
target_src_set = ssmod.source_set()
target_src_set.add_all(project_ss)
target_src_set.add(files(options.get('srcs', [])))
target_srcs = target_src_set.apply(target_configs)
# Handle debug flags
if options.get('debug', false)
target_c_args += '-D__DEBUG'
endif
# Handle map file and linker script
target_link_args += '-Wl,-Map,memory_@[email protected]'.format(target_name)
target_link_args += '-T' + (build_dir / lds_name)
lds = configure_file(
input: 'linker_sct' / options['linker_script'],
output: lds_name,
configuration: mem_cfg
)
exe = executable(
exe_name,
name_suffix: exe_sufix,
link_depends: lds,
sources: target_srcs.sources(),
dependencies: target_srcs.dependencies(),
c_args: target_c_args,
link_args: target_link_args
)
dis = custom_target(
target_name + '_dis',
depends: exe,
capture: true,
command: [
objdump_tool.full_path(),
'-SD',
exe.full_path()
],
output: fs.replace_suffix(exe.name(), '.s')
)
run_target(
target_name + '_size',
depends: dis,
command: [
size_tool.full_path(),
exe.full_path(),
]
)
top_objs += dis
bin = custom_target(
target_name + '_bin',
depends: dis,
command: [
objcopy_tool.full_path(),
'-O', 'binary',
exe.full_path(),
build_dir / '@OUTPUT@'
],
output: fs.replace_suffix(exe.name(), '.bin')
)
if options.get('debug', false)
dbg_target = exe
endif
if options.get('download', false)
dl_target = bin
endif
endforeach
# custom target to build all target in the 'target_dict'
run_target(
'top_all',
depends: top_objs,
command: ['echo', 'all target built done']
)
# generate vscode workspace file
cross_file = 'cross_files' / target_mcu + '.ini'
dfp_file = meson.get_external_property('dfp_file')
device_name = meson.get_external_property('device_name')
debugger_file = meson.get_external_property('debugger_file')
configure_file(
input: 'proj_files' / 'workspace_demo.json',
output: proj_name + '.code-workspace',
configuration: {
'debug_file': dbg_target.name(),
'gdb_path': gdb_tool.full_path(),
'cross_file': cross_file,
'dfp_file': dfp_file,
'device_name': device_name,
'debugger_file': debugger_file,
'debugger_file_ini': fs.replace_suffix(debugger_file, '.ini'),
'toolchain_path': find_program('c').full_path(),
},
)
# generate download script
configure_file(
input: 'proj_files' / 'download_demo.sh',
output: 'download.sh',
configuration: {
'flash_base': mem_cfg['flash_base']
}
)
# download target
dl_cmd = 'flash write_image erase '
dl_cmd += dl_target.full_path() + ' '
dl_cmd += mem_cfg['flash_base'] + ' '
supported_tools = {
'stlink': 'stlink.cfg',
'dap': 'cmsis-dap.cfg'
}
foreach tool, cfg : supported_tools
run_target(
tool + ' - download',
depends: dl_target,
command: [
'openocd',
'-f', '..' / 'openocd_cfg' / cfg,
'-f', '..' / 'openocd_cfg' / 'stm32f1x.cfg',
'-c init;reset halt;wait_halt;',
'-c', dl_cmd,
'-c reset; shutdown;'
]
)
endforeach