Skip to content

Commit 190569e

Browse files
nlopezgismukherj1
authored andcommitted
fix rules_docker with bazel head (#1182)
* fix rules_docker with bazel head * fix on mac
1 parent 3312ab4 commit 190569e

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

WORKSPACE

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,14 @@ dockerfile_image(
401401
]]
402402

403403
# Register the default py_toolchain / platform for containerized execution
404-
register_toolchains("//toolchains:container_py_toolchain")
404+
load("//toolchains:py_toolchains.bzl", "py_toolchains")
405+
406+
py_toolchains(name = "container_py_toolchain")
407+
408+
register_toolchains(
409+
"//toolchains:container_py_toolchain",
410+
"@container_py_toolchain//:container_cc_toolchain",
411+
)
405412

406413
register_execution_platforms("//platforms:local_container_platform")
407414

python/image.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ load(
2929
"//repositories:go_repositories.bzl",
3030
_go_deps = "go_deps",
3131
)
32+
load(
33+
"//toolchains:py_toolchains.bzl",
34+
_py_toolchains = "py_toolchains",
35+
)
3236

3337
# Load the resolved digests.
3438
load(":python.bzl", "DIGESTS")
@@ -42,7 +46,12 @@ def repositories():
4246
_go_deps()
4347

4448
# Register the default py_toolchain / platform for containerized execution
45-
native.register_toolchains("@io_bazel_rules_docker//toolchains:container_py_toolchain")
49+
if "container_py_toolchain" not in native.existing_rules().keys():
50+
_py_toolchains(name = "container_py_toolchain")
51+
native.register_toolchains(
52+
"@io_bazel_rules_docker//toolchains:container_py_toolchain",
53+
"@container_py_toolchain//:container_cc_toolchain",
54+
)
4655
native.register_execution_platforms("@io_bazel_rules_docker//platforms:local_container_platform")
4756

4857
excludes = native.existing_rules().keys()

python3/image.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ load(
2828
"//repositories:go_repositories.bzl",
2929
_go_deps = "go_deps",
3030
)
31+
load(
32+
"//toolchains:py_toolchains.bzl",
33+
_py_toolchains = "py_toolchains",
34+
)
3135

3236
# Load the resolved digests.
3337
load(":python3.bzl", "DIGESTS")
@@ -41,7 +45,12 @@ def repositories():
4145
_go_deps()
4246

4347
# Register the default py_toolchain / platform for containerized execution
44-
native.register_toolchains("@io_bazel_rules_docker//toolchains:container_py_toolchain")
48+
if "container_py_toolchain" not in native.existing_rules().keys():
49+
_py_toolchains(name = "container_py_toolchain")
50+
native.register_toolchains(
51+
"@io_bazel_rules_docker//toolchains:container_py_toolchain",
52+
"@container_py_toolchain//:container_cc_toolchain",
53+
)
4554
native.register_execution_platforms("@io_bazel_rules_docker//platforms:local_container_platform")
4655

4756
excludes = native.existing_rules().keys()

toolchains/py_toolchains.bzl

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)