Skip to content

Commit 9bf3d7d

Browse files
committed
Fix ctx.fragments.apple.single_arch_cpu returning incorrect cpu for
tools when host cpu and exec cpu are different
1 parent 8174262 commit 9bf3d7d

File tree

2 files changed

+115
-2
lines changed

2 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_";
@@ -122,7 +125,7 @@ public static AppleCpus create(AppleCommandLineOptions options, CoreOptions core
122125
: ImmutableList.copyOf(options.tvosCpus);
123126
ImmutableList<String> macosCpus =
124127
(options.macosCpus == null || options.macosCpus.isEmpty())
125-
? ImmutableList.of(AppleCommandLineOptions.DEFAULT_MACOS_CPU)
128+
? ImmutableList.of(macosCpuFromCpu(coreOptions.cpu))
126129
: ImmutableList.copyOf(options.macosCpus);
127130
ImmutableList<String> catalystCpus =
128131
(options.catalystCpus == null || options.catalystCpus.isEmpty())
@@ -148,7 +151,7 @@ public static AppleCpus create(AppleCommandLineOptions options, CoreOptions core
148151
abstract ImmutableList<String> catalystCpus();
149152
}
150153

151-
/** Determines cpu value from apple-specific toolchain identifier. */
154+
/** Determines iOS cpu value from apple-specific toolchain identifier. */
152155
public static String iosCpuFromCpu(String cpu) {
153156
if (cpu.startsWith(IOS_CPU_PREFIX)) {
154157
return cpu.substring(IOS_CPU_PREFIX.length());
@@ -157,6 +160,14 @@ public static String iosCpuFromCpu(String cpu) {
157160
}
158161
}
159162

163+
/** Determines macOS cpu value from apple-specific toolchain identifier. */
164+
private static String macosCpuFromCpu(String cpu) {
165+
if (cpu.startsWith(MACOS_CPU_PREFIX)) {
166+
return cpu.substring(MACOS_CPU_PREFIX.length());
167+
}
168+
return AppleCommandLineOptions.DEFAULT_MACOS_CPU;
169+
}
170+
160171
public AppleCommandLineOptions getOptions() {
161172
return options;
162173
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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.config.BuildConfigurationValue;
22+
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
23+
import com.google.devtools.build.lib.cmdline.Label;
24+
import com.google.devtools.build.lib.packages.Provider;
25+
import com.google.devtools.build.lib.packages.StarlarkProvider;
26+
import com.google.devtools.build.lib.packages.StructImpl;
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 action properties on rule configured targets of Apple related rules. */
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+
" '@platforms//cpu:aarch64',",
75+
" '@platforms//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(
98+
Label.parseAbsolute("//:rules.bzl", ImmutableMap.of()), "MyInfo");
99+
StructImpl myInfo = (StructImpl) configuredTarget.get(key);
100+
assertThat((String) myInfo.getValue("exec_cpu")).isEqualTo("arm64");
101+
}
102+
}

0 commit comments

Comments
 (0)