blob: 92d2431aea7c2a9cfc8cacfdf51ff42befd13475 [file] [log] [blame]
Simon Glass377bca82019-10-31 07:43:05 -06001#!/usr/bin/env python3
Tom Rini10e47792018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glass2574ef62016-11-25 20:15:51 -07003
4# Copyright (c) 2016 Google, Inc
5# Written by Simon Glass <sjg@chromium.org>
6#
Simon Glass2574ef62016-11-25 20:15:51 -07007# Creates binary images from input files controlled by a description
8#
9
10"""See README for more information"""
11
12import os
Simon Glass40778d72019-07-08 13:18:36 -060013import site
Simon Glass2574ef62016-11-25 20:15:51 -070014import sys
15import traceback
Simon Glass2574ef62016-11-25 20:15:51 -070016
Andy Shevchenko12ae3472021-12-06 14:44:12 +030017# Get the absolute path to this file at run-time
18our_path = os.path.dirname(os.path.realpath(__file__))
19our1_path = os.path.dirname(our_path)
20our2_path = os.path.dirname(our1_path)
21
Andy Shevchenko6f64b602021-12-06 14:44:13 +030022# Extract $(srctree) from Kbuild environment, or use relative paths below
23srctree = os.environ.get('srctree', our2_path)
24
Andy Shevchenko12ae3472021-12-06 14:44:12 +030025#
26# Do not pollute source tree with cache files:
27# https://stackoverflow.com/a/60024195/2511795
28# https://bugs.python.org/issue33499
29#
Andy Shevchenko6f64b602021-12-06 14:44:13 +030030sys.pycache_prefix = os.path.relpath(our_path, srctree)
Andy Shevchenko12ae3472021-12-06 14:44:12 +030031
Simon Glassf46732a2019-07-08 14:25:29 -060032# Bring in the patman and dtoc libraries (but don't override the first path
33# in PYTHONPATH)
Andy Shevchenko6f64b602021-12-06 14:44:13 +030034sys.path.insert(2, our1_path)
Simon Glass42143162020-04-17 18:09:05 -060035
Simon Glass620c4462022-01-09 20:14:11 -070036from binman import bintool
Simon Glass131444f2023-02-23 18:18:04 -070037from u_boot_pylib import test_util
Simon Glass2574ef62016-11-25 20:15:51 -070038
Simon Glass55901ff2017-05-27 07:38:22 -060039# Bring in the libfdt module
Simon Glassf46732a2019-07-08 14:25:29 -060040sys.path.insert(2, 'scripts/dtc/pylibfdt')
Andy Shevchenko6f64b602021-12-06 14:44:13 +030041sys.path.insert(2, os.path.join(srctree, 'scripts/dtc/pylibfdt'))
Philippe Reynesbbdeb212022-01-27 15:03:13 +010042sys.path.insert(2, os.path.join(srctree, 'build-sandbox/scripts/dtc/pylibfdt'))
Andy Shevchenko6f64b602021-12-06 14:44:13 +030043sys.path.insert(2, os.path.join(srctree, 'build-sandbox_spl/scripts/dtc/pylibfdt'))
Simon Glass55901ff2017-05-27 07:38:22 -060044
Simon Glassc585dd42020-04-17 18:09:03 -060045from binman import cmdline
46from binman import control
Simon Glass131444f2023-02-23 18:18:04 -070047from u_boot_pylib import test_util
Simon Glass2574ef62016-11-25 20:15:51 -070048
Simon Glasscebfab22019-07-08 13:18:50 -060049def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
Simon Glass5666f9a2018-06-01 09:38:18 -060050 """Run the functional tests and any embedded doctests
51
52 Args:
53 debug: True to enable debugging, which shows a full stack trace on error
Simon Glass8a50b4a2019-07-08 13:18:48 -060054 verbosity: Verbosity level to use
Simon Glass1c420c92019-07-08 13:18:49 -060055 test_preserve_dirs: True to preserve the input directory used by tests
56 so that it can be examined afterwards (only useful for debugging
57 tests). If a single test is selected (in args[0]) it also preserves
58 the output directory for this test. Both directories are displayed
59 on the command line.
60 processes: Number of processes to use to run tests (None=same as #CPUs)
Simon Glass5666f9a2018-06-01 09:38:18 -060061 args: List of positional args provided to binman. This can hold a test
Simon Glassf46732a2019-07-08 14:25:29 -060062 name to execute (as in 'binman test testSections', for example)
Simon Glasscebfab22019-07-08 13:18:50 -060063 toolpath: List of paths to use for tools
Simon Glass5666f9a2018-06-01 09:38:18 -060064 """
Simon Glass162017b2022-01-09 20:13:57 -070065 from binman import bintool_test
Simon Glassc585dd42020-04-17 18:09:03 -060066 from binman import cbfs_util_test
67 from binman import elf_test
68 from binman import entry_test
69 from binman import fdt_test
Simon Glass2697b7c2021-11-23 21:08:58 -070070 from binman import fip_util_test
Simon Glassc585dd42020-04-17 18:09:03 -060071 from binman import ftest
72 from binman import image_test
Simon Glass2574ef62016-11-25 20:15:51 -070073 import doctest
74
Simon Glass73306922020-04-17 18:09:01 -060075 test_name = args and args[0] or None
Simon Glass8f521362017-11-12 21:52:21 -070076
77 # Run the entry tests first ,since these need to be the first to import the
78 # 'entry' module.
Alper Nebi Yasakca1c5882022-04-02 20:06:06 +030079 result = test_util.run_test_suites(
80 'binman', debug, verbosity, test_preserve_dirs, processes, test_name,
Simon Glass73306922020-04-17 18:09:01 -060081 toolpath,
Simon Glass162017b2022-01-09 20:13:57 -070082 [bintool_test.TestBintool, entry_test.TestEntry, ftest.TestFunctional,
83 fdt_test.TestFdt, elf_test.TestElf, image_test.TestImage,
84 cbfs_util_test.TestCbfs, fip_util_test.TestFip])
Simon Glassd1ba61c2019-05-14 15:53:38 -060085
Alper Nebi Yasakca1c5882022-04-02 20:06:06 +030086 return (0 if result.wasSuccessful() else 1)
Simon Glass2574ef62016-11-25 20:15:51 -070087
Simon Glass56292b72023-02-23 18:18:18 -070088def RunTestCoverage(toolpath, build_dir):
Simon Glass2574ef62016-11-25 20:15:51 -070089 """Run the tests and check that we get 100% coverage"""
Simon Glass220ff5f2020-08-05 13:27:46 -060090 glob_list = control.GetEntryModules(False)
Tom Rinic2a849d2018-07-06 10:27:14 -060091 all_set = set([os.path.splitext(os.path.basename(item))[0]
92 for item in glob_list if '_testing' not in item])
Simon Glass5d5930d2020-07-09 18:39:29 -060093 extra_args = ''
94 if toolpath:
95 for path in toolpath:
96 extra_args += ' --toolpath %s' % path
Simon Glass1b53d902022-01-29 14:14:14 -070097 test_util.run_test_coverage('tools/binman/binman', None,
Simon Glass131444f2023-02-23 18:18:04 -070098 ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*',
99 'tools/u_boot_pylib/*'],
Simon Glass56292b72023-02-23 18:18:18 -0700100 build_dir, all_set, extra_args or None)
Simon Glass2574ef62016-11-25 20:15:51 -0700101
Simon Glassf46732a2019-07-08 14:25:29 -0600102def RunBinman(args):
Simon Glass2574ef62016-11-25 20:15:51 -0700103 """Main entry point to binman once arguments are parsed
104
105 Args:
Simon Glassf46732a2019-07-08 14:25:29 -0600106 args: Command line arguments Namespace object
Simon Glass2574ef62016-11-25 20:15:51 -0700107 """
108 ret_code = 0
109
Simon Glassf46732a2019-07-08 14:25:29 -0600110 if not args.debug:
Simon Glass2574ef62016-11-25 20:15:51 -0700111 sys.tracebacklimit = 0
112
Simon Glass901ec8f2020-07-09 18:39:30 -0600113 # Provide a default toolpath in the hope of finding a mkimage built from
114 # current source
115 if not args.toolpath:
116 args.toolpath = ['./tools', 'build-sandbox/tools']
117
Simon Glassf46732a2019-07-08 14:25:29 -0600118 if args.cmd == 'test':
119 if args.test_coverage:
Simon Glass56292b72023-02-23 18:18:18 -0700120 RunTestCoverage(args.toolpath, args.build_dir)
Simon Glassf46732a2019-07-08 14:25:29 -0600121 else:
122 ret_code = RunTests(args.debug, args.verbosity, args.processes,
123 args.test_preserve_dirs, args.tests,
124 args.toolpath)
Simon Glass2574ef62016-11-25 20:15:51 -0700125
Simon Glass620c4462022-01-09 20:14:11 -0700126 elif args.cmd == 'bintool-docs':
127 control.write_bintool_docs(bintool.Bintool.get_tool_list())
128
Simon Glassf46732a2019-07-08 14:25:29 -0600129 elif args.cmd == 'entry-docs':
Simon Glass220ff5f2020-08-05 13:27:46 -0600130 control.WriteEntryDocs(control.GetEntryModules())
Simon Glass2574ef62016-11-25 20:15:51 -0700131
132 else:
133 try:
Simon Glassf46732a2019-07-08 14:25:29 -0600134 ret_code = control.Binman(args)
Simon Glass2574ef62016-11-25 20:15:51 -0700135 except Exception as e:
Simon Glassb5a8a942020-07-09 18:39:26 -0600136 print('binman: %s' % e, file=sys.stderr)
Simon Glassf46732a2019-07-08 14:25:29 -0600137 if args.debug:
Simon Glass7cca27d2019-05-14 15:53:37 -0600138 print()
Simon Glass2574ef62016-11-25 20:15:51 -0700139 traceback.print_exc()
140 ret_code = 1
141 return ret_code
142
143
Simon Glass56292b72023-02-23 18:18:18 -0700144def start_binman():
Simon Glassf46732a2019-07-08 14:25:29 -0600145 args = cmdline.ParseArgs(sys.argv[1:])
146
147 ret_code = RunBinman(args)
Simon Glass2574ef62016-11-25 20:15:51 -0700148 sys.exit(ret_code)
Simon Glass56292b72023-02-23 18:18:18 -0700149
150
151if __name__ == "__main__":
152 start_binman()