hc
2024-08-16 a24a44ff9ca902811b99aa9663d697cf452e08ef
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
 
import os
import re
import sys
 
def common_config(configs):
    allconfigs = {}
 
    for config in configs:
        if  not config.endswith("config"):
            continue
 
        allconfigs[config] = set()
 
        for line in open(config):
            m = re.match("#*\s*(BR2_\w+)[\s=](.*)$", line)
            if not m:
                continue
            option, value = m.groups()
            allconfigs[config].add((option, value))
 
    common = allconfigs.values()[0].copy()
    for config in allconfigs.keys():
        common &= allconfigs[config]
    for config in allconfigs.keys():
        allconfigs[config] -= common
 
    print(common)
 
def load_base(base_cfgs, cfg):
    base_cfgs[cfg] = set()
 
    for line in open(cfg):
        n = re.match('\s*#include\s*"(\w+\.\w+)"\s*$', line)
        if n:
            name = n.group(1)
            path = os.path.dirname(os.path.realpath(cfg))
            if not path.endswith("rockchip"):
                path += "/rockchip"
            name = path + '/' + name
            load_base(base_cfgs, name)
            continue
 
        m = re.match("#*\s*(BR2_\w+)[\s=](.*)$", line)
        if m:
            option, value = m.groups()
            base_cfgs[cfg].add((option, value))
 
 
def split_config(base_cfgs, config, output):
    load_base(base_cfgs, config)
    base_cfgs.pop(config)
 
    with open(output, "w+") as f:
        for line in open(config):
            m = re.match("#*\s*(BR2_\w+)[\s=](.*)$", line)
            if not m:
                f.write(line)
                continue
 
            option, value = m.groups()
            temp = set()
            temp.add((option, value))
 
            if option == "BR2_ROOTFS_OVERLAY" and value != "is not set":
                values = value.strip('"').split(' ')
 
                for cfg in base_cfgs.keys():
                    for item in base_cfgs[cfg]:
                        opt, val = item
                        if opt != option:   # Skip not BR2_ROOTFS_OVERLAY
                            continue
 
                        vals = val.strip('"').split(' ')
                        for v in vals:
                            try:
                                while True:
                                    values.remove(v)
                            except ValueError as e:
                                pass
 
                if len(values):
                    val = ''
                    for v in values:
                        val = val + v + ' ';
                    val = val.strip()
                    f.write('BR2_ROOTFS_OVERLAY="' + val + '"\n')
            else:
                discard = False
 
                for cfg in base_cfgs.keys():
                    if temp.issubset(base_cfgs[cfg]):
                        discard = True
 
                if not discard:
                    f.write(m.group() + '\n')
 
def merge_base(config):
    result = ''
    overlay = ''
    for line in open(config):
        n = re.match('#include\s*"(\w+\.\w+)"$', line)
 
        if n:
            path = os.path.dirname(os.path.realpath(config))
            if not path.endswith("rockchip"):
                path += "/rockchip"
            name = path + '/' + n.group(1)
 
            res, ovl = merge_base(name)
            result += res
            overlay = (overlay + " " + ovl).strip()
        else:
            # W/O quotation mark(")
            m = re.match('BR2_ROOTFS_OVERLAY="(.*)"$', line)
            if m:
                overlay = (overlay + " " + m.group(1)).strip()
            else:
                result += line
    return result, overlay
 
def merge_cfgs(config, output):
    with open(output, "w+") as f:
        result, overlay = merge_base(config)
 
        for line in open(config):
            # override ROOTS_OVERLAY
            m = re.match('BR2_ROOTFS_OVERLAY:="(.*)"$', line)
            if m:
                overlay = m.group(1)
 
        f.write(result)
        f.write('BR2_ROOTFS_OVERLAY="' + overlay + '"\n')
 
def print_usage():
        print ("Usage:")
        print ("   Split defconfig: " + sys.argv[0] + " -s <defconfig> <output>")
        print ("   Merge defconfig: " + sys.argv[0] + " -m <defconfig> <output>")
 
if __name__ == '__main__':
    #common_config(sys.argv)
 
    if len(sys.argv) != 4:
        print_usage()
        sys.exit(-1)
 
    if sys.argv[1] == '-s':
        base_cfgs = {}
        split_config(base_cfgs, sys.argv[2], sys.argv[3])
    elif sys.argv[1] == '-m':
        merge_cfgs(sys.argv[2], sys.argv[3])
    else:
        print_usage()