blob: d15ea57cf2b610d92813f6d9c19cb241222ff9c5 [file] [log] [blame]
Chris Fallinb3f6daf2014-12-11 18:58:04 -08001#!/usr/bin/python
Joshua Habermane59d2c82021-04-05 10:47:53 -07002#
Joshua Haberman823eb092021-04-05 12:26:41 -07003# Copyright (c) 2009-2021, Google LLC
Joshua Habermane59d2c82021-04-05 10:47:53 -07004# 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.
Chris Fallinb3f6daf2014-12-11 18:58:04 -080027
28import sys
29import re
Joshua Haberman985145c2019-04-24 17:36:17 +000030import os
Chris Fallinb3f6daf2014-12-11 18:58:04 -080031
32INCLUDE_RE = re.compile('^#include "([^"]*)"$')
33
34def parse_include(line):
35 match = INCLUDE_RE.match(line)
36 return match.groups()[0] if match else None
37
Joshua Habermanccd88d52022-09-03 01:52:54 +000038class Amalgamator:
39 def __init__(self, h_out, c_out):
40 self.include_paths = ["."]
41 self.included = set()
42 self.output_h = open(h_out, "w")
43 self.output_c = open(c_out, "w")
44 self.h_out = h_out.split("/")[-1]
45
46 def amalgamate(self, h_files, c_files):
47 self.h_files = set(h_files)
Joshua Haberman9bc79732019-01-23 17:16:31 -080048 self.output_c.write("/* Amalgamated source file */\n")
Joshua Habermanccd88d52022-09-03 01:52:54 +000049 self.output_c.write('#include "%s"\n' % (self.h_out))
50 if self.h_out == "ruby-upb.h":
Joshua Habermanafffa9e2022-03-10 15:21:55 -080051 self.output_h.write("// Ruby is still using proto3 enum semantics for proto2\n")
52 self.output_h.write("#define UPB_DISABLE_PROTO2_ENUM_CHECKING\n")
Chris Fallinb3f6daf2014-12-11 18:58:04 -080053
Joshua Haberman9bc79732019-01-23 17:16:31 -080054 self.output_h.write("/* Amalgamated source file */\n")
Joshua Haberman3a37b912018-09-06 13:41:30 -070055
Eric Salof6307872022-11-05 16:16:27 -070056 port_def = self._find_include_file("upb/port/def.inc")
57 port_undef = self._find_include_file("upb/port/undef.inc")
Joshua Habermanccd88d52022-09-03 01:52:54 +000058 self._process_file(port_def, self.output_h)
59 self._process_file(port_def, self.output_c)
Joshua Haberman985145c2019-04-24 17:36:17 +000060
Joshua Habermanccd88d52022-09-03 01:52:54 +000061 for file in c_files:
62 self._process_file(file, self.output_c)
63
64 self._process_file(port_undef, self.output_h)
65 self._process_file(port_undef, self.output_c)
Chris Fallinb3f6daf2014-12-11 18:58:04 -080066
67 def _process_file(self, infile_name, outfile):
Joshua Habermanccd88d52022-09-03 01:52:54 +000068 lines = open(infile_name).readlines()
Joshua Haberman4f1e48e2021-04-16 14:39:29 -070069
70 has_copyright = lines[1].startswith(" * Copyright")
71 if has_copyright:
72 while not lines[0].startswith(" */"):
73 lines.pop(0)
74 lines.pop(0)
75
Joshua Haberman4f1e48e2021-04-16 14:39:29 -070076 for line in lines:
Joshua Habermanccd88d52022-09-03 01:52:54 +000077 if not self._process_include(line):
Chris Fallinb3f6daf2014-12-11 18:58:04 -080078 outfile.write(line)
79
Joshua Habermanccd88d52022-09-03 01:52:54 +000080 def _find_include_file(self, name):
81 for h_file in self.h_files:
82 if h_file.endswith(name):
83 return h_file
84
85 def _process_include(self, line):
Joshua Haberman2559e782020-04-09 14:36:24 -070086 include = parse_include(line)
87 if not include:
88 return False
Joshua Haberman64abb5e2020-11-02 09:00:53 -080089 if not (include.startswith("upb") or include.startswith("google")):
Joshua Haberman2559e782020-04-09 14:36:24 -070090 return False
Eric Salof6307872022-11-05 16:16:27 -070091 if include and (include.endswith("port/def.inc") or include.endswith("port/undef.inc")):
Joshua Habermanccd88d52022-09-03 01:52:54 +000092 # Skip, we handle this separately
93 return True
Joshua Haberman2559e782020-04-09 14:36:24 -070094 if include.endswith("hpp"):
95 # Skip, we don't support the amalgamation from C++.
96 return True
Joshua Habermanccd88d52022-09-03 01:52:54 +000097 elif include in self.included:
98 return True
Joshua Haberman2559e782020-04-09 14:36:24 -070099 else:
100 # Include this upb header inline.
Joshua Habermanccd88d52022-09-03 01:52:54 +0000101 h_file = self._find_include_file(include)
102 if h_file:
103 self.h_files.remove(h_file)
Joshua Haberman2559e782020-04-09 14:36:24 -0700104 self.included.add(include)
Joshua Habermanccd88d52022-09-03 01:52:54 +0000105 self._process_file(h_file, self.output_h)
106 return True
107 raise RuntimeError("Couldn't find include: " + include + ", h_files=" + repr(self.h_files))
Chris Fallinb3f6daf2014-12-11 18:58:04 -0800108
109# ---- main ----
110
Joshua Habermanccd88d52022-09-03 01:52:54 +0000111c_out = sys.argv[1]
112h_out = sys.argv[2]
Joshua Habermanccd88d52022-09-03 01:52:54 +0000113amalgamator = Amalgamator(h_out, c_out)
114c_files = []
115h_files = []
Chris Fallinb3f6daf2014-12-11 18:58:04 -0800116
Joshua Haberman16facab2020-05-08 16:40:24 -0700117for arg in sys.argv[3:]:
Joshua Haberman985145c2019-04-24 17:36:17 +0000118 arg = arg.strip()
Joshua Habermanccd88d52022-09-03 01:52:54 +0000119 if arg.endswith(".h") or arg.endswith(".inc"):
120 h_files.append(arg)
Joshua Haberman985145c2019-04-24 17:36:17 +0000121 else:
Joshua Habermanccd88d52022-09-03 01:52:54 +0000122 c_files.append(arg)
Joshua Haberman985145c2019-04-24 17:36:17 +0000123
Joshua Habermanccd88d52022-09-03 01:52:54 +0000124amalgamator.amalgamate(h_files, c_files)