blob: 8c1e478d54cea8342a829808c14d70f393c50dba [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
Simon Glass53cd5d92019-07-08 14:25:29 -060019# Bring in the patman and dtoc libraries (but don't override the first path
20# in PYTHONPATH)
Simon Glassbf7fd502016-11-25 20:15:51 -070021our_path = os.path.dirname(os.path.realpath(__file__))
Simon Glassb4fa9492020-04-17 18:09:05 -060022sys.path.insert(2, os.path.join(our_path, '..'))
23
24from patman import test_util
Simon Glassbf7fd502016-11-25 20:15:51 -070025
Simon Glassb4360202017-05-27 07:38:22 -060026# Bring in the libfdt module
Simon Glass53cd5d92019-07-08 14:25:29 -060027sys.path.insert(2, 'scripts/dtc/pylibfdt')
Simon Glassfdb30402020-07-09 18:39:28 -060028sys.path.insert(2, os.path.join(our_path, '../../scripts/dtc/pylibfdt'))
Simon Glass53cd5d92019-07-08 14:25:29 -060029sys.path.insert(2, os.path.join(our_path,
Simon Glassed59e002018-10-01 21:12:40 -060030 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
Simon Glassb4360202017-05-27 07:38:22 -060031
Simon Glass86679ce2019-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 Glass16287932020-04-17 18:09:03 -060038from binman import cmdline
39from binman import control
Simon Glassbf776672020-04-17 18:09:04 -060040from patman import test_util
Simon Glassbf7fd502016-11-25 20:15:51 -070041
Simon Glass8acce602019-07-08 13:18:50 -060042def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
Simon Glass084059a2018-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 Glassee0c9a72019-07-08 13:18:48 -060047 verbosity: Verbosity level to use
Simon Glassd5164a72019-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 Glass084059a2018-06-01 09:38:18 -060054 args: List of positional args provided to binman. This can hold a test
Simon Glass53cd5d92019-07-08 14:25:29 -060055 name to execute (as in 'binman test testSections', for example)
Simon Glass8acce602019-07-08 13:18:50 -060056 toolpath: List of paths to use for tools
Simon Glass084059a2018-06-01 09:38:18 -060057 """
Simon Glass16287932020-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
62 from binman import ftest
63 from binman import image_test
Simon Glassbf7fd502016-11-25 20:15:51 -070064 import doctest
65
66 result = unittest.TestResult()
Simon Glassce0dc2e2020-04-17 18:09:01 -060067 test_name = args and args[0] or None
Simon Glass934cdcf2017-11-12 21:52:21 -070068
69 # Run the entry tests first ,since these need to be the first to import the
70 # 'entry' module.
Simon Glassce0dc2e2020-04-17 18:09:01 -060071 test_util.RunTestSuites(
72 result, debug, verbosity, test_preserve_dirs, processes, test_name,
73 toolpath,
74 [entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
75 elf_test.TestElf, image_test.TestImage, cbfs_util_test.TestCbfs])
Simon Glassbf7fd502016-11-25 20:15:51 -070076
Simon Glassce0dc2e2020-04-17 18:09:01 -060077 return test_util.ReportResult('binman', test_name, result)
Simon Glassbf7fd502016-11-25 20:15:51 -070078
Simon Glass32eb66d2020-07-09 18:39:29 -060079def RunTestCoverage(toolpath):
Simon Glassbf7fd502016-11-25 20:15:51 -070080 """Run the tests and check that we get 100% coverage"""
Simon Glass87d43322020-08-05 13:27:46 -060081 glob_list = control.GetEntryModules(False)
Tom Rini16d836c2018-07-06 10:27:14 -060082 all_set = set([os.path.splitext(os.path.basename(item))[0]
83 for item in glob_list if '_testing' not in item])
Simon Glass32eb66d2020-07-09 18:39:29 -060084 extra_args = ''
85 if toolpath:
86 for path in toolpath:
87 extra_args += ' --toolpath %s' % path
Simon Glassc07ab6e2020-04-17 18:08:58 -060088 test_util.RunTestCoverage('tools/binman/binman', None,
89 ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'],
Simon Glass32eb66d2020-07-09 18:39:29 -060090 args.build_dir, all_set, extra_args or None)
Simon Glassbf7fd502016-11-25 20:15:51 -070091
Simon Glass53cd5d92019-07-08 14:25:29 -060092def RunBinman(args):
Simon Glassbf7fd502016-11-25 20:15:51 -070093 """Main entry point to binman once arguments are parsed
94
95 Args:
Simon Glass53cd5d92019-07-08 14:25:29 -060096 args: Command line arguments Namespace object
Simon Glassbf7fd502016-11-25 20:15:51 -070097 """
98 ret_code = 0
99
Simon Glass53cd5d92019-07-08 14:25:29 -0600100 if not args.debug:
Simon Glassbf7fd502016-11-25 20:15:51 -0700101 sys.tracebacklimit = 0
102
Simon Glassb5287c42020-07-09 18:39:30 -0600103 # Provide a default toolpath in the hope of finding a mkimage built from
104 # current source
105 if not args.toolpath:
106 args.toolpath = ['./tools', 'build-sandbox/tools']
107
Simon Glass53cd5d92019-07-08 14:25:29 -0600108 if args.cmd == 'test':
109 if args.test_coverage:
Simon Glass32eb66d2020-07-09 18:39:29 -0600110 RunTestCoverage(args.toolpath)
Simon Glass53cd5d92019-07-08 14:25:29 -0600111 else:
112 ret_code = RunTests(args.debug, args.verbosity, args.processes,
113 args.test_preserve_dirs, args.tests,
114 args.toolpath)
Simon Glassbf7fd502016-11-25 20:15:51 -0700115
Simon Glass53cd5d92019-07-08 14:25:29 -0600116 elif args.cmd == 'entry-docs':
Simon Glass87d43322020-08-05 13:27:46 -0600117 control.WriteEntryDocs(control.GetEntryModules())
Simon Glassbf7fd502016-11-25 20:15:51 -0700118
119 else:
120 try:
Simon Glass53cd5d92019-07-08 14:25:29 -0600121 ret_code = control.Binman(args)
Simon Glassbf7fd502016-11-25 20:15:51 -0700122 except Exception as e:
Simon Glass6c8e0bf2020-07-09 18:39:26 -0600123 print('binman: %s' % e, file=sys.stderr)
Simon Glass53cd5d92019-07-08 14:25:29 -0600124 if args.debug:
Simon Glass2ca84682019-05-14 15:53:37 -0600125 print()
Simon Glassbf7fd502016-11-25 20:15:51 -0700126 traceback.print_exc()
127 ret_code = 1
128 return ret_code
129
130
131if __name__ == "__main__":
Simon Glass53cd5d92019-07-08 14:25:29 -0600132 args = cmdline.ParseArgs(sys.argv[1:])
133
134 ret_code = RunBinman(args)
Simon Glassbf7fd502016-11-25 20:15:51 -0700135 sys.exit(ret_code)