rockchip: rk3399: Add Nanopi M4 2GB board support
[platform/kernel/u-boot.git] / tools / binman / main.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0+
3
4 # Copyright (c) 2016 Google, Inc
5 # Written by Simon Glass <sjg@chromium.org>
6 #
7 # Creates binary images from input files controlled by a description
8 #
9
10 """See README for more information"""
11
12 from distutils.sysconfig import get_python_lib
13 import glob
14 import os
15 import site
16 import sys
17 import traceback
18 import unittest
19
20 # Bring in the patman and dtoc libraries (but don't override the first path
21 # in PYTHONPATH)
22 our_path = os.path.dirname(os.path.realpath(__file__))
23 sys.path.insert(2, os.path.join(our_path, '..'))
24
25 from patman import test_util
26
27 # Bring in the libfdt module
28 sys.path.insert(2, 'scripts/dtc/pylibfdt')
29 sys.path.insert(2, os.path.join(our_path,
30                 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
31
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
38 from binman import cmdline
39 from binman import control
40 from patman import test_util
41
42 def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
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
47         verbosity: Verbosity level to use
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)
54         args: List of positional args provided to binman. This can hold a test
55             name to execute (as in 'binman test testSections', for example)
56         toolpath: List of paths to use for tools
57     """
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
64     from binman import test
65     import doctest
66
67     result = unittest.TestResult()
68     test_name = args and args[0] or None
69
70     # Run the entry tests first ,since these need to be the first to import the
71     # 'entry' module.
72     test_util.RunTestSuites(
73         result, debug, verbosity, test_preserve_dirs, processes, test_name,
74         toolpath,
75         [entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
76          elf_test.TestElf, image_test.TestImage, cbfs_util_test.TestCbfs])
77
78     return test_util.ReportResult('binman', test_name, result)
79
80 def GetEntryModules(include_testing=True):
81     """Get a set of entry class implementations
82
83     Returns:
84         Set of paths to entry class filenames
85     """
86     glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
87     return set([os.path.splitext(os.path.basename(item))[0]
88                 for item in glob_list
89                 if include_testing or '_testing' not in item])
90
91 def RunTestCoverage():
92     """Run the tests and check that we get 100% coverage"""
93     glob_list = GetEntryModules(False)
94     all_set = set([os.path.splitext(os.path.basename(item))[0]
95                    for item in glob_list if '_testing' not in item])
96     test_util.RunTestCoverage('tools/binman/binman', None,
97             ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*'],
98             args.build_dir, all_set)
99
100 def RunBinman(args):
101     """Main entry point to binman once arguments are parsed
102
103     Args:
104         args: Command line arguments Namespace object
105     """
106     ret_code = 0
107
108     if not args.debug:
109         sys.tracebacklimit = 0
110
111     if args.cmd == 'test':
112         if args.test_coverage:
113             RunTestCoverage()
114         else:
115             ret_code = RunTests(args.debug, args.verbosity, args.processes,
116                                 args.test_preserve_dirs, args.tests,
117                                 args.toolpath)
118
119     elif args.cmd == 'entry-docs':
120         control.WriteEntryDocs(GetEntryModules())
121
122     else:
123         try:
124             ret_code = control.Binman(args)
125         except Exception as e:
126             print('binman: %s' % e)
127             if args.debug:
128                 print()
129                 traceback.print_exc()
130             ret_code = 1
131     return ret_code
132
133
134 if __name__ == "__main__":
135     args = cmdline.ParseArgs(sys.argv[1:])
136
137     ret_code = RunBinman(args)
138     sys.exit(ret_code)