blob: 2fa55ff549aed36cd3b6f3ff428cf7cac0e8e2c5 [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
24#
25# Do not pollute source tree with cache files:
26# https://stackoverflow.com/a/60024195/2511795
27# https://bugs.python.org/issue33499
28#
29sys.pycache_prefix = os.path.relpath(our_path, os.environ.get('srctree', our2_path))
30
Simon Glass53cd5d92019-07-08 14:25:29 -060031# Bring in the patman and dtoc libraries (but don't override the first path
32# in PYTHONPATH)
Simon Glassb4fa9492020-04-17 18:09:05 -060033sys.path.insert(2, os.path.join(our_path, '..'))
34
35from patman import test_util
Simon Glassbf7fd502016-11-25 20:15:51 -070036
Simon Glassb4360202017-05-27 07:38:22 -060037# Bring in the libfdt module
Simon Glass53cd5d92019-07-08 14:25:29 -060038sys.path.insert(2, 'scripts/dtc/pylibfdt')
Simon Glassfdb30402020-07-09 18:39:28 -060039sys.path.insert(2, os.path.join(our_path, '../../scripts/dtc/pylibfdt'))
Simon Glass53cd5d92019-07-08 14:25:29 -060040sys.path.insert(2, os.path.join(our_path,
Simon Glassed59e002018-10-01 21:12:40 -060041 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
Simon Glassb4360202017-05-27 07:38:22 -060042
Simon Glass86679ce2019-07-08 13:18:36 -060043# When running under python-coverage on Ubuntu 16.04, the dist-packages
44# directories are dropped from the python path. Add them in so that we can find
45# the elffile module. We could use site.getsitepackages() here but unfortunately
46# that is not available in a virtualenv.
47sys.path.append(get_python_lib())
48
Simon Glass16287932020-04-17 18:09:03 -060049from binman import cmdline
50from binman import control
Simon Glassbf776672020-04-17 18:09:04 -060051from patman import test_util
Simon Glassbf7fd502016-11-25 20:15:51 -070052
Simon Glass8acce602019-07-08 13:18:50 -060053def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
Simon Glass084059a2018-06-01 09:38:18 -060054 """Run the functional tests and any embedded doctests
55
56 Args:
57 debug: True to enable debugging, which shows a full stack trace on error
Simon Glassee0c9a72019-07-08 13:18:48 -060058 verbosity: Verbosity level to use
Simon Glassd5164a72019-07-08 13:18:49 -060059 test_preserve_dirs: True to preserve the input directory used by tests
60 so that it can be examined afterwards (only useful for debugging
61 tests). If a single test is selected (in args[0]) it also preserves
62 the output directory for this test. Both directories are displayed
63 on the command line.
64 processes: Number of processes to use to run tests (None=same as #CPUs)
Simon Glass084059a2018-06-01 09:38:18 -060065 args: List of positional args provided to binman. This can hold a test
Simon Glass53cd5d92019-07-08 14:25:29 -060066 name to execute (as in 'binman test testSections', for example)
Simon Glass8acce602019-07-08 13:18:50 -060067 toolpath: List of paths to use for tools
Simon Glass084059a2018-06-01 09:38:18 -060068 """
Simon Glass16287932020-04-17 18:09:03 -060069 from binman import cbfs_util_test
70 from binman import elf_test
71 from binman import entry_test
72 from binman import fdt_test
Simon Glassed16b122021-11-23 21:08:58 -070073 from binman import fip_util_test
Simon Glass16287932020-04-17 18:09:03 -060074 from binman import ftest
75 from binman import image_test
Simon Glassbf7fd502016-11-25 20:15:51 -070076 import doctest
77
78 result = unittest.TestResult()
Simon Glassce0dc2e2020-04-17 18:09:01 -060079 test_name = args and args[0] or None
Simon Glass934cdcf2017-11-12 21:52:21 -070080
81 # Run the entry tests first ,since these need to be the first to import the
82 # 'entry' module.
Simon Glassce0dc2e2020-04-17 18:09:01 -060083 test_util.RunTestSuites(
84 result, debug, verbosity, test_preserve_dirs, processes, test_name,
85 toolpath,
86 [entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
Simon Glassed16b122021-11-23 21:08:58 -070087 elf_test.TestElf, image_test.TestImage, cbfs_util_test.TestCbfs,
88 fip_util_test.TestFip])
Simon Glassbf7fd502016-11-25 20:15:51 -070089
Simon Glassce0dc2e2020-04-17 18:09:01 -060090 return test_util.ReportResult('binman', test_name, result)
Simon Glassbf7fd502016-11-25 20:15:51 -070091
Simon Glass32eb66d2020-07-09 18:39:29 -060092def RunTestCoverage(toolpath):
Simon Glassbf7fd502016-11-25 20:15:51 -070093 """Run the tests and check that we get 100% coverage"""
Simon Glass87d43322020-08-05 13:27:46 -060094 glob_list = control.GetEntryModules(False)
Tom Rini16d836c2018-07-06 10:27:14 -060095 all_set = set([os.path.splitext(os.path.basename(item))[0]
96 for item in glob_list if '_testing' not in item])
Simon Glass32eb66d2020-07-09 18:39:29 -060097 extra_args = ''
98 if toolpath:
99 for path in toolpath:
100 extra_args += ' --toolpath %s' % path
Simon Glassc07ab6e2020-04-17 18:08:58 -0600101 test_util.RunTestCoverage('tools/binman/binman', None,
102 ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'],
Simon Glass32eb66d2020-07-09 18:39:29 -0600103 args.build_dir, all_set, extra_args or None)
Simon Glassbf7fd502016-11-25 20:15:51 -0700104
Simon Glass53cd5d92019-07-08 14:25:29 -0600105def RunBinman(args):
Simon Glassbf7fd502016-11-25 20:15:51 -0700106 """Main entry point to binman once arguments are parsed
107
108 Args:
Simon Glass53cd5d92019-07-08 14:25:29 -0600109 args: Command line arguments Namespace object
Simon Glassbf7fd502016-11-25 20:15:51 -0700110 """
111 ret_code = 0
112
Simon Glass53cd5d92019-07-08 14:25:29 -0600113 if not args.debug:
Simon Glassbf7fd502016-11-25 20:15:51 -0700114 sys.tracebacklimit = 0
115
Simon Glassb5287c42020-07-09 18:39:30 -0600116 # Provide a default toolpath in the hope of finding a mkimage built from
117 # current source
118 if not args.toolpath:
119 args.toolpath = ['./tools', 'build-sandbox/tools']
120
Simon Glass53cd5d92019-07-08 14:25:29 -0600121 if args.cmd == 'test':
122 if args.test_coverage:
Simon Glass32eb66d2020-07-09 18:39:29 -0600123 RunTestCoverage(args.toolpath)
Simon Glass53cd5d92019-07-08 14:25:29 -0600124 else:
125 ret_code = RunTests(args.debug, args.verbosity, args.processes,
126 args.test_preserve_dirs, args.tests,
127 args.toolpath)
Simon Glassbf7fd502016-11-25 20:15:51 -0700128
Simon Glass53cd5d92019-07-08 14:25:29 -0600129 elif args.cmd == 'entry-docs':
Simon Glass87d43322020-08-05 13:27:46 -0600130 control.WriteEntryDocs(control.GetEntryModules())
Simon Glassbf7fd502016-11-25 20:15:51 -0700131
132 else:
133 try:
Simon Glass53cd5d92019-07-08 14:25:29 -0600134 ret_code = control.Binman(args)
Simon Glassbf7fd502016-11-25 20:15:51 -0700135 except Exception as e:
Simon Glass6c8e0bf2020-07-09 18:39:26 -0600136 print('binman: %s' % e, file=sys.stderr)
Simon Glass53cd5d92019-07-08 14:25:29 -0600137 if args.debug:
Simon Glass2ca84682019-05-14 15:53:37 -0600138 print()
Simon Glassbf7fd502016-11-25 20:15:51 -0700139 traceback.print_exc()
140 ret_code = 1
141 return ret_code
142
143
144if __name__ == "__main__":
Simon Glass53cd5d92019-07-08 14:25:29 -0600145 args = cmdline.ParseArgs(sys.argv[1:])
146
147 ret_code = RunBinman(args)
Simon Glassbf7fd502016-11-25 20:15:51 -0700148 sys.exit(ret_code)