gen-sources.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. # This file takes the .mk files from an upstream opus repo and generates the
  5. # sources.mozbuild file. It is invoked as part of update.sh
  6. import sys
  7. import re
  8. def add_value(values, text):
  9. text = text.replace('\\', '')
  10. text = text.strip()
  11. if text:
  12. values.append(text)
  13. def write_values(output, values):
  14. for value in sorted(values, key=lambda x: x.lower()):
  15. output.write(" '%s',\n" % value)
  16. output.write(']\n\n')
  17. def generate_sources_mozbuild(path):
  18. makefiles = [
  19. 'celt_sources.mk',
  20. 'opus_sources.mk',
  21. 'silk_sources.mk',
  22. ]
  23. var_definition = re.compile('([A-Z0-9_]*) = (.*)')
  24. with open('sources.mozbuild', 'w') as output:
  25. output.write('# THIS FILE WAS AUTOMATICALLY GENERATED BY %s. DO NOT EDIT.\n' % sys.argv[0])
  26. for makefile in makefiles:
  27. values = []
  28. definition_started = False
  29. with open('%s/%s' % (path, makefile), 'r') as mk:
  30. for line in mk:
  31. line = line.rstrip()
  32. result = var_definition.match(line)
  33. if result:
  34. if definition_started:
  35. write_values(output, values)
  36. values = []
  37. definition_started = True
  38. # Some variable definitions have the first entry on the
  39. # first line. Eg:
  40. #
  41. # CELT_SOURCES = celt/bands.c
  42. #
  43. # So we treat the first group as the variable name and
  44. # the second group as a potential value.
  45. #
  46. # Note that we write the variable name in lower case (so
  47. # "CELT_SOURCES" in the .mk file becomes "celt_sources"
  48. # in the .mozbuild file) because moz.build reserves
  49. # upper-case variable names for build system outputs.
  50. output.write('%s = [\n' % result.group(1).lower())
  51. add_value(values, result.group(2))
  52. else:
  53. add_value(values, line)
  54. write_values(output, values)
  55. if __name__ == '__main__':
  56. if len(sys.argv) != 2:
  57. print "Usage: %s /path/to/opus" % (sys.argv[0])
  58. sys.exit(1)
  59. generate_sources_mozbuild(sys.argv[1])