Skip to content

Commit

Permalink
Fix objc unit test to use C++ toolchains
Browse files Browse the repository at this point in the history
This is preparation to make --incompatible_enable_cc_toolchain_resolution a no-op.
All the unit tests need to work with the flag enabled.

Remove the dummy C++ toolchain (used by Android) in BazelAnalysisMock. This is necessary to correctly resolve a later appended (mocked) Mac C++ toolchain. Because of this some other tests needed additional mocked C++ toolchains.

PiperOrigin-RevId: 586597562
Change-Id: Id70368697fe71b33970f19058c7b384a90599a61
  • Loading branch information
comius authored and copybara-github committed Nov 30, 2023
1 parent d8ed538 commit 648fc4f
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,38 +298,8 @@ public void setupMockClient(MockToolsConfig config, List<String> workspaceConten
config.create(
"embedded_tools/tools/android/emulator/BUILD",
Iterables.toArray(createToolsAndroidEmulatorContents(), String.class));
// Create a dummy toolchain to make toolchain resolution happy.
config.create(
"embedded_tools/tools/android/dummy_sdk/dummy-cc-toolchain-config.bzl",
"def _impl(ctx):",
" out = ctx.actions.declare_file(ctx.label.name)",
" ctx.actions.write(out, 'Fake executable')",
" return [",
" cc_common.create_cc_toolchain_config_info(",
" ctx = ctx,",
" toolchain_identifier = 'dummy-toolchain',",
" host_system_name = 'nothing',",
" target_system_name = 'nothing',",
" target_cpu = 'nothing',",
" target_libc = 'nothing',",
" cc_target_os = 'nothing',",
" compiler = 'bin-false',",
" abi_version = 'nothing',",
" abi_libc_version = 'eleventy',",
" ),",
" DefaultInfo(",
" executable = out,",
" ),",
" ]",
"dummy_cc_toolchain_config = rule(",
" implementation = _impl,",
" attrs = {},",
" provides = [CcToolchainConfigInfo],",
" executable = True,",
")");
config.create(
"embedded_tools/tools/android/dummy_sdk/BUILD",
"load(':dummy-cc-toolchain-config.bzl'," + " 'dummy_cc_toolchain_config')",
"package(default_visibility=['//visibility:public'])",
"toolchain(",
" name = 'dummy-sdk',",
Expand Down Expand Up @@ -363,25 +333,6 @@ public void setupMockClient(MockToolsConfig config, List<String> workspaceConten
" shrinked_android_jar = 'dummy.jar',",
" zipalign = ':empty_binary',",
" tags = ['__ANDROID_RULES_MIGRATION__'],",
")",
"toolchain(",
" name = 'dummy-cc_toolchain',",
" toolchain = ':dummy_cc_toolchain_impl',",
" toolchain_type = '@bazel_tools//tools/cpp:toolchain_type',",
")",
"cc_toolchain(",
" name = 'dummy_cc_toolchain_impl',",
" all_files = ':nothing',",
" as_files = ':nothing',",
" compiler_files = ':nothing',",
" dwp_files = ':nothing',",
" linker_files = ':nothing',",
" objcopy_files = ':nothing',",
" strip_files = ':nothing',",
" toolchain_config = ':dummy-cc-toolchain-config',",
")",
"dummy_cc_toolchain_config(",
" name = 'dummy-cc-toolchain-config',",
")");
config.create(
"android_gmaven_r8/jar/BUILD",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/analysis:configured_target",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/test/java/com/google/devtools/build/lib/analysis/util",
"//src/test/java/com/google/devtools/build/lib/packages:testutil",
"//third_party:guava",
"//third_party:junit4",
"//third_party:truth",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.devtools.build.lib.analysis.test.TestRunnerAction;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.util.Crosstool.CcToolchainConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -57,6 +58,14 @@ public void testNonWindowsWrapper() throws Exception {

@Test
public void testWindowsWrapper() throws Exception {
getAnalysisMock()
.ccSupport()
.setupCcToolchainConfig(
mockToolsConfig,
CcToolchainConfig.builder()
.withToolchainTargetConstraints("@platforms//os:windows")
.withToolchainExecConstraints()
.withCpu("fake"));
scratch.file(
"platforms/BUILD",
"platform(name = 'windows', constraint_values = ['@platforms//os:windows'])");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.devtools.build.lib.packages.util;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.devtools.build.lib.rules.python.PythonTestUtils.getPyLoad;

import com.google.common.collect.ImmutableList;
Expand All @@ -22,6 +23,7 @@
import com.google.devtools.build.lib.packages.util.Crosstool.CcToolchainConfig;
import com.google.devtools.build.lib.testutil.TestConstants;
import java.io.IOException;
import java.util.stream.Stream;

/** Creates mock BUILD files required for the objc rules. */
public final class MockObjcSupport {
Expand Down Expand Up @@ -56,34 +58,50 @@ public final class MockObjcSupport {
public static final String DEFAULT_XCODE_VERSION = "7.3.1";
public static final String DEFAULT_IOS_SDK_VERSION = "8.4";

public static ImmutableList<String> requiredObjcPlatformFlags() {
public static final String APPLE_SIMULATOR_PLATFORM_PACKAGE =
TestConstants.APPLE_PLATFORM_PACKAGE_ROOT
+ (TestConstants.PRODUCT_NAME.equals("bazel") ? "" : "/simulator");

public static final String DARWIN_X86_64 =
TestConstants.APPLE_PLATFORM_PACKAGE_ROOT + ":darwin_x86_64";
public static final String IOS_X86_64 = APPLE_SIMULATOR_PLATFORM_PACKAGE + ":ios_x86_64";
public static final String IOS_ARM64 = TestConstants.APPLE_PLATFORM_PACKAGE_ROOT + ":ios_arm64";
public static final String IOS_ARMV7 =
TestConstants.APPLE_PLATFORM_PACKAGE_ROOT + ":ios_armv7"; // legacy for testing
public static final String IOS_I386 =
APPLE_SIMULATOR_PLATFORM_PACKAGE + ":ios_i386"; // legacy for testing
public static final String WATCHOS_ARMV7K =
TestConstants.APPLE_PLATFORM_PACKAGE_ROOT + ":watchos_armv7k";

public static ImmutableList<String> requiredObjcPlatformFlags(String... args) {
ImmutableList.Builder<String> builder = ImmutableList.builder();
return builder
.addAll(requiredObjcPlatformFlagsNoXcodeConfig())
.addAll(requiredObjcPlatformFlagsNoXcodeConfig(args))
.add("--xcode_version_config=" + MockObjcSupport.XCODE_VERSION_CONFIG)
.build();
}

/** Returns the set of flags required to build objc libraries using the mock OSX crosstool. */
public static ImmutableList<String> requiredObjcCrosstoolFlags() {
public static ImmutableList<String> requiredObjcCrosstoolFlags(String... args) {
ImmutableList.Builder<String> builder = ImmutableList.builder();
return builder
.addAll(requiredObjcCrosstoolFlagsNoXcodeConfig())
.addAll(requiredObjcCrosstoolFlagsNoXcodeConfig(args))
.add("--xcode_version_config=" + MockObjcSupport.XCODE_VERSION_CONFIG)
.build();
}

public static ImmutableList<String> requiredObjcPlatformFlagsNoXcodeConfig() {
public static ImmutableList<String> requiredObjcPlatformFlagsNoXcodeConfig(String... args) {
ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();

argsBuilder.add("--platforms=" + TestConstants.CONSTRAINTS_PATH + "/apple:darwin_x86_64");
argsBuilder.addAll(Stream.of(args).collect(toImmutableList()));
if (Stream.of(args).noneMatch(arg -> arg.startsWith("--platforms="))) {
argsBuilder.add("--platforms=" + MockObjcSupport.DARWIN_X86_64);
}

// Set a crosstool_top that is compatible with Apple transitions. Currently, even though this
// references the old cc_toolchain_suite, it's still required of cc builds even when the
// incompatible_enable_cc_toolchain_resolution flag is active.
argsBuilder.add("--apple_crosstool_top=" + MockObjcSupport.DEFAULT_OSX_CROSSTOOL);

argsBuilder.add("--incompatible_enable_cc_toolchain_resolution");
argsBuilder.add("--incompatible_enable_apple_toolchain_resolution");

return argsBuilder.build();
Expand All @@ -93,16 +111,19 @@ public static ImmutableList<String> requiredObjcPlatformFlagsNoXcodeConfig() {
* Returns the set of flags required to build objc libraries using the mock OSX crosstool except
* for --xcode_version_config.
*/
public static ImmutableList<String> requiredObjcCrosstoolFlagsNoXcodeConfig() {
public static ImmutableList<String> requiredObjcCrosstoolFlagsNoXcodeConfig(String... args) {

ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();
argsBuilder.addAll(Stream.of(args).collect(toImmutableList()));
if (Stream.of(args).noneMatch(arg -> arg.startsWith("--platforms="))) {
argsBuilder.add("--platforms=" + MockObjcSupport.DARWIN_X86_64);
}

// TODO(b/68751876): Set --apple_crosstool_top and --crosstool_top using the
// AppleCrosstoolTransition
argsBuilder
.add("--apple_crosstool_top=" + MockObjcSupport.DEFAULT_OSX_CROSSTOOL)
.add("--crosstool_top=" + MockObjcSupport.DEFAULT_OSX_CROSSTOOL)
.add("--noincompatible_enable_cc_toolchain_resolution");
.add("--crosstool_top=" + MockObjcSupport.DEFAULT_OSX_CROSSTOOL);
return argsBuilder.build();
}

Expand Down Expand Up @@ -159,6 +180,20 @@ public static void setup(MockToolsConfig config) throws IOException {
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:ios',",
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:arm64',",
" ],",
")",
"platform(", // legacy platform only used to support tests
" name = 'ios_armv7',",
" constraint_values = [",
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:ios',",
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:armv7',",
" ],",
")",
"platform(",
" name = 'watchos_armv7k',",
" constraint_values = [",
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:watchos',",
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:armv7k',",
" ],",
")");

String[] simulatorPlatforms = {
Expand All @@ -170,6 +205,13 @@ public static void setup(MockToolsConfig config) throws IOException {
" ],",
")",
"platform(",
" name = 'ios_i386',", // legacy platform only used to support tests
" constraint_values = [",
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:ios',",
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:x86_32',",
" ],",
")",
"platform(",
" name = 'watchos_x86_64',",
" constraint_values = [",
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:watchos',",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.CommandAction;
import com.google.devtools.build.lib.analysis.util.ScratchAttributeWriter;
import com.google.devtools.build.lib.rules.apple.AppleConfiguration.ConfigurationDistinguisher;
import com.google.devtools.build.lib.packages.util.MockObjcSupport;
import com.google.devtools.build.lib.rules.cpp.CppLinkAction;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -48,9 +48,7 @@ public void testToolchainSelectionCcDepDefault() throws Exception {
" deps = ['//b:lib'],",
")");
Action lipoAction = actionProducingArtifact("//a:bin", "_lipobin");
String x8664Bin =
configurationBin("x86_64", ConfigurationDistinguisher.APPLEBIN_IOS) + "a/bin_bin";
Artifact binArtifact = getFirstArtifactEndingWith(lipoAction.getInputs(), x8664Bin);
Artifact binArtifact = lipoAction.getInputs().getSingleton();
CppLinkAction linkAction = (CppLinkAction) getGeneratingAction(binArtifact);
CppLinkAction ccArchiveAction =
(CppLinkAction)
Expand All @@ -62,9 +60,9 @@ public void testToolchainSelectionCcDepDefault() throws Exception {

@Test
public void testToolchainSelectionCcDepDevice() throws Exception {
useConfiguration("--apple_platform_type=ios", "--cpu=ios_armv7");
ScratchAttributeWriter
.fromLabelString(this, "cc_library", "//b:lib")
useConfiguration(
"--apple_platform_type=ios", "--cpu=ios_armv7", "--platforms=" + MockObjcSupport.IOS_ARMV7);
ScratchAttributeWriter.fromLabelString(this, "cc_library", "//b:lib")
.setList("srcs", "b.cc")
.write();
addAppleBinaryStarlarkRule(scratch);
Expand All @@ -77,9 +75,11 @@ public void testToolchainSelectionCcDepDevice() throws Exception {
" deps = ['//b:lib'],",
")");
Action lipoAction = actionProducingArtifact("//a:bin", "_lipobin");
String armv7Bin =
configurationBin("armv7", ConfigurationDistinguisher.APPLEBIN_IOS) + "a/bin_bin";
Artifact binArtifact = getFirstArtifactEndingWith(lipoAction.getInputs(), armv7Bin);
Artifact binArtifact =
lipoAction.getInputs().toList().stream()
.filter(artifact -> artifact.getPath().toString().contains("armv7"))
.findAny()
.get();
CppLinkAction linkAction = (CppLinkAction) getGeneratingAction(binArtifact);
CppLinkAction ccArchiveAction =
(CppLinkAction)
Expand All @@ -105,9 +105,11 @@ public void testToolchainSelectionMultiArchIos() throws Exception {
" deps = ['//b:lib'],",
")");
Action lipoAction = actionProducingArtifact("//a:bin", "_lipobin");
String armv64Bin =
configurationBin("arm64", ConfigurationDistinguisher.APPLEBIN_IOS) + "a/bin_bin";
Artifact binArtifact = getFirstArtifactEndingWith(lipoAction.getInputs(), armv64Bin);
Artifact binArtifact =
lipoAction.getInputs().toList().stream()
.filter(artifact -> artifact.getPath().toString().contains("arm64"))
.findAny()
.get();
CppLinkAction linkAction = (CppLinkAction) getGeneratingAction(binArtifact);
CppLinkAction objcLibArchiveAction = (CppLinkAction) getGeneratingAction(
getFirstArtifactEndingWith(linkAction.getInputs(), "liblib.a"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,11 @@ protected ImmutableList<CppCompileAction> getActionsForInputsOfGeneratingActionT

@Test
public void testCompileActionTemplateFromGenJar() throws Exception {
useConfiguration("--apple_platform_type=ios", "--cpu=ios_i386", "--ios_minimum_os=1.0");
useConfiguration(
"--apple_platform_type=ios",
"--cpu=ios_i386",
"--ios_minimum_os=1.0",
"--platforms=" + MockObjcSupport.IOS_I386);
addSimpleJ2ObjcLibraryWithJavaPlugin();
Artifact archive = j2objcArchive("//java/com/google/app/test:transpile", "test");
CommandAction archiveAction = (CommandAction) getGeneratingAction(archive);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ protected void initializeMockClient() throws IOException {
protected void useConfiguration(String... args) throws Exception {
ImmutableList<String> extraArgs =
ImmutableList.<String>builder()
.add("--noincompatible_enable_cc_toolchain_resolution")
.add("--xcode_version_config=" + MockObjcSupport.XCODE_VERSION_CONFIG)
.add("--apple_crosstool_top=" + MockObjcSupport.DEFAULT_OSX_CROSSTOOL)
.add("--crosstool_top=" + MockObjcSupport.DEFAULT_OSX_CROSSTOOL)
Expand All @@ -75,15 +74,14 @@ protected void useConfiguration(String... args) throws Exception {
@Test
public void testAppleBuildVariablesIos() throws Exception {
useConfiguration(
"--crosstool_top=//tools/osx/crosstool", "--xcode_version=5.8",
"--ios_minimum_os=12.345", "--watchos_minimum_os=11.111",
"--cpu=ios_x86_64", "--apple_platform_type=ios");
scratch.file(
"x/BUILD",
"cc_binary(",
" name = 'bin',",
" srcs = ['a.cc'],",
")");
"--crosstool_top=//tools/osx/crosstool",
"--xcode_version=5.8",
"--ios_minimum_os=12.345",
"--watchos_minimum_os=11.111",
"--cpu=ios_x86_64",
"--apple_platform_type=ios",
"--platforms=" + MockObjcSupport.IOS_X86_64);
scratch.file("x/BUILD", "cc_binary(", " name = 'bin',", " srcs = ['a.cc'],", ")");
scratch.file("x/a.cc");

ConfiguredTarget target = getConfiguredTarget("//x:bin");
Expand All @@ -103,7 +101,7 @@ public void testAppleBuildVariablesWatchos() throws Exception {
useConfiguration(
"--crosstool_top=//tools/osx/crosstool", "--xcode_version=5.8",
"--ios_minimum_os=12.345", "--watchos_minimum_os=" + dummyMinimumOsValue,
"--watchos_cpus=armv7k");
"--watchos_cpus=armv7k", "--platforms=" + MockObjcSupport.WATCHOS_ARMV7K);
ObjcRuleTestCase.addAppleBinaryStarlarkRule(scratch);
scratch.file(
"x/BUILD",
Expand Down Expand Up @@ -145,13 +143,11 @@ public void testAppleBuildVariablesWatchos() throws Exception {
@Test
public void testDefaultBuildVariablesIos() throws Exception {
useConfiguration(
"--apple_platform_type=ios", "--crosstool_top=//tools/osx/crosstool", "--cpu=ios_x86_64");
scratch.file(
"x/BUILD",
"cc_binary(",
" name = 'bin',",
" srcs = ['a.cc'],",
")");
"--apple_platform_type=ios",
"--crosstool_top=//tools/osx/crosstool",
"--cpu=ios_x86_64",
"--platforms=" + MockObjcSupport.IOS_X86_64);
scratch.file("x/BUILD", "cc_binary(", " name = 'bin',", " srcs = ['a.cc'],", ")");
scratch.file("x/a.cc");

ConfiguredTarget target = getConfiguredTarget("//x:bin");
Expand Down
Loading

0 comments on commit 648fc4f

Please sign in to comment.