|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -def cc_library(name, static_libstdcxx = False, **kwargs): |
| 15 | +def _with_libstdcxx(rule, name, gcc_toolchain_prefix = "gcc_toolchain", static_libstdcxx = False, **kwargs): |
| 16 | + target_name = "libstdcxx{}".format("_static" if static_libstdcxx else "") |
16 | 17 | deps = kwargs.pop("deps", []) |
17 | 18 | deps += select({ |
18 | | - "@platforms//cpu:aarch64": ["@gcc_toolchain_aarch64//:libstdcxx{}".format("_static" if static_libstdcxx else "")], |
19 | | - "@platforms//cpu:armv7": ["@gcc_toolchain_armv7//:libstdcxx{}".format("_static" if static_libstdcxx else "")], |
20 | | - "@platforms//cpu:x86_64": ["@gcc_toolchain_x86_64//:libstdcxx{}".format("_static" if static_libstdcxx else "")], |
| 19 | + "@platforms//cpu:aarch64": ["{}_aarch64//:{}".format(gcc_toolchain_prefix, target_name)], |
| 20 | + "@platforms//cpu:armv7": ["{}_armv7//:{}".format(gcc_toolchain_prefix, target_name)], |
| 21 | + "@platforms//cpu:x86_64": ["{}_x86_64//:{}".format(gcc_toolchain_prefix, target_name)], |
21 | 22 | }) |
22 | | - native.cc_library( |
| 23 | + rule( |
23 | 24 | name = name, |
24 | 25 | deps = deps, |
25 | 26 | **kwargs |
26 | 27 | ) |
27 | 28 |
|
| 29 | +def cc_library(name, static_libstdcxx = False, **kwargs): |
| 30 | + _with_libstdcxx( |
| 31 | + rule = native.cc_library, |
| 32 | + name = name, |
| 33 | + gcc_toolchain_prefix = "@gcc_toolchain", |
| 34 | + static_libstdcxx = static_libstdcxx, |
| 35 | + **kwargs, |
| 36 | + ) |
| 37 | + |
28 | 38 | def cc_binary(name, static_libstdcxx = False, **kwargs): |
29 | | - deps = kwargs.pop("deps", []) |
30 | | - deps += select({ |
31 | | - "@platforms//cpu:aarch64": ["@gcc_toolchain_aarch64//:libstdcxx{}".format("_static" if static_libstdcxx else "")], |
32 | | - "@platforms//cpu:armv7": ["@gcc_toolchain_armv7//:libstdcxx{}".format("_static" if static_libstdcxx else "")], |
33 | | - "@platforms//cpu:x86_64": ["@gcc_toolchain_x86_64//:libstdcxx{}".format("_static" if static_libstdcxx else "")], |
34 | | - }) |
35 | | - native.cc_binary( |
| 39 | + _with_libstdcxx( |
| 40 | + rule = native.cc_binary, |
36 | 41 | name = name, |
37 | | - deps = deps, |
38 | | - **kwargs |
| 42 | + gcc_toolchain_prefix = "@gcc_toolchain", |
| 43 | + static_libstdcxx = static_libstdcxx, |
| 44 | + **kwargs, |
39 | 45 | ) |
40 | 46 |
|
41 | 47 | def cc_test(name, static_libstdcxx = False, **kwargs): |
42 | | - deps = kwargs.pop("deps", []) |
43 | | - deps += select({ |
44 | | - "@platforms//cpu:aarch64": ["@gcc_toolchain_aarch64//:libstdcxx{}".format("_static" if static_libstdcxx else "")], |
45 | | - "@platforms//cpu:armv7": ["@gcc_toolchain_armv7//:libstdcxx{}".format("_static" if static_libstdcxx else "")], |
46 | | - "@platforms//cpu:x86_64": ["@gcc_toolchain_x86_64//:libstdcxx{}".format("_static" if static_libstdcxx else "")], |
47 | | - }) |
48 | | - native.cc_test( |
| 48 | + _with_libstdcxx( |
| 49 | + rule = native.cc_test, |
49 | 50 | name = name, |
50 | | - deps = deps, |
51 | | - **kwargs |
| 51 | + gcc_toolchain_prefix = "@gcc_toolchain", |
| 52 | + static_libstdcxx = static_libstdcxx, |
| 53 | + **kwargs, |
| 54 | + ) |
| 55 | + |
| 56 | +# These targets should only be used to patch dependencies of gcc-toolchain that should use our toolchain. |
| 57 | +# For instance, we depend on protobuf to run `examples/protobuf`, but we also want protobuf to link against libstdc++, so we patch it to use our version of the targets. |
| 58 | +# You may be wondering: Why can't we use the "public" versions of the targets? |
| 59 | +# In bzlmod, the repositories `@gcc_toolchain_<arch>` that we create in MODULE.bazel are not visible to our dependencies. |
| 60 | +# Furthermore, this repository, `@gcc_toolchain`, is _also_ not visible (probably because it would be a circular dependency), |
| 61 | +# so we can't even patch the `protobuf/MODULE.bazel` file to bring the `@gcc_toolchain_<arch>` repositories into scope. |
| 62 | +# |
| 63 | +# Therefore, we need to use the "absolute path" of these repositories to address them (main~extension~toolchain. |
| 64 | +# Documentation: https://bazel.build/external/extension#repository_names_and_visibility |
| 65 | +# |
| 66 | +# Yes, this is brittle and the docs warn us against it, |
| 67 | +# but I haven't found any other way to reference `@gcc_toolchain_<arch>` targets from `@protobuf`. |
| 68 | +def cc_library_internal(name, static_libstdcxx = False, **kwargs): |
| 69 | + _with_libstdcxx( |
| 70 | + rule = native.cc_library, |
| 71 | + name = name, |
| 72 | + gcc_toolchain_prefix = "@@_main~gcc_toolchains~gcc_toolchain", |
| 73 | + static_libstdcxx = static_libstdcxx, |
| 74 | + **kwargs, |
| 75 | + ) |
| 76 | + |
| 77 | +def cc_binary_internal(name, static_libstdcxx = False, **kwargs): |
| 78 | + _with_libstdcxx( |
| 79 | + rule = native.cc_binary, |
| 80 | + name = name, |
| 81 | + gcc_toolchain_prefix = "@@_main~gcc_toolchains~gcc_toolchain", |
| 82 | + static_libstdcxx = static_libstdcxx, |
| 83 | + **kwargs, |
| 84 | + ) |
| 85 | + |
| 86 | +def cc_test_internal(name, static_libstdcxx = False, **kwargs): |
| 87 | + _with_libstdcxx( |
| 88 | + rule = native.cc_test, |
| 89 | + name = name, |
| 90 | + gcc_toolchain_prefix = "@@_main~gcc_toolchains~gcc_toolchain", |
| 91 | + static_libstdcxx = static_libstdcxx, |
| 92 | + **kwargs, |
52 | 93 | ) |
0 commit comments