Simon Glass | 3885601 | 2019-10-31 07:43:05 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | bf7fd50 | 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 | bf7fd50 | 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 | 86679ce | 2019-07-08 13:18:36 -0600 | [diff] [blame] | 12 | from distutils.sysconfig import get_python_lib |
Simon Glass | a25ebed | 2017-11-12 21:52:24 -0700 | [diff] [blame] | 13 | import glob |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 14 | import os |
Simon Glass | 86679ce | 2019-07-08 13:18:36 -0600 | [diff] [blame] | 15 | import site |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 16 | import sys |
| 17 | import traceback |
| 18 | import unittest |
| 19 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 20 | # Bring in the patman and dtoc libraries (but don't override the first path |
| 21 | # in PYTHONPATH) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 22 | our_path = os.path.dirname(os.path.realpath(__file__)) |
Simon Glass | b4fa949 | 2020-04-17 18:09:05 -0600 | [diff] [blame] | 23 | sys.path.insert(2, os.path.join(our_path, '..')) |
| 24 | |
| 25 | from patman import test_util |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 26 | |
Simon Glass | b436020 | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 27 | # Bring in the libfdt module |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 28 | sys.path.insert(2, 'scripts/dtc/pylibfdt') |
Simon Glass | fdb3040 | 2020-07-09 18:39:28 -0600 | [diff] [blame] | 29 | sys.path.insert(2, os.path.join(our_path, '../../scripts/dtc/pylibfdt')) |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 30 | sys.path.insert(2, os.path.join(our_path, |
Simon Glass | ed59e00 | 2018-10-01 21:12:40 -0600 | [diff] [blame] | 31 | '../../build-sandbox_spl/scripts/dtc/pylibfdt')) |
Simon Glass | b436020 | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 32 | |
Simon Glass | 86679ce | 2019-07-08 13:18:36 -0600 | [diff] [blame] | 33 | # 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. |
| 37 | sys.path.append(get_python_lib()) |
| 38 | |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 39 | from binman import cmdline |
| 40 | from binman import control |
Simon Glass | bf77667 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 41 | from patman import test_util |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 42 | |
Simon Glass | 8acce60 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 43 | def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath): |
Simon Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 44 | """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 Glass | ee0c9a7 | 2019-07-08 13:18:48 -0600 | [diff] [blame] | 48 | verbosity: Verbosity level to use |
Simon Glass | d5164a7 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 49 | 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 Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 55 | args: List of positional args provided to binman. This can hold a test |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 56 | name to execute (as in 'binman test testSections', for example) |
Simon Glass | 8acce60 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 57 | toolpath: List of paths to use for tools |
Simon Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 58 | """ |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 59 | 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 Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 66 | import doctest |
| 67 | |
| 68 | result = unittest.TestResult() |
Simon Glass | ce0dc2e | 2020-04-17 18:09:01 -0600 | [diff] [blame] | 69 | test_name = args and args[0] or None |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 70 | |
| 71 | # Run the entry tests first ,since these need to be the first to import the |
| 72 | # 'entry' module. |
Simon Glass | ce0dc2e | 2020-04-17 18:09:01 -0600 | [diff] [blame] | 73 | 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 Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 78 | |
Simon Glass | ce0dc2e | 2020-04-17 18:09:01 -0600 | [diff] [blame] | 79 | return test_util.ReportResult('binman', test_name, result) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 80 | |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 81 | def 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 Glass | 32eb66d | 2020-07-09 18:39:29 -0600 | [diff] [blame^] | 92 | def RunTestCoverage(toolpath): |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 93 | """Run the tests and check that we get 100% coverage""" |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 94 | glob_list = GetEntryModules(False) |
Tom Rini | 16d836c | 2018-07-06 10:27:14 -0600 | [diff] [blame] | 95 | all_set = set([os.path.splitext(os.path.basename(item))[0] |
| 96 | for item in glob_list if '_testing' not in item]) |
Simon Glass | 32eb66d | 2020-07-09 18:39:29 -0600 | [diff] [blame^] | 97 | extra_args = '' |
| 98 | if toolpath: |
| 99 | for path in toolpath: |
| 100 | extra_args += ' --toolpath %s' % path |
Simon Glass | c07ab6e | 2020-04-17 18:08:58 -0600 | [diff] [blame] | 101 | test_util.RunTestCoverage('tools/binman/binman', None, |
| 102 | ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'], |
Simon Glass | 32eb66d | 2020-07-09 18:39:29 -0600 | [diff] [blame^] | 103 | args.build_dir, all_set, extra_args or None) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 104 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 105 | def RunBinman(args): |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 106 | """Main entry point to binman once arguments are parsed |
| 107 | |
| 108 | Args: |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 109 | args: Command line arguments Namespace object |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 110 | """ |
| 111 | ret_code = 0 |
| 112 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 113 | if not args.debug: |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 114 | sys.tracebacklimit = 0 |
| 115 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 116 | if args.cmd == 'test': |
| 117 | if args.test_coverage: |
Simon Glass | 32eb66d | 2020-07-09 18:39:29 -0600 | [diff] [blame^] | 118 | RunTestCoverage(args.toolpath) |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 119 | else: |
| 120 | ret_code = RunTests(args.debug, args.verbosity, args.processes, |
| 121 | args.test_preserve_dirs, args.tests, |
| 122 | args.toolpath) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 123 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 124 | elif args.cmd == 'entry-docs': |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 125 | control.WriteEntryDocs(GetEntryModules()) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 126 | |
| 127 | else: |
| 128 | try: |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 129 | ret_code = control.Binman(args) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 130 | except Exception as e: |
Simon Glass | 6c8e0bf | 2020-07-09 18:39:26 -0600 | [diff] [blame] | 131 | print('binman: %s' % e, file=sys.stderr) |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 132 | if args.debug: |
Simon Glass | 2ca8468 | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 133 | print() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 134 | traceback.print_exc() |
| 135 | ret_code = 1 |
| 136 | return ret_code |
| 137 | |
| 138 | |
| 139 | if __name__ == "__main__": |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 140 | args = cmdline.ParseArgs(sys.argv[1:]) |
| 141 | |
| 142 | ret_code = RunBinman(args) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 143 | sys.exit(ret_code) |