Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic platform detection from inbound crosstool_top and cpu #2451

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions go/platform/crosstool.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2020 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.

def _match_apple(crosstool_top, cpu):
"""_match_apple will try to detect wether the inbound crosstool/cpu is
targeting the Apple ecosystem. Apple crosstool CPUs are prefixed, so
matching is easy."""
platform = {
"darwin_x86_64": "darwin_amd64",
"ios_arm64": "ios_arm64",
"ios_armv7": "ios_arm",
"ios_i386": "ios_386",
"ios_x86_64": "ios_amd64",
}.get(cpu)
if platform:
return "{}_cgo".format(platform)
return None

def _match_android(crosstool_top, cpu):
"""_match_android will try to detect wether the inbound crosstool is the
Android NDK toolchain. It can either be `//external:android/crosstool` or be
part of the `@androidndk` workspace. After that, translate Android CPUs to
Go CPUs."""
if str(crosstool_top) == "//external:android/crosstool" or \
crosstool_top.workspace_name == "androidndk":
platform_cpu = {
"arm64-v8a": "arm64",
"armeabi-v7a": "arm",
"x86": "386",
"x86_64": "amd64",
}.get(cpu)
if platform_cpu:
return "android_{}_cgo".format(platform_cpu)
return None

def platform_from_crosstool(crosstool_top, cpu):
"""platform_from_crosstool runs matchers against the crosstool_top/cpu pair
to automatically infer the target platform."""
matchers = [
_match_apple,
_match_android,
]
for matcher in matchers:
platform = matcher(crosstool_top, cpu)
if platform:
return "@io_bazel_rules_go//go/toolchain:{}".format(platform)
return None
13 changes: 13 additions & 0 deletions go/private/rules/transition.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ load(
"@io_bazel_rules_go_name_hack//:def.bzl",
"IS_RULES_GO",
)
load(
"@io_bazel_rules_go//go/platform:crosstool.bzl",
"platform_from_crosstool",
)

def _filter_transition_label(label):
"""Transforms transition labels for the current workspace.
Expand Down Expand Up @@ -106,6 +110,8 @@ def _go_transition_impl(settings, attr):
goarch = getattr(attr, "goarch", "auto")
pure = getattr(attr, "pure", "auto")
_check_ternary("pure", pure)
crosstool_top = settings.pop("//command_line_option:crosstool_top")
cpu = settings.pop("//command_line_option:cpu")
if goos != "auto" or goarch != "auto":
if goos == "auto":
fail("goos must be set if goarch is set")
Expand All @@ -118,6 +124,11 @@ def _go_transition_impl(settings, attr):
fail('pure is "off" but cgo is not supported on {} {}'.format(goos, goarch))
platform = "@io_bazel_rules_go//go/toolchain:{}_{}{}".format(goos, goarch, "_cgo" if cgo else "")
settings["//command_line_option:platforms"] = platform
else:
# If not auto, try to detect the platform the inbound crosstool/cpu.
platform = platform_from_crosstool(crosstool_top, cpu)
if platform:
settings["//command_line_option:platforms"] = platform
if pure != "auto":
pure_label = _filter_transition_label("@io_bazel_rules_go//go/config:pure")
settings[pure_label] = pure == "on"
Expand All @@ -138,6 +149,8 @@ def _go_transition_impl(settings, attr):
go_transition = transition(
implementation = _go_transition_impl,
inputs = [_filter_transition_label(label) for label in [
"//command_line_option:cpu",
"//command_line_option:crosstool_top",
"//command_line_option:platforms",
"@io_bazel_rules_go//go/config:static",
"@io_bazel_rules_go//go/config:msan",
Expand Down