michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: # This file takes the .mk files from an upstream opus repo and generates the michael@0: # sources.mozbuild file. It is invoked as part of update.sh michael@0: michael@0: import sys michael@0: import re michael@0: michael@0: def add_value(values, text): michael@0: text = text.replace('\\', '') michael@0: text = text.strip() michael@0: if text: michael@0: values.append(text) michael@0: michael@0: def write_values(output, values): michael@0: for value in sorted(values, key=lambda x: x.lower()): michael@0: output.write(" '%s',\n" % value) michael@0: output.write(']\n\n') michael@0: michael@0: def generate_sources_mozbuild(path): michael@0: makefiles = [ michael@0: 'celt_sources.mk', michael@0: 'opus_sources.mk', michael@0: 'silk_sources.mk', michael@0: ] michael@0: michael@0: var_definition = re.compile('([A-Z_]*) = (.*)') michael@0: with open('sources.mozbuild', 'w') as output: michael@0: michael@0: output.write('# THIS FILE WAS AUTOMATICALLY GENERATED BY %s. DO NOT EDIT.\n' % sys.argv[0]) michael@0: for makefile in makefiles: michael@0: values = [] michael@0: definition_started = False michael@0: michael@0: with open('%s/%s' % (path, makefile), 'r') as mk: michael@0: for line in mk: michael@0: line = line.rstrip() michael@0: result = var_definition.match(line) michael@0: if result: michael@0: if definition_started: michael@0: write_values(output, values) michael@0: values = [] michael@0: definition_started = True michael@0: michael@0: # Some variable definitions have the first entry on the michael@0: # first line. Eg: michael@0: # michael@0: # CELT_SOURCES = celt/bands.c michael@0: # michael@0: # So we treat the first group as the variable name and michael@0: # the second group as a potential value. michael@0: # michael@0: # Note that we write the variable name in lower case (so michael@0: # "CELT_SOURCES" in the .mk file becomes "celt_sources" michael@0: # in the .mozbuild file) because moz.build reserves michael@0: # upper-case variable names for build system outputs. michael@0: output.write('%s = [\n' % result.group(1).lower()) michael@0: add_value(values, result.group(2)) michael@0: else: michael@0: add_value(values, line) michael@0: write_values(output, values) michael@0: michael@0: if __name__ == '__main__': michael@0: if len(sys.argv) != 2: michael@0: print "Usage: %s /path/to/opus" % (sys.argv[0]) michael@0: sys.exit(1) michael@0: michael@0: generate_sources_mozbuild(sys.argv[1])