| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2009-2021, Google LLC |
| 4 |
# All rights reserved. |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# * Redistributions of source code must retain the above copyright |
| 9 |
# notice, this list of conditions and the following disclaimer. |
| 10 |
# * Redistributions in binary form must reproduce the above copyright |
| 11 |
# notice, this list of conditions and the following disclaimer in the |
| 12 |
# documentation and/or other materials provided with the distribution. |
| 13 |
# * Neither the name of Google LLC nor the |
| 14 |
# names of its contributors may be used to endorse or promote products |
| 15 |
# derived from this software without specific prior written permission. |
| 16 |
# |
| 17 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 18 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 |
# DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY |
| 21 |
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 24 |
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 26 |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
import sys |
| 29 |
import re |
| 30 |
import os |
| 31 |
|
| 32 |
INCLUDE_RE = re.compile('^#include "([^"]*)"$') |
| 33 |
|
| 34 |
def parse_include(line): |
| 35 |
match = INCLUDE_RE.match(line) |
| 36 |
return match.groups()[0] if match else None |
| 37 |
|
| 38 |
class Amalgamator: |
| 39 |
def __init__(self, output_path, prefix): |
| 40 |
self.include_paths = ["."] |
| 41 |
self.included = set(["upb/port_def.inc", "upb/port_undef.inc"]) |
| 42 |
self.output_h = open(output_path + prefix + "upb.h", "w") |
| 43 |
self.output_c = open(output_path + prefix + "upb.c", "w") |
| 44 |
|
| 45 |
self.output_c.write("/* Amalgamated source file */\n") |
| 46 |
self.output_c.write('#include "%supb.h"\n' % (prefix)) |
| 47 |
if prefix == "ruby-": |
| 48 |
self.output_h.write("// Ruby is still using proto3 enum semantics for proto2\n") |
| 49 |
self.output_h.write("#define UPB_DISABLE_PROTO2_ENUM_CHECKING\n") |
| 50 |
self.output_c.write(open("upb/port_def.inc").read()) |
| 51 |
|
| 52 |
self.output_h.write("/* Amalgamated source file */\n") |
| 53 |
self.output_h.write(open("upb/port_def.inc").read()) |
| 54 |
|
| 55 |
def add_include_path(self, path): |
| 56 |
self.include_paths.append(path) |
| 57 |
|
| 58 |
def finish(self): |
| 59 |
self._add_header("upb/port_undef.inc") |
| 60 |
self.add_src("upb/port_undef.inc") |
| 61 |
|
| 62 |
def _process_file(self, infile_name, outfile): |
| 63 |
file = None |
| 64 |
for path in self.include_paths: |
| 65 |
try: |
| 66 |
full_path = os.path.join(path, infile_name) |
| 67 |
file = open(full_path) |
| 68 |
break |
| 69 |
except IOError: |
| 70 |
pass |
| 71 |
if not file: |
| 72 |
raise RuntimeError("Couldn't open file " + infile_name) |
| 73 |
|
| 74 |
lines = file.readlines() |
| 75 |
|
| 76 |
has_copyright = lines[1].startswith(" * Copyright") |
| 77 |
if has_copyright: |
| 78 |
while not lines[0].startswith(" */"): |
| 79 |
lines.pop(0) |
| 80 |
lines.pop(0) |
| 81 |
|
| 82 |
lines.insert(0, "\n/** " + infile_name + " " + ("*" * 60) +"/"); |
| 83 |
|
| 84 |
for line in lines: |
| 85 |
if not self._process_include(line, outfile): |
| 86 |
outfile.write(line) |
| 87 |
|
| 88 |
def _process_include(self, line, outfile): |
| 89 |
include = parse_include(line) |
| 90 |
if not include: |
| 91 |
return False |
| 92 |
if not (include.startswith("upb") or include.startswith("google")): |
| 93 |
return False |
| 94 |
if include.endswith("hpp"): |
| 95 |
# Skip, we don't support the amalgamation from C++. |
| 96 |
return True |
| 97 |
else: |
| 98 |
# Include this upb header inline. |
| 99 |
if include not in self.included: |
| 100 |
self.included.add(include) |
| 101 |
self._add_header(include) |
| 102 |
return True |
| 103 |
|
| 104 |
def _add_header(self, filename): |
| 105 |
self._process_file(filename, self.output_h) |
| 106 |
|
| 107 |
def add_src(self, filename): |
| 108 |
self._process_file(filename, self.output_c) |
| 109 |
|
| 110 |
# ---- main ---- |
| 111 |
|
| 112 |
output_path = sys.argv[1] |
| 113 |
prefix = sys.argv[2] |
| 114 |
amalgamator = Amalgamator(output_path, prefix) |
| 115 |
files = [] |
| 116 |
|
| 117 |
for arg in sys.argv[3:]: |
| 118 |
arg = arg.strip() |
| 119 |
if arg.startswith("-I"): |
| 120 |
amalgamator.add_include_path(arg[2:]) |
| 121 |
elif arg.endswith(".h") or arg.endswith(".inc"): |
| 122 |
pass |
| 123 |
else: |
| 124 |
files.append(arg) |
| 125 |
|
| 126 |
for filename in files: |
| 127 |
amalgamator.add_src(filename) |
| 128 |
|
| 129 |
amalgamator.finish() |