Add platforms to apple crosstool transition
This is needed to enable C++ toolchainisation in Bazel.
PiperOrigin-RevId: 558140728
Change-Id: Id0b532de76c9e403790107f6916e8a3a8be23d95
diff --git a/src/MODULE.tools b/src/MODULE.tools
index ba2ae27..d7a7e0d 100644
--- a/src/MODULE.tools
+++ b/src/MODULE.tools
@@ -37,3 +37,6 @@
remote_android_extensions = use_extension("//tools/android:android_extensions.bzl", "remote_android_tools_extensions")
use_repo(remote_android_extensions, "android_gmaven_r8", "android_tools")
+
+# Platforms used by transitions in builtins
+bazel_dep(name = "apple_support", version = "1.5.0", repo_name = "build_bazel_apple_support")
diff --git a/src/main/starlark/builtins_bzl/common/objc/semantics.bzl b/src/main/starlark/builtins_bzl/common/objc/semantics.bzl
index 970e4d2..9601361 100644
--- a/src/main/starlark/builtins_bzl/common/objc/semantics.bzl
+++ b/src/main/starlark/builtins_bzl/common/objc/semantics.bzl
@@ -16,6 +16,28 @@
load(":common/cc/cc_common.bzl", "cc_common")
+_CPU_TO_PLATFORM = {
+ "darwin_x86_64": "@build_bazel_apple_support//platforms:darwin_x86_64",
+ "darwin_arm64": "@build_bazel_apple_support//platforms:darwin_arm64",
+ "darwin_arm64e": "@build_bazel_apple_support//platforms:darwin_arm64e",
+ "ios_x86_64": "@build_bazel_apple_support//platforms:ios_x86_64",
+ "ios_arm64": "@build_bazel_apple_support//platforms:ios_arm64",
+ "ios_sim_arm64": "@build_bazel_apple_support//platforms:ios_sim_arm64",
+ "ios_arm64e": "@build_bazel_apple_support//platforms:ios_arm64e",
+ "tvos_sim_arm64": "@build_bazel_apple_support//platforms:tvos_sim_arm64",
+ "tvos_arm64": "@build_bazel_apple_support//platforms:tvos_arm64",
+ "tvos_x86_64": "@build_bazel_apple_support//platforms:tvos_x86_64",
+ "visionos_arm64": "@build_bazel_apple_support//platforms:visionos_arm64",
+ "visionos_sim_arm64": "@build_bazel_apple_support//platforms:visionos_sim_arm64",
+ "visionos_x86_64": "@build_bazel_apple_support//platforms:visionos_x86_64",
+ "watchos_armv7k": "@build_bazel_apple_support//platforms:watchos_armv7k",
+ "watchos_arm64": "@build_bazel_apple_support//platforms:watchos_arm64",
+ "watchos_device_arm64": "@build_bazel_apple_support//platforms:watchos_arm64",
+ "watchos_device_arm64e": "@build_bazel_apple_support//platforms:watchos_arm64e",
+ "watchos_arm64_32": "@build_bazel_apple_support//platforms:watchos_arm64_32",
+ "watchos_x86_64": "@build_bazel_apple_support//platforms:watchos_x86_64",
+}
+
def _check_toolchain_supports_objc_compile(ctx, cc_toolchain):
feature_configuration = cc_common.configure_features(
ctx = ctx,
@@ -47,4 +69,5 @@
get_semantics = _get_semantics,
get_repo = _get_repo,
get_licenses_attr = _get_licenses_attr,
+ cpu_to_platform = lambda cpu: _CPU_TO_PLATFORM[cpu],
)
diff --git a/src/main/starlark/builtins_bzl/common/objc/transitions.bzl b/src/main/starlark/builtins_bzl/common/objc/transitions.bzl
index 67fedd2..5f535fd 100644
--- a/src/main/starlark/builtins_bzl/common/objc/transitions.bzl
+++ b/src/main/starlark/builtins_bzl/common/objc/transitions.bzl
@@ -14,6 +14,8 @@
"""Definition of incoming apple crosstool transition."""
+load(":common/objc/semantics.bzl", "semantics")
+
transition = _builtins.toplevel.transition
def _cpu_string(platform_type, settings):
@@ -118,7 +120,7 @@
return {}
# Ensure platforms aren't set so that platform mapping can take place.
- return _output_dictionary(settings, cpu, platform_type, [])
+ return _output_dictionary(settings, cpu, platform_type, semantics.cpu_to_platform(cpu))
_apple_rule_base_transition_inputs = [
"//command_line_option:apple configuration distinguisher",
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java
index 876146a..65d8fcf 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java
@@ -63,6 +63,7 @@
String androidGmavenR8Workspace = config.getPath("android_gmaven_r8").getPathString();
String localConfigPlatformWorkspace =
config.getPath("local_config_platform_workspace").getPathString();
+ String appleSupport = config.getPath("build_bazel_apple_support").getPathString();
return ImmutableList.of(
"# __SKIP_WORKSPACE_PREFIX__",
@@ -85,6 +86,7 @@
"local_repository(name = 'rules_java', path = '" + rulesJavaWorkspace + "')",
"local_repository(name = 'rules_java_builtin', path = '" + rulesJavaWorkspace + "')",
"local_repository(name = 'android_gmaven_r8', path = '" + androidGmavenR8Workspace + "')",
+ "local_repository(name = 'build_bazel_apple_support', path = '" + appleSupport + "')",
"register_toolchains('@rules_java//java/toolchains/runtime:all')",
"register_toolchains('@rules_java//java/toolchains/javac:all')",
"bind(name = 'android/sdk', actual='@bazel_tools//tools/android:sdk')",
@@ -111,7 +113,8 @@
"local_config_xcode",
"platforms",
"rules_java",
- "rules_java_builtin");
+ "rules_java_builtin",
+ "build_bazel_apple_support");
}
@Override
@@ -138,6 +141,7 @@
"local_config_platform_workspace/WORKSPACE", "workspace(name = 'local_config_platform')");
config.create(
"local_config_platform_workspace/MODULE.bazel", "module(name = 'local_config_platform')");
+ config.create("build_bazel_apple_support/WORKSPACE", "workspace(name = 'apple_support')");
config.create("embedded_tools/WORKSPACE", "workspace(name = 'bazel_tools')");
Runfiles runfiles = Runfiles.create();
for (String filename :
diff --git a/src/test/java/com/google/devtools/build/lib/packages/util/MockObjcSupport.java b/src/test/java/com/google/devtools/build/lib/packages/util/MockObjcSupport.java
index 03a5ae7..6b3ee08 100644
--- a/src/test/java/com/google/devtools/build/lib/packages/util/MockObjcSupport.java
+++ b/src/test/java/com/google/devtools/build/lib/packages/util/MockObjcSupport.java
@@ -141,7 +141,7 @@
// Any device, simulator or maccatalyst platforms created by Apple tests should consider
// building on one of these targets as parents, to ensure that the proper constraints are set.
config.create(
- TestConstants.CONSTRAINTS_PATH + "/apple/BUILD",
+ TestConstants.APPLE_PLATFORM_PATH + "/BUILD",
"package(default_visibility=['//visibility:public'])",
"licenses(['notice'])",
"platform(",
@@ -157,22 +157,31 @@
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:ios',",
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:arm64',",
" ],",
- ")",
- "platform(",
- " name = 'ios_x86_64',",
- " constraint_values = [",
- " '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:ios',",
- " '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:x86_64',",
- " ],",
- ")",
- "platform(",
- " name = 'watchos_x86_64',",
- " constraint_values = [",
- " '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:watchos',",
- " '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:x86_64',",
- " ],",
")");
+ String[] simulatorPlatforms = {
+ "platform(",
+ " name = 'ios_x86_64',",
+ " constraint_values = [",
+ " '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:ios',",
+ " '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:x86_64',",
+ " ],",
+ ")",
+ "platform(",
+ " name = 'watchos_x86_64',",
+ " constraint_values = [",
+ " '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:watchos',",
+ " '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:x86_64',",
+ " ],",
+ ")"
+ };
+
+ if (TestConstants.PRODUCT_NAME.equals("bazel")) {
+ config.append(TestConstants.APPLE_PLATFORM_PATH + "/BUILD", simulatorPlatforms);
+ } else {
+ config.create(TestConstants.APPLE_PLATFORM_PATH + "/simulator/BUILD", simulatorPlatforms);
+ }
+
for (String tool : ImmutableSet.of("objc_dummy.mm", "gcov", "testrunner", "mcov", "libtool")) {
config.create(TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/objc/" + tool);
}
diff --git a/src/test/java/com/google/devtools/build/lib/rules/objc/ApplePlatformsToolchainSelectionTest.java b/src/test/java/com/google/devtools/build/lib/rules/objc/ApplePlatformsToolchainSelectionTest.java
index f479cc3..d08b7ba 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/objc/ApplePlatformsToolchainSelectionTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/objc/ApplePlatformsToolchainSelectionTest.java
@@ -59,7 +59,7 @@
// Verify the macOS platform.
ConfiguredTarget darwinPlatform =
- getConfiguredTarget(TestConstants.CONSTRAINTS_PACKAGE_ROOT + "apple:darwin_x86_64");
+ getConfiguredTarget(TestConstants.APPLE_PLATFORM_PACKAGE_ROOT + ":darwin_x86_64");
PlatformInfo darwinPlatformInfo = PlatformProviderUtils.platform(darwinPlatform);
assertThat(darwinPlatformInfo).isNotNull();
}
@@ -81,7 +81,7 @@
// Verify the iOS 64 bit device platform.
ConfiguredTarget iosDevicePlatform =
- getConfiguredTarget(TestConstants.CONSTRAINTS_PACKAGE_ROOT + "apple:ios_arm64");
+ getConfiguredTarget(TestConstants.APPLE_PLATFORM_PACKAGE_ROOT + ":ios_arm64");
PlatformInfo iosDevicePlatformInfo = PlatformProviderUtils.platform(iosDevicePlatform);
assertThat(iosDevicePlatformInfo).isNotNull();
}
diff --git a/src/test/java/com/google/devtools/build/lib/rules/objc/J2ObjcLibraryTest.java b/src/test/java/com/google/devtools/build/lib/rules/objc/J2ObjcLibraryTest.java
index d071df3..192bff4 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/objc/J2ObjcLibraryTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/objc/J2ObjcLibraryTest.java
@@ -20,7 +20,6 @@
import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.packages.util.MockJ2ObjcSupport;
-import com.google.devtools.build.lib.packages.util.MockObjcSupport;
import com.google.devtools.build.lib.packages.util.MockProtoSupport;
import com.google.devtools.build.lib.testutil.TestConstants;
import java.util.Collection;
@@ -74,7 +73,6 @@
" name = 'transpile',",
" tags = ['__J2OBJC_LIBRARY_MIGRATION_DO_NOT_USE_WILL_BREAK__'],",
" deps = ['test'])");
- MockObjcSupport.setup(mockToolsConfig);
MockJ2ObjcSupport.setup(mockToolsConfig);
MockProtoSupport.setup(mockToolsConfig);
diff --git a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcBuildVariablesTest.java b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcBuildVariablesTest.java
index a0b7003..f752f01 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcBuildVariablesTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcBuildVariablesTest.java
@@ -74,7 +74,6 @@
@Test
public void testAppleBuildVariablesIos() throws Exception {
- MockObjcSupport.setup(mockToolsConfig);
useConfiguration(
"--crosstool_top=//tools/osx/crosstool", "--xcode_version=5.8",
"--ios_minimum_os=12.345", "--watchos_minimum_os=11.111",
@@ -145,7 +144,6 @@
@Test
public void testDefaultBuildVariablesIos() throws Exception {
- MockObjcSupport.setup(mockToolsConfig);
useConfiguration(
"--apple_platform_type=ios", "--crosstool_top=//tools/osx/crosstool", "--cpu=ios_x86_64");
scratch.file(
diff --git a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcRuleTestCase.java b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcRuleTestCase.java
index 20a3b80..8e74338 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcRuleTestCase.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/objc/ObjcRuleTestCase.java
@@ -136,29 +136,6 @@
+ "bin/";
}
- /**
- * Returns the genfiles dir for artifacts built for a given Apple architecture and minimum OS
- * version (as set by a configuration transition) and configuration distinguisher but the global
- * default for {@code --cpu}.
- *
- * @param arch the given Apple architecture which artifacts are built under this configuration.
- * Note this will likely be different than the value of {@code --cpu}.
- * @param configurationDistinguisher the configuration distinguisher used to describe the a
- * configuration transition
- * @param minOsVersion the minimum os version for which to compile artifacts in the configuration
- */
- protected String configurationGenfiles(
- String arch,
- ConfigurationDistinguisher configurationDistinguisher,
- DottedVersion minOsVersion) {
- return configurationDir(
- arch, configurationDistinguisher, minOsVersion, CompilationMode.FASTBUILD)
- + getTargetConfiguration()
- .getGenfilesDirectory(RepositoryName.MAIN)
- .getExecPath()
- .getBaseName();
- }
-
@SuppressWarnings("MissingCasesInEnumSwitch")
private String configurationDir(
String arch,
diff --git a/src/test/java/com/google/devtools/build/lib/testutil/TestConstants.java b/src/test/java/com/google/devtools/build/lib/testutil/TestConstants.java
index 4a6659c..f89e204 100644
--- a/src/test/java/com/google/devtools/build/lib/testutil/TestConstants.java
+++ b/src/test/java/com/google/devtools/build/lib/testutil/TestConstants.java
@@ -140,7 +140,8 @@
+ " - deps(" + TOOLS_REPOSITORY + "//tools/cpp:current_cc_toolchain)"
+ " - deps(" + TOOLS_REPOSITORY + "//tools/cpp:grep-includes)";
- public static final String PLATFORM_PACKAGE_ROOT = "@bazel_tools//platforms";
+ public static final String APPLE_PLATFORM_PATH = "build_bazel_apple_support/platforms";
+ public static final String APPLE_PLATFORM_PACKAGE_ROOT = "@build_bazel_apple_support//platforms";
public static final String CONSTRAINTS_PACKAGE_ROOT = "@platforms//";
public static final String LOCAL_CONFIG_PLATFORM_PACKAGE_ROOT =
"@local_config_platform//";
diff --git a/src/test/shell/integration/target_compatible_with_test.sh b/src/test/shell/integration/target_compatible_with_test.sh
index aaba612..19dc829 100755
--- a/src/test/shell/integration/target_compatible_with_test.sh
+++ b/src/test/shell/integration/target_compatible_with_test.sh
@@ -1107,6 +1107,18 @@
# for https://github.com/bazelbuild/bazel/issues/12897.
function test_incompatible_with_missing_toolchain() {
set_up_custom_toolchain
+ cat >> WORKSPACE <<'EOF'
+local_repository(name = 'build_bazel_apple_support', path = 'build_bazel_apple_support')
+EOF
+ mkdir -p build_bazel_apple_support/platforms
+ touch build_bazel_apple_support/WORKSPACE
+ cat > build_bazel_apple_support/platforms/BUILD <<'EOF'
+package(default_visibility=["//visibility:public"])
+platform(
+ name = "darwin_x86_64",
+ constraint_values = ["@platforms//os:macos", "@platforms//cpu:x86_64"],
+)
+EOF
cat >> target_skipping/BUILD <<'EOF'
load(