Skip to content

Commit 5a73201

Browse files
comiuscopybara-github
authored andcommitted
Fix unit tests to work with cc toolchain resolution enabled.
Most of the change is straightforward - adding platform configuration and `--platforms` flags. ObjcRuleTestCase has a platform/no-platform test setup, however only non-platform test is running and a lot is broken on the platform test. PiperOrigin-RevId: 470948924 Change-Id: I7d2f7f8c7a836672b0faf83ecf494d340d513ed4
1 parent 85463a8 commit 5a73201

12 files changed

+118
-25
lines changed

src/test/java/com/google/devtools/build/lib/packages/util/Crosstool.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,14 @@ public static class Builder {
124124
private String targetLibc = "local";
125125
private String abiVersion = "local";
126126
private String abiLibcVersion = "local";
127-
private ImmutableList<String> toolchainExecConstraints = ImmutableList.of();
128-
private ImmutableList<String> toolchainTargetConstraints = ImmutableList.of();
127+
private ImmutableList<String> toolchainExecConstraints =
128+
ImmutableList.of(
129+
TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:x86_64",
130+
TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:linux");
131+
private ImmutableList<String> toolchainTargetConstraints =
132+
ImmutableList.of(
133+
TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:x86_64",
134+
TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:linux");
129135

130136
@CanIgnoreReturnValue
131137
public Builder withCpu(String cpu) {
@@ -710,8 +716,8 @@ public void writeOSX() throws IOException {
710716
: " static_runtime_lib = '" + staticRuntimeLabel + "',",
711717
")",
712718
"toolchain(name = 'cc-toolchain-" + toolchainConfig.getTargetCpu() + "',",
713-
" exec_compatible_with = [],",
714-
" target_compatible_with = [],",
719+
toolchainConfig.getToolchainExecConstraints(),
720+
toolchainConfig.getToolchainTargetConstraints(),
715721
" toolchain = ':cc-compiler-" + toolchainConfig.getTargetCpu() + "',",
716722
" toolchain_type = '" + TestConstants.TOOLS_REPOSITORY + "//tools/cpp:toolchain_type'",
717723
")");

src/test/java/com/google/devtools/build/lib/packages/util/MockCcSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ private void writeToolchainsForRealFilesystemTools(
258258
" toolchain = '//" + crosstoolTopPath + ":cc-compiler-arm-llvm',",
259259
" toolchain_type = '" + TestConstants.TOOLS_REPOSITORY + "//tools/cpp:toolchain_type',",
260260
" target_compatible_with = [",
261-
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:arm',",
261+
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:armv7',",
262262
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:android',",
263263
" ],",
264264
")");

src/test/java/com/google/devtools/build/lib/packages/util/MockObjcSupport.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public static ImmutableList<String> requiredObjcCrosstoolFlags() {
7676
public static ImmutableList<String> requiredObjcPlatformFlagsNoXcodeConfig() {
7777
ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();
7878

79+
argsBuilder.add("--platforms=" + TestConstants.CONSTRAINTS_PATH + "/apple:darwin_x86_64");
80+
7981
// Set a crosstool_top that is compatible with Apple transitions. Currently, even though this
8082
// references the old cc_toolchain_suite, it's still required of cc builds even when the
8183
// incompatible_enable_cc_toolchain_resolution flag is active.
@@ -99,8 +101,8 @@ public static ImmutableList<String> requiredObjcCrosstoolFlagsNoXcodeConfig() {
99101
// AppleCrosstoolTransition
100102
argsBuilder
101103
.add("--apple_crosstool_top=" + MockObjcSupport.DEFAULT_OSX_CROSSTOOL)
102-
.add("--crosstool_top=" + MockObjcSupport.DEFAULT_OSX_CROSSTOOL);
103-
104+
.add("--crosstool_top=" + MockObjcSupport.DEFAULT_OSX_CROSSTOOL)
105+
.add("--noincompatible_enable_cc_toolchain_resolution");
104106
return argsBuilder.build();
105107
}
106108

@@ -157,6 +159,20 @@ public static void setup(MockToolsConfig config) throws IOException {
157159
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:ios',",
158160
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:arm64',",
159161
" ],",
162+
")",
163+
"platform(",
164+
" name = 'ios_x86_64',",
165+
" constraint_values = [",
166+
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:ios',",
167+
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:x86_64',",
168+
" ],",
169+
")",
170+
"platform(",
171+
" name = 'watchos_x86_64',",
172+
" constraint_values = [",
173+
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:watchos',",
174+
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:x86_64',",
175+
" ],",
160176
")");
161177

162178
for (String tool :
@@ -198,8 +214,7 @@ public static void setup(MockToolsConfig config) throws IOException {
198214
"xcode_version(",
199215
" name = 'version5',",
200216
" version = '5',",
201-
")",
202-
"objc_library(name = 'dummy_lib', srcs = ['objc_dummy.mm'])");
217+
")");
203218
// If the bazel tools repository is not in the workspace, also create a workspace tools/objc
204219
// package with a few lingering dependencies.
205220
// TODO(b/64537078): Move these dependencies underneath the tools workspace.

src/test/java/com/google/devtools/build/lib/packages/util/MockPlatformSupport.java

+22
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,26 @@ public static void addMockK8Platform(MockToolsConfig mockToolsConfig, Label cros
203203
" target_compatible_with = [':mock_value'],",
204204
")");
205205
}
206+
207+
/** Adds a mock PPC platform. */
208+
public static void addMockPPCPlatform(MockToolsConfig mockToolsConfig, Label crosstoolLabel)
209+
throws Exception {
210+
mockToolsConfig.create(
211+
"mock_platform/BUILD",
212+
"package(default_visibility=['//visibility:public'])",
213+
"constraint_setting(name = 'mock_setting')",
214+
"constraint_value(name = 'mock_value', constraint_setting = ':mock_setting')",
215+
"platform(",
216+
" name = 'mock-ppc-platform',",
217+
" constraint_values = [':mock_value'],",
218+
")",
219+
"toolchain(",
220+
" name = 'toolchain_cc-compiler-ppc',",
221+
" toolchain_type = '" + TestConstants.TOOLS_REPOSITORY + "//tools/cpp:toolchain_type',",
222+
" toolchain = '"
223+
+ crosstoolLabel.getRelativeWithRemapping("cc-compiler-ppc-compiler", ImmutableMap.of())
224+
+ "',",
225+
" target_compatible_with = [':mock_value'],",
226+
")");
227+
}
206228
}

src/test/java/com/google/devtools/build/lib/rules/android/AndroidBuildViewTestCase.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,21 @@ protected boolean platformBasedToolchains() {
7373
}
7474

7575
protected String defaultPlatformFlag() {
76-
return String.format(
77-
"--platforms=%sandroid:armeabi-v7a", TestConstants.CONSTRAINTS_PACKAGE_ROOT);
76+
return "";
7877
}
7978

8079
@Override
8180
protected void useConfiguration(ImmutableMap<String, Object> starlarkOptions, String... args)
8281
throws Exception {
8382

8483
if (!platformBasedToolchains()) {
85-
super.useConfiguration(starlarkOptions, args);
84+
super.useConfiguration(
85+
starlarkOptions,
86+
ImmutableList.builder()
87+
.add((Object[]) args)
88+
.add("--noincompatible_enable_cc_toolchain_resolution")
89+
.build()
90+
.toArray(new String[0]));
8691
return;
8792
}
8893

@@ -116,8 +121,12 @@ protected void useConfiguration(ImmutableMap<String, Object> starlarkOptions, St
116121
}
117122
}
118123
if (!hasPlatform) {
124+
fullArgs.add(
125+
String.format(
126+
"--platforms=%sandroid:armeabi-v7a", TestConstants.CONSTRAINTS_PACKAGE_ROOT));
119127
fullArgs.add(defaultPlatformFlag());
120128
}
129+
fullArgs.add("--incompatible_enable_cc_toolchain_resolution");
121130
super.useConfiguration(starlarkOptions, fullArgs.build().toArray(new String[0]));
122131
}
123132

src/test/java/com/google/devtools/build/lib/rules/android/AndroidDataBindingV2Test.java

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
3030
import com.google.devtools.build.lib.cmdline.RepositoryName;
3131
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
32+
import com.google.devtools.build.lib.packages.util.BazelMockAndroidSupport;
3233
import com.google.devtools.build.lib.rules.android.AndroidDataBindingV2Test.WithPlatforms;
3334
import com.google.devtools.build.lib.rules.android.AndroidDataBindingV2Test.WithoutPlatforms;
3435
import com.google.devtools.build.lib.rules.android.databinding.DataBinding;
@@ -59,6 +60,12 @@ public static class WithPlatforms extends AndroidDataBindingV2Test {
5960
protected boolean platformBasedToolchains() {
6061
return true;
6162
}
63+
64+
@Before
65+
public void setupCcToolchain() throws Exception {
66+
BazelMockAndroidSupport.setupNdk(mockToolsConfig);
67+
invalidatePackages(false);
68+
}
6269
}
6370

6471
@Before

src/test/java/com/google/devtools/build/lib/rules/android/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ java_test(
378378
"//src/main/java/com/google/devtools/build/lib/rules/java:java-compilation",
379379
"//src/main/protobuf:extra_actions_base_java_proto",
380380
"//src/test/java/com/google/devtools/build/lib/actions/util",
381+
"//src/test/java/com/google/devtools/build/lib/packages:testutil",
381382
"//src/test/java/com/google/devtools/build/lib/rules/java:java_compile_action_test_helper",
382383
"//third_party:guava",
383384
"//third_party:junit4",

src/test/java/com/google/devtools/build/lib/rules/cpp/CcHostToolchainAliasTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ public void testThatHostCrosstoolTopCommandLineArgumentWorks() throws Exception
8484

8585
scratch.file("a/BUILD", "cc_host_toolchain_alias(name='current_cc_host_toolchain')");
8686

87-
useConfiguration("--host_crosstool_top=//b:my_custom_toolchain_suite", "--host_cpu=k8");
87+
useConfiguration(
88+
"--noincompatible_enable_cc_toolchain_resolution",
89+
"--host_crosstool_top=//b:my_custom_toolchain_suite",
90+
"--host_cpu=k8");
8891
ConfiguredTarget target = getConfiguredTarget("//a:current_cc_host_toolchain");
8992

9093
CcToolchainProvider ccToolchainProvider = target.get(CcToolchainProvider.PROVIDER);

src/test/java/com/google/devtools/build/lib/rules/cpp/CcToolchainProviderTest.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -751,14 +751,25 @@ public void testWhenStaticRuntimeLibAttributeMandatoryWhenSupportsEmbeddedRuntim
751751
CcToolchainConfig.builder()
752752
.withFeatures(CppRuleClasses.STATIC_LINK_CPP_RUNTIMES)
753753
.build()
754-
.getCcToolchainConfigRule());
754+
.getCcToolchainConfigRule(),
755+
"toolchain(",
756+
" name = 'cc-toolchain-b',",
757+
" toolchain_type = '" + TestConstants.TOOLS_REPOSITORY + "//tools/cpp:toolchain_type',",
758+
" toolchain = ':b',",
759+
" target_compatible_with = [],",
760+
" exec_compatible_with = [],",
761+
")");
755762
analysisMock.ccSupport().setupCcToolchainConfig(mockToolsConfig, CcToolchainConfig.builder());
756763
mockToolsConfig.create(
757764
"a/cc_toolchain_config.bzl",
758765
ResourceLoader.readFromResources(
759766
"com/google/devtools/build/lib/analysis/mock/cc_toolchain_config.bzl"));
760767
reporter.removeHandler(failFastHandler);
761-
useConfiguration("--crosstool_top=//a:a", "--cpu=k8", "--host_cpu=k8");
768+
useConfiguration(
769+
"--extra_toolchains=//a:cc-toolchain-b",
770+
"--crosstool_top=//a:a",
771+
"--cpu=k8",
772+
"--host_cpu=k8");
762773
assertThat(getConfiguredTarget("//a:main")).isNull();
763774
assertContainsEvent(
764775
"Toolchain supports embedded runtimes, but didn't provide static_runtime_lib attribute.");
@@ -795,14 +806,26 @@ public void testWhenDynamicRuntimeLibAttributeMandatoryWhenSupportsEmbeddedRunti
795806
.withFeatures(
796807
CppRuleClasses.STATIC_LINK_CPP_RUNTIMES, CppRuleClasses.SUPPORTS_DYNAMIC_LINKER)
797808
.build()
798-
.getCcToolchainConfigRule());
809+
.getCcToolchainConfigRule(),
810+
"toolchain(",
811+
" name = 'cc-toolchain-b',",
812+
" toolchain_type = '" + TestConstants.TOOLS_REPOSITORY + "//tools/cpp:toolchain_type',",
813+
" toolchain = ':b',",
814+
" target_compatible_with = [],",
815+
" exec_compatible_with = [],",
816+
")");
799817
analysisMock.ccSupport().setupCcToolchainConfig(mockToolsConfig, CcToolchainConfig.builder());
800818
mockToolsConfig.create(
801819
"a/cc_toolchain_config.bzl",
802820
ResourceLoader.readFromResources(
803821
"com/google/devtools/build/lib/analysis/mock/cc_toolchain_config.bzl"));
804822
reporter.removeHandler(failFastHandler);
805-
useConfiguration("--crosstool_top=//a:a", "--cpu=k8", "--host_cpu=k8", "--dynamic_mode=fully");
823+
useConfiguration(
824+
"--extra_toolchains=//a:cc-toolchain-b",
825+
"--crosstool_top=//a:a",
826+
"--cpu=k8",
827+
"--host_cpu=k8",
828+
"--dynamic_mode=fully");
806829
assertThat(getConfiguredTarget("//a:test")).isNull();
807830
assertContainsEvent(
808831
"Toolchain supports embedded runtimes, but didn't provide dynamic_runtime_lib attribute.");

src/test/java/com/google/devtools/build/lib/rules/objc/BazelJ2ObjcLibraryTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,9 @@ public void testJ2ObjcDeadCodeRemovalActionWithOptFlag() throws Exception {
11501150
"--output_archive",
11511151
removeConfigFragment(prunedArchive.getExecPathString()),
11521152
"--dummy_archive",
1153-
execPath + TestConstants.TOOLS_REPOSITORY_PATH_PREFIX + "tools/objc/libdummy_lib.a",
1153+
execPath
1154+
+ TestConstants.TOOLS_REPOSITORY_PATH_PREFIX
1155+
+ "tools/objc/dummy/libdummy_lib.a",
11541156
"--xcrunwrapper",
11551157
removeConfigFragment(MOCK_XCRUNWRAPPER_EXECUTABLE_PATH),
11561158
"--dependency_mapping_files",

src/test/java/com/google/devtools/build/lib/rules/objc/ObjcBuildVariablesTest.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ protected void initializeMockClient() throws IOException {
6161

6262
@Override
6363
protected void useConfiguration(String... args) throws Exception {
64-
ImmutableList<String> extraArgs = ImmutableList.<String>builder()
65-
.add("--xcode_version_config=" + MockObjcSupport.XCODE_VERSION_CONFIG)
66-
.add("--apple_crosstool_top=" + MockObjcSupport.DEFAULT_OSX_CROSSTOOL)
67-
.add("--crosstool_top=" + MockObjcSupport.DEFAULT_OSX_CROSSTOOL)
68-
.addAll(ImmutableList.copyOf(args))
69-
.build();
64+
ImmutableList<String> extraArgs =
65+
ImmutableList.<String>builder()
66+
.add("--noincompatible_enable_cc_toolchain_resolution")
67+
.add("--xcode_version_config=" + MockObjcSupport.XCODE_VERSION_CONFIG)
68+
.add("--apple_crosstool_top=" + MockObjcSupport.DEFAULT_OSX_CROSSTOOL)
69+
.add("--crosstool_top=" + MockObjcSupport.DEFAULT_OSX_CROSSTOOL)
70+
.addAll(ImmutableList.copyOf(args))
71+
.build();
7072

7173
super.useConfiguration(extraArgs.toArray(new String[extraArgs.size()]));
7274
}

src/test/java/com/google/devtools/build/lib/rules/objc/ObjcRuleTestCase.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,9 @@ protected static void addAppleBinaryStarlarkRule(Scratch scratch) throws Excepti
506506
scratch.file("test_starlark/BUILD");
507507
RepositoryName toolsRepo = TestConstants.TOOLS_REPOSITORY;
508508
String toolsLoc = toolsRepo + "//tools/objc";
509+
scratch.file(
510+
TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/objc/dummy/BUILD",
511+
"objc_library(name = 'dummy_lib', srcs = ['objc_dummy.mm'])");
509512

510513
scratch.file(
511514
"test_starlark/apple_binary_starlark.bzl",
@@ -576,7 +579,7 @@ protected static void addAppleBinaryStarlarkRule(Scratch scratch) throws Excepti
576579
" cfg=apple_common.multi_arch_split,",
577580
" default=Label('" + toolsRepo + "//tools/cpp:current_cc_toolchain'),),",
578581
" '_dummy_lib': attr.label(",
579-
" default = Label('" + toolsLoc + ":dummy_lib'),),",
582+
" default = Label('" + toolsLoc + "/dummy:dummy_lib'),),",
580583
" '_grep_includes': attr.label(",
581584
" cfg = 'host',",
582585
" allow_single_file = True,",

0 commit comments

Comments
 (0)