aboutsummaryrefslogtreecommitdiff
path: root/android_tools/generate_bp.py
blob: a11846de1bd56aa0be8dbdbd907cf542c83f785c (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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python

import json
import sys

# Set this to True to generate a default entry with all the flags and defines
# common to all the modules that needs to be curated by hand after it's
# generated. When set to False it prints the last curated version of the
# default, which could be incomplete.
GENERATE_FULL_DEFAULT = False
PRINT_ORIGINALS = False
GENERATE_ALL_FLAGS = False

if len(sys.argv) != 2:
    print('wrong number of arguments')
    exit()

def FormatList(l):
    return json.dumps(l)

def FilterHeaders(l):
    return [x for x in l if not x.endswith('.h')]

def PrintOriginCommentedOut(target):
    if PRINT_ORIGINALS:
        print('/* From target:')
        print(json.dumps(target, sort_keys = True, indent = 4))
        print('*/')

def MakeRelatives(l):
    return [x.split('//').pop() for x in l]

def FormatName(name):
    return 'webrtc_' + name.split('/').pop().replace(':', '__')# .replace('/', '_').replace(':', '_')

def FilterFlags(flags, to_skip = set([])):
    if GENERATE_ALL_FLAGS:
        skip = set([
            '-L',
            '-isystem',
            '-Xclang',
            '-B',
            '--sysroot',
            '-fcrash-diagnostics-dir',
            '.',
            '-fdebug-compilation-dir',
            '-instcombine-lower-dbg-declare=0',
            '-Wno-non-c-typedef-for-linkage',
            '-Werror',
            '-fcomplete-member-pointers',
            '-m64',
            '-march=x86-64'
            ]).union(to_skip)
        return [x for x in flags if not any([x.startswith(y) for y in skip])]
    else:
        return [x for x in flags if x == '-msse2']

def GenerateDefault(targets):
    in_default = {
            'cflags' : [],
            'cflags_c' : [],
            'cflags_cc' : [],
            'ldflags' : [],
            'asmflags' : [],
            'defines' : []
    }
    first = True
    for target in targets:
        typ = target['type']
        if typ == 'static_library':
            if first:
                first = False
                # Add all the flags to the default, we'll remove some later
                for flag_type in in_default.keys():
                    in_default[flag_type] = []
                    for flag in target[flag_type]:
                        in_default[flag_type].append(flag)
            else:
                for flag_type in in_default.keys():
                    flags_to_remove = []
                    for flag in in_default[flag_type]:
                        if flag not in target[flag_type]:
                            flags_to_remove.append[flag_type]
                    for flag in flags_to_remove:
                        in_default[flag_type].remove(flag)
    defines = in_default['defines']
    in_default.pop('defines')
    in_default['cflags'].extend(['-D{0}'.format(x) for x in defines])
    if GENERATE_FULL_DEFAULT:
        print('cc_defaults {')
        print('    name: "webrtc_defaults",')
        print('    local_include_dirs: ["."],')
        for typ in in_default.keys():
            print('    {0}: ['.format(typ.replace('asmflags', 'asflags')
                .replace('cflags_cc', 'cppflags')
                .replace('cflags_c', 'conlyflags')))
            for flag in FilterFlags(in_default[typ]):
                print('        "{0}",'.format(flag.replace('"', '\\"')))
            print('    ],')
        print('}')
    else:
        print('cc_defaults {')
        print('    name: "webrtc_defaults",')
        print('    local_include_dirs: [')
        print('      ".",')
        print('    ],')
        print('    cflags: [')
        print('        "-Wno-unused-parameter",')
        print('        "-Wno-missing-field-initializers",')
        print('        "-DUSE_UDEV",')
        print('        "-DUSE_AURA=1",')
        print('        "-DUSE_GLIB=1",')
        print('        "-DUSE_NSS_CERTS=1",')
        print('        "-DUSE_X11=1",')
        print('        "-D_FILE_OFFSET_BITS=64",')
        print('        "-D_LARGEFILE_SOURCE",')
        print('        "-D_LARGEFILE64_SOURCE",')
        print('        "-D_GNU_SOURCE",')
        print('        "-DWEBRTC_ENABLE_PROTOBUF=0",')
        print('        "-DWEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE",')
        print('        "-DRTC_ENABLE_VP9",')
        print('        "-DHAVE_SCTP",')
        print('        "-DWEBRTC_LIBRARY_IMPL",')
        print('        "-DWEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS=1",')
        print('        "-DWEBRTC_POSIX",')
        print('        "-DWEBRTC_LINUX",')
        print('    ],')
        print('    header_libs: [')
        print('      "libwebrtc_absl_headers",')
        print('    ],')
        print('    static_libs: [')
        print('        "libaom",')
        print('        "libevent",')
        print('        "libopus",')
        print('        "libsrtp2",')
        print('        "libvpx",')
        print('        "libyuv",')
        print('        "libpffft",')
        print('        "rnnoise_rnn_vad",')
        print('        "usrsctplib",')
        print('    ],')
        print('    shared_libs: [')
        print('        "libcrypto",')
        print('        "libssl",')
        print('    ],')
        print('    host_supported: true,')
        print('    device_supported: false,')
        print('    arch: {')
        print('        arm: {')
        print('            enabled: false,')
        print('        },')
        print('    },')
        print('    target: {')
        print('        darwin: {')
        print('            enabled: false,')
        print('        },')
        print('    },')
        print('}')
    in_default['cflags'].extend([
        "-Wno-unused-parameter",
        "-Wno-missing-field-initializers",
        "-DUSE_UDEV",
        "-DUSE_AURA=1",
        "-DUSE_GLIB=1",
        "-DUSE_NSS_CERTS=1",
        "-DUSE_X11=1",
        "-D_FILE_OFFSET_BITS=64",
        "-D_LARGEFILE_SOURCE",
        "-D_LARGEFILE64_SOURCE",
        "-D_GNU_SOURCE",
        "-DWEBRTC_ENABLE_PROTOBUF=0",
        "-DWEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE",
        "-DRTC_ENABLE_VP9",
        "-DHAVE_SCTP",
        "-DWEBRTC_LIBRARY_IMPL",
        "-DWEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS=1",
        "-DWEBRTC_POSIX",
        "-DWEBRTC_LINUX"
        ])
    return in_default

def GenerateGroup(target):
    PrintOriginCommentedOut(target)

def GenerateSourceSet(target):
    PrintOriginCommentedOut(target)
    if target.has_key('sources'):
        name = FormatName(target['name'])
        print('filegroup {')
        print('    name: "{0}",'.format(name))
        print('    srcs: {0},'.format(FormatList(MakeRelatives(FilterHeaders(target['sources'])))))
        print('}')
        return name
    return ""

def GenerateStaticLib(target, targets, flags_in_default):
    PrintOriginCommentedOut(target)
    name = FormatName(target['name'])
    print('cc_library_static {')
    print('    name: "{0}",'.format(name))
    print('    defaults: ["webrtc_defaults"],')
    sources = []
    sources.extend(MakeRelatives(FilterHeaders(target['sources'])))
    for trg in targets:
        if trg['type'] == 'source_set' and trg['name'] in target['deps']:
            sources.append(':' + FormatName(trg['name']))
    print('    srcs: {0},'.format(FormatList(sources)))
    print('    host_supported: true,')
    if target.has_key('asmflags'):
        asmflags = FilterFlags(target['asmflags'], set(flags_in_default['asmflags']))
        if len(asmflags) > 0:
            print('    asflags: {0},'.format(FormatList(asmflags)))
    cflags = []
    if target.has_key('cflags'):
        cflags.extend(target['cflags'])
    cflags = FilterFlags(cflags, set(flags_in_default['cflags']))
    if target.has_key('defines'):
        cflags.extend(['-D' + x for x in target['defines']])
    cflags = [x for x in cflags if x not in flags_in_default['cflags']]
    if len(cflags) > 0:
        print('    cflags: {0},'.format(FormatList(cflags)))
    if target.has_key('cflags_c'):
        cflags_c = FilterFlags(target['cflags_c'], set(flags_in_default['cflags_c']))
        if len(cflags_c) > 0:
            print('    conlyflags: {0},'.format(FormatList(cflags_c)))
    if target.has_key('cflags_cc'):
        cflags_cc = FilterFlags(target['cflags_cc'], set(flags_in_default['cflags_cc']))
        if len(cflags_cc) > 0:
            print('    cppflags: {0},'.format(FormatList(cflags_cc)))
    if target.has_key('ldflags'):
        ldflags = FilterFlags(target['ldflags'], set(flags_in_default['ldflags']))
        if len(ldflags) > 0:
            print('    ldflags: {0},'.format(FormatList(ldflags)))
    static_libs = []
    for trg in targets:
        if trg['type'] == 'static_library' and target['deps'].count(trg['name']) > 0:
            static_libs.append(FormatName(trg['name']))
    if len(static_libs) > 0:
        print('    static_libs: {0},'.format(FormatList(static_libs)))
    for dep in target['deps']:
        if FormatName(dep) not in static_libs:
            #print('    // omitted dep: {0}'.format(dep))
            pass
    print('}')
    return name

json_file = open(sys.argv[1], "r")
targets = json.load(json_file)

flags_in_default = GenerateDefault(targets)
print("\n\n")

static_libs = []
file_groups = []

for target in targets:
    typ = target['type']
    if typ == 'static_library':
        lib_name = GenerateStaticLib(target, targets, flags_in_default)
        static_libs.append(lib_name)
    elif typ == 'source_set':
        group_name = GenerateSourceSet(target)
        if len(group_name) > 0:
          file_groups.append(group_name)
    elif typ == 'group':
        GenerateGroup(target)
    else:
        print('Unknown type: {0}'.format(typ))
    print("\n\n")

print('cc_library_static {')
print('    name: "libwebrtc",')
print('    defaults: ["webrtc_defaults"],')
print('    export_include_dirs: ["."],')
print('    whole_static_libs: {0},'.format(FormatList(static_libs + ['libpffft', 'rnnoise_rnn_vad', 'usrsctplib'])))
print('    srcs: {0},').format(FormatList([':{0}'.format(x) for x in file_groups]))
print('}')