-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeson.build
184 lines (157 loc) · 6.16 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
# SPDX-License-Identifier: Apache-2.0
#
# Copyright 2023 - 2024 Ledger SAS
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
project(
'kconfig',
meson_version: '>=1.3.0',
version: run_command('dunamai', 'from', 'git', '--style', 'semver', '--dirty', check: true)
.stdout().strip(),
license: 'Apache-2.0',
license_files: ['LICENSES/Apache-2.0.txt'],
)
pymod = import('python')
keyval = import('keyval')
fs = import('fs')
py3 = pymod.find_installation('python3', modules: ['kconfiglib'])
menuconfig = find_program('menuconfig')
oldconfig = find_program('oldconfig')
alldefconfig = find_program('alldefconfig')
savedefconfig = find_program('savedefconfig')
jinja_cli = find_program('jinja')
stamp_configure = find_program('scripts/stamp_configure.py')
kconfig_py = find_program('scripts/kconfig.py')
kconfig = get_option('kconfig')
kconfig_config_in = get_option('config')
kconfig_olddotconfig = meson.current_build_dir() / '.config.old'
kconfig_dotconfig = meson.current_build_dir() / '.config'
kconfig_savedefconfig = meson.current_build_dir() / 'defconfig'
# define kconfig usefull envvars
#
# $(KCONFIG_CONFIG) is the alternate output filename for .config
# $(srctree) is the kconfig standard variable for top-level Kconfig basename
# $(subprojects) is a variable for top-level subprojects directory
#
# usage:
# - One can use one of those to include sub-level Kconfig definition, e.g. an
# application that want to include a library Kconfig menu(entry).
# > source $(subprojects)/<deps>/Kconfig
kconfig_srctree = meson.global_source_root()
kconfig_subprojects_tree = kconfig_srctree / 'subprojects'
# get back sdk root (excluding PREFIX), based on pkg_config_path
pkgconfig_paths_opt = meson.get_external_property('pkg_config_libdir', '')
if pkgconfig_paths_opt.contains('pkgconfig')
kconfig_sdk_root_tree = fs.parent(fs.parent(pkgconfig_paths_opt))
message('using SDK: ' + kconfig_sdk_root_tree)
else
message('no SDK declared in cross-file through pkg_config_libdir')
kconfig_sdk_root_tree = ''
endif
kconfig_env = environment()
kconfig_env.set('KCONFIG_CONFIG', kconfig_dotconfig)
kconfig_env.set('srctree', kconfig_srctree)
kconfig_env.set('subprojects', kconfig_subprojects_tree)
kconfig_env.set('sdk', kconfig_sdk_root_tree)
# Add envvars to devenv to ease developer to use menuconfig directly w/o dedicated target
meson.add_devenv(kconfig_env)
kconfig_py_wrapper = '''
import os
import sys
import subprocess
os.environ["srctree"] = "@0@"
os.environ["subprojects"] = "@1@"
os.environ["sdk"] = "@2@"
res = subprocess.run(args=sys.argv[1:], check=True, capture_output=True, text=True)
print(res.stdout)
'''.format(kconfig_srctree, kconfig_subprojects_tree, kconfig_sdk_root_tree)
# kconfig options is mandatory
assert(fs.is_file(kconfig), 'Please provide a Kconfig file, see meson.options for more informations')
if not fs.is_file(kconfig_dotconfig)
message('Use @0@ as .config'.format(kconfig_config_in))
if not fs.is_absolute(kconfig_config_in)
kconfig_config_in = meson.global_source_root() / kconfig_config_in
endif
assert(fs.is_file(kconfig_config_in), 'Please provide a config file, one could generates a default one with:\n alldefconfig @0@'.format(kconfig))
kconfig_copy_script = '''
import shutil
import sys
from pathlib import Path
orig = Path(sys.argv[1])
oldconfig = Path(sys.argv[2])
config = Path(sys.argv[3])
shutil.copyfile(orig, oldconfig)
shutil.copyfile(oldconfig, config)
'''
configure_file(
output: '.config.old',
command: [py3, '-c', kconfig_copy_script, kconfig_config_in, '@OUTPUT@', kconfig_dotconfig]
)
endif
# dry run in order to generate dep file only, oldconfig is out of scope of meson configure step
configure_file(
output: '.config',
depfile: 'kconfig.deps',
command: [ py3, '-c', kconfig_py_wrapper, py3, kconfig_py, kconfig, '--dry-run', '@OUTPUT@', '@DEPFILE@', '@OUTPUT@' ],
)
# Create a ksconfig stamp file with current dotconfig as deps.
# Thus, if the dotconfig file changes (manually copy/pasted, after menuconfig, etc.,
# project is reconfigured
kconfig_stamp = configure_file(
output: '.kconfig.configure.stamp',
depfile: '.kconfig.configure.deps',
command: [stamp_configure, '@DEPFILE@', kconfig_dotconfig ],
)
# Convert kconfig entry set to 'y' in 1/0 style boolean
# Thus, boolean kconfig true entry will translate to `#define CONFIG_XXXX 1`
kconfig_data = configuration_data(keyval.load(kconfig_dotconfig))
rust_cfg_cmdline = []
foreach k: kconfig_data.keys()
if kconfig_data.get(k) == 'y'
kconfig_data.set10(k, true)
rust_cfg_cmdline += ['@0@'.format(k)]
else
rust_cfg_cmdline += ['@0@="@1@"'.format(k, kconfig_data.get_unquoted(k))]
endif
endforeach
# Generates C header from config_data
kconfig_h = configure_file(
configuration: kconfig_data,
output: 'generated_kconfig.h'
)
# Generates json file from config_data
kconfig_json = configure_file(
configuration: kconfig_data,
output: 'generated_kconfig.json',
output_format: 'json',
)
# Generates rustarg file from config_data
kconfig_rustargs = configure_file(
input: 'templates/rustargs.in',
command: [jinja_cli, '@INPUT@', '--define', 'flags', ' '.join(rust_cfg_cmdline), '-o', '@OUTPUT@'],
output: 'rustflags.ini',
)
# The followings are helper targets that mimic well known project such as Linux/uboot
# One can edit the current configuration with `ninja menuconfig` within the build dir.
run_target('menuconfig',
command: [menuconfig, kconfig],
env: kconfig_env,
)
run_target('oldconfig',
command: [oldconfig, kconfig],
env: kconfig_env,
)
run_target('savedefconfig',
command: [savedefconfig, '--kconfig', kconfig, '--out', kconfig_savedefconfig],
env: kconfig_env,
)