1 # Copyright (c) 2009-2021, Google LLC
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 # * Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer.
8 # * Redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution.
11 # * Neither the name of Google LLC nor the
12 # names of its contributors may be used to endorse or promote products
13 # derived from this software without specific prior written permission.
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 # DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
19 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 """Internal rules for building upb."""
28 load(":upb_proto_library.bzl", "GeneratedSrcsInfo")
30 UPB_DEFAULT_CPPOPTS = select({
32 "//conditions:default": [
33 # copybara:strip_for_google3_begin
35 # "-Wshorten-64-to-32", # not in GCC (and my Kokoro images doesn't have Clang)
42 UPB_DEFAULT_COPTS = select({
44 "//:fasttable_enabled_setting": ["-std=gnu99", "-DUPB_ENABLE_FASTTABLE"],
45 "//conditions:default": [
46 # copybara:strip_for_google3_begin
51 "-Wstrict-prototypes",
52 # GCC (at least) emits spurious warnings for this that cannot be fixed
53 # without introducing redundant initialization (with runtime cost):
54 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635
55 #"-Wno-maybe-uninitialized",
64 # --- begin runfiles.bash initialization v2 ---
65 # Copy-pasted from the Bazel Bash runfiles library v2.
66 set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
67 source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
68 source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
69 source "$0.runfiles/$f" 2>/dev/null || \
70 source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
71 source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
72 { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
73 # --- end runfiles.bash initialization v2 ---
76 def _get_real_short_path(file):
77 # For some reason, files from other archives have short paths that look like:
78 # ../com_google_protobuf/google/protobuf/descriptor.proto
79 short_path = file.short_path
80 if short_path.startswith("../"):
81 second_slash = short_path.index("/", 3)
82 short_path = short_path[second_slash + 1:]
85 def _get_real_root(file):
86 real_short_path = _get_real_short_path(file)
87 return file.path[:-len(real_short_path) - 1]
89 def _get_real_roots(files):
92 real_root = _get_real_root(file)
94 roots[real_root] = True
97 def _remove_prefix(str, prefix):
98 if not str.startswith(prefix):
99 fail("%s doesn't start with %s" % (str, prefix))
100 return str[len(prefix):]
102 def _remove_suffix(str, suffix):
103 if not str.endswith(suffix):
104 fail("%s doesn't end with %s" % (str, suffix))
105 return str[:-len(suffix)]
107 def make_shell_script(name, contents, out):
108 contents = runfiles_init + contents # copybara:strip_for_google3
109 contents = contents.replace("$", "$$")
111 name = "gen_" + name,
113 cmd = "(cat <<'HEREDOC'\n%s\nHEREDOC\n) > $@" % contents,
116 # upb_amalgamation() rule, with file_list aspect.
120 "srcs": "list of srcs",
124 def _file_list_aspect_impl(target, ctx):
125 if GeneratedSrcsInfo in target:
126 srcs = target[GeneratedSrcsInfo]
127 return [SrcList(srcs = srcs.srcs + srcs.hdrs)]
130 for src in ctx.rule.attr.srcs:
131 srcs += src.files.to_list()
132 for hdr in ctx.rule.attr.hdrs:
133 srcs += hdr.files.to_list()
134 for hdr in ctx.rule.attr.textual_hdrs:
135 srcs += hdr.files.to_list()
136 return [SrcList(srcs = srcs)]
138 _file_list_aspect = aspect(
139 implementation = _file_list_aspect_impl,
142 def _upb_amalgamation(ctx):
144 for lib in ctx.attr.libs:
145 inputs += lib[SrcList].srcs
146 srcs = [src for src in inputs if src.path.endswith("c")]
149 outputs = ctx.outputs.outs,
150 arguments = [ctx.bin_dir.path + "/", ctx.attr.prefix] + [f.path for f in srcs] + ["-I" + root for root in _get_real_roots(inputs)],
151 progress_message = "Making amalgamation",
152 executable = ctx.executable.amalgamator,
156 upb_amalgamation = rule(
158 "amalgamator": attr.label(
162 "prefix": attr.string(
165 "libs": attr.label_list(aspects = [_file_list_aspect]),
166 "outs": attr.output_list(),
168 implementation = _upb_amalgamation,