Simon Glass | 377bca8 | 2019-10-31 07:43:05 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 3 | |
| 4 | # Copyright (c) 2016 Google, Inc |
| 5 | # Written by Simon Glass <sjg@chromium.org> |
| 6 | # |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 7 | # Creates binary images from input files controlled by a description |
| 8 | # |
| 9 | |
| 10 | """See README for more information""" |
| 11 | |
Simon Glass | 40778d7 | 2019-07-08 13:18:36 -0600 | [diff] [blame] | 12 | from distutils.sysconfig import get_python_lib |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 13 | import os |
Simon Glass | 40778d7 | 2019-07-08 13:18:36 -0600 | [diff] [blame] | 14 | import site |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 15 | import sys |
| 16 | import traceback |
| 17 | import unittest |
| 18 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 19 | # Bring in the patman and dtoc libraries (but don't override the first path |
| 20 | # in PYTHONPATH) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 21 | our_path = os.path.dirname(os.path.realpath(__file__)) |
Simon Glass | 4214316 | 2020-04-17 18:09:05 -0600 | [diff] [blame] | 22 | sys.path.insert(2, os.path.join(our_path, '..')) |
| 23 | |
| 24 | from patman import test_util |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 25 | |
Simon Glass | 55901ff | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 26 | # Bring in the libfdt module |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 27 | sys.path.insert(2, 'scripts/dtc/pylibfdt') |
Simon Glass | 6a5d36e | 2020-07-09 18:39:28 -0600 | [diff] [blame] | 28 | sys.path.insert(2, os.path.join(our_path, '../../scripts/dtc/pylibfdt')) |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 29 | sys.path.insert(2, os.path.join(our_path, |
Simon Glass | 5656ca2 | 2018-10-01 21:12:40 -0600 | [diff] [blame] | 30 | '../../build-sandbox_spl/scripts/dtc/pylibfdt')) |
Simon Glass | 55901ff | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 31 | |
Simon Glass | 40778d7 | 2019-07-08 13:18:36 -0600 | [diff] [blame] | 32 | # 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. |
| 36 | sys.path.append(get_python_lib()) |
| 37 | |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 38 | from binman import cmdline |
| 39 | from binman import control |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 40 | from patman import test_util |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 41 | |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 42 | def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath): |
Simon Glass | 5666f9a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 43 | """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 Glass | 8a50b4a | 2019-07-08 13:18:48 -0600 | [diff] [blame] | 47 | verbosity: Verbosity level to use |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 48 | 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 Glass | 5666f9a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 54 | args: List of positional args provided to binman. This can hold a test |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 55 | name to execute (as in 'binman test testSections', for example) |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 56 | toolpath: List of paths to use for tools |
Simon Glass | 5666f9a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 57 | """ |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 58 | 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 Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 64 | import doctest |
| 65 | |
| 66 | result = unittest.TestResult() |
Simon Glass | 7330692 | 2020-04-17 18:09:01 -0600 | [diff] [blame] | 67 | test_name = args and args[0] or None |
Simon Glass | 8f52136 | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 68 | |
| 69 | # Run the entry tests first ,since these need to be the first to import the |
| 70 | # 'entry' module. |
Simon Glass | 7330692 | 2020-04-17 18:09:01 -0600 | [diff] [blame] | 71 | 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 Glass | d1ba61c | 2019-05-14 15:53:38 -0600 | [diff] [blame] | 76 | |
Simon Glass | 7330692 | 2020-04-17 18:09:01 -0600 | [diff] [blame] | 77 | return test_util.ReportResult('binman', test_name, result) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 78 | |
Simon Glass | 5d5930d | 2020-07-09 18:39:29 -0600 | [diff] [blame] | 79 | def RunTestCoverage(toolpath): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 80 | """Run the tests and check that we get 100% coverage""" |
Simon Glass | 220ff5f | 2020-08-05 13:27:46 -0600 | [diff] [blame] | 81 | glob_list = control.GetEntryModules(False) |
Tom Rini | c2a849d | 2018-07-06 10:27:14 -0600 | [diff] [blame] | 82 | all_set = set([os.path.splitext(os.path.basename(item))[0] |
| 83 | for item in glob_list if '_testing' not in item]) |
Simon Glass | 5d5930d | 2020-07-09 18:39:29 -0600 | [diff] [blame] | 84 | extra_args = '' |
| 85 | if toolpath: |
| 86 | for path in toolpath: |
| 87 | extra_args += ' --toolpath %s' % path |
Simon Glass | ebbb543 | 2020-04-17 18:08:58 -0600 | [diff] [blame] | 88 | test_util.RunTestCoverage('tools/binman/binman', None, |
| 89 | ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'], |
Simon Glass | 5d5930d | 2020-07-09 18:39:29 -0600 | [diff] [blame] | 90 | args.build_dir, all_set, extra_args or None) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 91 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 92 | def RunBinman(args): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 93 | """Main entry point to binman once arguments are parsed |
| 94 | |
| 95 | Args: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 96 | args: Command line arguments Namespace object |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 97 | """ |
| 98 | ret_code = 0 |
| 99 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 100 | if not args.debug: |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 101 | sys.tracebacklimit = 0 |
| 102 | |
Simon Glass | 901ec8f | 2020-07-09 18:39:30 -0600 | [diff] [blame] | 103 | # 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 Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 108 | if args.cmd == 'test': |
| 109 | if args.test_coverage: |
Simon Glass | 5d5930d | 2020-07-09 18:39:29 -0600 | [diff] [blame] | 110 | RunTestCoverage(args.toolpath) |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 111 | else: |
| 112 | ret_code = RunTests(args.debug, args.verbosity, args.processes, |
| 113 | args.test_preserve_dirs, args.tests, |
| 114 | args.toolpath) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 115 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 116 | elif args.cmd == 'entry-docs': |
Simon Glass | 220ff5f | 2020-08-05 13:27:46 -0600 | [diff] [blame] | 117 | control.WriteEntryDocs(control.GetEntryModules()) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 118 | |
| 119 | else: |
| 120 | try: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 121 | ret_code = control.Binman(args) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 122 | except Exception as e: |
Simon Glass | b5a8a94 | 2020-07-09 18:39:26 -0600 | [diff] [blame] | 123 | print('binman: %s' % e, file=sys.stderr) |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 124 | if args.debug: |
Simon Glass | 7cca27d | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 125 | print() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 126 | traceback.print_exc() |
| 127 | ret_code = 1 |
| 128 | return ret_code |
| 129 | |
| 130 | |
| 131 | if __name__ == "__main__": |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 132 | args = cmdline.ParseArgs(sys.argv[1:]) |
| 133 | |
| 134 | ret_code = RunBinman(args) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 135 | sys.exit(ret_code) |