blob: e543a7d06a7a5957fdbdf4f04bb71e6c07effb81 [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 Glass2c3cf452017-11-12 21:52:24 -070013import glob
Simon Glass2574ef62016-11-25 20:15:51 -070014import os
Simon Glass40778d72019-07-08 13:18:36 -060015import site
Simon Glass2574ef62016-11-25 20:15:51 -070016import sys
17import traceback
18import unittest
19
Simon Glassf46732a2019-07-08 14:25:29 -060020# Bring in the patman and dtoc libraries (but don't override the first path
21# in PYTHONPATH)
Simon Glass2574ef62016-11-25 20:15:51 -070022our_path = os.path.dirname(os.path.realpath(__file__))
Simon Glass42143162020-04-17 18:09:05 -060023sys.path.insert(2, os.path.join(our_path, '..'))
24
25from patman import test_util
Simon Glass2574ef62016-11-25 20:15:51 -070026
Simon Glass55901ff2017-05-27 07:38:22 -060027# Bring in the libfdt module
Simon Glassf46732a2019-07-08 14:25:29 -060028sys.path.insert(2, 'scripts/dtc/pylibfdt')
Simon Glass6a5d36e2020-07-09 18:39:28 -060029sys.path.insert(2, os.path.join(our_path, '../../scripts/dtc/pylibfdt'))
Simon Glassf46732a2019-07-08 14:25:29 -060030sys.path.insert(2, os.path.join(our_path,
Simon Glass5656ca22018-10-01 21:12:40 -060031 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
Simon Glass55901ff2017-05-27 07:38:22 -060032
Simon Glass40778d72019-07-08 13:18:36 -060033# When running under python-coverage on Ubuntu 16.04, the dist-packages
34# directories are dropped from the python path. Add them in so that we can find
35# the elffile module. We could use site.getsitepackages() here but unfortunately
36# that is not available in a virtualenv.
37sys.path.append(get_python_lib())
38
Simon Glassc585dd42020-04-17 18:09:03 -060039from binman import cmdline
40from binman import control
Simon Glassa997ea52020-04-17 18:09:04 -060041from patman import test_util
Simon Glass2574ef62016-11-25 20:15:51 -070042
Simon Glasscebfab22019-07-08 13:18:50 -060043def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
Simon Glass5666f9a2018-06-01 09:38:18 -060044 """Run the functional tests and any embedded doctests
45
46 Args:
47 debug: True to enable debugging, which shows a full stack trace on error
Simon Glass8a50b4a2019-07-08 13:18:48 -060048 verbosity: Verbosity level to use
Simon Glass1c420c92019-07-08 13:18:49 -060049 test_preserve_dirs: True to preserve the input directory used by tests
50 so that it can be examined afterwards (only useful for debugging
51 tests). If a single test is selected (in args[0]) it also preserves
52 the output directory for this test. Both directories are displayed
53 on the command line.
54 processes: Number of processes to use to run tests (None=same as #CPUs)
Simon Glass5666f9a2018-06-01 09:38:18 -060055 args: List of positional args provided to binman. This can hold a test
Simon Glassf46732a2019-07-08 14:25:29 -060056 name to execute (as in 'binman test testSections', for example)
Simon Glasscebfab22019-07-08 13:18:50 -060057 toolpath: List of paths to use for tools
Simon Glass5666f9a2018-06-01 09:38:18 -060058 """
Simon Glassc585dd42020-04-17 18:09:03 -060059 from binman import cbfs_util_test
60 from binman import elf_test
61 from binman import entry_test
62 from binman import fdt_test
63 from binman import ftest
64 from binman import image_test
65 from binman import test
Simon Glass2574ef62016-11-25 20:15:51 -070066 import doctest
67
68 result = unittest.TestResult()
Simon Glass73306922020-04-17 18:09:01 -060069 test_name = args and args[0] or None
Simon Glass8f521362017-11-12 21:52:21 -070070
71 # Run the entry tests first ,since these need to be the first to import the
72 # 'entry' module.
Simon Glass73306922020-04-17 18:09:01 -060073 test_util.RunTestSuites(
74 result, debug, verbosity, test_preserve_dirs, processes, test_name,
75 toolpath,
76 [entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
77 elf_test.TestElf, image_test.TestImage, cbfs_util_test.TestCbfs])
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 Glass969616c2018-07-17 13:25:36 -060081def GetEntryModules(include_testing=True):
82 """Get a set of entry class implementations
83
84 Returns:
85 Set of paths to entry class filenames
86 """
87 glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
88 return set([os.path.splitext(os.path.basename(item))[0]
89 for item in glob_list
90 if include_testing or '_testing' not in item])
91
Simon Glass5d5930d2020-07-09 18:39:29 -060092def RunTestCoverage(toolpath):
Simon Glass2574ef62016-11-25 20:15:51 -070093 """Run the tests and check that we get 100% coverage"""
Simon Glass969616c2018-07-17 13:25:36 -060094 glob_list = GetEntryModules(False)
Tom Rinic2a849d2018-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 Glass5d5930d2020-07-09 18:39:29 -060097 extra_args = ''
98 if toolpath:
99 for path in toolpath:
100 extra_args += ' --toolpath %s' % path
Simon Glassebbb5432020-04-17 18:08:58 -0600101 test_util.RunTestCoverage('tools/binman/binman', None,
102 ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'],
Simon Glass5d5930d2020-07-09 18:39:29 -0600103 args.build_dir, all_set, extra_args or None)
Simon Glass2574ef62016-11-25 20:15:51 -0700104
Simon Glassf46732a2019-07-08 14:25:29 -0600105def RunBinman(args):
Simon Glass2574ef62016-11-25 20:15:51 -0700106 """Main entry point to binman once arguments are parsed
107
108 Args:
Simon Glassf46732a2019-07-08 14:25:29 -0600109 args: Command line arguments Namespace object
Simon Glass2574ef62016-11-25 20:15:51 -0700110 """
111 ret_code = 0
112
Simon Glassf46732a2019-07-08 14:25:29 -0600113 if not args.debug:
Simon Glass2574ef62016-11-25 20:15:51 -0700114 sys.tracebacklimit = 0
115
Simon Glass901ec8f2020-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 Glassf46732a2019-07-08 14:25:29 -0600121 if args.cmd == 'test':
122 if args.test_coverage:
Simon Glass5d5930d2020-07-09 18:39:29 -0600123 RunTestCoverage(args.toolpath)
Simon Glassf46732a2019-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 Glass2574ef62016-11-25 20:15:51 -0700128
Simon Glassf46732a2019-07-08 14:25:29 -0600129 elif args.cmd == 'entry-docs':
Simon Glass969616c2018-07-17 13:25:36 -0600130 control.WriteEntryDocs(GetEntryModules())
Simon Glass2574ef62016-11-25 20:15:51 -0700131
132 else:
133 try:
Simon Glassf46732a2019-07-08 14:25:29 -0600134 ret_code = control.Binman(args)
Simon Glass2574ef62016-11-25 20:15:51 -0700135 except Exception as e:
Simon Glassb5a8a942020-07-09 18:39:26 -0600136 print('binman: %s' % e, file=sys.stderr)
Simon Glassf46732a2019-07-08 14:25:29 -0600137 if args.debug:
Simon Glass7cca27d2019-05-14 15:53:37 -0600138 print()
Simon Glass2574ef62016-11-25 20:15:51 -0700139 traceback.print_exc()
140 ret_code = 1
141 return ret_code
142
143
144if __name__ == "__main__":
Simon Glassf46732a2019-07-08 14:25:29 -0600145 args = cmdline.ParseArgs(sys.argv[1:])
146
147 ret_code = RunBinman(args)
Simon Glass2574ef62016-11-25 20:15:51 -0700148 sys.exit(ret_code)