Skip to content

Commit

Permalink
Only infer platforms from crosstool/cpu if at default (bazelbuild#3485)
Browse files Browse the repository at this point in the history
If the current target platform differs from the default value (the host
platform), then we don't want to override it with a value inferred from
the legacy --cpu and --crosstool_top flags. Otherwise it would become
impossible to "platformize" a Go build without having a matching
platform mappings file.

This is not expected to break any existing use cases and if it does, it
can and should be worked around with a platform mappings file that maps
the legacy flag values to `//go/platform` targets.
  • Loading branch information
fmeum authored and jacqueline.lee committed Jul 19, 2023
1 parent 09a951d commit 85c1ca6
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions go/private/rules/transition.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ load(
"platform_from_crosstool",
)

_DEFAULT_PLATFORMS_VALUE = [Label("@local_config_platform//:host")]
_PLATFORMS_LABEL = "//command_line_option:platforms"

# A list of rules_go settings that are possibly set by go_transition.
# Keep their package name in sync with the implementation of
# _original_setting_key.
Expand Down Expand Up @@ -110,10 +113,16 @@ def _go_transition_impl(settings, attr):
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 the current target platform differs from the default value (the
# host platform), then we don't want to override it with a value
# inferred from the legacy --cpu and --crosstool_top flags. Otherwise
# it would become impossible to "platformize" a Go build without having
# a matching platform mappings file.
if settings[_PLATFORMS_LABEL] == _DEFAULT_PLATFORMS_VALUE:
# Detect the platform the inbound crosstool/cpu.
platform = platform_from_crosstool(crosstool_top, cpu)
if platform:
settings[_PLATFORMS_LABEL] = platform

tags = getattr(attr, "gotags", [])
if tags:
Expand Down

0 comments on commit 85c1ca6

Please sign in to comment.