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 | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 23 | for dirname in ['../patman', '../dtoc', '../concurrencytest', '..']: |
| 24 | sys.path.insert(2, os.path.realpath(os.path.join(our_path, dirname))) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 25 | |
Simon Glass | b436020 | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 26 | # Bring in the libfdt module |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 27 | sys.path.insert(2, 'scripts/dtc/pylibfdt') |
| 28 | sys.path.insert(2, os.path.join(our_path, |
Simon Glass | ed59e00 | 2018-10-01 21:12:40 -0600 | [diff] [blame] | 29 | '../../build-sandbox_spl/scripts/dtc/pylibfdt')) |
Simon Glass | b436020 | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 30 | |
Simon Glass | 86679ce | 2019-07-08 13:18:36 -0600 | [diff] [blame] | 31 | # When running under python-coverage on Ubuntu 16.04, the dist-packages |
| 32 | # directories are dropped from the python path. Add them in so that we can find |
| 33 | # the elffile module. We could use site.getsitepackages() here but unfortunately |
| 34 | # that is not available in a virtualenv. |
| 35 | sys.path.append(get_python_lib()) |
| 36 | |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 37 | from binman import cmdline |
| 38 | from binman import control |
Simon Glass | bf77667 | 2020-04-17 18:09:04 -0600 | [diff] [blame^] | 39 | from patman import test_util |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 40 | |
Simon Glass | 8acce60 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 41 | def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath): |
Simon Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 42 | """Run the functional tests and any embedded doctests |
| 43 | |
| 44 | Args: |
| 45 | 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] | 46 | verbosity: Verbosity level to use |
Simon Glass | d5164a7 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 47 | test_preserve_dirs: True to preserve the input directory used by tests |
| 48 | so that it can be examined afterwards (only useful for debugging |
| 49 | tests). If a single test is selected (in args[0]) it also preserves |
| 50 | the output directory for this test. Both directories are displayed |
| 51 | on the command line. |
| 52 | 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] | 53 | 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] | 54 | name to execute (as in 'binman test testSections', for example) |
Simon Glass | 8acce60 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 55 | toolpath: List of paths to use for tools |
Simon Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 56 | """ |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 57 | from binman import cbfs_util_test |
| 58 | from binman import elf_test |
| 59 | from binman import entry_test |
| 60 | from binman import fdt_test |
| 61 | from binman import ftest |
| 62 | from binman import image_test |
| 63 | from binman import test |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 64 | import doctest |
| 65 | |
| 66 | result = unittest.TestResult() |
Simon Glass | ce0dc2e | 2020-04-17 18:09:01 -0600 | [diff] [blame] | 67 | test_name = args and args[0] or None |
Simon Glass | 934cdcf | 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 | ce0dc2e | 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 | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 76 | |
Simon Glass | ce0dc2e | 2020-04-17 18:09:01 -0600 | [diff] [blame] | 77 | return test_util.ReportResult('binman', test_name, result) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 78 | |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 79 | def GetEntryModules(include_testing=True): |
| 80 | """Get a set of entry class implementations |
| 81 | |
| 82 | Returns: |
| 83 | Set of paths to entry class filenames |
| 84 | """ |
| 85 | glob_list = glob.glob(os.path.join(our_path, 'etype/*.py')) |
| 86 | return set([os.path.splitext(os.path.basename(item))[0] |
| 87 | for item in glob_list |
| 88 | if include_testing or '_testing' not in item]) |
| 89 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 90 | def RunTestCoverage(): |
| 91 | """Run the tests and check that we get 100% coverage""" |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 92 | glob_list = GetEntryModules(False) |
Tom Rini | 16d836c | 2018-07-06 10:27:14 -0600 | [diff] [blame] | 93 | all_set = set([os.path.splitext(os.path.basename(item))[0] |
| 94 | for item in glob_list if '_testing' not in item]) |
Simon Glass | c07ab6e | 2020-04-17 18:08:58 -0600 | [diff] [blame] | 95 | test_util.RunTestCoverage('tools/binman/binman', None, |
| 96 | ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'], |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 97 | args.build_dir, all_set) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 98 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 99 | def RunBinman(args): |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 100 | """Main entry point to binman once arguments are parsed |
| 101 | |
| 102 | Args: |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 103 | args: Command line arguments Namespace object |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 104 | """ |
| 105 | ret_code = 0 |
| 106 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 107 | if not args.debug: |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 108 | sys.tracebacklimit = 0 |
| 109 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 110 | if args.cmd == 'test': |
| 111 | if args.test_coverage: |
| 112 | RunTestCoverage() |
| 113 | else: |
| 114 | ret_code = RunTests(args.debug, args.verbosity, args.processes, |
| 115 | args.test_preserve_dirs, args.tests, |
| 116 | args.toolpath) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 117 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 118 | elif args.cmd == 'entry-docs': |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 119 | control.WriteEntryDocs(GetEntryModules()) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 120 | |
| 121 | else: |
| 122 | try: |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 123 | ret_code = control.Binman(args) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 124 | except Exception as e: |
Simon Glass | 2ca8468 | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 125 | print('binman: %s' % e) |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 126 | if args.debug: |
Simon Glass | 2ca8468 | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 127 | print() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 128 | traceback.print_exc() |
| 129 | ret_code = 1 |
| 130 | return ret_code |
| 131 | |
| 132 | |
| 133 | if __name__ == "__main__": |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 134 | args = cmdline.ParseArgs(sys.argv[1:]) |
| 135 | |
| 136 | ret_code = RunBinman(args) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 137 | sys.exit(ret_code) |