blob: ab25b48b5fbf6ea4129fc5279c24dce2a4d28bee [file] [log] [blame]
Simon Glass38856012019-10-31 07:43:05 -06001#!/usr/bin/env python3
Tom Rini83d290c2018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glassbf7fd502016-11-25 20:15:51 -07003
4# Copyright (c) 2016 Google, Inc
5# Written by Simon Glass <sjg@chromium.org>
6#
Simon Glassbf7fd502016-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
Simon Glass86679ce2019-07-08 13:18:36 -060012from distutils.sysconfig import get_python_lib
Simon Glassbf7fd502016-11-25 20:15:51 -070013import os
Simon Glass86679ce2019-07-08 13:18:36 -060014import site
Simon Glassbf7fd502016-11-25 20:15:51 -070015import sys
16import traceback
17import unittest
18
Andy Shevchenko022f6b02021-12-06 14:44:12 +030019# Get the absolute path to this file at run-time
20our_path = os.path.dirname(os.path.realpath(__file__))
21our1_path = os.path.dirname(our_path)
22our2_path = os.path.dirname(our1_path)
23
Andy Shevchenko33f27f42021-12-06 14:44:13 +030024# Extract $(srctree) from Kbuild environment, or use relative paths below
25srctree = os.environ.get('srctree', our2_path)
26
Andy Shevchenko022f6b02021-12-06 14:44:12 +030027#
28# Do not pollute source tree with cache files:
29# https://stackoverflow.com/a/60024195/2511795
30# https://bugs.python.org/issue33499
31#
Andy Shevchenko33f27f42021-12-06 14:44:13 +030032sys.pycache_prefix = os.path.relpath(our_path, srctree)
Andy Shevchenko022f6b02021-12-06 14:44:12 +030033
Simon Glass53cd5d92019-07-08 14:25:29 -060034# Bring in the patman and dtoc libraries (but don't override the first path
35# in PYTHONPATH)
Andy Shevchenko33f27f42021-12-06 14:44:13 +030036sys.path.insert(2, our1_path)
Simon Glassb4fa9492020-04-17 18:09:05 -060037
Simon Glassbc570642022-01-09 20:14:11 -070038from binman import bintool
Simon Glassb4fa9492020-04-17 18:09:05 -060039from patman import test_util
Simon Glassbf7fd502016-11-25 20:15:51 -070040
Simon Glassb4360202017-05-27 07:38:22 -060041# Bring in the libfdt module
Simon Glass53cd5d92019-07-08 14:25:29 -060042sys.path.insert(2, 'scripts/dtc/pylibfdt')
Andy Shevchenko33f27f42021-12-06 14:44:13 +030043sys.path.insert(2, os.path.join(srctree, 'scripts/dtc/pylibfdt'))
44sys.path.insert(2, os.path.join(srctree, 'build-sandbox_spl/scripts/dtc/pylibfdt'))
Simon Glassb4360202017-05-27 07:38:22 -060045
Simon Glass86679ce2019-07-08 13:18:36 -060046# When running under python-coverage on Ubuntu 16.04, the dist-packages
47# directories are dropped from the python path. Add them in so that we can find
48# the elffile module. We could use site.getsitepackages() here but unfortunately
49# that is not available in a virtualenv.
50sys.path.append(get_python_lib())
51
Simon Glass16287932020-04-17 18:09:03 -060052from binman import cmdline
53from binman import control
Simon Glassbf776672020-04-17 18:09:04 -060054from patman import test_util
Simon Glassbf7fd502016-11-25 20:15:51 -070055
Simon Glass8acce602019-07-08 13:18:50 -060056def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
Simon Glass084059a2018-06-01 09:38:18 -060057 """Run the functional tests and any embedded doctests
58
59 Args:
60 debug: True to enable debugging, which shows a full stack trace on error
Simon Glassee0c9a72019-07-08 13:18:48 -060061 verbosity: Verbosity level to use
Simon Glassd5164a72019-07-08 13:18:49 -060062 test_preserve_dirs: True to preserve the input directory used by tests
63 so that it can be examined afterwards (only useful for debugging
64 tests). If a single test is selected (in args[0]) it also preserves
65 the output directory for this test. Both directories are displayed
66 on the command line.
67 processes: Number of processes to use to run tests (None=same as #CPUs)
Simon Glass084059a2018-06-01 09:38:18 -060068 args: List of positional args provided to binman. This can hold a test
Simon Glass53cd5d92019-07-08 14:25:29 -060069 name to execute (as in 'binman test testSections', for example)
Simon Glass8acce602019-07-08 13:18:50 -060070 toolpath: List of paths to use for tools
Simon Glass084059a2018-06-01 09:38:18 -060071 """
Simon Glass56ee85e2022-01-09 20:13:57 -070072 from binman import bintool_test
Simon Glass16287932020-04-17 18:09:03 -060073 from binman import cbfs_util_test
74 from binman import elf_test
75 from binman import entry_test
76 from binman import fdt_test
Simon Glassed16b122021-11-23 21:08:58 -070077 from binman import fip_util_test
Simon Glass16287932020-04-17 18:09:03 -060078 from binman import ftest
79 from binman import image_test
Simon Glassbf7fd502016-11-25 20:15:51 -070080 import doctest
81
82 result = unittest.TestResult()
Simon Glassce0dc2e2020-04-17 18:09:01 -060083 test_name = args and args[0] or None
Simon Glass934cdcf2017-11-12 21:52:21 -070084
85 # Run the entry tests first ,since these need to be the first to import the
86 # 'entry' module.
Simon Glass5e2ab402022-01-29 14:14:14 -070087 test_util.run_test_suites(
Simon Glassce0dc2e2020-04-17 18:09:01 -060088 result, debug, verbosity, test_preserve_dirs, processes, test_name,
89 toolpath,
Simon Glass56ee85e2022-01-09 20:13:57 -070090 [bintool_test.TestBintool, entry_test.TestEntry, ftest.TestFunctional,
91 fdt_test.TestFdt, elf_test.TestElf, image_test.TestImage,
92 cbfs_util_test.TestCbfs, fip_util_test.TestFip])
Simon Glassbf7fd502016-11-25 20:15:51 -070093
Simon Glass5e2ab402022-01-29 14:14:14 -070094 return test_util.report_result('binman', test_name, result)
Simon Glassbf7fd502016-11-25 20:15:51 -070095
Simon Glass32eb66d2020-07-09 18:39:29 -060096def RunTestCoverage(toolpath):
Simon Glassbf7fd502016-11-25 20:15:51 -070097 """Run the tests and check that we get 100% coverage"""
Simon Glass87d43322020-08-05 13:27:46 -060098 glob_list = control.GetEntryModules(False)
Tom Rini16d836c2018-07-06 10:27:14 -060099 all_set = set([os.path.splitext(os.path.basename(item))[0]
100 for item in glob_list if '_testing' not in item])
Simon Glass32eb66d2020-07-09 18:39:29 -0600101 extra_args = ''
102 if toolpath:
103 for path in toolpath:
104 extra_args += ' --toolpath %s' % path
Simon Glass5e2ab402022-01-29 14:14:14 -0700105 test_util.run_test_coverage('tools/binman/binman', None,
Simon Glassc07ab6e2020-04-17 18:08:58 -0600106 ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'],
Simon Glass32eb66d2020-07-09 18:39:29 -0600107 args.build_dir, all_set, extra_args or None)
Simon Glassbf7fd502016-11-25 20:15:51 -0700108
Simon Glass53cd5d92019-07-08 14:25:29 -0600109def RunBinman(args):
Simon Glassbf7fd502016-11-25 20:15:51 -0700110 """Main entry point to binman once arguments are parsed
111
112 Args:
Simon Glass53cd5d92019-07-08 14:25:29 -0600113 args: Command line arguments Namespace object
Simon Glassbf7fd502016-11-25 20:15:51 -0700114 """
115 ret_code = 0
116
Simon Glass53cd5d92019-07-08 14:25:29 -0600117 if not args.debug:
Simon Glassbf7fd502016-11-25 20:15:51 -0700118 sys.tracebacklimit = 0
119
Simon Glassb5287c42020-07-09 18:39:30 -0600120 # Provide a default toolpath in the hope of finding a mkimage built from
121 # current source
122 if not args.toolpath:
123 args.toolpath = ['./tools', 'build-sandbox/tools']
124
Simon Glass53cd5d92019-07-08 14:25:29 -0600125 if args.cmd == 'test':
126 if args.test_coverage:
Simon Glass32eb66d2020-07-09 18:39:29 -0600127 RunTestCoverage(args.toolpath)
Simon Glass53cd5d92019-07-08 14:25:29 -0600128 else:
129 ret_code = RunTests(args.debug, args.verbosity, args.processes,
130 args.test_preserve_dirs, args.tests,
131 args.toolpath)
Simon Glassbf7fd502016-11-25 20:15:51 -0700132
Simon Glassbc570642022-01-09 20:14:11 -0700133 elif args.cmd == 'bintool-docs':
134 control.write_bintool_docs(bintool.Bintool.get_tool_list())
135
Simon Glass53cd5d92019-07-08 14:25:29 -0600136 elif args.cmd == 'entry-docs':
Simon Glass87d43322020-08-05 13:27:46 -0600137 control.WriteEntryDocs(control.GetEntryModules())
Simon Glassbf7fd502016-11-25 20:15:51 -0700138
139 else:
140 try:
Simon Glass53cd5d92019-07-08 14:25:29 -0600141 ret_code = control.Binman(args)
Simon Glassbf7fd502016-11-25 20:15:51 -0700142 except Exception as e:
Simon Glass6c8e0bf2020-07-09 18:39:26 -0600143 print('binman: %s' % e, file=sys.stderr)
Simon Glass53cd5d92019-07-08 14:25:29 -0600144 if args.debug:
Simon Glass2ca84682019-05-14 15:53:37 -0600145 print()
Simon Glassbf7fd502016-11-25 20:15:51 -0700146 traceback.print_exc()
147 ret_code = 1
148 return ret_code
149
150
151if __name__ == "__main__":
Simon Glass53cd5d92019-07-08 14:25:29 -0600152 args = cmdline.ParseArgs(sys.argv[1:])
153
154 ret_code = RunBinman(args)
Simon Glassbf7fd502016-11-25 20:15:51 -0700155 sys.exit(ret_code)