blob: 35944f314a21ef67c4615e027dc9233464799928 [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
Simon Glass40778d72019-07-08 13:18:36 -060012from distutils.sysconfig import get_python_lib
Simon Glass2574ef62016-11-25 20:15:51 -070013import os
Simon Glass40778d72019-07-08 13:18:36 -060014import site
Simon Glass2574ef62016-11-25 20:15:51 -070015import sys
16import traceback
17import unittest
18
Andy Shevchenko12ae3472021-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 Shevchenko6f64b602021-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 Shevchenko12ae3472021-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 Shevchenko6f64b602021-12-06 14:44:13 +030032sys.pycache_prefix = os.path.relpath(our_path, srctree)
Andy Shevchenko12ae3472021-12-06 14:44:12 +030033
Simon Glassf46732a2019-07-08 14:25:29 -060034# Bring in the patman and dtoc libraries (but don't override the first path
35# in PYTHONPATH)
Andy Shevchenko6f64b602021-12-06 14:44:13 +030036sys.path.insert(2, our1_path)
Simon Glass42143162020-04-17 18:09:05 -060037
38from patman import test_util
Simon Glass2574ef62016-11-25 20:15:51 -070039
Simon Glass55901ff2017-05-27 07:38:22 -060040# Bring in the libfdt module
Simon Glassf46732a2019-07-08 14:25:29 -060041sys.path.insert(2, 'scripts/dtc/pylibfdt')
Andy Shevchenko6f64b602021-12-06 14:44:13 +030042sys.path.insert(2, os.path.join(srctree, 'scripts/dtc/pylibfdt'))
43sys.path.insert(2, os.path.join(srctree, 'build-sandbox_spl/scripts/dtc/pylibfdt'))
Simon Glass55901ff2017-05-27 07:38:22 -060044
Simon Glass40778d72019-07-08 13:18:36 -060045# When running under python-coverage on Ubuntu 16.04, the dist-packages
46# directories are dropped from the python path. Add them in so that we can find
47# the elffile module. We could use site.getsitepackages() here but unfortunately
48# that is not available in a virtualenv.
49sys.path.append(get_python_lib())
50
Simon Glassc585dd42020-04-17 18:09:03 -060051from binman import cmdline
52from binman import control
Simon Glassa997ea52020-04-17 18:09:04 -060053from patman import test_util
Simon Glass2574ef62016-11-25 20:15:51 -070054
Simon Glasscebfab22019-07-08 13:18:50 -060055def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
Simon Glass5666f9a2018-06-01 09:38:18 -060056 """Run the functional tests and any embedded doctests
57
58 Args:
59 debug: True to enable debugging, which shows a full stack trace on error
Simon Glass8a50b4a2019-07-08 13:18:48 -060060 verbosity: Verbosity level to use
Simon Glass1c420c92019-07-08 13:18:49 -060061 test_preserve_dirs: True to preserve the input directory used by tests
62 so that it can be examined afterwards (only useful for debugging
63 tests). If a single test is selected (in args[0]) it also preserves
64 the output directory for this test. Both directories are displayed
65 on the command line.
66 processes: Number of processes to use to run tests (None=same as #CPUs)
Simon Glass5666f9a2018-06-01 09:38:18 -060067 args: List of positional args provided to binman. This can hold a test
Simon Glassf46732a2019-07-08 14:25:29 -060068 name to execute (as in 'binman test testSections', for example)
Simon Glasscebfab22019-07-08 13:18:50 -060069 toolpath: List of paths to use for tools
Simon Glass5666f9a2018-06-01 09:38:18 -060070 """
Simon Glassc585dd42020-04-17 18:09:03 -060071 from binman import cbfs_util_test
72 from binman import elf_test
73 from binman import entry_test
74 from binman import fdt_test
Simon Glass2697b7c2021-11-23 21:08:58 -070075 from binman import fip_util_test
Simon Glassc585dd42020-04-17 18:09:03 -060076 from binman import ftest
77 from binman import image_test
Simon Glass2574ef62016-11-25 20:15:51 -070078 import doctest
79
80 result = unittest.TestResult()
Simon Glass73306922020-04-17 18:09:01 -060081 test_name = args and args[0] or None
Simon Glass8f521362017-11-12 21:52:21 -070082
83 # Run the entry tests first ,since these need to be the first to import the
84 # 'entry' module.
Simon Glass73306922020-04-17 18:09:01 -060085 test_util.RunTestSuites(
86 result, debug, verbosity, test_preserve_dirs, processes, test_name,
87 toolpath,
88 [entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
Simon Glass2697b7c2021-11-23 21:08:58 -070089 elf_test.TestElf, image_test.TestImage, cbfs_util_test.TestCbfs,
90 fip_util_test.TestFip])
Simon Glassd1ba61c2019-05-14 15:53:38 -060091
Simon Glass73306922020-04-17 18:09:01 -060092 return test_util.ReportResult('binman', test_name, result)
Simon Glass2574ef62016-11-25 20:15:51 -070093
Simon Glass5d5930d2020-07-09 18:39:29 -060094def RunTestCoverage(toolpath):
Simon Glass2574ef62016-11-25 20:15:51 -070095 """Run the tests and check that we get 100% coverage"""
Simon Glass220ff5f2020-08-05 13:27:46 -060096 glob_list = control.GetEntryModules(False)
Tom Rinic2a849d2018-07-06 10:27:14 -060097 all_set = set([os.path.splitext(os.path.basename(item))[0]
98 for item in glob_list if '_testing' not in item])
Simon Glass5d5930d2020-07-09 18:39:29 -060099 extra_args = ''
100 if toolpath:
101 for path in toolpath:
102 extra_args += ' --toolpath %s' % path
Simon Glassebbb5432020-04-17 18:08:58 -0600103 test_util.RunTestCoverage('tools/binman/binman', None,
104 ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'],
Simon Glass5d5930d2020-07-09 18:39:29 -0600105 args.build_dir, all_set, extra_args or None)
Simon Glass2574ef62016-11-25 20:15:51 -0700106
Simon Glassf46732a2019-07-08 14:25:29 -0600107def RunBinman(args):
Simon Glass2574ef62016-11-25 20:15:51 -0700108 """Main entry point to binman once arguments are parsed
109
110 Args:
Simon Glassf46732a2019-07-08 14:25:29 -0600111 args: Command line arguments Namespace object
Simon Glass2574ef62016-11-25 20:15:51 -0700112 """
113 ret_code = 0
114
Simon Glassf46732a2019-07-08 14:25:29 -0600115 if not args.debug:
Simon Glass2574ef62016-11-25 20:15:51 -0700116 sys.tracebacklimit = 0
117
Simon Glass901ec8f2020-07-09 18:39:30 -0600118 # Provide a default toolpath in the hope of finding a mkimage built from
119 # current source
120 if not args.toolpath:
121 args.toolpath = ['./tools', 'build-sandbox/tools']
122
Simon Glassf46732a2019-07-08 14:25:29 -0600123 if args.cmd == 'test':
124 if args.test_coverage:
Simon Glass5d5930d2020-07-09 18:39:29 -0600125 RunTestCoverage(args.toolpath)
Simon Glassf46732a2019-07-08 14:25:29 -0600126 else:
127 ret_code = RunTests(args.debug, args.verbosity, args.processes,
128 args.test_preserve_dirs, args.tests,
129 args.toolpath)
Simon Glass2574ef62016-11-25 20:15:51 -0700130
Simon Glassf46732a2019-07-08 14:25:29 -0600131 elif args.cmd == 'entry-docs':
Simon Glass220ff5f2020-08-05 13:27:46 -0600132 control.WriteEntryDocs(control.GetEntryModules())
Simon Glass2574ef62016-11-25 20:15:51 -0700133
134 else:
135 try:
Simon Glassf46732a2019-07-08 14:25:29 -0600136 ret_code = control.Binman(args)
Simon Glass2574ef62016-11-25 20:15:51 -0700137 except Exception as e:
Simon Glassb5a8a942020-07-09 18:39:26 -0600138 print('binman: %s' % e, file=sys.stderr)
Simon Glassf46732a2019-07-08 14:25:29 -0600139 if args.debug:
Simon Glass7cca27d2019-05-14 15:53:37 -0600140 print()
Simon Glass2574ef62016-11-25 20:15:51 -0700141 traceback.print_exc()
142 ret_code = 1
143 return ret_code
144
145
146if __name__ == "__main__":
Simon Glassf46732a2019-07-08 14:25:29 -0600147 args = cmdline.ParseArgs(sys.argv[1:])
148
149 ret_code = RunBinman(args)
Simon Glass2574ef62016-11-25 20:15:51 -0700150 sys.exit(ret_code)