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 28load(":upb_proto_library.bzl", "GeneratedSrcsInfo") 29 30UPB_DEFAULT_CPPOPTS = select({ 31 "//:windows": [], 32 "//conditions:default": [ 33 # copybara:strip_for_google3_begin 34 "-Wextra", 35 # "-Wshorten-64-to-32", # not in GCC (and my Kokoro images doesn't have Clang) 36 "-Werror", 37 "-Wno-long-long", 38 # copybara:strip_end 39 ], 40}) 41 42UPB_DEFAULT_COPTS = select({ 43 "//:windows": [], 44 "//:fasttable_enabled_setting": ["-std=gnu99", "-DUPB_ENABLE_FASTTABLE"], 45 "//conditions:default": [ 46 # copybara:strip_for_google3_begin 47 "-std=c99", 48 "-pedantic", 49 "-Werror=pedantic", 50 "-Wall", 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", 56 # copybara:strip_end 57 ], 58}) 59 60def _librule(name): 61 return name + "_lib" 62 63runfiles_init = """\ 64# --- begin runfiles.bash initialization v2 --- 65# Copy-pasted from the Bazel Bash runfiles library v2. 66set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash 67source "${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 --- 74""" 75 76def _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:] 83 return short_path 84 85def _get_real_root(file): 86 real_short_path = _get_real_short_path(file) 87 return file.path[:-len(real_short_path) - 1] 88 89def _get_real_roots(files): 90 roots = {} 91 for file in files: 92 real_root = _get_real_root(file) 93 if real_root: 94 roots[real_root] = True 95 return roots.keys() 96 97def _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):] 101 102def _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)] 106 107def make_shell_script(name, contents, out): 108 contents = runfiles_init + contents # copybara:strip_for_google3 109 contents = contents.replace("$", "$$") 110 native.genrule( 111 name = "gen_" + name, 112 outs = [out], 113 cmd = "(cat <<'HEREDOC'\n%s\nHEREDOC\n) > $@" % contents, 114 ) 115 116# upb_amalgamation() rule, with file_list aspect. 117 118SrcList = provider( 119 fields = { 120 "srcs": "list of srcs", 121 }, 122) 123 124def _file_list_aspect_impl(target, ctx): 125 if GeneratedSrcsInfo in target: 126 srcs = target[GeneratedSrcsInfo] 127 return [SrcList(srcs = srcs.srcs + srcs.hdrs)] 128 129 srcs = [] 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)] 137 138_file_list_aspect = aspect( 139 implementation = _file_list_aspect_impl, 140) 141 142def _upb_amalgamation(ctx): 143 inputs = [] 144 for lib in ctx.attr.libs: 145 inputs += lib[SrcList].srcs 146 srcs = [src for src in inputs if src.path.endswith("c")] 147 ctx.actions.run( 148 inputs = inputs, 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, 153 ) 154 return [] 155 156upb_amalgamation = rule( 157 attrs = { 158 "amalgamator": attr.label( 159 executable = True, 160 cfg = "host", 161 ), 162 "prefix": attr.string( 163 default = "", 164 ), 165 "libs": attr.label_list(aspects = [_file_list_aspect]), 166 "outs": attr.output_list(), 167 }, 168 implementation = _upb_amalgamation, 169) 170