• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DEFAULT_CPPOPTS = []
29_DEFAULT_COPTS = []
30
31# begin:github_only
32_DEFAULT_CPPOPTS.extend([
33    "-Wextra",
34    # "-Wshorten-64-to-32",  # not in GCC (and my Kokoro images doesn't have Clang)
35    "-Werror",
36    "-Wno-unused-parameter",
37    "-Wno-long-long",
38])
39_DEFAULT_COPTS.extend([
40    "-std=c99",
41    "-Wall",
42    "-Wstrict-prototypes",
43    # GCC (at least) emits spurious warnings for this that cannot be fixed
44    # without introducing redundant initialization (with runtime cost):
45    #   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635
46    #"-Wno-maybe-uninitialized",
47])
48# end:github_only
49
50UPB_DEFAULT_CPPOPTS = select({
51    "//:windows": [],
52    "//conditions:default": _DEFAULT_CPPOPTS,
53})
54
55UPB_DEFAULT_COPTS = select({
56    "//:windows": [],
57    "//:fasttable_enabled_setting": ["-std=gnu99", "-DUPB_ENABLE_FASTTABLE"],
58    "//conditions:default": _DEFAULT_COPTS,
59})
60
61runfiles_init = """\
62# --- begin runfiles.bash initialization v2 ---
63# Copy-pasted from the Bazel Bash runfiles library v2.
64set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
65source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
66  source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
67  source "$0.runfiles/$f" 2>/dev/null || \
68  source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
69  source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
70  { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
71# --- end runfiles.bash initialization v2 ---
72"""
73
74def _get_real_short_path(file):
75    # For some reason, files from other archives have short paths that look like:
76    #   ../com_google_protobuf/google/protobuf/descriptor.proto
77    short_path = file.short_path
78    if short_path.startswith("../"):
79        second_slash = short_path.index("/", 3)
80        short_path = short_path[second_slash + 1:]
81    return short_path
82
83def _get_real_root(file):
84    real_short_path = _get_real_short_path(file)
85    return file.path[:-len(real_short_path) - 1]
86
87def _get_real_roots(files):
88    roots = {}
89    for file in files:
90        real_root = _get_real_root(file)
91        if real_root:
92            roots[real_root] = True
93    return roots.keys()
94
95def make_shell_script(name, contents, out):
96    contents = contents.replace("$", "$$")
97    native.genrule(
98        name = "gen_" + name,
99        outs = [out],
100        cmd = "(cat <<'HEREDOC'\n%s\nHEREDOC\n) > $@" % contents,
101    )
102