diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 13ab495a99..9a1f107aa9 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -171,7 +171,6 @@ platforms: - "-//tests/core/coverage:coverage_test_test" - "-//tests/core/coverage:cross_cover_test_test" - "-//tests/core/cross:cross_test" - - "-//tests/core/cross:ios_select_test" - "-//tests/core/go_binary:go_default_test" - "-//tests/core/go_path:go_path" - "-//tests/core/go_path:go_path_test" @@ -341,7 +340,6 @@ platforms: - "-//tests/core/coverage:coverage_test_test" - "-//tests/core/coverage:cross_cover_test_test" - "-//tests/core/cross:cross_test" - - "-//tests/core/cross:ios_select_test" - "-//tests/core/go_binary:go_default_test" - "-//tests/core/go_binary:stamp_test" - "-//tests/core/go_path:go_path" diff --git a/go/platform/list.bzl b/go/platform/list.bzl index 635dc2c169..91985ae696 100644 --- a/go/platform/list.bzl +++ b/go/platform/list.bzl @@ -28,38 +28,45 @@ GOARCH = _GOARCH RACE_GOOS_GOARCH = _RACE_GOOS_GOARCH MSAN_GOOS_GOARCH = _MSAN_GOOS_GOARCH -def _os_constraint(goos): - if goos == "darwin": - return "@io_bazel_rules_go//go/toolchain:is_darwin" - else: - return "@io_bazel_rules_go//go/toolchain:" + goos - -def _arch_constraint(goarch): - return "@io_bazel_rules_go//go/toolchain:" + goarch - def declare_config_settings(): """Generates config_setting targets for each goos, goarch, and valid goos_goarch pair. These targets may be used in select expressions. Each target refers to a corresponding constraint_value in //go/toolchain. - - Note that the "darwin" targets are true when building for either - macOS or iOS. """ for goos in GOOS: native.config_setting( name = goos, - constraint_values = [_os_constraint(goos)], + constraint_values = ["@io_bazel_rules_go//go/toolchain:" + goos], ) for goarch in GOARCH: native.config_setting( name = goarch, - constraint_values = [_arch_constraint(goarch)], + constraint_values = ["@io_bazel_rules_go//go/toolchain:" + goarch], ) for goos, goarch in GOOS_GOARCH: native.config_setting( name = goos + "_" + goarch, constraint_values = [ - _os_constraint(goos), - _arch_constraint(goarch), + "@io_bazel_rules_go//go/toolchain:" + goos, + "@io_bazel_rules_go//go/toolchain:" + goarch, ], ) + + # Additional settings for ios. Unfortunately, we cannot have a "darwin" + # setting that covers both operating systems, so "darwin" here means macOS. + # The "darwin" build tag will be true for both during execution; this only + # matters when evaluating select expressions. + native.config_setting( + name = "ios", + constraint_values = [ + "@bazel_tools//platforms:ios", + ], + ) + for goarch in ("arm", "arm64", "386", "amd64"): + native.config_setting( + name = "ios_" + goarch, + constraint_values = [ + "@bazel_tools//platforms:ios", + "@io_bazel_rules_go//go/toolchain:" + goarch, + ] + ) diff --git a/go/private/compat/compat_repo.bzl b/go/private/compat/compat_repo.bzl index 0e89cd7d22..1273753f50 100644 --- a/go/private/compat/compat_repo.bzl +++ b/go/private/compat/compat_repo.bzl @@ -27,13 +27,6 @@ load("@io_bazel_rules_go//go/private:skylib/lib/versions.bzl", "versions") def _go_rules_compat_impl(ctx): - darwin_tpl_path = ctx.path(Label("@io_bazel_rules_go//go/private:compat/darwin.bzl.tpl")) - if "mac" in ctx.os.name: - default_darwin_constraint_value = "@io_bazel_rules_go//go/toolchain:is_darwin" - else: - default_darwin_constraint_value = "@io_bazel_rules_go//go/toolchain:not_darwin" - ctx.template("darwin.bzl", darwin_tpl_path, {"{default_darwin_constraint_value}": default_darwin_constraint_value}) - ctx.file("BUILD.bazel") ctx.symlink(ctx.attr.impl, "compat.bzl") diff --git a/go/private/compat/darwin.bzl.tpl b/go/private/compat/darwin.bzl.tpl deleted file mode 100644 index 254a8c1afd..0000000000 --- a/go/private/compat/darwin.bzl.tpl +++ /dev/null @@ -1,15 +0,0 @@ -# Copyright 2019 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. - -DEFAULT_DARWIN_CONSTRAINT_VALUE = "{default_darwin_constraint_value}" diff --git a/go/private/go_toolchain.bzl b/go/private/go_toolchain.bzl index 349569e9eb..50b76926de 100644 --- a/go/private/go_toolchain.bzl +++ b/go/private/go_toolchain.bzl @@ -112,6 +112,17 @@ def declare_toolchains(host, sdk, builder): cgo_context_data = "@io_bazel_rules_go//:cgo_context_data" if p.cgo else None + constraints = p.constraints + if p.goos != host_goos or p.goarch != host_goarch: + # When cross-compiling, don't require cgo_off. + # It won't be set on a custom platform outside of //go/toolchain. + constraints = [c for c in constraints if c != "@io_bazel_rules_go//go/toolchain:cgo_off"] + else: + # When compiling for the host platform, don't require cgo_on. + # It won't be set on @bazel_tools//platforms:target_platform, + # which is probably the target. + constraints = [c for c in constraints if c != "@io_bazel_rules_go//go/toolchain:cgo_on"] + go_toolchain( name = impl_name, goos = p.goos, @@ -131,6 +142,6 @@ def declare_toolchains(host, sdk, builder): "@io_bazel_rules_go//go/toolchain:" + host_goos, "@io_bazel_rules_go//go/toolchain:" + host_goarch, ], - target_compatible_with = p.constraints, + target_compatible_with = constraints, toolchain = ":" + impl_name, ) diff --git a/go/private/platforms.bzl b/go/private/platforms.bzl index 45dd4af7a5..e1a14cdabb 100644 --- a/go/private/platforms.bzl +++ b/go/private/platforms.bzl @@ -141,7 +141,6 @@ def _generate_platforms(): GOOS_CONSTRAINTS[goos], GOARCH_CONSTRAINTS[goarch], ] - constraints.append("@io_bazel_rules_go//go/toolchain:" + ("is_darwin" if goos == "darwin" else "not_darwin")) platforms.append(struct( name = goos + "_" + goarch, goos = goos, @@ -162,22 +161,19 @@ def _generate_platforms(): constraints = [ "@bazel_tools//platforms:ios", GOARCH_CONSTRAINTS[goarch], - "@io_bazel_rules_go//go/toolchain:is_darwin", - "@io_bazel_rules_go//go/toolchain:cgo_off", ] platforms.append(struct( name = "ios_" + goarch, goos = "darwin", goarch = goarch, - constraints = constraints, + constraints = constraints + ["@io_bazel_rules_go//go/toolchain:cgo_off"], cgo = False, )) - constraints[-1] = "@io_bazel_rules_go//go/toolchain:cgo_on" platforms.append(struct( name = "ios_" + goarch + "_cgo", goos = "darwin", goarch = goarch, - constraints = constraints, + constraints = constraints + ["@io_bazel_rules_go//go/toolchain:cgo_on"], cgo = True, )) diff --git a/go/toolchain/toolchains.bzl b/go/toolchain/toolchains.bzl index ed505bfcd5..f323f54300 100644 --- a/go/toolchain/toolchains.bzl +++ b/go/toolchain/toolchains.bzl @@ -28,10 +28,6 @@ load( "GOOS_CONSTRAINTS", "PLATFORMS", ) -load( - "@io_bazel_rules_go_compat//:darwin.bzl", - "DEFAULT_DARWIN_CONSTRAINT_VALUE", -) # These symbols should be loaded from sdk.bzl or deps.bzl instead of here.. DEFAULT_VERSION = _DEFAULT_VERSION @@ -48,10 +44,6 @@ def declare_constraints(): To avoid redundancy, if there is an equivalent value in @bazel_tools, we define an alias here instead of another constraint_value. - There is a special constraint_setting for Darwin, "darwin_constraint". - The value is "is_darwin" when the target is macOS or iOS and "not_darwin" - otherwise. - Each platform defined here selects a goos and goarch constraint value. These platforms may be used with --platforms for cross-compilation, though users may create their own platforms (and @@ -81,24 +73,8 @@ def declare_constraints(): actual = constraint, ) - native.constraint_setting( - name = "darwin_constraint", - default_constraint_value = DEFAULT_DARWIN_CONSTRAINT_VALUE, - ) - - native.constraint_value( - name = "is_darwin", - constraint_setting = ":darwin_constraint", - ) - - native.constraint_value( - name = "not_darwin", - constraint_setting = ":darwin_constraint", - ) - native.constraint_setting( name = "cgo_constraint", - default_constraint_value = ":cgo_on", ) native.constraint_value( diff --git a/tests/core/cross/BUILD.bazel b/tests/core/cross/BUILD.bazel index 13a14fdda6..fda43497ae 100644 --- a/tests/core/cross/BUILD.bazel +++ b/tests/core/cross/BUILD.bazel @@ -51,35 +51,9 @@ go_test( deps = ["//go/tools/bazel:go_default_library"], ) -bazel_test( +go_bazel_test( name = "ios_select_test", - args = ["--platforms=@io_bazel_rules_go//go/toolchain:ios_amd64"], - command = "build", - targets = [":ios_lib"], -) - -go_library( - name = "use_ios_lib", - importpath = "github.com/bazelbuild/rules_go/tests/core/cross/use_ios_lib", - deps = select({ - ":is_osx": [":ios_lib"], - "//conditions:default": [], - }), -) - -config_setting( - name = "is_osx", - constraint_values = ["@bazel_tools//platforms:osx"], -) - -go_library( - name = "ios_lib", - srcs = select({ - "@io_bazel_rules_go//go/platform:darwin": ["ios_good.go"], - "//conditions:default": ["ios_bad.go"], - }), - importpath = "github.com/bazelbuild/rules_go/tests/core/cross/ios_lib", - tags = ["manual"], + srcs = ["ios_select_test.go"], ) go_bazel_test( diff --git a/tests/core/cross/ios_bad.go b/tests/core/cross/ios_bad.go deleted file mode 100644 index d49f52bc8d..0000000000 --- a/tests/core/cross/ios_bad.go +++ /dev/null @@ -1,2 +0,0 @@ -donotbuild - diff --git a/tests/core/cross/ios_good.go b/tests/core/cross/ios_good.go deleted file mode 100644 index d76c722798..0000000000 --- a/tests/core/cross/ios_good.go +++ /dev/null @@ -1 +0,0 @@ -package ios_lib diff --git a/tests/core/cross/ios_select_test.go b/tests/core/cross/ios_select_test.go new file mode 100644 index 0000000000..fa871c6289 --- /dev/null +++ b/tests/core/cross/ios_select_test.go @@ -0,0 +1,66 @@ +// Copyright 2019 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. + +package ios_select_test + +import ( + "testing" + + "github.com/bazelbuild/rules_go/go/tools/bazel_testing" +) + +func TestMain(m *testing.M) { + bazel_testing.TestMain(m, bazel_testing.Args{ + Main: ` +-- BUILD.bazel -- +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "use_ios_lib", + importpath = "github.com/bazelbuild/rules_go/tests/core/cross/use_ios_lib", + deps = select({ + ":is_osx": [":ios_lib"], + "//conditions:default": [], + }), +) + +config_setting( + name = "is_osx", + constraint_values = ["@bazel_tools//platforms:osx"], +) + +go_library( + name = "ios_lib", + srcs = select({ + "@io_bazel_rules_go//go/platform:darwin": ["ios_good.go"], + "@io_bazel_rules_go//go/platform:ios": ["ios_good.go"], + "//conditions:default": ["ios_bad.go"], + }), + importpath = "github.com/bazelbuild/rules_go/tests/core/cross/ios_lib", +) + +-- ios_good.go -- +package ios_lib + +-- ios_bad.go -- +donotbuild +`, + }) +} + +func Test(t *testing.T) { + if err := bazel_testing.RunBazel("build", "--platforms=@io_bazel_rules_go//go/toolchain:ios_amd64", ":ios_lib"); err != nil { + t.Fatal(err) + } +}