|
| 1 | +# Copyright 2017 The Bazel Authors. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Repo rule to register toolchains required for py_image and py3_image rules. |
| 15 | +""" |
| 16 | + |
| 17 | +load( |
| 18 | + "@bazel_tools//tools/cpp:lib_cc_configure.bzl", |
| 19 | + "get_cpu_value", |
| 20 | +) |
| 21 | + |
| 22 | +def _impl(repository_ctx): |
| 23 | + """Core implementation of _py_toolchains.""" |
| 24 | + |
| 25 | + cpu_value = get_cpu_value(repository_ctx) |
| 26 | + env = repository_ctx.os.environ |
| 27 | + if "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN" in env and env["BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN"] == "1": |
| 28 | + # Create an alias to the bazel_tools local toolchain as no cpp toolchain will be produced |
| 29 | + repository_ctx.file("BUILD", content = ("""# Alias to local toolchain |
| 30 | +package(default_visibility = ["//visibility:public"]) |
| 31 | +
|
| 32 | +licenses(["notice"]) # Apache 2.0 |
| 33 | +
|
| 34 | +alias( |
| 35 | + name = "container_cc_toolchain", |
| 36 | + actual = "@bazel_tools//tools/cpp:cc-toolchain-local", |
| 37 | +) |
| 38 | +
|
| 39 | +"""), executable = False) |
| 40 | + |
| 41 | + else: |
| 42 | + if cpu_value == "x64_windows": |
| 43 | + # Note this is not well tested |
| 44 | + cpu_value = "x64_windows_msys" |
| 45 | + toolchain = "@local_config_cc//:cc-compiler-%s" % cpu_value |
| 46 | + if cpu_value == "darwin": |
| 47 | + # This needs further testing too. |
| 48 | + toolchain = "@bazel_tools//tools/cpp:cc-compiler-local" |
| 49 | + |
| 50 | + repository_ctx.file("BUILD", content = ("""# Toolchain required for xx_image targets that rely on xx_binary |
| 51 | +# which transitively require a C/C++ toolchain (currently only |
| 52 | +# py_binary). This one is for local execution and will be required |
| 53 | +# with versions of Bazel > 1.0.0 |
| 54 | +package(default_visibility = ["//visibility:public"]) |
| 55 | +
|
| 56 | +licenses(["notice"]) # Apache 2.0 |
| 57 | +
|
| 58 | +load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS") |
| 59 | +
|
| 60 | +toolchain( |
| 61 | + name = "container_cc_toolchain", |
| 62 | + exec_compatible_with = HOST_CONSTRAINTS + ["@io_bazel_rules_docker//platforms:run_in_container"], |
| 63 | + target_compatible_with = HOST_CONSTRAINTS, |
| 64 | + toolchain = "%s", |
| 65 | + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", |
| 66 | +) |
| 67 | +""") % toolchain, executable = False) |
| 68 | + |
| 69 | +py_toolchains = repository_rule( |
| 70 | + attrs = {}, |
| 71 | + implementation = _impl, |
| 72 | +) |
0 commit comments