summaryrefslogtreecommitdiff
path: root/tools/amalgamate.py
blob: 4739a94e5d7fa1bfdd3550d2abc11fbc8ca004be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python

import sys
import re

INCLUDE_RE = re.compile('^#include "([^"]*)"$')

def parse_include(line):
  match = INCLUDE_RE.match(line)
  return match.groups()[0] if match else None

class Amalgamator:
  def __init__(self, include_path, output_path):
    self.include_path = include_path
    self.included = set(["upb/port_def.inc", "upb/port_undef.inc"])
    self.output_h = open(output_path + "upb.h", "w")
    self.output_c = open(output_path + "upb.c", "w")

    self.output_c.write("// Amalgamated source file\n")
    self.output_c.write('#include "upb.h"\n')
    self.output_c.write(open("upb/port_def.inc").read())

    self.output_h.write("// Amalgamated source file\n")
    self.output_h.write(open("upb/port_def.inc").read())

  def finish(self):
    self.output_c.write(open("upb/port_undef.inc").read())
    self.output_h.write(open("upb/port_undef.inc").read())

  def _process_file(self, infile_name, outfile):
    for line in open(infile_name):
      include = parse_include(line)
      if include is not None and (include.startswith("upb") or
                                  include.startswith("google")):
        if include not in self.included:
          self.included.add(include)
          self._add_header(self.include_path + include)
      else:
        outfile.write(line)

  def _add_header(self, filename):
    self._process_file(filename, self.output_h)

  def add_src(self, filename):
    self._process_file(filename, self.output_c)

# ---- main ----

include_path = sys.argv[1]
output_path = sys.argv[2]
amalgamator = Amalgamator(include_path, output_path)

for filename in sys.argv[3:]:
  amalgamator.add_src(filename.strip())

amalgamator.finish()
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback