Skip to content

Commit fce7ea8

Browse files
thiicopybara-github
authored andcommitted
Fix ctx.fragments.apple.single_arch_cpu returning incorrect cpu for tools when host cpu and exec cpu are different
Fixes #14291 Closes #14665. PiperOrigin-RevId: 425886938
1 parent 368fcf7 commit fce7ea8

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

src/main/java/com/google/devtools/build/lib/rules/apple/AppleConfiguration.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public class AppleConfiguration extends Fragment implements AppleConfigurationAp
7373
/** Prefix for iOS cpu values */
7474
public static final String IOS_CPU_PREFIX = "ios_";
7575

76+
/** Prefix for macOS cpu values */
77+
private static final String MACOS_CPU_PREFIX = "darwin_";
78+
7679
// TODO(b/180572694): Remove after platforms based toolchain resolution supported.
7780
/** Prefix for forced iOS simulator cpu values */
7881
public static final String IOS_FORCED_SIMULATOR_CPU_PREFIX = "sim_";
@@ -120,7 +123,7 @@ public static AppleCpus create(AppleCommandLineOptions options, CoreOptions core
120123
: ImmutableList.copyOf(options.tvosCpus);
121124
ImmutableList<String> macosCpus =
122125
(options.macosCpus == null || options.macosCpus.isEmpty())
123-
? ImmutableList.of(AppleCommandLineOptions.DEFAULT_MACOS_CPU)
126+
? ImmutableList.of(macosCpuFromCpu(coreOptions.cpu))
124127
: ImmutableList.copyOf(options.macosCpus);
125128
ImmutableList<String> catalystCpus =
126129
(options.catalystCpus == null || options.catalystCpus.isEmpty())
@@ -146,7 +149,7 @@ public static AppleCpus create(AppleCommandLineOptions options, CoreOptions core
146149
abstract ImmutableList<String> catalystCpus();
147150
}
148151

149-
/** Determines cpu value from apple-specific toolchain identifier. */
152+
/** Determines iOS cpu value from apple-specific toolchain identifier. */
150153
public static String iosCpuFromCpu(String cpu) {
151154
if (cpu.startsWith(IOS_CPU_PREFIX)) {
152155
return cpu.substring(IOS_CPU_PREFIX.length());
@@ -155,6 +158,14 @@ public static String iosCpuFromCpu(String cpu) {
155158
}
156159
}
157160

161+
/** Determines macOS cpu value from apple-specific toolchain identifier. */
162+
private static String macosCpuFromCpu(String cpu) {
163+
if (cpu.startsWith(MACOS_CPU_PREFIX)) {
164+
return cpu.substring(MACOS_CPU_PREFIX.length());
165+
}
166+
return AppleCommandLineOptions.DEFAULT_MACOS_CPU;
167+
}
168+
158169
public AppleCommandLineOptions getOptions() {
159170
return options;
160171
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2022 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.devtools.build.lib.rules.apple;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
19+
import com.google.common.collect.ImmutableMap;
20+
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
21+
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
22+
import com.google.devtools.build.lib.cmdline.Label;
23+
import com.google.devtools.build.lib.packages.Provider;
24+
import com.google.devtools.build.lib.packages.StarlarkProvider;
25+
import com.google.devtools.build.lib.packages.StructImpl;
26+
import com.google.devtools.build.lib.testutil.TestConstants;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.JUnit4;
31+
32+
/** Tests for the Starlark interface of Apple fragment. */
33+
@RunWith(JUnit4.class)
34+
public class AppleFragmentTest extends BuildViewTestCase {
35+
36+
@Before
37+
public void setup() throws Exception {
38+
scratch.file(
39+
"rules.bzl",
40+
"MyInfo = provider()",
41+
"def _my_binary_impl(ctx):",
42+
" out = ctx.actions.declare_file(ctx.label.name)",
43+
" ctx.actions.write(out, '')",
44+
" return [",
45+
" DefaultInfo(executable = out),",
46+
" MyInfo(",
47+
" exec_cpu = ctx.fragments.apple.single_arch_cpu,",
48+
" ),",
49+
" ]",
50+
"my_binary = rule(",
51+
" fragments = ['apple'],",
52+
" implementation = _my_binary_impl,",
53+
")",
54+
"def _my_rule_impl(ctx):",
55+
" return ctx.attr._tool[MyInfo]",
56+
"my_rule = rule(",
57+
" _my_rule_impl,",
58+
" attrs = {",
59+
" '_tool': attr.label(",
60+
" cfg = 'exec',",
61+
" executable = True,",
62+
" default = ('//:bin'),",
63+
" ),",
64+
" },",
65+
")");
66+
scratch.file(
67+
"BUILD",
68+
"load(':rules.bzl', 'my_binary', 'my_rule')",
69+
"my_binary(name = 'bin')",
70+
"my_rule(name = 'a')",
71+
"platform(",
72+
" name = 'macos_arm64',",
73+
" constraint_values = [",
74+
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "cpu:aarch64',",
75+
" '" + TestConstants.CONSTRAINTS_PACKAGE_ROOT + "os:osx',",
76+
" ],",
77+
")");
78+
scratch.file(
79+
"/workspace/platform_mappings",
80+
"platforms:",
81+
" //:macos_arm64",
82+
" --cpu=darwin_arm64",
83+
"flags:",
84+
" --cpu=darwin_arm64",
85+
" --apple_platform_type=macos",
86+
" //:macos_arm64");
87+
invalidatePackages(false);
88+
}
89+
90+
@Test
91+
public void appleFragmentSingleArchCpuOnExtraExecPlatform() throws Exception {
92+
// Test that ctx.fragments.apple.single_arch_cpu returns the execution
93+
// platform's cpu in a tool's rule context.
94+
useConfiguration("--extra_execution_platforms=//:macos_arm64");
95+
ConfiguredTarget configuredTarget = getConfiguredTarget("//:a");
96+
Provider.Key key =
97+
new StarlarkProvider.Key(Label.parseAbsolute("//:rules.bzl", ImmutableMap.of()), "MyInfo");
98+
StructImpl myInfo = (StructImpl) configuredTarget.get(key);
99+
assertThat((String) myInfo.getValue("exec_cpu")).isEqualTo("arm64");
100+
}
101+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ java_library(
3232
"//src/test/java/com/google/devtools/build/lib/analysis/util",
3333
"//src/test/java/com/google/devtools/build/lib/packages:testutil",
3434
"//src/test/java/com/google/devtools/build/lib/testutil",
35+
"//src/test/java/com/google/devtools/build/lib/testutil:TestConstants",
3536
"//third_party:guava",
3637
"//third_party:guava-testlib",
3738
"//third_party:jsr305",

0 commit comments

Comments
 (0)