1#!/usr/bin/env python3
2#
3# Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7#
8# Little script to build a header file including every other header file in the SDK!
9# (also checks we don't have "conflicting" header-filenames)
10# Edit the IGNORE_DIRS variable to filter out which directories get looked in.
11#
12# Usage:
13#
14# tools/build_all_headers.py <root of source tree> <output file>
15
16
17import os
18import sys
19
20IGNORE_DIRS = set(['host', 'boards'])
21IGNORE_DIRS.add('common/boot_picoboot')
22IGNORE_DIRS.add('common/boot_uf2')
23IGNORE_DIRS.add('common/pico_usb_reset_interface')
24IGNORE_DIRS.add('rp2_common/cmsis')
25IGNORE_DIRS.add('rp2_common/pico_async_context')
26IGNORE_DIRS.add('rp2_common/pico_btstack')
27IGNORE_DIRS.add('rp2_common/pico_cyw43_arch')
28IGNORE_DIRS.add('rp2_common/pico_cyw43_driver')
29IGNORE_DIRS.add('rp2_common/pico_lwip')
30IGNORE_DIRS.add('rp2_common/pico_stdio_semihosting')
31IGNORE_DIRS.add('rp2_common/pico_stdio_usb')
32
33if len(sys.argv) != 3:
34    print("Usage: {} top_dir output_header".format(os.path.basename(sys.argv[0])))
35    sys.exit(1)
36
37top_dir = os.path.join(sys.argv[1], 'src')
38output_header = sys.argv[2]
39
40if not os.path.isdir(top_dir):
41    print("{} doesn't exist!".format(top_dir))
42    sys.exit(1)
43
44include_dirs = set()
45for root, dirs, files in os.walk(top_dir):
46    prune_dirs = []
47    for d in dirs:
48        if os.path.relpath(os.path.join(root, d), top_dir) in IGNORE_DIRS:
49            prune_dirs.append(d)
50    for d in prune_dirs:
51        dirs.remove(d)
52
53    if 'include' in dirs:
54        include_dirs.add(os.path.join(root, 'include'))
55        dirs.remove('include')
56
57include_files = list()
58include_locations = dict()
59for d in sorted(include_dirs):
60    for root, dirs, files in os.walk(d):
61        for f in sorted(files):
62            if f.endswith('.h'):
63                include_file = os.path.relpath(os.path.join(root, f), d)
64                include_path = os.path.relpath(d, top_dir)
65                if include_file in include_files:
66                    raise Exception("Duplicate include file '{}' (found in both {} and {})".format(include_file, include_locations[include_file], include_path))
67                include_files.append(include_file)
68                include_locations[include_file] = include_path
69
70with open(output_header, 'w') as fh:
71    fh.write('''/*
72 * Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
73 *
74 * SPDX-License-Identifier: BSD-3-Clause
75 */
76
77// This file is autogenerated, do not edit by hand
78
79''')
80    last_location = ''
81    for f in include_files:
82        if include_locations[f] != last_location:
83            fh.write('\n// {}\n'.format(include_locations[f]))
84        fh.write('#include "{}"\n'.format(f))
85        last_location = include_locations[f]
86    fh.write('\n')
87
88