Skip to content

Commit 52f2c0c

Browse files
authored
Prepartation for Bazel 9: get xcode version from @bazel_tools//tools/cpp:host_xcodes (#8003)
The previous target (@bazel_tools//tools/osx:current_xcode_config) which we used in the cquery no longer exists in Bazel 9. Therefore, we have to switch to `@bazel_tools//tools/cpp:host_xcodes` for Bazel 9. We only use the `host_xcodes` for Bazel 9+ and keep the previous logic for older Bazel versions because `host_xcodes` only provides the default xcode version. However, it should be fine for most cases. With the new aspect we'll receive the correct xcode version from the aspect and can deprecate this entire cquery setup.
1 parent 013b0ff commit 52f2c0c

File tree

6 files changed

+32
-12
lines changed

6 files changed

+32
-12
lines changed

cpp/src/com/google/idea/blaze/cpp/BlazeConfigurationResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public BlazeConfigurationResolverResult update(
8686
context, blazeProjectData.getTargetMap());
8787

8888
Optional<XcodeCompilerSettings> xcodeSettings =
89-
BlazeConfigurationToolchainResolver.resolveXcodeCompilerSettings(context, project);
89+
BlazeConfigurationToolchainResolver.resolveXcodeCompilerSettings(context, project, blazeProjectData);
9090

9191
ImmutableMap<CToolchainIdeInfo, BlazeCompilerSettings> compilerSettings =
9292
BlazeConfigurationToolchainResolver.buildCompilerSettingsMap(

cpp/src/com/google/idea/blaze/cpp/BlazeConfigurationToolchainResolver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.idea.blaze.base.ideinfo.TargetIdeInfo;
3636
import com.google.idea.blaze.base.ideinfo.TargetKey;
3737
import com.google.idea.blaze.base.ideinfo.TargetMap;
38+
import com.google.idea.blaze.base.model.BlazeProjectData;
3839
import com.google.idea.blaze.base.model.primitives.ExecutionRootPath;
3940
import com.google.idea.blaze.base.model.primitives.LanguageClass;
4041
import com.google.idea.blaze.base.model.primitives.TargetExpression;
@@ -424,14 +425,13 @@ private static <T> ListenableFuture<T> submit(Callable<T> callable) {
424425
return BlazeExecutor.getInstance().submit(callable);
425426
}
426427

427-
public static Optional<XcodeCompilerSettings> resolveXcodeCompilerSettings(BlazeContext context,
428-
Project project) {
428+
public static Optional<XcodeCompilerSettings> resolveXcodeCompilerSettings(BlazeContext context, Project project, BlazeProjectData projectData) {
429429
return Scope.push(
430430
context,
431431
childContext -> {
432432
childContext.push(new TimingScope("Resolve Xcode information", EventType.Other));
433433
try {
434-
return XcodeCompilerSettingsProvider.getInstance(project).fromContext(context, project);
434+
return XcodeCompilerSettingsProvider.getInstance(project).fromContext(context, project, projectData);
435435
} catch (XcodeCompilerSettingsException e) {
436436
IssueOutput.warn(
437437
String.format("There was an error fetching the Xcode information from the build: %s\n\nSome C++ functionality may not be available.", e.toString())

cpp/src/com/google/idea/blaze/cpp/XcodeCompilerSettingsProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.google.idea.blaze.cpp;
1717

1818
import com.google.common.collect.ImmutableMap;
19+
import com.google.idea.blaze.base.model.BlazeProjectData;
1920
import com.google.idea.blaze.base.scope.BlazeContext;
2021
import com.intellij.openapi.components.ServiceManager;
2122
import com.intellij.openapi.project.Project;
@@ -30,7 +31,7 @@ static XcodeCompilerSettingsProvider getInstance(Project project) {
3031
return ServiceManager.getService(project, XcodeCompilerSettingsProvider.class);
3132
}
3233

33-
Optional<XcodeCompilerSettings> fromContext(BlazeContext context, Project project)
34+
Optional<XcodeCompilerSettings> fromContext(BlazeContext context, Project project, BlazeProjectData projectData)
3435
throws XcodeCompilerSettingsException;
3536

3637
default ImmutableMap<String, String> asEnvironmentVariables(

cpp/src/com/google/idea/blaze/cpp/XcodeCompilerSettingsProviderImpl.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.idea.blaze.base.command.BlazeCommand;
2727
import com.google.idea.blaze.base.command.BlazeCommandName;
2828
import com.google.idea.blaze.base.console.BlazeConsoleLineProcessorProvider;
29+
import com.google.idea.blaze.base.model.BlazeProjectData;
2930
import com.google.idea.blaze.base.model.primitives.Label;
3031
import com.google.idea.blaze.base.model.primitives.WorkspaceRoot;
3132
import com.google.idea.blaze.base.scope.BlazeContext;
@@ -45,20 +46,26 @@
4546

4647

4748
public class XcodeCompilerSettingsProviderImpl implements XcodeCompilerSettingsProvider {
48-
49+
50+
// provides only the default xcode version but is available in Bazel 9 as well
51+
private static final String HOST_XCODES_TARGET = "@bazel_tools//tools/cpp:host_xcodes";
52+
53+
// provides the current xcode version but is not available in Bazel 9 anymore
54+
private static final String CURRENT_XCODE_CONFIG_TARGET = "@bazel_tools//tools/osx:current_xcode_config";
55+
4956
private static final String QUERY_STARLARK_FILE = "xcode_query.bzl";
5057

5158
// This only exists because it's impossible to escape a `deps()` query expression correctly in a Java string.
5259
private static final String[] QUERY_XCODE_VERSION_SCRIPT_LINES = new String[]{
5360
"#!/bin/bash",
5461
"__BAZEL_BIN__ cquery \\",
55-
" 'deps(\"@bazel_tools//tools/osx:current_xcode_config\")' \\",
62+
" 'deps(__TARGET__)' \\",
5663
" --output=starlark \\",
5764
" --starlark:file='__QUERY_FILE__'",
5865
};
5966

6067
@Override
61-
public Optional<XcodeCompilerSettings> fromContext(BlazeContext context, Project project)
68+
public Optional<XcodeCompilerSettings> fromContext(BlazeContext context, Project project, BlazeProjectData projectData)
6269
throws XcodeCompilerSettingsException {
6370
WorkspaceRoot workspaceRoot = WorkspaceRoot.fromProject(project);
6471
BuildSystem.BuildInvoker invoker =
@@ -68,7 +75,8 @@ public Optional<XcodeCompilerSettings> fromContext(BlazeContext context, Project
6875
context,
6976
invoker,
7077
workspaceRoot,
71-
project);
78+
project,
79+
projectData);
7280

7381
if (!xcodeAndSdkVersions.isPresent()) {
7482
return Optional.empty();
@@ -205,14 +213,22 @@ private static Optional<XcodeAndSdkVersions> queryXcodeAndSdkVersions(
205213
BlazeContext context,
206214
BuildInvoker invoker,
207215
WorkspaceRoot workspaceRoot,
208-
Project project)
216+
Project project,
217+
BlazeProjectData projectData)
209218
throws XcodeCompilerSettingsException {
210219
// this will not work with query sync, since at the moment aspects are only written as part of the async
211220
final var queryFile = AspectStorageService.of(project)
212221
.resolve(QUERY_STARLARK_FILE)
213222
.orElseThrow(() -> new IllegalStateException("could not resolve query file"))
214223
.toFilePath()
215224
.toString();
225+
226+
final String queryTarget;
227+
if (projectData.getBlazeVersionData().bazelIsAtLeastVersion(9, 0, 0)) {
228+
queryTarget = HOST_XCODES_TARGET;
229+
} else {
230+
queryTarget = CURRENT_XCODE_CONFIG_TARGET;
231+
}
216232

217233
File blazeCqueryWrapper = null;
218234
try {
@@ -226,6 +242,7 @@ private static Optional<XcodeAndSdkVersions> queryXcodeAndSdkVersions(
226242
Arrays.stream(QUERY_XCODE_VERSION_SCRIPT_LINES).forEach(line -> {
227243
line = line.replace("__BAZEL_BIN__", invoker.getBinaryPath());
228244
line = line.replace("__QUERY_FILE__", queryFile);
245+
line = line.replace("__TARGET__", queryTarget);
229246

230247
pw.println(line);
231248
});

cpp/src/com/google/idea/blaze/cpp/XcodeCompilerSettingsProviderNoopImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.google.idea.blaze.cpp;
22

33
import com.google.common.collect.ImmutableMap;
4+
import com.google.idea.blaze.base.model.BlazeProjectData;
45
import com.google.idea.blaze.base.scope.BlazeContext;
56
import com.intellij.openapi.project.Project;
67
import java.util.Optional;
@@ -13,7 +14,7 @@
1314
public class XcodeCompilerSettingsProviderNoopImpl implements XcodeCompilerSettingsProvider {
1415

1516
@Override
16-
public Optional<XcodeCompilerSettings> fromContext(BlazeContext context, Project project)
17+
public Optional<XcodeCompilerSettings> fromContext(BlazeContext context, Project project, BlazeProjectData projectData)
1718
throws XcodeCompilerSettingsException {
1819
return Optional.empty();
1920
}

cpp/tests/utils/com/google/idea/blaze/cpp/MockXcodeSettingsProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.google.idea.blaze.cpp;
1717

1818
import com.google.idea.blaze.base.scope.BlazeContext;
19+
import com.google.idea.blaze.base.model.BlazeProjectData;
1920
import com.intellij.openapi.project.Project;
2021
import java.util.Optional;
2122

@@ -28,7 +29,7 @@ public void setXcodeSettings(XcodeCompilerSettings settings) {
2829
}
2930

3031
@Override
31-
public Optional<XcodeCompilerSettings> fromContext(BlazeContext context, Project project)
32+
public Optional<XcodeCompilerSettings> fromContext(BlazeContext context, Project project, BlazeProjectData projectData)
3233
throws XcodeCompilerSettingsException {
3334
if (this.settings != null) {
3435
return Optional.of(this.settings);

0 commit comments

Comments
 (0)