blob: f5af5359f3c03efe5cacf0eab444b2a4c4b436c6 [file] [log] [blame]
Jörg Krausecd6d46e2017-03-06 21:07:11 +01001#!/usr/bin/env python2
Tom Rini10e47792018-05-06 17:58:06 -04002# SPDX-License-Identifier: GPL-2.0+
Simon Glass2574ef62016-11-25 20:15:51 -07003
4# Copyright (c) 2016 Google, Inc
5# Written by Simon Glass <sjg@chromium.org>
6#
Simon Glass2574ef62016-11-25 20:15:51 -07007# Creates binary images from input files controlled by a description
8#
9
10"""See README for more information"""
11
Simon Glass2c3cf452017-11-12 21:52:24 -070012import glob
Simon Glass2574ef62016-11-25 20:15:51 -070013import os
14import sys
15import traceback
16import unittest
17
18# Bring in the patman and dtoc libraries
19our_path = os.path.dirname(os.path.realpath(__file__))
Simon Glassdde3e712017-06-20 21:28:49 -060020for dirname in ['../patman', '../dtoc', '..']:
21 sys.path.insert(0, os.path.join(our_path, dirname))
Simon Glass2574ef62016-11-25 20:15:51 -070022
Simon Glass55901ff2017-05-27 07:38:22 -060023# Bring in the libfdt module
Masahiro Yamada47ae5392017-10-17 13:42:43 +090024sys.path.insert(0, 'scripts/dtc/pylibfdt')
Simon Glass5656ca22018-10-01 21:12:40 -060025sys.path.insert(0, os.path.join(our_path,
26 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
Simon Glass55901ff2017-05-27 07:38:22 -060027
Simon Glass2574ef62016-11-25 20:15:51 -070028import cmdline
29import command
30import control
Simon Glass132be852018-07-06 10:27:23 -060031import test_util
Simon Glass2574ef62016-11-25 20:15:51 -070032
Simon Glass5666f9a2018-06-01 09:38:18 -060033def 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 """
Simon Glass24ad3652017-11-13 18:54:54 -070041 import elf_test
Simon Glass2574ef62016-11-25 20:15:51 -070042 import entry_test
43 import fdt_test
Simon Glass076e63b2017-11-12 21:52:08 -070044 import ftest
Simon Glass4ca8e042017-11-13 18:55:01 -070045 import image_test
Simon Glass2574ef62016-11-25 20:15:51 -070046 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]]
Simon Glass075a45c2017-11-13 18:55:00 -070055 if debug:
56 sys.argv.append('-D')
Simon Glass8f521362017-11-12 21:52:21 -070057
58 # Run the entry tests first ,since these need to be the first to import the
59 # 'entry' module.
Simon Glass5666f9a2018-06-01 09:38:18 -060060 test_name = args and args[0] or None
Simon Glass02e0fc62018-07-06 10:27:18 -060061 for module in (entry_test.TestEntry, ftest.TestFunctional, fdt_test.TestFdt,
62 elf_test.TestElf, image_test.TestImage):
Simon Glass5666f9a2018-06-01 09:38:18 -060063 if test_name:
64 try:
Simon Glass02e0fc62018-07-06 10:27:18 -060065 suite = unittest.TestLoader().loadTestsFromName(test_name, module)
Simon Glass5666f9a2018-06-01 09:38:18 -060066 except AttributeError:
67 continue
68 else:
69 suite = unittest.TestLoader().loadTestsFromTestCase(module)
Simon Glass2574ef62016-11-25 20:15:51 -070070 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:
Simon Glass9ba021c2017-11-12 21:52:29 -070076 print err, result.failures
77 if result.errors or result.failures:
78 print 'binman tests FAILED'
79 return 1
80 return 0
Simon Glass2574ef62016-11-25 20:15:51 -070081
Simon Glass969616c2018-07-17 13:25:36 -060082def 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
Simon Glass2574ef62016-11-25 20:15:51 -070093def RunTestCoverage():
94 """Run the tests and check that we get 100% coverage"""
Simon Glass969616c2018-07-17 13:25:36 -060095 glob_list = GetEntryModules(False)
Tom Rinic2a849d2018-07-06 10:27:14 -060096 all_set = set([os.path.splitext(os.path.basename(item))[0]
97 for item in glob_list if '_testing' not in item])
Simon Glass132be852018-07-06 10:27:23 -060098 test_util.RunTestCoverage('tools/binman/binman.py', None,
99 ['*test*', '*binman.py', 'tools/patman/*', 'tools/dtoc/*'],
100 options.build_dir, all_set)
Simon Glass2574ef62016-11-25 20:15:51 -0700101
102def 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:
Simon Glass5666f9a2018-06-01 09:38:18 -0600118 ret_code = RunTests(options.debug, args[1:])
Simon Glass2574ef62016-11-25 20:15:51 -0700119
120 elif options.test_coverage:
121 RunTestCoverage()
122
Simon Glass969616c2018-07-17 13:25:36 -0600123 elif options.entry_docs:
124 control.WriteEntryDocs(GetEntryModules())
Simon Glass2574ef62016-11-25 20:15:51 -0700125
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
138if __name__ == "__main__":
139 (options, args) = cmdline.ParseArgs(sys.argv)
140 ret_code = RunBinman(options, args)
141 sys.exit(ret_code)