Skip to content

Commit

Permalink
Add cpp build target
Browse files Browse the repository at this point in the history
  • Loading branch information
SocksDevil committed Apr 8, 2021
1 parent d8a3fb9 commit 9fa25dd
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 40 deletions.
31 changes: 15 additions & 16 deletions src/main/java/org/jetbrains/bsp/bazel/commons/Constants.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.jetbrains.bsp.bazel.commons;

import com.google.common.collect.ImmutableList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class Constants {

Expand All @@ -12,6 +14,7 @@ public class Constants {
public static final String SCALA = "scala";
public static final String JAVA = "java";
public static final String KOTLIN = "kotlin";
public static final String CPP = "cpp";

public static final String SCALAC = "Scalac";
public static final String JAVAC = "Javac";
Expand All @@ -20,28 +23,24 @@ public class Constants {
public static final List<String> SUPPORTED_LANGUAGES = ImmutableList.of(SCALA, JAVA, KOTLIN);
public static final List<String> SUPPORTED_COMPILERS = ImmutableList.of(SCALAC, JAVAC, KOTLINC);

public static final String SCALA_EXTENSION = ".scala";
public static final String JAVA_EXTENSION = ".java";
public static final String KOTLIN_EXTENSION = ".kt";
public static final List<String> SCALA_EXTENSIONS = ImmutableList.of(".scala");
public static final List<String> JAVA_EXTENSIONS = ImmutableList.of(".java");
public static final List<String> KOTLIN_EXTENSIONS = ImmutableList.of(".kt");
public static final List<String> CPP_EXTENSIONS =
ImmutableList.of(".C", ".cc", ".cpp", ".CPP", ".c++", ".cp", "cxx", ".h", ".hpp");

public static final String MAIN_CLASS_ATTR_NAME = "main_class";
public static final String ARGS_ATTR_NAME = "args";
public static final String JVM_FLAGS_ATTR_NAME = "jvm_flags";

public static final List<String> OTHER_FILE_EXTENSIONS =
ImmutableList.of(".kts", ".sh", ".bzl", ".py", ".js", ".c", ".h", ".cpp", ".hpp");

public static final List<String> FILE_EXTENSIONS =
ImmutableList.of(
SCALA_EXTENSION,
JAVA_EXTENSION,
KOTLIN_EXTENSION,
".kts",
".sh",
".bzl",
".py",
".js",
".c",
".h",
".cpp",
".hpp");
ImmutableList.of(SCALA_EXTENSIONS, JAVA_EXTENSIONS, KOTLIN_EXTENSIONS, OTHER_FILE_EXTENSIONS)
.stream()
.flatMap(Collection::stream)
.collect(Collectors.toUnmodifiableList());

public static final String BAZEL_BUILD_COMMAND = "build";

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/jetbrains/bsp/bazel/install/aspects.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ scala_compiler_classpath_aspect = aspect(
implementation = _scala_compiler_classpath_impl,
)

def _fetch_cpp_compiler(target, ctx):
if cc_common.CcToolchainInfo in target:
toolchain_info = target[cc_common.CcToolchainInfo]
print(toolchain_info.compiler)
print(toolchain_info.compiler_executable)
return []

fetch_cpp_compiler = aspect(
implementation = _fetch_cpp_compiler,
fragments = ["cpp"],
attr_aspects = ["_cc_toolchain"],
required_aspect_providers = [[CcInfo]],
)

def _fetch_java_target_version(target, ctx):
if hasattr(ctx.rule.attr, "target_version"):
print(ctx.rule.attr.target_version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
public class BazelBspServerBuildManager {

private static final Logger LOGGER = LogManager.getLogger(BazelBspServerBuildManager.class);

private static final String BAZEL_CPP_TOOLCHAIN = "@bazel_tools//tools/cpp:toolchain";
private static final String FETCH_CPP_ASPECT = "@//.bazelbsp:aspects.bzl%fetch_cpp_compiler";
public static final String DEBUG_MESSAGE = "DEBUG:";
public static final String ASPECT_LOCATION = ".bazelbsp/aspects.bzl";
public static final String FETCH_JAVA_VERSION_ASPECT =
Expand Down Expand Up @@ -88,14 +89,18 @@ public BuildTarget getBuildTargetForRule(Build.Rule rule) {
Set<String> extensions = new TreeSet<>();

for (SourceItem source : sources) {
if (source.getUri().endsWith(Constants.SCALA_EXTENSION)) {
if (Constants.SCALA_EXTENSIONS.stream().anyMatch(ext -> source.getUri().endsWith(ext))) {
extensions.add(Constants.SCALA);
} else if (source.getUri().endsWith(Constants.JAVA_EXTENSION)) {
} else if (Constants.JAVA_EXTENSIONS.stream()
.anyMatch(ext -> source.getUri().endsWith(ext))) {
extensions.add(Constants.JAVA);
} else if (source.getUri().endsWith(Constants.KOTLIN_EXTENSION)) {
} else if (Constants.KOTLIN_EXTENSIONS.stream()
.anyMatch(ext -> source.getUri().endsWith(ext))) {
extensions.add(Constants.KOTLIN);
extensions.add(
Constants.JAVA); // TODO(andrefmrocha): Remove this when kotlin is natively supported
} else if (Constants.CPP_EXTENSIONS.stream().anyMatch(ext -> source.getUri().endsWith(ext))) {
extensions.add(Constants.CPP);
}
}

Expand Down Expand Up @@ -126,6 +131,15 @@ public BuildTarget getBuildTargetForRule(Build.Rule rule) {
target.setDataKind(BuildTargetDataKind.JVM);
target.setTags(Lists.newArrayList(BuildManagerParsingUtils.getRuleType(rule.getRuleClass())));
target.setData(getJVMBuildTarget(rule));
} else if (extensions.contains(Constants.CPP)) {
getCppBuildTarget()
.ifPresent(
buildTarget -> {
target.setDataKind(BuildTargetDataKind.CPP);
target.setTags(
Lists.newArrayList(BuildManagerParsingUtils.getRuleType(rule.getRuleClass())));
target.setData(buildTarget);
});
}
return target;
}
Expand Down Expand Up @@ -236,41 +250,74 @@ private Optional<Build.Rule> traverseDependency(
return Optional.of(currentRule);
}

private Optional<Build.Target> getTargetFromAttribute(Build.Attribute attribute) {
BazelProcess processResult =
bazelRunner
.commandBuilder()
.query()
.withFlag(BazelRunnerFlag.OUTPUT_PROTO)
.withArgument(attribute.getStringValue())
.executeBazelBesCommand();
private CppBuildTarget cppBuildTarget;

return QueryResolver.getQueryResultForProcess(processResult).getTargetList().stream()
.findFirst();
}

private Optional<String> getJavaVersion() {
if (javaVersion == null) {
private Optional<CppBuildTarget> getCppBuildTarget() {
if (cppBuildTarget == null) {
List<String> lines =
bazelRunner
.commandBuilder()
.build()
.withFlag(BazelRunnerFlag.ASPECTS, FETCH_JAVA_VERSION_ASPECT)
.withArgument(BAZEL_JDK_CURRENT_JAVA_TOOLCHAIN)
.withFlag(BazelRunnerFlag.ASPECTS, FETCH_CPP_ASPECT)
.withArgument(BAZEL_CPP_TOOLCHAIN)
.executeBazelCommand()
.getStderr();

Optional<String> javaVersion =
List<String> cppInfo =
lines.stream()
.map(line -> Splitter.on(" ").splitToList(line))
.filter(
parts ->
parts.size() == 3
&& parts.get(0).equals(DEBUG_MESSAGE)
&& parts.get(1).contains(ASPECT_LOCATION)
&& parts.get(2).chars().allMatch(Character::isDigit))
&& parts.get(1).contains(ASPECT_LOCATION))
.map(parts -> parts.get(2))
.findFirst();
.limit(2)
.collect(Collectors.toList());

String compiler = cppInfo.get(0);
String compilerExecutable = cppInfo.get(1);
cppBuildTarget = new CppBuildTarget(null, compiler, compilerExecutable, compilerExecutable);
}

return Optional.of(cppBuildTarget);
}

private Optional<Build.Target> getTargetFromAttribute(Build.Attribute attribute) {
BazelProcess processResult =
bazelRunner
.commandBuilder()
.query()
.withFlag(BazelRunnerFlag.OUTPUT_PROTO)
.withArgument(attribute.getStringValue())
.executeBazelBesCommand();

return QueryResolver.getQueryResultForProcess(processResult).getTargetList().stream()
.findFirst();
}

private Optional<String> getJavaVersion() {
if (javaVersion == null) {
List<String> lines =
bazelRunner
.commandBuilder()
.build()
.withFlag(BazelRunnerFlag.ASPECTS, FETCH_JAVA_VERSION_ASPECT)
.withArgument(BAZEL_JDK_CURRENT_JAVA_TOOLCHAIN)
.executeBazelCommand()
.getStderr();

Optional<String> javaVersion =
lines.stream()
.map(line -> Splitter.on(" ").splitToList(line))
.filter(
parts ->
parts.size() == 3
&& parts.get(0).equals(DEBUG_MESSAGE)
&& parts.get(1).contains(ASPECT_LOCATION)
&& parts.get(2).chars().allMatch(Character::isDigit))
.map(parts -> parts.get(2))
.findFirst();

javaVersion.ifPresent(version -> this.javaVersion = version);
}
Expand Down

0 comments on commit 9fa25dd

Please sign in to comment.