aboutsummaryrefslogtreecommitdiff
path: root/tools/dtoc/main.py
blob: 6c91450410eee6f87818fa45a86d6e6b22724912 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0+
#
# Copyright (C) 2016 Google, Inc
# Written by Simon Glass <sjg@chromium.org>
#

"""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 several output files - see OUTPUT_FILES in dtb_platdata.py

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
see doc/driver-model/of-plat.rst
"""

from argparse import ArgumentParser
import os
import pathlib
import sys

# Bring in the patman libraries
our_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(our_path, '..'))

# 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'))

from dtoc import dtb_platdata
from u_boot_pylib import test_util

DTOC_DIR = pathlib.Path(__file__).parent
HAVE_TESTS = (DTOC_DIR / 'test_dtoc.py').exists()

def run_tests(processes, args):
    """Run all the test we have for dtoc

    Args:
        processes: Number of processes to use to run tests (None=same as #CPUs)
        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)
    """
    from dtoc import test_src_scan
    from dtoc import test_dtoc

    sys.argv = [sys.argv[0]]
    test_name = args.files and args.files[0] or None

    test_dtoc.setup()

    result = test_util.run_test_suites(
        toolname='dtoc', debug=True, verbosity=1, test_preserve_dirs=False,
        processes=processes, test_name=test_name, toolpath=[],
        class_and_module_list=[test_dtoc.TestDtoc,test_src_scan.TestSrcScan])

    return (0 if result.wasSuccessful() else 1)


def RunTestCoverage(build_dir):
    """Run the tests and check that we get 100% coverage"""
    sys.argv = [sys.argv[0]]
    test_util.run_test_coverage('tools/dtoc/dtoc', '/main.py',
            ['tools/patman/*.py', 'tools/u_boot_pylib/*','*/fdt*', '*test*'],
            build_dir)


def run_dtoc():
    epilog = 'Generate C code from devicetree files. See of-plat.rst for details'

    parser = ArgumentParser(epilog=epilog)
    parser.add_argument('-B', '--build-dir', type=str, default='b',
            help='Directory containing the build output')
    parser.add_argument('-c', '--c-output-dir', action='store',
                      help='Select output directory for C files')
    parser.add_argument(
        '-C', '--h-output-dir', action='store',
        help='Select output directory for H files (defaults to --c-output-di)')
    parser.add_argument('-d', '--dtb-file', action='store',
                      help='Specify the .dtb input file')
    parser.add_argument(
        '-i', '--instantiate', action='store_true', default=False,
        help='Instantiate devices to avoid needing device_bind()')
    parser.add_argument('--include-disabled', action='store_true',
                      help='Include disabled nodes')
    parser.add_argument('-o', '--output', action='store',
                      help='Select output filename')
    parser.add_argument(
        '-p', '--phase', type=str,
        help='set phase of U-Boot this invocation is for (spl/tpl)')
    parser.add_argument('-P', '--processes', type=int,
                      help='set number of processes to use for running tests')
    if HAVE_TESTS:
        parser.add_argument('-t', '--test', action='store_true', dest='test',
                            default=False, help='run tests')
        parser.add_argument(
            '-T', '--test-coverage', action='store_true',
            default=False, help='run tests and check for 100%% coverage')
    parser.add_argument('files', nargs='*')
    args = parser.parse_args()

    # Run our meagre tests
    if HAVE_TESTS and args.test:
        ret_code = run_tests(args.processes, args)
        sys.exit(ret_code)

    elif HAVE_TESTS and args.test_coverage:
        RunTestCoverage(args.build_dir)

    else:
        dtb_platdata.run_steps(args.files, args.dtb_file, args.include_disabled,
                               args.output,
                               [args.c_output_dir, args.h_output_dir],
                               args.phase, instantiate=args.instantiate)


if __name__ == '__main__':
    run_dtoc()