Skip to content

Commit

Permalink
fix rules_docker with bazel head (#1182)
Browse files Browse the repository at this point in the history
* fix rules_docker with bazel head

* fix on mac
  • Loading branch information
nlopezgi authored and smukherj1 committed Oct 7, 2019
1 parent 3312ab4 commit 190569e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
9 changes: 8 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,14 @@ dockerfile_image(
]]

# Register the default py_toolchain / platform for containerized execution
register_toolchains("//toolchains:container_py_toolchain")
load("//toolchains:py_toolchains.bzl", "py_toolchains")

py_toolchains(name = "container_py_toolchain")

register_toolchains(
"//toolchains:container_py_toolchain",
"@container_py_toolchain//:container_cc_toolchain",
)

register_execution_platforms("//platforms:local_container_platform")

Expand Down
11 changes: 10 additions & 1 deletion python/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ load(
"//repositories:go_repositories.bzl",
_go_deps = "go_deps",
)
load(
"//toolchains:py_toolchains.bzl",
_py_toolchains = "py_toolchains",
)

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

# Register the default py_toolchain / platform for containerized execution
native.register_toolchains("@io_bazel_rules_docker//toolchains:container_py_toolchain")
if "container_py_toolchain" not in native.existing_rules().keys():
_py_toolchains(name = "container_py_toolchain")
native.register_toolchains(
"@io_bazel_rules_docker//toolchains:container_py_toolchain",
"@container_py_toolchain//:container_cc_toolchain",
)
native.register_execution_platforms("@io_bazel_rules_docker//platforms:local_container_platform")

excludes = native.existing_rules().keys()
Expand Down
11 changes: 10 additions & 1 deletion python3/image.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ load(
"//repositories:go_repositories.bzl",
_go_deps = "go_deps",
)
load(
"//toolchains:py_toolchains.bzl",
_py_toolchains = "py_toolchains",
)

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

# Register the default py_toolchain / platform for containerized execution
native.register_toolchains("@io_bazel_rules_docker//toolchains:container_py_toolchain")
if "container_py_toolchain" not in native.existing_rules().keys():
_py_toolchains(name = "container_py_toolchain")
native.register_toolchains(
"@io_bazel_rules_docker//toolchains:container_py_toolchain",
"@container_py_toolchain//:container_cc_toolchain",
)
native.register_execution_platforms("@io_bazel_rules_docker//platforms:local_container_platform")

excludes = native.existing_rules().keys()
Expand Down
72 changes: 72 additions & 0 deletions toolchains/py_toolchains.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Repo rule to register toolchains required for py_image and py3_image rules.
"""

load(
"@bazel_tools//tools/cpp:lib_cc_configure.bzl",
"get_cpu_value",
)

def _impl(repository_ctx):
"""Core implementation of _py_toolchains."""

cpu_value = get_cpu_value(repository_ctx)
env = repository_ctx.os.environ
if "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN" in env and env["BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN"] == "1":
# Create an alias to the bazel_tools local toolchain as no cpp toolchain will be produced
repository_ctx.file("BUILD", content = ("""# Alias to local toolchain
package(default_visibility = ["//visibility:public"])
licenses(["notice"]) # Apache 2.0
alias(
name = "container_cc_toolchain",
actual = "@bazel_tools//tools/cpp:cc-toolchain-local",
)
"""), executable = False)

else:
if cpu_value == "x64_windows":
# Note this is not well tested
cpu_value = "x64_windows_msys"
toolchain = "@local_config_cc//:cc-compiler-%s" % cpu_value
if cpu_value == "darwin":
# This needs further testing too.
toolchain = "@bazel_tools//tools/cpp:cc-compiler-local"

repository_ctx.file("BUILD", content = ("""# Toolchain required for xx_image targets that rely on xx_binary
# which transitively require a C/C++ toolchain (currently only
# py_binary). This one is for local execution and will be required
# with versions of Bazel > 1.0.0
package(default_visibility = ["//visibility:public"])
licenses(["notice"]) # Apache 2.0
load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS")
toolchain(
name = "container_cc_toolchain",
exec_compatible_with = HOST_CONSTRAINTS + ["@io_bazel_rules_docker//platforms:run_in_container"],
target_compatible_with = HOST_CONSTRAINTS,
toolchain = "%s",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
""") % toolchain, executable = False)

py_toolchains = repository_rule(
attrs = {},
implementation = _impl,
)

0 comments on commit 190569e

Please sign in to comment.