main.py 3.7 KB
Newer Older
S
Simon Glass 已提交
1
#!/usr/bin/env python3
2
# SPDX-License-Identifier: GPL-2.0+
3 4 5 6 7
#
# Copyright (C) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#

S
Simon Glass 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
"""Device tree to C tool

This tool converts a device tree binary file (.dtb) into two C files. The
indent is to allow a C program to access data from the device tree without
having to link against libfdt. By putting the data from the device tree into
C structures, normal C code can be used. This helps to reduce the size of the
compiled program.

Dtoc produces two output files:

   dt-structs.h  - contains struct definitions
   dt-platdata.c - contains data from the device tree using the struct
                      definitions, as well as U-Boot driver definitions.

This tool is used in U-Boot to provide device tree data to SPL without
increasing the code size of SPL. This supports the CONFIG_SPL_OF_PLATDATA
options. For more information about the use of this options and tool please
25
see doc/driver-model/of-plat.rst
S
Simon Glass 已提交
26 27
"""

28
from optparse import OptionParser
29 30
import os
import sys
S
Simon Glass 已提交
31
import unittest
32 33 34

# Bring in the patman libraries
our_path = os.path.dirname(os.path.realpath(__file__))
S
Simon Glass 已提交
35
sys.path.append(os.path.join(our_path, '..'))
36

37 38 39 40 41
# Bring in the libfdt module
sys.path.insert(0, 'scripts/dtc/pylibfdt')
sys.path.insert(0, os.path.join(our_path,
                '../../build-sandbox_spl/scripts/dtc/pylibfdt'))

S
Simon Glass 已提交
42 43
from dtoc import dtb_platdata
from patman import test_util
44

45 46 47 48
def run_tests(args):
    """Run all the test we have for dtoc

    Args:
S
Simon Glass 已提交
49 50
        args: List of positional args provided to dtoc. This can hold a test
            name to execute (as in 'dtoc -t test_empty_file', for example)
51
    """
S
Simon Glass 已提交
52
    import test_dtoc
53

S
Simon Glass 已提交
54 55
    result = unittest.TestResult()
    sys.argv = [sys.argv[0]]
56
    test_name = args and args[0] or None
S
Simon Glass 已提交
57
    for module in (test_dtoc.TestDtoc,):
58 59 60 61 62 63 64
        if test_name:
            try:
                suite = unittest.TestLoader().loadTestsFromName(test_name, module)
            except AttributeError:
                continue
        else:
            suite = unittest.TestLoader().loadTestsFromTestCase(module)
S
Simon Glass 已提交
65 66
        suite.run(result)

S
Simon Glass 已提交
67
    print(result)
S
Simon Glass 已提交
68
    for _, err in result.errors:
S
Simon Glass 已提交
69
        print(err)
S
Simon Glass 已提交
70
    for _, err in result.failures:
S
Simon Glass 已提交
71
        print(err)
72 73 74 75
    if result.errors or result.failures:
        print('dtoc tests FAILED')
        return 1
    return 0
S
Simon Glass 已提交
76

S
Simon Glass 已提交
77 78 79
def RunTestCoverage():
    """Run the tests and check that we get 100% coverage"""
    sys.argv = [sys.argv[0]]
S
Simon Glass 已提交
80
    test_util.RunTestCoverage('tools/dtoc/dtoc', '/main.py',
S
Simon Glass 已提交
81 82 83
            ['tools/patman/*.py', '*/fdt*', '*test*'], options.build_dir)


S
Simon Glass 已提交
84 85
if __name__ != '__main__':
    sys.exit(1)
86 87

parser = OptionParser()
S
Simon Glass 已提交
88 89
parser.add_option('-B', '--build-dir', type='string', default='b',
        help='Directory containing the build output')
90 91 92 93 94 95
parser.add_option('-d', '--dtb-file', action='store',
                  help='Specify the .dtb input file')
parser.add_option('--include-disabled', action='store_true',
                  help='Include disabled nodes')
parser.add_option('-o', '--output', action='store', default='-',
                  help='Select output filename')
S
Simon Glass 已提交
96 97
parser.add_option('-P', '--processes', type=int,
                  help='set number of processes to use for running tests')
S
Simon Glass 已提交
98 99
parser.add_option('-t', '--test', action='store_true', dest='test',
                  default=False, help='run tests')
S
Simon Glass 已提交
100 101
parser.add_option('-T', '--test-coverage', action='store_true',
                default=False, help='run tests and check for 100% coverage')
102 103
(options, args) = parser.parse_args()

S
Simon Glass 已提交
104 105
# Run our meagre tests
if options.test:
106 107
    ret_code = run_tests(args)
    sys.exit(ret_code)
S
Simon Glass 已提交
108

S
Simon Glass 已提交
109 110 111
elif options.test_coverage:
    RunTestCoverage()

S
Simon Glass 已提交
112 113 114
else:
    dtb_platdata.run_steps(args, options.dtb_file, options.include_disabled,
                           options.output)