Skip to content

Commit 6d64c85

Browse files
author
Oren Cohen
committed
Specify extra args with -x and verbose_check_call
1 parent ef19ade commit 6d64c85

File tree

2 files changed

+65
-44
lines changed

2 files changed

+65
-44
lines changed

tools/psa/README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ This document describes the following scripts:
1111

1212
## \_\_init\_\_.py
1313

14-
This file holds common functions dedicated to help SiP with their postbuild logic.
14+
This file holds common functions dedicated to help SiP with their post-build logic.
1515

16-
* find_secure_image - Scans a Resource object to find the correct binary of the secure image to merge with the nonsecure build.
16+
* find_secure_image - Scans a Resource object to find the correct binary of the secure image to merge with the non-secure build.
1717

1818
## Code generation scripts
1919

@@ -22,33 +22,36 @@ Mbed OS holds two implementations of PSA:
2222
* MBED_SPM - Implementation for dual-core v7 targets.
2323
* TF-M - Implementation for v8 targets.
2424

25-
Each implementation requires a set of autogenerated files describing the secure partitions:
25+
Each implementation requires a set of auto-generated files describing the secure partitions:
2626

2727
* `generate_partition_code.py` - Generate files for both implementations.
2828
* `generate_mbed_spm_partition_code.py` - Generate files for MBED_SPM.
2929
* `generate_tfm_partition_code.py` - Generate files for TF-M.
30-
* `mbed_spm_tfm_common.py` - Holds common functions for both.
30+
* `mbed_spm_tfm_common.py` - Holds common functions for both.
3131

3232
## Secure image generation
3333

3434
`release.py` is the script assigned with compiling the secure images:
3535

3636
```
3737
usage: release.py [-h] [-m MCU] [-d] [-q] [-l] [--commit] [--skip-tests]
38+
[-x ...]
3839
3940
optional arguments:
40-
-h, --help show this help message and exit
41-
-m MCU, --mcu MCU build for the given MCU
42-
-d, --debug set build profile to debug
43-
-q, --quiet No Build log will be printed
44-
-l, --list Print supported PSA secure targets
45-
--commit create a git commit for each platform
46-
--skip-tests skip the test build phase
41+
-h, --help show this help message and exit
42+
-m MCU, --mcu MCU build for the given MCU
43+
-d, --debug set build profile to debug
44+
-q, --quiet No Build log will be printed
45+
-l, --list Print supported PSA secure targets
46+
--commit create a git commit for each platform
47+
--skip-tests skip the test build phase
48+
-x ..., --extra ... additional build parameters
4749
```
4850

4951
* When `MCU ` is not specified, the script compiles all the images for all the targets.
5052
* When `-d/--debug` is not specified, the script compiles the images using the release profile.
5153
* When `--commit` is not specified, the script will not commit the images to git.
54+
* A user can specify additional commands that will be passed on to the build commands (Ex. -D for compilation defines).
5255

5356
This script should be run in following scenarios:
5457

tools/psa/release.py

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import sys
2121
import shutil
2222
import logging
23-
from argparse import ArgumentParser
23+
import argparse
2424

2525
FNULL = open(os.devnull, 'w')
2626
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__),
@@ -89,13 +89,14 @@ def _get_psa_secure_targets_list():
8989
Target.get_target(t).is_PSA_secure_target]
9090

9191

92-
def _get_default_image_build_command(target, toolchain, profile):
92+
def _get_default_image_build_command(target, toolchain, profile, args):
9393
"""
9494
Creates a build command for a default image.
9595
9696
:param target: target to be built.
9797
:param toolchain: toolchain to be used.
9898
:param profile: build profile.
99+
:param args: list of extra arguments.
99100
:return: Build command in a list form.
100101
"""
101102
cmd = [
@@ -112,7 +113,22 @@ def _get_default_image_build_command(target, toolchain, profile):
112113
else:
113114
cmd += ['--artifact-name', 'psa_release_1.0']
114115

115-
return cmd
116+
return cmd + args
117+
118+
119+
def verbose_check_call(cmd, check_call=True):
120+
"""
121+
122+
:param cmd: command to run as a list
123+
:param check_call: choose subprocess method (call/check_call)
124+
:return: return code of the executed command
125+
"""
126+
logger.info('Running: {}'.format(' '.join(cmd)))
127+
if check_call:
128+
return subprocess.check_call(cmd, stdout=subprocess_output,
129+
stderr=subprocess_err)
130+
131+
return subprocess.call(cmd, stdout=subprocess_output, stderr=subprocess_err)
116132

117133

118134
def get_mbed_official_psa_release(target=None):
@@ -143,19 +159,19 @@ def create_mbed_ignore(build_dir):
143159
f.write('*\n')
144160

145161

146-
def build_tests_mbed_spm_platform(target, toolchain, profile):
162+
def build_tests_mbed_spm_platform(target, toolchain, profile, args):
147163
"""
148164
Builds Secure images for MBED-SPM target.
149165
150166
:param target: target to be built.
151167
:param toolchain: toolchain to be used.
152168
:param profile: build profile.
169+
:param args: list of extra arguments.
153170
"""
154171
logger.info(
155172
"Building tests images({}) for {} using {} with {} profile".format(
156173
MBED_PSA_TESTS, target, toolchain, profile))
157-
158-
subprocess.check_call([
174+
cmd = [
159175
sys.executable, TEST_PY_LOCATTION,
160176
'--greentea',
161177
'--profile', profile,
@@ -167,30 +183,30 @@ def build_tests_mbed_spm_platform(target, toolchain, profile):
167183
target, 'test_spec.json'),
168184
'--build-data', os.path.join(ROOT, 'BUILD', 'tests',
169185
target, 'build_data.json'),
170-
'-n', MBED_PSA_TESTS
171-
], stdout=subprocess_output, stderr=subprocess_err)
172-
186+
'-n', MBED_PSA_TESTS] + args
173187

188+
verbose_check_call(cmd)
174189
logger.info(
175190
"Finished building tests images({}) for {} successfully".format(
176191
MBED_PSA_TESTS, target))
177192

178193

179-
def build_tests_tfm_platform(target, toolchain, profile):
194+
def build_tests_tfm_platform(target, toolchain, profile, args):
180195
"""
181196
Builds Secure images for TF-M target.
182197
183198
:param target: target to be built.
184199
:param toolchain: toolchain to be used.
185200
:param profile: build profile.
201+
:param args: list of extra arguments.
186202
"""
187203
for test in TFM_TESTS.keys():
188204
logger.info(
189205
"Building tests image({}) for {} using {} with {} profile".format(
190206
test, target, toolchain, profile))
191207

192208
test_defines = ['-D{}'.format(define) for define in TFM_TESTS[test]]
193-
subprocess.check_call([
209+
cmd = [
194210
sys.executable, TEST_PY_LOCATTION,
195211
'--greentea',
196212
'--profile', profile,
@@ -203,9 +219,9 @@ def build_tests_tfm_platform(target, toolchain, profile):
203219
'--build-data', os.path.join(ROOT, 'BUILD', 'tests',
204220
target, 'build_data.json'),
205221
'-n', test,
206-
'--app-config', TFM_MBED_APP] + test_defines,
207-
stdout=subprocess_output, stderr=subprocess_err)
222+
'--app-config', TFM_MBED_APP] + test_defines + args
208223

224+
verbose_check_call(cmd)
209225
logger.info(
210226
"Finished Building tests image({}) for {}".format(test, target))
211227

@@ -218,36 +234,33 @@ def commit_binaries(target, delivery_dir):
218234
:param delivery_dir: Secure images should be moved to this folder
219235
by the build system.
220236
"""
221-
changes_made = subprocess.call([
237+
changes_made = verbose_check_call([
222238
'git',
223239
'-C', ROOT,
224240
'diff', '--exit-code', '--quiet',
225-
delivery_dir
226-
], stdout=subprocess_output, stderr=subprocess_err)
241+
delivery_dir], check_call=False)
227242

228243
if changes_made:
229244
logger.info("Change in images for {} has been detected".format(target))
230-
subprocess.check_call([
245+
verbose_check_call([
231246
'git',
232247
'-C', ROOT,
233-
'add', os.path.relpath(delivery_dir, ROOT)
234-
], stdout=subprocess_output, stderr=subprocess_err)
248+
'add', os.path.relpath(delivery_dir, ROOT)])
235249

236250
logger.info("Committing images for {}".format(target))
237251
commit_message = '--message="Update secure binaries for {}"'.format(
238252
target)
239-
subprocess.check_call([
253+
verbose_check_call([
240254
'git',
241255
'-C', ROOT,
242256
'commit',
243-
commit_message
244-
], stdout=subprocess_output, stderr=subprocess_err)
257+
commit_message])
245258
else:
246259
logger.info("No changes detected for {}, Skipping commit".format(target))
247260

248261

249-
def build_psa_platform(target, toolchain, delivery_dir, debug=False,
250-
git_commit=False, skip_tests=False):
262+
def build_psa_platform(target, toolchain, delivery_dir, debug, git_commit,
263+
skip_tests, args):
251264
"""
252265
Calls the correct build function and commits if requested.
253266
@@ -257,21 +270,20 @@ def build_psa_platform(target, toolchain, delivery_dir, debug=False,
257270
:param debug: Build with debug profile.
258271
:param git_commit: Commit the changes.
259272
:param skip_tests: skip the test images build phase.
273+
:param args: list of extra arguments.
260274
"""
261275
profile = 'debug' if debug else 'release'
262276
if not skip_tests:
263277
if _psa_backend(target) is 'TFM':
264-
build_tests_tfm_platform(target, toolchain, profile)
278+
build_tests_tfm_platform(target, toolchain, profile, args)
265279
else:
266-
build_tests_mbed_spm_platform(target, toolchain, profile)
280+
build_tests_mbed_spm_platform(target, toolchain, profile, args)
267281

268282
logger.info("Building default image for {} using {} with {} profile".format(
269283
target, toolchain, profile))
270284

271-
subprocess.check_call(
272-
_get_default_image_build_command(target, toolchain, profile),
273-
stdout=subprocess_output, stderr=subprocess_err)
274-
285+
cmd = _get_default_image_build_command(target, toolchain, profile, args)
286+
verbose_check_call(cmd)
275287
logger.info(
276288
"Finished building default image for {} successfully".format(target))
277289

@@ -280,7 +292,7 @@ def build_psa_platform(target, toolchain, delivery_dir, debug=False,
280292

281293

282294
def get_parser():
283-
parser = ArgumentParser()
295+
parser = argparse.ArgumentParser()
284296
parser.add_argument("-m", "--mcu",
285297
help="build for the given MCU",
286298
default=None,
@@ -312,6 +324,11 @@ def get_parser():
312324
default=False,
313325
help="skip the test build phase")
314326

327+
parser.add_argument('-x', '--extra',
328+
dest='extra_args',
329+
nargs=argparse.REMAINDER,
330+
help="additional build parameters")
331+
315332
return parser
316333

317334

@@ -339,7 +356,7 @@ def main():
339356
subprocess_err = subprocess.STDOUT
340357

341358
if options.list:
342-
logger.info("Avialable platforms are: {}".format(
359+
logger.info("Available platforms are: {}".format(
343360
', '.join([t for t in _get_psa_secure_targets_list()])))
344361
return
345362

@@ -350,7 +367,8 @@ def main():
350367

351368
for target, tc, directory in psa_platforms_list:
352369
build_psa_platform(target, tc, directory, options.debug,
353-
options.commit, options.skip_tests)
370+
options.commit, options.skip_tests,
371+
options.extra_args)
354372

355373
logger.info("Finished Updating PSA images")
356374

0 commit comments

Comments
 (0)