Skip to content

Commit

Permalink
Prevent the bazel plugin from being used for non-Android C++ developm…
Browse files Browse the repository at this point in the history
…ent in Android Studio.

Also ensure that bazel with clion remains usable for all C++ development.

PiperOrigin-RevId: 611040268
  • Loading branch information
Googler authored and copybara-github committed Mar 5, 2024
1 parent 3283e9d commit bd3db6d
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 32 deletions.
1 change: 1 addition & 0 deletions aswb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ java_library(
deps = [
"//base",
"//common/experiments",
"//cpp",
"//intellij_platform_sdk:jsr305", # unuseddeps: keep
"//intellij_platform_sdk:kotlin",
"//intellij_platform_sdk:plugin_api",
Expand Down
4 changes: 4 additions & 0 deletions aswb/src/META-INF/aswb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@
<android.ApkBuildStepProvider implementation="com.google.idea.blaze.android.run.BazelApkBuildStepProvider" />
</extensions>

<extensions defaultExtensionNs="com.google.idea.blaze.cpp">
<CppSupportChecker implementation="com.google.idea.blaze.android.cppimpl.NdkCppSupportChecker" />
</extensions>

<extensions defaultExtensionNs="com.android.ide">
<sdkEventListener implementation="com.google.idea.blaze.android.sdk.AndroidSdkListener"/>
</extensions>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2024 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.idea.blaze.android.cppimpl;

import com.android.tools.ndk.AndroidSysroot;
import com.android.tools.ndk.configuration.NdkConfigurationValidatorKt;
import com.google.common.collect.ImmutableList;
import com.google.idea.blaze.cpp.CppSupportChecker;
import java.nio.file.Path;

/**
* Uses the Android Studio API to check if a CPP configuration is supported, i.e. is for NDK
* development rather than non-Android CPP.
*/
public class NdkCppSupportChecker implements CppSupportChecker {

@Override
public boolean supportsCppConfiguration(
ImmutableList<String> compilerSwitches, Path workspaceRoot) {
return NdkConfigurationValidatorKt.isValidNdkConfiguration(
compilerSwitches,
workspaceRoot,
AndroidSysroot::isValidAndroidSysroot,
AndroidSysroot::isValidAndroidSysrootUsrInclude,
AndroidSysroot::isPotentialNonAndroidSysrootUsrInclude);
}
}
4 changes: 4 additions & 0 deletions clwb/src/META-INF/clwb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<TestContextProvider implementation="com.google.idea.blaze.clwb.run.producers.CppTestContextProvider"/>
</extensions>

<extensions defaultExtensionNs="com.google.idea.blaze.cpp">
<CppSupportChecker implementation="com.google.idea.blaze.clwb.ClwbSupportsCpp" />
</extensions>

<project-components>
<component>
<implementation-class>com.google.idea.blaze.plugin.ClwbProjectSpecificInitializer</implementation-class>
Expand Down
30 changes: 30 additions & 0 deletions clwb/src/com/google/idea/blaze/clwb/ClwbSupportsCpp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2024 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.idea.blaze.clwb;

import com.google.common.collect.ImmutableList;
import com.google.idea.blaze.cpp.CppSupportChecker;
import java.nio.file.Path;

/** CLwB supports all CPP development. */
public class ClwbSupportsCpp implements CppSupportChecker {

@Override
public boolean supportsCppConfiguration(
ImmutableList<String> compilerSwitches, Path workspaceRoot) {
return true;
}
}
21 changes: 14 additions & 7 deletions cpp/sdkcompat/v223/com/google/idea/blaze/cpp/BlazeCWorkspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import com.jetbrains.cidr.lang.CLanguageKind;
import com.jetbrains.cidr.lang.OCLanguageKind;
import com.jetbrains.cidr.lang.toolchains.CidrCompilerSwitches;
import com.jetbrains.cidr.lang.toolchains.CidrCompilerSwitches.Format;
import com.jetbrains.cidr.lang.toolchains.CidrSwitchBuilder;
import com.jetbrains.cidr.lang.toolchains.CidrToolEnvironment;
import com.jetbrains.cidr.lang.workspace.OCCompilerSettings;
Expand Down Expand Up @@ -311,18 +312,24 @@ private static void addConfiguration(
id, displayName, null, OCResolveConfiguration.DEFAULT_FILE_SEPARATORS);
for (Map.Entry<OCLanguageKind, PerLanguageCompilerOpts> languageEntry :
configLanguages.entrySet()) {
OCCompilerSettings.ModifiableModel langSettings =
config.getLanguageCompilerSettings(languageEntry.getKey());
PerLanguageCompilerOpts configForLanguage = languageEntry.getValue();
langSettings.setCompiler(configForLanguage.kind, configForLanguage.compiler, directory);
langSettings.setCompilerSwitches(configForLanguage.switches);
if (CppSupportChecker.isSupportedCppConfiguration(
configForLanguage.switches.getList(Format.RAW), directory.toPath())) {
OCCompilerSettings.ModifiableModel langSettings =
config.getLanguageCompilerSettings(languageEntry.getKey());
langSettings.setCompiler(configForLanguage.kind, configForLanguage.compiler, directory);
langSettings.setCompilerSwitches(configForLanguage.switches);
}
}

for (Map.Entry<VirtualFile, PerFileCompilerOpts> fileEntry : configSourceFiles.entrySet()) {
PerFileCompilerOpts compilerOpts = fileEntry.getValue();
OCCompilerSettings.ModifiableModel fileCompilerSettings =
config.addSource(fileEntry.getKey(), compilerOpts.kind);
fileCompilerSettings.setCompilerSwitches(compilerOpts.switches);
if (CppSupportChecker.isSupportedCppConfiguration(
compilerOpts.switches.getList(Format.RAW), directory.toPath())) {
OCCompilerSettings.ModifiableModel fileCompilerSettings =
config.addSource(fileEntry.getKey(), compilerOpts.kind);
fileCompilerSettings.setCompilerSwitches(compilerOpts.switches);
}
}
}

Expand Down
21 changes: 14 additions & 7 deletions cpp/sdkcompat/v231/com/google/idea/blaze/cpp/BlazeCWorkspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import com.jetbrains.cidr.lang.CLanguageKind;
import com.jetbrains.cidr.lang.OCLanguageKind;
import com.jetbrains.cidr.lang.toolchains.CidrCompilerSwitches;
import com.jetbrains.cidr.lang.toolchains.CidrCompilerSwitches.Format;
import com.jetbrains.cidr.lang.toolchains.CidrSwitchBuilder;
import com.jetbrains.cidr.lang.toolchains.CidrToolEnvironment;
import com.jetbrains.cidr.lang.workspace.OCCompilerSettings;
Expand Down Expand Up @@ -311,18 +312,24 @@ private static void addConfiguration(
id, displayName, null, OCResolveConfiguration.DEFAULT_FILE_SEPARATORS);
for (Map.Entry<OCLanguageKind, PerLanguageCompilerOpts> languageEntry :
configLanguages.entrySet()) {
OCCompilerSettings.ModifiableModel langSettings =
config.getLanguageCompilerSettings(languageEntry.getKey());
PerLanguageCompilerOpts configForLanguage = languageEntry.getValue();
langSettings.setCompiler(configForLanguage.kind, configForLanguage.compiler, directory);
langSettings.setCompilerSwitches(configForLanguage.switches);
if (CppSupportChecker.isSupportedCppConfiguration(
configForLanguage.switches.getList(Format.RAW), directory.toPath())) {
OCCompilerSettings.ModifiableModel langSettings =
config.getLanguageCompilerSettings(languageEntry.getKey());
langSettings.setCompiler(configForLanguage.kind, configForLanguage.compiler, directory);
langSettings.setCompilerSwitches(configForLanguage.switches);
}
}

for (Map.Entry<VirtualFile, PerFileCompilerOpts> fileEntry : configSourceFiles.entrySet()) {
PerFileCompilerOpts compilerOpts = fileEntry.getValue();
OCCompilerSettings.ModifiableModel fileCompilerSettings =
config.addSource(fileEntry.getKey(), compilerOpts.kind);
fileCompilerSettings.setCompilerSwitches(compilerOpts.switches);
if (CppSupportChecker.isSupportedCppConfiguration(
compilerOpts.switches.getList(Format.RAW), directory.toPath())) {
OCCompilerSettings.ModifiableModel fileCompilerSettings =
config.addSource(fileEntry.getKey(), compilerOpts.kind);
fileCompilerSettings.setCompilerSwitches(compilerOpts.switches);
}
}
}

Expand Down
21 changes: 14 additions & 7 deletions cpp/sdkcompat/v232/com/google/idea/blaze/cpp/BlazeCWorkspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import com.jetbrains.cidr.lang.CLanguageKind;
import com.jetbrains.cidr.lang.OCLanguageKind;
import com.jetbrains.cidr.lang.toolchains.CidrCompilerSwitches;
import com.jetbrains.cidr.lang.toolchains.CidrCompilerSwitches.Format;
import com.jetbrains.cidr.lang.toolchains.CidrSwitchBuilder;
import com.jetbrains.cidr.lang.toolchains.CidrToolEnvironment;
import com.jetbrains.cidr.lang.workspace.OCCompilerSettings;
Expand Down Expand Up @@ -311,18 +312,24 @@ private static void addConfiguration(
id, displayName, null, OCResolveConfiguration.DEFAULT_FILE_SEPARATORS);
for (Map.Entry<OCLanguageKind, PerLanguageCompilerOpts> languageEntry :
configLanguages.entrySet()) {
OCCompilerSettings.ModifiableModel langSettings =
config.getLanguageCompilerSettings(languageEntry.getKey());
PerLanguageCompilerOpts configForLanguage = languageEntry.getValue();
langSettings.setCompiler(configForLanguage.kind, configForLanguage.compiler, directory);
langSettings.setCompilerSwitches(configForLanguage.switches);
if (CppSupportChecker.isSupportedCppConfiguration(
configForLanguage.switches.getList(Format.RAW), directory.toPath())) {
OCCompilerSettings.ModifiableModel langSettings =
config.getLanguageCompilerSettings(languageEntry.getKey());
langSettings.setCompiler(configForLanguage.kind, configForLanguage.compiler, directory);
langSettings.setCompilerSwitches(configForLanguage.switches);
}
}

for (Map.Entry<VirtualFile, PerFileCompilerOpts> fileEntry : configSourceFiles.entrySet()) {
PerFileCompilerOpts compilerOpts = fileEntry.getValue();
OCCompilerSettings.ModifiableModel fileCompilerSettings =
config.addSource(fileEntry.getKey(), compilerOpts.kind);
fileCompilerSettings.setCompilerSwitches(compilerOpts.switches);
if (CppSupportChecker.isSupportedCppConfiguration(
compilerOpts.switches.getList(Format.RAW), directory.toPath())) {
OCCompilerSettings.ModifiableModel fileCompilerSettings =
config.addSource(fileEntry.getKey(), compilerOpts.kind);
fileCompilerSettings.setCompilerSwitches(compilerOpts.switches);
}
}
}

Expand Down
21 changes: 14 additions & 7 deletions cpp/sdkcompat/v233/com/google/idea/blaze/cpp/BlazeCWorkspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import com.jetbrains.cidr.lang.CLanguageKind;
import com.jetbrains.cidr.lang.OCLanguageKind;
import com.jetbrains.cidr.lang.toolchains.CidrCompilerSwitches;
import com.jetbrains.cidr.lang.toolchains.CidrCompilerSwitches.Format;
import com.jetbrains.cidr.lang.toolchains.CidrSwitchBuilder;
import com.jetbrains.cidr.lang.toolchains.CidrToolEnvironment;
import com.jetbrains.cidr.lang.workspace.OCCompilerSettings;
Expand Down Expand Up @@ -312,18 +313,24 @@ private static void addConfiguration(
id, displayName, null, OCResolveConfiguration.DEFAULT_FILE_SEPARATORS);
for (Map.Entry<OCLanguageKind, PerLanguageCompilerOpts> languageEntry :
configLanguages.entrySet()) {
OCCompilerSettings.ModifiableModel langSettings =
config.getLanguageCompilerSettings(languageEntry.getKey());
PerLanguageCompilerOpts configForLanguage = languageEntry.getValue();
langSettings.setCompiler(configForLanguage.kind, configForLanguage.compiler, directory);
langSettings.setCompilerSwitches(configForLanguage.switches);
if (CppSupportChecker.isSupportedCppConfiguration(
configForLanguage.switches.getList(Format.RAW), directory.toPath())) {
OCCompilerSettings.ModifiableModel langSettings =
config.getLanguageCompilerSettings(languageEntry.getKey());
langSettings.setCompiler(configForLanguage.kind, configForLanguage.compiler, directory);
langSettings.setCompilerSwitches(configForLanguage.switches);
}
}

for (Map.Entry<VirtualFile, PerFileCompilerOpts> fileEntry : configSourceFiles.entrySet()) {
PerFileCompilerOpts compilerOpts = fileEntry.getValue();
OCCompilerSettings.ModifiableModel fileCompilerSettings =
config.addSource(fileEntry.getKey(), compilerOpts.kind);
fileCompilerSettings.setCompilerSwitches(compilerOpts.switches);
if (CppSupportChecker.isSupportedCppConfiguration(
compilerOpts.switches.getList(Format.RAW), directory.toPath())) {
OCCompilerSettings.ModifiableModel fileCompilerSettings =
config.addSource(fileEntry.getKey(), compilerOpts.kind);
fileCompilerSettings.setCompilerSwitches(compilerOpts.switches);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions cpp/src/META-INF/blaze-cpp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<extensionPoints>
<extensionPoint qualifiedName="com.google.idea.blaze.cpp.BlazeCompilerFlagsProcessorProvider"
interface="com.google.idea.blaze.cpp.BlazeCompilerFlagsProcessor$Provider"/>
<extensionPoint interface="com.google.idea.blaze.cpp.CppSupportChecker"
qualifiedName="com.google.idea.blaze.cpp.CppSupportChecker" />
</extensionPoints>

<extensions defaultExtensionNs="com.google.idea.blaze">
Expand Down
43 changes: 43 additions & 0 deletions cpp/src/com/google/idea/blaze/cpp/CppSupportChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2024 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.idea.blaze.cpp;

import com.google.common.collect.ImmutableList;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.ExtensionPointName;
import java.nio.file.Path;
import java.util.List;

/** Checks whether or not the IDE supports a given CPP configuration. */
public interface CppSupportChecker {

ExtensionPointName<CppSupportChecker> EP_NAME =
new ExtensionPointName<>("com.google.idea.blaze.cpp.CppSupportChecker");

Logger logger = Logger.getInstance(CppSupportChecker.class);

static boolean isSupportedCppConfiguration(List<String> compilerSwitches, Path workspaceRoot) {
for (CppSupportChecker checker : EP_NAME.getExtensionList()) {
if (checker.supportsCppConfiguration(ImmutableList.copyOf(compilerSwitches), workspaceRoot)) {
logger.info("CPP support allowed by " + checker.getClass().getName());
return true;
}
}
return false;
}

boolean supportsCppConfiguration(ImmutableList<String> compilerSwitches, Path workspaceRoot);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.idea.blaze.base.util.UrlUtil;
import com.google.idea.blaze.common.Context;
import com.google.idea.blaze.common.PrintOutput;
import com.google.idea.blaze.cpp.CppSupportChecker;
import com.google.idea.blaze.qsync.cc.FlagResolver;
import com.google.idea.blaze.qsync.project.ProjectPath;
import com.google.idea.blaze.qsync.project.ProjectPath.Root;
Expand All @@ -41,6 +42,7 @@
import com.intellij.util.containers.MultiMap;
import com.jetbrains.cidr.lang.CLanguageKind;
import com.jetbrains.cidr.lang.toolchains.CidrCompilerSwitches;
import com.jetbrains.cidr.lang.toolchains.CidrCompilerSwitches.Format;
import com.jetbrains.cidr.lang.toolchains.CidrToolEnvironment;
import com.jetbrains.cidr.lang.workspace.OCCompilerSettings;
import com.jetbrains.cidr.lang.workspace.OCResolveConfiguration;
Expand Down Expand Up @@ -110,6 +112,12 @@ private void visitCompilationContext(CcCompilationContext ccCc) {
private void visitLanguageCompilerSettingsMap(
Map<String, CcCompilerSettings> map, OCResolveConfiguration.ModifiableModel config) {
for (Map.Entry<String, CcCompilerSettings> e : map.entrySet()) {
CidrCompilerSwitches switches =
checkNotNull(compilerSwitches.get(e.getValue().getFlagSetId()));
if (!CppSupportChecker.isSupportedCppConfiguration(
switches.getList(Format.RAW), compilerWorkingDir.toPath())) {
return;
}
CLanguageKind lang =
getLanguageKind(
ProjectProto.CcLanguage.valueOf(
Expand All @@ -119,12 +127,18 @@ private void visitLanguageCompilerSettingsMap(
config.getLanguageCompilerSettings(lang);
compilerSettings.setCompiler(
ClangCompilerKind.INSTANCE, getCompilerExecutable(e.getValue()), compilerWorkingDir);
compilerSettings.setCompilerSwitches(
checkNotNull(compilerSwitches.get(e.getValue().getFlagSetId())));
compilerSettings.setCompilerSwitches(switches);
}
}

private void visitSourceFile(CcSourceFile source, OCResolveConfiguration.ModifiableModel config) {
CidrCompilerSwitches switches =
checkNotNull(compilerSwitches.get(source.getCompilerSettings().getFlagSetId()));
if (!CppSupportChecker.isSupportedCppConfiguration(
switches.getList(Format.RAW), pathResolver.resolve(ProjectPath.WORKSPACE_ROOT))) {
// Ignore the file if it's not supported by the current IDE.
return;
}
Path srcPath = Path.of(source.getWorkspacePath());
CLanguageKind language =
getLanguageKind(source.getLanguage(), "Source file " + source.getWorkspacePath());
Expand All @@ -134,8 +148,7 @@ private void visitSourceFile(CcSourceFile source, OCResolveConfiguration.Modifia
}
OCCompilerSettings.ModifiableModel perSourceCompilerSettings =
config.addSource(UrlUtil.pathToIdeaDirectoryUrl(srcPath), language);
perSourceCompilerSettings.setCompilerSwitches(
checkNotNull(compilerSwitches.get(source.getCompilerSettings().getFlagSetId())));
perSourceCompilerSettings.setCompilerSwitches(switches);
perSourceCompilerSettings.setCompiler(
ClangCompilerKind.INSTANCE,
getCompilerExecutable(source.getCompilerSettings()),
Expand Down

0 comments on commit bd3db6d

Please sign in to comment.