binman: Add a default path to libfdt.py
[oweals/u-boot.git] / tools / binman / binman.py
1 #!/usr/bin/env python2
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 import glob
13 import os
14 import sys
15 import traceback
16 import unittest
17
18 # Bring in the patman and dtoc libraries
19 our_path = os.path.dirname(os.path.realpath(__file__))
20 for dirname in ['../patman', '../dtoc', '..']:
21     sys.path.insert(0, os.path.join(our_path, dirname))
22
23 # Bring in the libfdt module
24 sys.path.insert(0, 'scripts/dtc/pylibfdt')
25 sys.path.insert(0, os.path.join(our_path,
26                 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
27
28 import cmdline
29 import command
30 import control
31 import test_util
32
33 def RunTests(debug, args):
34     """Run the functional tests and any embedded doctests
35
36     Args:
37         debug: True to enable debugging, which shows a full stack trace on error
38         args: List of positional args provided to binman. This can hold a test
39             name to execute (as in 'binman -t testSections', for example)
40     """
41     import elf_test
42     import entry_test
43     import fdt_test
44     import ftest
45     import image_test
46     import test
47     import doctest
48
49     result = unittest.TestResult()
50     for module in []:
51         suite = doctest.DocTestSuite(module)
52         suite.run(result)
53
54     sys.argv = [sys.argv[0]]
55     if debug:
56         sys.argv.append('-D')
57
58     # Run the entry tests first ,since these need to be the first to import the
59     # 'entry' module.
60     test_name = args and args[0] or None
61     for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
62                    elf_test.TestElf, image_test.TestImage):
63         if test_name:
64             try:
65                 suite = unittest.TestLoader().loadTestsFromName(test_name, module)
66             except AttributeError:
67                 continue
68         else:
69             suite = unittest.TestLoader().loadTestsFromTestCase(module)
70         suite.run(result)
71
72     print result
73     for test, err in result.errors:
74         print test.id(), err
75     for test, err in result.failures:
76         print err, result.failures
77     if result.errors or result.failures:
78       print 'binman tests FAILED'
79       return 1
80     return 0
81
82 def GetEntryModules(include_testing=True):
83     """Get a set of entry class implementations
84
85     Returns:
86         Set of paths to entry class filenames
87     """
88     glob_list = glob.glob(os.path.join(our_path, 'etype/*.py'))
89     return set([os.path.splitext(os.path.basename(item))[0]
90                 for item in glob_list
91                 if include_testing or '_testing' not in item])
92
93 def RunTestCoverage():
94     """Run the tests and check that we get 100% coverage"""
95     glob_list = GetEntryModules(False)
96     all_set = set([os.path.splitext(os.path.basename(item))[0]
97                    for item in glob_list if '_testing' not in item])
98     test_util.RunTestCoverage('tools/binman/binman.py', None,
99             ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
100             options.build_dir, all_set)
101
102 def RunBinman(options, args):
103     """Main entry point to binman once arguments are parsed
104
105     Args:
106         options: Command-line options
107         args: Non-option arguments
108     """
109     ret_code = 0
110
111     # For testing: This enables full exception traces.
112     #options.debug = True
113
114     if not options.debug:
115         sys.tracebacklimit = 0
116
117     if options.test:
118         ret_code = RunTests(options.debug, args[1:])
119
120     elif options.test_coverage:
121         RunTestCoverage()
122
123     elif options.entry_docs:
124         control.WriteEntryDocs(GetEntryModules())
125
126     else:
127         try:
128             ret_code = control.Binman(options, args)
129         except Exception as e:
130             print 'binman: %s' % e
131             if options.debug:
132                 print
133                 traceback.print_exc()
134             ret_code = 1
135     return ret_code
136
137
138 if __name__ == "__main__":
139     (options, args) = cmdline.ParseArgs(sys.argv)
140     ret_code = RunBinman(options, args)
141     sys.exit(ret_code)