]> git.ipfire.org Git - thirdparty/u-boot.git/blame - tools/dtoc/dtoc.py
patman: Drop references to __future__
[thirdparty/u-boot.git] / tools / dtoc / dtoc.py
CommitLineData
3c19dc8b 1#!/usr/bin/env python3
83d290c5 2# SPDX-License-Identifier: GPL-2.0+
69f2ed77
SG
3#
4# Copyright (C) 2016 Google, Inc
5# Written by Simon Glass <sjg@chromium.org>
6#
69f2ed77 7
14f5acfc
SG
8"""Device tree to C tool
9
10This tool converts a device tree binary file (.dtb) into two C files. The
11indent is to allow a C program to access data from the device tree without
12having to link against libfdt. By putting the data from the device tree into
13C structures, normal C code can be used. This helps to reduce the size of the
14compiled program.
15
16Dtoc produces two output files:
17
18 dt-structs.h - contains struct definitions
19 dt-platdata.c - contains data from the device tree using the struct
20 definitions, as well as U-Boot driver definitions.
21
22This tool is used in U-Boot to provide device tree data to SPL without
23increasing the code size of SPL. This supports the CONFIG_SPL_OF_PLATDATA
24options. For more information about the use of this options and tool please
2799a69e 25see doc/driver-model/of-plat.rst
14f5acfc
SG
26"""
27
7581c01a 28from optparse import OptionParser
69f2ed77
SG
29import os
30import sys
c0791928 31import unittest
69f2ed77 32
69f2ed77
SG
33# Bring in the patman libraries
34our_path = os.path.dirname(os.path.realpath(__file__))
35sys.path.append(os.path.join(our_path, '../patman'))
36
ed59e005
SG
37# Bring in the libfdt module
38sys.path.insert(0, 'scripts/dtc/pylibfdt')
39sys.path.insert(0, os.path.join(our_path,
40 '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
41
7581c01a 42import dtb_platdata
ba765217 43import test_util
69f2ed77 44
3def0cf2
SG
45def run_tests(args):
46 """Run all the test we have for dtoc
47
48 Args:
dfe5f5b9
SG
49 args: List of positional args provided to dtoc. This can hold a test
50 name to execute (as in 'dtoc -t test_empty_file', for example)
3def0cf2 51 """
c0791928 52 import test_dtoc
69f2ed77 53
c0791928
SG
54 result = unittest.TestResult()
55 sys.argv = [sys.argv[0]]
3def0cf2 56 test_name = args and args[0] or None
c0791928 57 for module in (test_dtoc.TestDtoc,):
3def0cf2
SG
58 if test_name:
59 try:
60 suite = unittest.TestLoader().loadTestsFromName(test_name, module)
61 except AttributeError:
62 continue
63 else:
64 suite = unittest.TestLoader().loadTestsFromTestCase(module)
c0791928
SG
65 suite.run(result)
66
90a8132f 67 print(result)
c0791928 68 for _, err in result.errors:
90a8132f 69 print(err)
c0791928 70 for _, err in result.failures:
90a8132f 71 print(err)
1000096b
SG
72 if result.errors or result.failures:
73 print('dtoc tests FAILED')
74 return 1
75 return 0
c0791928 76
ba765217
SG
77def RunTestCoverage():
78 """Run the tests and check that we get 100% coverage"""
79 sys.argv = [sys.argv[0]]
80 test_util.RunTestCoverage('tools/dtoc/dtoc.py', '/dtoc.py',
81 ['tools/patman/*.py', '*/fdt*', '*test*'], options.build_dir)
82
83
c0791928
SG
84if __name__ != '__main__':
85 sys.exit(1)
69f2ed77
SG
86
87parser = OptionParser()
ba765217
SG
88parser.add_option('-B', '--build-dir', type='string', default='b',
89 help='Directory containing the build output')
69f2ed77
SG
90parser.add_option('-d', '--dtb-file', action='store',
91 help='Specify the .dtb input file')
92parser.add_option('--include-disabled', action='store_true',
93 help='Include disabled nodes')
94parser.add_option('-o', '--output', action='store', default='-',
95 help='Select output filename')
11ae93ee
SG
96parser.add_option('-P', '--processes', type=int,
97 help='set number of processes to use for running tests')
c0791928
SG
98parser.add_option('-t', '--test', action='store_true', dest='test',
99 default=False, help='run tests')
ba765217
SG
100parser.add_option('-T', '--test-coverage', action='store_true',
101 default=False, help='run tests and check for 100% coverage')
69f2ed77
SG
102(options, args) = parser.parse_args()
103
c0791928
SG
104# Run our meagre tests
105if options.test:
1000096b
SG
106 ret_code = run_tests(args)
107 sys.exit(ret_code)
c0791928 108
ba765217
SG
109elif options.test_coverage:
110 RunTestCoverage()
111
c0791928
SG
112else:
113 dtb_platdata.run_steps(args, options.dtb_file, options.include_disabled,
114 options.output)