2 # SPDX-License-Identifier: GPL-2.0+
4 # Copyright (c) 2016 Google, Inc
5 # Written by Simon Glass <sjg@chromium.org>
7 # Creates binary images from input files controlled by a description
10 """See README for more information"""
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)
22 # Extract $(srctree) from Kbuild environment, or use relative paths below
23 srctree = os.environ.get('srctree', our2_path)
26 # Do not pollute source tree with cache files:
27 # https://stackoverflow.com/a/60024195/2511795
28 # https://bugs.python.org/issue33499
30 sys.pycache_prefix = os.path.relpath(our_path, srctree)
32 # Bring in the patman and dtoc libraries (but don't override the first path
34 sys.path.insert(2, our1_path)
36 from binman import bintool
37 from patman import test_util
39 # Bring in the libfdt module
40 sys.path.insert(2, 'scripts/dtc/pylibfdt')
41 sys.path.insert(2, os.path.join(srctree, 'scripts/dtc/pylibfdt'))
42 sys.path.insert(2, os.path.join(srctree, 'build-sandbox/scripts/dtc/pylibfdt'))
43 sys.path.insert(2, os.path.join(srctree, 'build-sandbox_spl/scripts/dtc/pylibfdt'))
45 from binman import cmdline
46 from binman import control
47 from patman import test_util
49 def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
50 """Run the functional tests and any embedded doctests
53 debug: True to enable debugging, which shows a full stack trace on error
54 verbosity: Verbosity level to use
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
60 processes: Number of processes to use to run tests (None=same as #CPUs)
61 args: List of positional args provided to binman. This can hold a test
62 name to execute (as in 'binman test testSections', for example)
63 toolpath: List of paths to use for tools
65 from binman import bintool_test
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
70 from binman import fip_util_test
71 from binman import ftest
72 from binman import image_test
75 test_name = args and args[0] or None
77 # Run the entry tests first ,since these need to be the first to import the
79 result = test_util.run_test_suites(
80 'binman', debug, verbosity, test_preserve_dirs, processes, test_name,
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])
86 return (0 if result.wasSuccessful() else 1)
88 def RunTestCoverage(toolpath):
89 """Run the tests and check that we get 100% coverage"""
90 glob_list = control.GetEntryModules(False)
91 all_set = set([os.path.splitext(os.path.basename(item))[0]
92 for item in glob_list if '_testing' not in item])
96 extra_args += ' --toolpath %s' % path
97 test_util.run_test_coverage('tools/binman/binman', None,
98 ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'],
99 args.build_dir, all_set, extra_args or None)
102 """Main entry point to binman once arguments are parsed
105 args: Command line arguments Namespace object
110 sys.tracebacklimit = 0
112 # Provide a default toolpath in the hope of finding a mkimage built from
114 if not args.toolpath:
115 args.toolpath = ['./tools', 'build-sandbox/tools']
117 if args.cmd == 'test':
118 if args.test_coverage:
119 RunTestCoverage(args.toolpath)
121 ret_code = RunTests(args.debug, args.verbosity, args.processes,
122 args.test_preserve_dirs, args.tests,
125 elif args.cmd == 'bintool-docs':
126 control.write_bintool_docs(bintool.Bintool.get_tool_list())
128 elif args.cmd == 'entry-docs':
129 control.WriteEntryDocs(control.GetEntryModules())
133 ret_code = control.Binman(args)
134 except Exception as e:
135 print('binman: %s' % e, file=sys.stderr)
138 traceback.print_exc()
143 if __name__ == "__main__":
144 args = cmdline.ParseArgs(sys.argv[1:])
146 ret_code = RunBinman(args)