summaryrefslogtreecommitdiff
path: root/scripts/list-required-firmware
blob: 64280e03f5dd8bb6632c2dc14d79b3404c8b346e (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
#!/usr/bin/env python3
# Copyright (C) 2020 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import re
import os
import sys
import glob


SRC_DIR = sys.argv[1]
KERNEL_CONFIG = sys.argv[2]

def load_config(path):
    with open(KERNEL_CONFIG, 'r') as f:
        config = f.read()
    targets = re.findall(r'(.*)=(?:y|m)', config)
    return targets

def find_subdirs(config, path):
    try:
        with open(os.path.join(path, 'Makefile'), 'r') as f:
            makefile = f.read()
    except OSError:
        # No Makefile
        return []

    dir_stmts = re.findall(r'obj-\$\((.*)\)\s+\+=\s+(.*)(?:\n|$)', makefile)
    subdirs = []
    for ds in dir_stmts:
        print("Processing make targets from {0} ({1})".format(ds[1], ds[0]), file=sys.stderr)
        if ds[0] in config:
            dirname = os.path.dirname(ds[1])
            if dirname:
                subdirs.append(dirname)
        else:
            print("{0} is disabled in the config, ignoring {1}".format(ds[0], ds[1]), file=sys.stderr)

    return subdirs

def find_firmware(file):
    with open(file, 'r') as f:
        source = f.read()
    fws = re.findall(r'MODULE_FIRMWARE\((.*)\)', source)
    return fws

def walk_dir(config, path):
    subdirs = find_subdirs(config, path)

    print("Looking for C files in {0}".format(path), file=sys.stderr)
    c_files = glob.glob("{0}/*.c".format(path))
    for cf in c_files:
        fws = find_firmware(cf)
        if fws:
            print("Referenced firmware: {0}".format(fws))

    for d in subdirs:
        d = os.path.join(path, d)
        walk_dir(config, d)

if __name__ == '__main__':
    config = load_config(KERNEL_CONFIG)
    walk_dir(config, SRC_DIR)