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 | |
| 12 | import os |
Simon Glass | 86679ce | 2019-07-08 13:18:36 -0600 | [diff] [blame] | 13 | import site |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 14 | import sys |
| 15 | import traceback |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 16 | |
Andy Shevchenko | 022f6b0 | 2021-12-06 14:44:12 +0300 | [diff] [blame] | 17 | # Get the absolute path to this file at run-time |
| 18 | our_path = os.path.dirname(os.path.realpath(__file__)) |
| 19 | our1_path = os.path.dirname(our_path) |
| 20 | our2_path = os.path.dirname(our1_path) |
| 21 | |
Andy Shevchenko | 33f27f4 | 2021-12-06 14:44:13 +0300 | [diff] [blame] | 22 | # Extract $(srctree) from Kbuild environment, or use relative paths below |
| 23 | srctree = os.environ.get('srctree', our2_path) |
| 24 | |
Andy Shevchenko | 022f6b0 | 2021-12-06 14:44:12 +0300 | [diff] [blame] | 25 | # |
| 26 | # Do not pollute source tree with cache files: |
| 27 | # https://stackoverflow.com/a/60024195/2511795 |
| 28 | # https://bugs.python.org/issue33499 |
| 29 | # |
Andy Shevchenko | 33f27f4 | 2021-12-06 14:44:13 +0300 | [diff] [blame] | 30 | sys.pycache_prefix = os.path.relpath(our_path, srctree) |
Andy Shevchenko | 022f6b0 | 2021-12-06 14:44:12 +0300 | [diff] [blame] | 31 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 32 | # Bring in the patman and dtoc libraries (but don't override the first path |
| 33 | # in PYTHONPATH) |
Andy Shevchenko | 33f27f4 | 2021-12-06 14:44:13 +0300 | [diff] [blame] | 34 | sys.path.insert(2, our1_path) |
Simon Glass | b4fa949 | 2020-04-17 18:09:05 -0600 | [diff] [blame] | 35 | |
Simon Glass | bc57064 | 2022-01-09 20:14:11 -0700 | [diff] [blame] | 36 | from binman import bintool |
Simon Glass | b4fa949 | 2020-04-17 18:09:05 -0600 | [diff] [blame] | 37 | from patman import test_util |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 38 | |
Simon Glass | b436020 | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 39 | # Bring in the libfdt module |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 40 | sys.path.insert(2, 'scripts/dtc/pylibfdt') |
Andy Shevchenko | 33f27f4 | 2021-12-06 14:44:13 +0300 | [diff] [blame] | 41 | sys.path.insert(2, os.path.join(srctree, 'scripts/dtc/pylibfdt')) |
Philippe Reynes | 7aa288e | 2022-01-27 15:03:13 +0100 | [diff] [blame] | 42 | sys.path.insert(2, os.path.join(srctree, 'build-sandbox/scripts/dtc/pylibfdt')) |
Andy Shevchenko | 33f27f4 | 2021-12-06 14:44:13 +0300 | [diff] [blame] | 43 | sys.path.insert(2, os.path.join(srctree, 'build-sandbox_spl/scripts/dtc/pylibfdt')) |
Simon Glass | b436020 | 2017-05-27 07:38:22 -0600 | [diff] [blame] | 44 | |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 45 | from binman import cmdline |
| 46 | from binman import control |
Simon Glass | bf77667 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 47 | from patman import test_util |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 48 | |
Simon Glass | 8acce60 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 49 | def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath): |
Simon Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 50 | """Run the functional tests and any embedded doctests |
| 51 | |
| 52 | Args: |
| 53 | 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] | 54 | verbosity: Verbosity level to use |
Simon Glass | d5164a7 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 55 | test_preserve_dirs: True to preserve the input directory used by tests |
| 56 | so that it can be examined afterwards (only useful for debugging |
| 57 | tests). If a single test is selected (in args[0]) it also preserves |
| 58 | the output directory for this test. Both directories are displayed |
| 59 | on the command line. |
| 60 | 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] | 61 | 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] | 62 | name to execute (as in 'binman test testSections', for example) |
Simon Glass | 8acce60 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 63 | toolpath: List of paths to use for tools |
Simon Glass | 084059a | 2018-06-01 09:38:18 -0600 | [diff] [blame] | 64 | """ |
Simon Glass | 56ee85e | 2022-01-09 20:13:57 -0700 | [diff] [blame] | 65 | from binman import bintool_test |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 66 | from binman import cbfs_util_test |
| 67 | from binman import elf_test |
| 68 | from binman import entry_test |
| 69 | from binman import fdt_test |
Simon Glass | ed16b12 | 2021-11-23 21:08:58 -0700 | [diff] [blame] | 70 | from binman import fip_util_test |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 71 | from binman import ftest |
| 72 | from binman import image_test |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 73 | import doctest |
| 74 | |
Simon Glass | ce0dc2e | 2020-04-17 18:09:01 -0600 | [diff] [blame] | 75 | test_name = args and args[0] or None |
Simon Glass | 934cdcf | 2017-11-12 21:52:21 -0700 | [diff] [blame] | 76 | |
| 77 | # Run the entry tests first ,since these need to be the first to import the |
| 78 | # 'entry' module. |
Alper Nebi Yasak | d8318fe | 2022-04-02 20:06:06 +0300 | [diff] [blame] | 79 | result = test_util.run_test_suites( |
| 80 | 'binman', debug, verbosity, test_preserve_dirs, processes, test_name, |
Simon Glass | ce0dc2e | 2020-04-17 18:09:01 -0600 | [diff] [blame] | 81 | toolpath, |
Simon Glass | 56ee85e | 2022-01-09 20:13:57 -0700 | [diff] [blame] | 82 | [bintool_test.TestBintool, entry_test.TestEntry, ftest.TestFunctional, |
| 83 | fdt_test.TestFdt, elf_test.TestElf, image_test.TestImage, |
| 84 | cbfs_util_test.TestCbfs, fip_util_test.TestFip]) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 85 | |
Alper Nebi Yasak | d8318fe | 2022-04-02 20:06:06 +0300 | [diff] [blame] | 86 | return (0 if result.wasSuccessful() else 1) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 87 | |
Simon Glass | 32eb66d | 2020-07-09 18:39:29 -0600 | [diff] [blame] | 88 | def RunTestCoverage(toolpath): |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 89 | """Run the tests and check that we get 100% coverage""" |
Simon Glass | 87d4332 | 2020-08-05 13:27:46 -0600 | [diff] [blame] | 90 | glob_list = control.GetEntryModules(False) |
Tom Rini | 16d836c | 2018-07-06 10:27:14 -0600 | [diff] [blame] | 91 | all_set = set([os.path.splitext(os.path.basename(item))[0] |
| 92 | for item in glob_list if '_testing' not in item]) |
Simon Glass | 32eb66d | 2020-07-09 18:39:29 -0600 | [diff] [blame] | 93 | extra_args = '' |
| 94 | if toolpath: |
| 95 | for path in toolpath: |
| 96 | extra_args += ' --toolpath %s' % path |
Simon Glass | 5e2ab40 | 2022-01-29 14:14:14 -0700 | [diff] [blame] | 97 | test_util.run_test_coverage('tools/binman/binman', None, |
Simon Glass | c07ab6e | 2020-04-17 18:08:58 -0600 | [diff] [blame] | 98 | ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'], |
Simon Glass | 32eb66d | 2020-07-09 18:39:29 -0600 | [diff] [blame] | 99 | args.build_dir, all_set, extra_args or None) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 100 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 101 | def RunBinman(args): |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 102 | """Main entry point to binman once arguments are parsed |
| 103 | |
| 104 | Args: |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 105 | args: Command line arguments Namespace object |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 106 | """ |
| 107 | ret_code = 0 |
| 108 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 109 | if not args.debug: |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 110 | sys.tracebacklimit = 0 |
| 111 | |
Simon Glass | b5287c4 | 2020-07-09 18:39:30 -0600 | [diff] [blame] | 112 | # Provide a default toolpath in the hope of finding a mkimage built from |
| 113 | # current source |
| 114 | if not args.toolpath: |
| 115 | args.toolpath = ['./tools', 'build-sandbox/tools'] |
| 116 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 117 | if args.cmd == 'test': |
| 118 | if args.test_coverage: |
Simon Glass | 32eb66d | 2020-07-09 18:39:29 -0600 | [diff] [blame] | 119 | RunTestCoverage(args.toolpath) |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 120 | else: |
| 121 | ret_code = RunTests(args.debug, args.verbosity, args.processes, |
| 122 | args.test_preserve_dirs, args.tests, |
| 123 | args.toolpath) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 124 | |
Simon Glass | bc57064 | 2022-01-09 20:14:11 -0700 | [diff] [blame] | 125 | elif args.cmd == 'bintool-docs': |
| 126 | control.write_bintool_docs(bintool.Bintool.get_tool_list()) |
| 127 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 128 | elif args.cmd == 'entry-docs': |
Simon Glass | 87d4332 | 2020-08-05 13:27:46 -0600 | [diff] [blame] | 129 | control.WriteEntryDocs(control.GetEntryModules()) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 130 | |
| 131 | else: |
| 132 | try: |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 133 | ret_code = control.Binman(args) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 134 | except Exception as e: |
Simon Glass | 6c8e0bf | 2020-07-09 18:39:26 -0600 | [diff] [blame] | 135 | print('binman: %s' % e, file=sys.stderr) |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 136 | if args.debug: |
Simon Glass | 2ca8468 | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 137 | print() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 138 | traceback.print_exc() |
| 139 | ret_code = 1 |
| 140 | return ret_code |
| 141 | |
| 142 | |
| 143 | if __name__ == "__main__": |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 144 | args = cmdline.ParseArgs(sys.argv[1:]) |
| 145 | |
| 146 | ret_code = RunBinman(args) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 147 | sys.exit(ret_code) |