read-sources-list.py 896 B

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/env python3
  2. #
  3. # opus/read-sources-list.py
  4. #
  5. # Parses .mk files and extracts list of source files.
  6. # Prints one line per source file list, with filenames space-separated.
  7. import sys
  8. if len(sys.argv) < 2:
  9. sys.exit('Usage: {} sources_foo.mk [sources_bar.mk...]'.format(sys.argv[0]))
  10. for input_fn in sys.argv[1:]:
  11. with open(input_fn, 'r', encoding='utf8') as f:
  12. text = f.read()
  13. text = text.replace('\\\n', '')
  14. # Remove empty lines
  15. lines = [line for line in text.split('\n') if line.strip()]
  16. # Print SOURCES_XYZ = file1.c file2.c
  17. for line in lines:
  18. values = line.strip().split('=', maxsplit=2)
  19. if len(values) != 2:
  20. raise RuntimeError('Unable to parse line "{}" from file "{}"'.format(line, input_fn))
  21. var, files = values
  22. sources_list = [f for f in files.split(' ') if f]
  23. print(var.strip(), '=', ' '.join(sources_list))