diff options
author | Chris Fallin <cfallin@c1f.net> | 2014-12-11 18:58:04 -0800 |
---|---|---|
committer | Chris Fallin <cfallin@c1f.net> | 2014-12-12 14:49:12 -0800 |
commit | b3f6daf83d8adf0040a1bf9401342c811502f690 (patch) | |
tree | 2b51257e4b9cebfc3640d98cda45f9b8be36fdb2 /tools/amalgamate.py | |
parent | 56913be6bb57f81dbbf7baf9cc9a0a2cd1a36493 (diff) |
Amalgamated distribution (upb.c/upb.h) tool.
There are a number of tweaks to get this to work:
- The #include dependence graph wasn't quite complete, and I had to add
a few #includes to get the tool to work.
- I had to change a number of symbol names to avoid conflicts between
'static' definitions in different .c files. This could be avoided if
the tool were smart enough to rename static symbols to have unique
prefixes instead, but (i) this requires semantic understanding of C,
and (ii) the macro-defined static functions (e.g., handlers for
primitive types in several places) would probably trip this up.
Verified that the resulting upb.h/upb.c compiles and doesn't have any
unresolved references.
Diffstat (limited to 'tools/amalgamate.py')
-rwxr-xr-x | tools/amalgamate.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/tools/amalgamate.py b/tools/amalgamate.py new file mode 100755 index 0000000..6fd8033 --- /dev/null +++ b/tools/amalgamate.py @@ -0,0 +1,47 @@ +#!/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() + 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_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(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()) |