| 1 |
# Copyright (c) 2009-2021, Google LLC |
| 2 |
# All rights reserved. |
| 3 |
# |
| 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. |
| 14 |
# |
| 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. |
| 25 |
|
| 26 |
"""Internal rules for building upb.""" |
| 27 |
|
| 28 |
load(":upb_proto_library.bzl", "GeneratedSrcsInfo") |
| 29 |
|
| 30 |
_DEFAULT_CPPOPTS = [] |
| 31 |
_DEFAULT_COPTS = [] |
| 32 |
|
| 33 |
# begin:github_only |
| 34 |
_DEFAULT_CPPOPTS.extend([ |
| 35 |
"-Wextra", |
| 36 |
# "-Wshorten-64-to-32", # not in GCC (and my Kokoro images doesn't have Clang) |
| 37 |
"-Werror", |
| 38 |
"-Wno-long-long", |
| 39 |
]) |
| 40 |
_DEFAULT_COPTS.extend([ |
| 41 |
"-std=c99", |
| 42 |
"-pedantic", |
| 43 |
"-Werror=pedantic", |
| 44 |
"-Wall", |
| 45 |
"-Wstrict-prototypes", |
| 46 |
# GCC (at least) emits spurious warnings for this that cannot be fixed |
| 47 |
# without introducing redundant initialization (with runtime cost): |
| 48 |
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635 |
| 49 |
#"-Wno-maybe-uninitialized", |
| 50 |
]) |
| 51 |
# end:github_only |
| 52 |
|
| 53 |
UPB_DEFAULT_CPPOPTS = select({ |
| 54 |
"//:windows": [], |
| 55 |
"//conditions:default": _DEFAULT_CPPOPTS, |
| 56 |
}) |
| 57 |
|
| 58 |
UPB_DEFAULT_COPTS = select({ |
| 59 |
"//:windows": [], |
| 60 |
"//:fasttable_enabled_setting": ["-std=gnu99", "-DUPB_ENABLE_FASTTABLE"], |
| 61 |
"//conditions:default": _DEFAULT_COPTS, |
| 62 |
}) |
| 63 |
|
| 64 |
runfiles_init = """\ |
| 65 |
# --- begin runfiles.bash initialization v2 --- |
| 66 |
# Copy-pasted from the Bazel Bash runfiles library v2. |
| 67 |
set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash |
| 68 |
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ |
| 69 |
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ |
| 70 |
source "$0.runfiles/$f" 2>/dev/null || \ |
| 71 |
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ |
| 72 |
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ |
| 73 |
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e |
| 74 |
# --- end runfiles.bash initialization v2 --- |
| 75 |
""" |
| 76 |
|
| 77 |
def _get_real_short_path(file): |
| 78 |
# For some reason, files from other archives have short paths that look like: |
| 79 |
# ../com_google_protobuf/google/protobuf/descriptor.proto |
| 80 |
short_path = file.short_path |
| 81 |
if short_path.startswith("../"): |
| 82 |
second_slash = short_path.index("/", 3) |
| 83 |
short_path = short_path[second_slash + 1:] |
| 84 |
return short_path |
| 85 |
|
| 86 |
def _get_real_root(file): |
| 87 |
real_short_path = _get_real_short_path(file) |
| 88 |
return file.path[:-len(real_short_path) - 1] |
| 89 |
|
| 90 |
def _get_real_roots(files): |
| 91 |
roots = {} |
| 92 |
for file in files: |
| 93 |
real_root = _get_real_root(file) |
| 94 |
if real_root: |
| 95 |
roots[real_root] = True |
| 96 |
return roots.keys() |
| 97 |
|
| 98 |
def make_shell_script(name, contents, out): |
| 99 |
contents = contents.replace("$", "$$") |
| 100 |
native.genrule( |
| 101 |
name = "gen_" + name, |
| 102 |
outs = [out], |
| 103 |
cmd = "(cat <<'HEREDOC'\n%s\nHEREDOC\n) > $@" % contents, |
| 104 |
) |
| 105 |
|
| 106 |
# upb_amalgamation() rule, with file_list aspect. |
| 107 |
|
| 108 |
SrcList = provider( |
| 109 |
fields = { |
| 110 |
"srcs": "list of srcs", |
| 111 |
}, |
| 112 |
) |
| 113 |
|
| 114 |
def _file_list_aspect_impl(target, ctx): |
| 115 |
if GeneratedSrcsInfo in target: |
| 116 |
srcs = target[GeneratedSrcsInfo] |
| 117 |
return [SrcList(srcs = srcs.srcs + srcs.hdrs)] |
| 118 |
|
| 119 |
srcs = [] |
| 120 |
for src in ctx.rule.attr.srcs: |
| 121 |
srcs += src.files.to_list() |
| 122 |
for hdr in ctx.rule.attr.hdrs: |
| 123 |
srcs += hdr.files.to_list() |
| 124 |
for hdr in ctx.rule.attr.textual_hdrs: |
| 125 |
srcs += hdr.files.to_list() |
| 126 |
return [SrcList(srcs = srcs)] |
| 127 |
|
| 128 |
_file_list_aspect = aspect( |
| 129 |
implementation = _file_list_aspect_impl, |
| 130 |
) |
| 131 |
|
| 132 |
def _upb_amalgamation(ctx): |
| 133 |
inputs = [] |
| 134 |
for lib in ctx.attr.libs: |
| 135 |
inputs += lib[SrcList].srcs |
| 136 |
srcs = [src for src in inputs if src.path.endswith("c")] |
| 137 |
ctx.actions.run( |
| 138 |
inputs = inputs, |
| 139 |
outputs = ctx.outputs.outs, |
| 140 |
arguments = [ctx.bin_dir.path + "/", ctx.attr.prefix] + [f.path for f in srcs] + ["-I" + root for root in _get_real_roots(inputs)], |
| 141 |
progress_message = "Making amalgamation", |
| 142 |
executable = ctx.executable._amalgamator, |
| 143 |
) |
| 144 |
return [] |
| 145 |
|
| 146 |
upb_amalgamation = rule( |
| 147 |
attrs = { |
| 148 |
"_amalgamator": attr.label( |
| 149 |
executable = True, |
| 150 |
cfg = "exec", |
| 151 |
default = "//bazel:amalgamate", |
| 152 |
), |
| 153 |
"prefix": attr.string( |
| 154 |
default = "", |
| 155 |
), |
| 156 |
"libs": attr.label_list(aspects = [_file_list_aspect]), |
| 157 |
"outs": attr.output_list(), |
| 158 |
}, |
| 159 |
implementation = _upb_amalgamation, |
| 160 |
) |