Last active
August 29, 2015 14:11
-
-
Save haberman/51de05fe7dc1c8e5b6e9 to your computer and use it in GitHub Desktop.
For building a upb amalgamator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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): | |
self.included = set() | |
self.output_h = open("upb.h", "w") | |
self.output_c = open("upb.c", "w") | |
self.output_c.write("// Amalgamated source file\n") | |
self.output_c.write('#include "upb.h"\n') | |
self.output_h.write("// Amalgamated source file\n") | |
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"): | |
if include not in self.included: | |
self.included.add(include) | |
self._add_header(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) | |
amalgamator = Amalgamator() | |
for filename in sys.argv[1:]: | |
amalgamator.add_src(filename.strip()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment