• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2009-2021, Google LLC
2# All rights reserved.
3#
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file or at
6# https://developers.google.com/open-source/licenses/bsd
7
8"""Internal rules for building upb."""
9
10_DEFAULT_CPPOPTS = []
11_DEFAULT_COPTS = []
12
13_DEFAULT_CPPOPTS.extend([
14    "-Wextra",
15    # "-Wshorten-64-to-32",  # not in GCC (and my Kokoro images doesn't have Clang)
16    "-Wno-unused-parameter",
17    "-Wno-long-long",
18])
19_DEFAULT_COPTS.extend([
20    "-std=c99",
21    "-Wall",
22    "-Wstrict-prototypes",
23    # GCC (at least) emits spurious warnings for this that cannot be fixed
24    # without introducing redundant initialization (with runtime cost):
25    #   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635
26    #"-Wno-maybe-uninitialized",
27])
28
29UPB_DEFAULT_CPPOPTS = select({
30    "//upb:windows": [],
31    "//conditions:default": _DEFAULT_CPPOPTS,
32})
33
34UPB_DEFAULT_COPTS = select({
35    "//upb:windows": [],
36    "//upb:fasttable_enabled_setting": ["-std=gnu99", "-DUPB_ENABLE_FASTTABLE"],
37    "//conditions:default": _DEFAULT_COPTS,
38})
39
40runfiles_init = """\
41# --- begin runfiles.bash initialization v2 ---
42# Copy-pasted from the Bazel Bash runfiles library v2.
43set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
44source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
45  source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
46  source "$0.runfiles/$f" 2>/dev/null || \
47  source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
48  source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
49  { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
50# --- end runfiles.bash initialization v2 ---
51"""
52
53def _get_real_short_path(file):
54    # For some reason, files from other archives have short paths that look like:
55    #   ../com_google_protobuf/google/protobuf/descriptor.proto
56    short_path = file.short_path
57    if short_path.startswith("../"):
58        second_slash = short_path.index("/", 3)
59        short_path = short_path[second_slash + 1:]
60    return short_path
61
62def _get_real_root(file):
63    real_short_path = _get_real_short_path(file)
64    return file.path[:-len(real_short_path) - 1]
65
66def _get_real_roots(files):
67    roots = {}
68    for file in files:
69        real_root = _get_real_root(file)
70        if real_root:
71            roots[real_root] = True
72    return roots.keys()
73
74def make_shell_script(name, contents, out):
75    contents = contents.replace("$", "$$")
76    native.genrule(
77        name = "gen_" + name,
78        outs = [out],
79        cmd = "(cat <<'HEREDOC'\n%s\nHEREDOC\n) > $@" % contents,
80    )
81