gen-sources.py (3036B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 # This file takes the .mk files from an upstream opus repo and generates the 6 # sources.mozbuild file. It is invoked as part of update.sh 7 8 import sys 9 import re 10 11 # These files are not used, ignore them. 12 ignore_list = [ 13 'silk/float/regularize_correlations_FLP.c', 14 'silk/float/LPC_inv_pred_gain_FLP.c', 15 'src/opus_projection_encoder.c', 16 'silk/debug.c', 17 'src/mapping_matrix.c', 18 'src/opus_projection_decoder.c', 19 ] 20 21 def should_ignore(value): 22 return any(item in value for item in ignore_list) 23 24 def add_value(values, text): 25 text = text.replace('\\', '') 26 text = text.strip() 27 if text and not should_ignore(text): 28 values.append(text) 29 30 def write_values(output, values): 31 for value in sorted(values, key=lambda x: x.lower()): 32 output.write(" '%s',\n" % value) 33 output.write(']\n\n') 34 35 def generate_sources_mozbuild(path): 36 makefiles = [ 37 'celt_sources.mk', 38 'opus_sources.mk', 39 'silk_sources.mk', 40 ] 41 42 var_definition = re.compile('([A-Z0-9_]*) = (.*)') 43 with open('sources.mozbuild', 'w') as output: 44 45 output.write('# THIS FILE WAS AUTOMATICALLY GENERATED BY %s. DO NOT EDIT.\n' % sys.argv[0]) 46 for makefile in makefiles: 47 values = [] 48 definition_started = False 49 50 with open('%s/%s' % (path, makefile), 'r') as mk: 51 for line in mk: 52 line = line.rstrip() 53 result = var_definition.match(line) 54 if result: 55 if definition_started: 56 write_values(output, values) 57 values = [] 58 definition_started = True 59 60 # Some variable definitions have the first entry on the 61 # first line. Eg: 62 # 63 # CELT_SOURCES = celt/bands.c 64 # 65 # So we treat the first group as the variable name and 66 # the second group as a potential value. 67 # 68 # Note that we write the variable name in lower case (so 69 # "CELT_SOURCES" in the .mk file becomes "celt_sources" 70 # in the .mozbuild file) because moz.build reserves 71 # upper-case variable names for build system outputs. 72 output.write('%s = [\n' % result.group(1).lower()) 73 add_value(values, result.group(2)) 74 else: 75 add_value(values, line) 76 write_values(output, values) 77 78 if __name__ == '__main__': 79 if len(sys.argv) != 2: 80 print("Usage: %s /path/to/opus" % (sys.argv[0])) 81 sys.exit(1) 82 83 generate_sources_mozbuild(sys.argv[1])