Skip to content

Commit

Permalink
CLion MSVC Support (bazelbuild#6500)
Browse files Browse the repository at this point in the history
* Add .exe if compiler cannot be found on windows

* Disable the generated compiler wrapper script for all platforms except mac

* Add arg max heuristic for windows

* Add MSVC support

* Add sdkcompat for MSVCCompilerToVersionCacheService

* Persist compiler information to select right debugger

* Disable gdb on windows

* Replaced generic switch builder with compiler specific

* fixed #api tags in sdkcompat

* added static to methods

fixes bazelbuild#6451
  • Loading branch information
LeFrosch authored Jul 24, 2024
1 parent 2036193 commit 26709ab
Show file tree
Hide file tree
Showing 36 changed files with 1,130 additions and 292 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.google.idea.blaze.base.async.process.ExternalTask;
import com.google.idea.common.experiments.BoolExperiment;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.SystemInfo;

import java.io.ByteArrayOutputStream;
import java.util.OptionalInt;
import java.util.function.Supplier;
Expand Down Expand Up @@ -51,9 +53,21 @@ public OptionalInt getShardSizeLimit() {
return OptionalInt.of((argMax - envSizeBytes) / targetStringSizeBytes);
}

/** Synchronously runs 'getconf ARG_MAX', returning null if unsuccessful. */
/**
* Synchronously runs 'getconf ARG_MAX', returning null if unsuccessful.
* <p>
* Returns 32767 on windows which is the maximum length of lpCommandLine argument for the
* <a href="https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa">
* CreateProcessA
* </a>
* function in the windows api.
*/
@Nullable
private static Integer queryArgMax() {
if (SystemInfo.isWindows) {
return 32767;
}

ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
int retVal =
Expand Down
1 change: 1 addition & 0 deletions clwb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ java_library(
"//intellij_platform_sdk:jsr305",
"//intellij_platform_sdk:plugin_api",
"//sdkcompat",
"//clwb/sdkcompat",
],
)

Expand Down
32 changes: 32 additions & 0 deletions clwb/sdkcompat/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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.

load(
"//intellij_platform_sdk:build_defs.bzl",
"select_for_plugin_api",
)

java_library(
name = "sdkcompat",
srcs = select_for_plugin_api({
"clion-2022.3": ["//clwb/sdkcompat/v223"],
"clion-2023.1": ["//clwb/sdkcompat/v231"],
"clion-2023.2": ["//clwb/sdkcompat/v232"],
"clion-2023.3": ["//clwb/sdkcompat/v233"],
"clion-2024.1": ["//clwb/sdkcompat/v241"],
"clion-2024.2": ["//clwb/sdkcompat/v242"],
}),
visibility = ["//clwb:__pkg__"],
deps = ["//intellij_platform_sdk:plugin_api"],
)
19 changes: 19 additions & 0 deletions clwb/sdkcompat/v223/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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.

filegroup(
name = "v223",
srcs = glob(["**/*.java"]),
visibility = ["//clwb/sdkcompat:__pkg__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.google.idea.blaze.clwb;

import com.jetbrains.cidr.cpp.toolchains.CPPEnvironment;

import java.io.File;
import javax.annotation.Nullable;

// #api223
public class MSVCCompilerVersionCompat {
public record ArchAndVersion() {}

public static @Nullable ArchAndVersion getCompilerVersion(File compiler) {
return new ArchAndVersion();
}

public static void setEnvironmentVersion(CPPEnvironment environment, ArchAndVersion version) { }
}
19 changes: 19 additions & 0 deletions clwb/sdkcompat/v231/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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.

filegroup(
name = "v231",
srcs = glob(["**/*.java"]),
visibility = ["//clwb/sdkcompat:__pkg__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.google.idea.blaze.clwb;

import com.intellij.openapi.application.ApplicationManager;
import com.jetbrains.cidr.cpp.toolchains.CPPEnvironment;
import com.jetbrains.cidr.cpp.toolchains.MSVC;
import com.jetbrains.cidr.cpp.toolchains.msvc.MSVCCompilerToVersionCacheService;
import java.io.File;
import javax.annotation.Nullable;

// #api231
public class MSVCCompilerVersionCompat {
public record ArchAndVersion(MSVCCompilerToVersionCacheService.ArchAndVersion delegate) {}

public static @Nullable ArchAndVersion getCompilerVersion(File compiler) {
final var service = ApplicationManager.getApplication()
.getService(MSVCCompilerToVersionCacheService.class);

final var result = service.getCompilerVersion(compiler.getAbsolutePath());
if (result == null) {
return null;
}

return new ArchAndVersion(result);
}

public static void setEnvironmentVersion(CPPEnvironment environment, ArchAndVersion version) {
final var msvc = (MSVC) environment.getToolSet();
msvc.setToolsVersion(version.delegate);
}
}
19 changes: 19 additions & 0 deletions clwb/sdkcompat/v232/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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.

filegroup(
name = "v232",
srcs = glob(["**/*.java"]),
visibility = ["//clwb/sdkcompat:__pkg__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.google.idea.blaze.clwb;

import com.intellij.openapi.application.ApplicationManager;
import com.jetbrains.cidr.cpp.toolchains.CPPEnvironment;
import com.jetbrains.cidr.cpp.toolchains.MSVC;
import com.jetbrains.cidr.cpp.toolchains.msvc.MSVCArchAndVersion;
import com.jetbrains.cidr.cpp.toolchains.msvc.MSVCCompilerToVersionCacheService;
import java.io.File;
import javax.annotation.Nullable;

// #api232
public class MSVCCompilerVersionCompat {
public record ArchAndVersion(MSVCArchAndVersion delegate) {}

public static @Nullable ArchAndVersion getCompilerVersion(File compiler) {
final var service = ApplicationManager.getApplication()
.getService(MSVCCompilerToVersionCacheService.class);

final var result = service.getCompilerVersion(compiler.getAbsolutePath());
if (result == null) {
return null;
}

return new ArchAndVersion(result);
}

public static void setEnvironmentVersion(CPPEnvironment environment, ArchAndVersion version) {
final var msvc = (MSVC) environment.getToolSet();
msvc.setToolsVersion(version.delegate);
}
}
19 changes: 19 additions & 0 deletions clwb/sdkcompat/v233/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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.

filegroup(
name = "v233",
srcs = glob(["**/*.java"]),
visibility = ["//clwb/sdkcompat:__pkg__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.google.idea.blaze.clwb;

import com.intellij.openapi.application.ApplicationManager;
import com.jetbrains.cidr.cpp.toolchains.CPPEnvironment;
import com.jetbrains.cidr.cpp.toolchains.MSVC;
import com.jetbrains.cidr.cpp.toolchains.msvc.MSVCArchAndVersion;
import com.jetbrains.cidr.cpp.toolchains.msvc.MSVCCompilerToVersionCacheService;
import java.io.File;
import javax.annotation.Nullable;

// #api232
public class MSVCCompilerVersionCompat {
public record ArchAndVersion(MSVCArchAndVersion delegate) {}

public static @Nullable ArchAndVersion getCompilerVersion(File compiler) {
final var service = ApplicationManager.getApplication()
.getService(MSVCCompilerToVersionCacheService.class);

final var result = service.getCompilerVersion(compiler.getAbsolutePath());
if (result == null) {
return null;
}

return new ArchAndVersion(result);
}

public static void setEnvironmentVersion(CPPEnvironment environment, ArchAndVersion version) {
final var msvc = (MSVC) environment.getToolSet();
msvc.setToolsVersion(version.delegate);
}
}
19 changes: 19 additions & 0 deletions clwb/sdkcompat/v241/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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.

filegroup(
name = "v241",
srcs = glob(["**/*.java"]),
visibility = ["//clwb/sdkcompat:__pkg__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.google.idea.blaze.clwb;

import com.intellij.openapi.application.ApplicationManager;
import com.jetbrains.cidr.cpp.toolchains.CPPEnvironment;
import com.jetbrains.cidr.cpp.toolchains.MSVC;
import com.jetbrains.cidr.cpp.toolchains.msvc.MSVCArchAndVersion;
import com.jetbrains.cidr.cpp.toolchains.msvc.MSVCCompilerToVersionCacheService;
import java.io.File;
import javax.annotation.Nullable;

// #api232
public class MSVCCompilerVersionCompat {
public record ArchAndVersion(MSVCArchAndVersion delegate) {}

public static @Nullable ArchAndVersion getCompilerVersion(File compiler) {
final var service = ApplicationManager.getApplication()
.getService(MSVCCompilerToVersionCacheService.class);

final var result = service.getCompilerVersion(compiler.getAbsolutePath());
if (result == null) {
return null;
}

return new ArchAndVersion(result);
}

public static void setEnvironmentVersion(CPPEnvironment environment, ArchAndVersion version) {
final var msvc = (MSVC) environment.getToolSet();
msvc.setToolsVersion(version.delegate);
}
}
19 changes: 19 additions & 0 deletions clwb/sdkcompat/v242/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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.

filegroup(
name = "v242",
srcs = glob(["**/*.java"]),
visibility = ["//clwb/sdkcompat:__pkg__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.google.idea.blaze.clwb;

import com.intellij.openapi.application.ApplicationManager;
import com.jetbrains.cidr.cpp.toolchains.CPPEnvironment;
import com.jetbrains.cidr.cpp.toolchains.MSVC;
import com.jetbrains.cidr.cpp.toolchains.msvc.MSVCArchAndVersion;
import com.jetbrains.cidr.cpp.toolchains.msvc.MSVCCompilerToVersionCacheService;
import java.io.File;
import javax.annotation.Nullable;

// #api232
public class MSVCCompilerVersionCompat {
public record ArchAndVersion(MSVCArchAndVersion delegate) {}

public static @Nullable ArchAndVersion getCompilerVersion(File compiler) {
final var service = ApplicationManager.getApplication()
.getService(MSVCCompilerToVersionCacheService.class);

final var result = service.getCompilerVersion(compiler.getAbsolutePath());
if (result == null) {
return null;
}

return new ArchAndVersion(result);
}

public static void setEnvironmentVersion(CPPEnvironment environment, ArchAndVersion version) {
final var msvc = (MSVC) environment.getToolSet();
msvc.setToolsVersion(version.delegate);
}
}
3 changes: 3 additions & 0 deletions clwb/src/META-INF/clwb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<actionConfigurationCustomizer implementation="com.google.idea.blaze.plugin.ClwbHideMakeActions"/>
<registryKey defaultValue="false" description="Disable the extra debug flags in debug C/C++ builds" key="bazel.clwb.debug.extraflags.disabled"/>
<registryKey defaultValue="false" description="Disable the fission flag in debug C/C++ builds" key="bazel.clwb.debug.fission.disabled"/>
<registryKey defaultValue="true" description="Use GDB-Server instead of bundled GDB version" key="bazel.clwb.debug.use.gdb.server"/>
<registryKey defaultValue="false" description="Use the debugger selected in the default toolchain" key="bazel.clwb.debug.use.default.toolchain"/>
</extensions>

<extensions defaultExtensionNs="com.google.idea.blaze">
Expand All @@ -45,6 +47,7 @@

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

<project-components>
Expand Down
Loading

0 comments on commit 26709ab

Please sign in to comment.