• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
8import sys
9
10if len(sys.argv) < 2:
11  sys.exit('Usage: {} sources_foo.mk [sources_bar.mk...]'.format(sys.argv[0]))
12
13for input_fn in sys.argv[1:]:
14  with open(input_fn, 'r', encoding='utf8') as f:
15    text = f.read()
16    text = text.replace('\\\n', '')
17
18    # Remove empty lines
19    lines = [line for line in text.split('\n') if line.strip()]
20
21    # Print SOURCES_XYZ = file1.c file2.c
22    for line in lines:
23      values = line.strip().split('=', maxsplit=2)
24      if len(values) != 2:
25        raise RuntimeError('Unable to parse line "{}" from file "{}"'.format(line, input_fn))
26      var, files = values
27      sources_list = [f for f in files.split(' ') if f]
28      print(var.strip(), '=', ' '.join(sources_list))
29