blob: 1a639f43e9edf0af79288662519ab97e61aca220 [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
Simon Glassf46732a2019-07-08 14:25:29 -060019# Bring in the patman and dtoc libraries (but don't override the first path
20# in PYTHONPATH)
Simon Glass2574ef62016-11-25 20:15:51 -070021our_path = os.path.dirname(os.path.realpath(__file__))
Simon Glass42143162020-04-17 18:09:05 -060022sys.path.insert(2, os.path.join(our_path, '..'))
23
24from patman import test_util
Simon Glass2574ef62016-11-25 20:15:51 -070025
Simon Glass55901ff2017-05-27 07:38:22 -060026# Bring in the libfdt module
Simon Glassf46732a2019-07-08 14:25:29 -060027sys.path.insert(2, 'scripts/dtc/pylibfdt')
Simon Glass6a5d36e2020-07-09 18:39:28 -060028sys.path.insert(2, os.path.join(our_path, '../../scripts/dtc/pylibfdt'))
Simon Glassf46732a2019-07-08 14:25:29 -060029sys.path.insert(2, os.path.join(our_path,
Simon Glass5656ca22018-10-01 21:12:40 -060030 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
Simon Glass55901ff2017-05-27 07:38:22 -060031
Simon Glass40778d72019-07-08 13:18:36 -060032# When running under python-coverage on Ubuntu 16.04, the dist-packages
33# directories are dropped from the python path. Add them in so that we can find
34# the elffile module. We could use site.getsitepackages() here but unfortunately
35# that is not available in a virtualenv.
36sys.path.append(get_python_lib())
37
Simon Glassc585dd42020-04-17 18:09:03 -060038from binman import cmdline
39from binman import control
Simon Glassa997ea52020-04-17 18:09:04 -060040from patman import test_util
Simon Glass2574ef62016-11-25 20:15:51 -070041
Simon Glasscebfab22019-07-08 13:18:50 -060042def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
Simon Glass5666f9a2018-06-01 09:38:18 -060043 """Run the functional tests and any embedded doctests
44
45 Args:
46 debug: True to enable debugging, which shows a full stack trace on error
Simon Glass8a50b4a2019-07-08 13:18:48 -060047 verbosity: Verbosity level to use
Simon Glass1c420c92019-07-08 13:18:49 -060048 test_preserve_dirs: True to preserve the input directory used by tests
49 so that it can be examined afterwards (only useful for debugging
50 tests). If a single test is selected (in args[0]) it also preserves
51 the output directory for this test. Both directories are displayed
52 on the command line.
53 processes: Number of processes to use to run tests (None=same as #CPUs)
Simon Glass5666f9a2018-06-01 09:38:18 -060054 args: List of positional args provided to binman. This can hold a test
Simon Glassf46732a2019-07-08 14:25:29 -060055 name to execute (as in 'binman test testSections', for example)
Simon Glasscebfab22019-07-08 13:18:50 -060056 toolpath: List of paths to use for tools
Simon Glass5666f9a2018-06-01 09:38:18 -060057 """
Simon Glassc585dd42020-04-17 18:09:03 -060058 from binman import cbfs_util_test
59 from binman import elf_test
60 from binman import entry_test
61 from binman import fdt_test
Simon Glass2697b7c2021-11-23 21:08:58 -070062 from binman import fip_util_test
Simon Glassc585dd42020-04-17 18:09:03 -060063 from binman import ftest
64 from binman import image_test
Simon Glass2574ef62016-11-25 20:15:51 -070065 import doctest
66
67 result = unittest.TestResult()
Simon Glass73306922020-04-17 18:09:01 -060068 test_name = args and args[0] or None
Simon Glass8f521362017-11-12 21:52:21 -070069
70 # Run the entry tests first ,since these need to be the first to import the
71 # 'entry' module.
Simon Glass73306922020-04-17 18:09:01 -060072 test_util.RunTestSuites(
73 result, debug, verbosity, test_preserve_dirs, processes, test_name,
74 toolpath,
75 [entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
Simon Glass2697b7c2021-11-23 21:08:58 -070076 elf_test.TestElf, image_test.TestImage, cbfs_util_test.TestCbfs,
77 fip_util_test.TestFip])
Simon Glassd1ba61c2019-05-14 15:53:38 -060078
Simon Glass73306922020-04-17 18:09:01 -060079 return test_util.ReportResult('binman', test_name, result)
Simon Glass2574ef62016-11-25 20:15:51 -070080
Simon Glass5d5930d2020-07-09 18:39:29 -060081def RunTestCoverage(toolpath):
Simon Glass2574ef62016-11-25 20:15:51 -070082 """Run the tests and check that we get 100% coverage"""
Simon Glass220ff5f2020-08-05 13:27:46 -060083 glob_list = control.GetEntryModules(False)
Tom Rinic2a849d2018-07-06 10:27:14 -060084 all_set = set([os.path.splitext(os.path.basename(item))[0]
85 for item in glob_list if '_testing' not in item])
Simon Glass5d5930d2020-07-09 18:39:29 -060086 extra_args = ''
87 if toolpath:
88 for path in toolpath:
89 extra_args += ' --toolpath %s' % path
Simon Glassebbb5432020-04-17 18:08:58 -060090 test_util.RunTestCoverage('tools/binman/binman', None,
91 ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'],
Simon Glass5d5930d2020-07-09 18:39:29 -060092 args.build_dir, all_set, extra_args or None)
Simon Glass2574ef62016-11-25 20:15:51 -070093
Simon Glassf46732a2019-07-08 14:25:29 -060094def RunBinman(args):
Simon Glass2574ef62016-11-25 20:15:51 -070095 """Main entry point to binman once arguments are parsed
96
97 Args:
Simon Glassf46732a2019-07-08 14:25:29 -060098 args: Command line arguments Namespace object
Simon Glass2574ef62016-11-25 20:15:51 -070099 """
100 ret_code = 0
101
Simon Glassf46732a2019-07-08 14:25:29 -0600102 if not args.debug:
Simon Glass2574ef62016-11-25 20:15:51 -0700103 sys.tracebacklimit = 0
104
Simon Glass901ec8f2020-07-09 18:39:30 -0600105 # Provide a default toolpath in the hope of finding a mkimage built from
106 # current source
107 if not args.toolpath:
108 args.toolpath = ['./tools', 'build-sandbox/tools']
109
Simon Glassf46732a2019-07-08 14:25:29 -0600110 if args.cmd == 'test':
111 if args.test_coverage:
Simon Glass5d5930d2020-07-09 18:39:29 -0600112 RunTestCoverage(args.toolpath)
Simon Glassf46732a2019-07-08 14:25:29 -0600113 else:
114 ret_code = RunTests(args.debug, args.verbosity, args.processes,
115 args.test_preserve_dirs, args.tests,
116 args.toolpath)
Simon Glass2574ef62016-11-25 20:15:51 -0700117
Simon Glassf46732a2019-07-08 14:25:29 -0600118 elif args.cmd == 'entry-docs':
Simon Glass220ff5f2020-08-05 13:27:46 -0600119 control.WriteEntryDocs(control.GetEntryModules())
Simon Glass2574ef62016-11-25 20:15:51 -0700120
121 else:
122 try:
Simon Glassf46732a2019-07-08 14:25:29 -0600123 ret_code = control.Binman(args)
Simon Glass2574ef62016-11-25 20:15:51 -0700124 except Exception as e:
Simon Glassb5a8a942020-07-09 18:39:26 -0600125 print('binman: %s' % e, file=sys.stderr)
Simon Glassf46732a2019-07-08 14:25:29 -0600126 if args.debug:
Simon Glass7cca27d2019-05-14 15:53:37 -0600127 print()
Simon Glass2574ef62016-11-25 20:15:51 -0700128 traceback.print_exc()
129 ret_code = 1
130 return ret_code
131
132
133if __name__ == "__main__":
Simon Glassf46732a2019-07-08 14:25:29 -0600134 args = cmdline.ParseArgs(sys.argv[1:])
135
136 ret_code = RunBinman(args)
Simon Glass2574ef62016-11-25 20:15:51 -0700137 sys.exit(ret_code)