Skip to content

Commit

Permalink
feat: Support providing compiler arguments (#80)
Browse files Browse the repository at this point in the history
Signed-off-by: Sheng Chen <[email protected]>
  • Loading branch information
jdneo authored Sep 15, 2023
1 parent aab36a3 commit 7eaf009
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.microsoft.java.bs.gradle.model;

import java.io.File;
import java.util.List;
import java.util.Set;

/**
Expand Down Expand Up @@ -89,6 +90,11 @@ public interface GradleSourceSet {
*/
public String getTargetCompatibility();

/**
* The list of compiler arguments.
*/
public List<String> getCompilerArgs();

/**
* Module dependencies.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -102,6 +103,7 @@ public Object buildAll(String modelName, Project rootProject) {
gradleSourceSet.setGradleVersion(gradleVersion);
gradleSourceSet.setSourceCompatibility(getSourceCompatibility(project, sourceSet));
gradleSourceSet.setTargetCompatibility(getTargetCompatibility(project, sourceSet));
gradleSourceSet.setCompilerArgs(getCompilerArgs(project, sourceSet));
gradleSourceSets.add(gradleSourceSet);
});

Expand Down Expand Up @@ -302,6 +304,18 @@ private String getTargetCompatibility(Project project, SourceSet sourceSet) {
return "";
}

/**
* Get the compilation arguments of the source set.
*/
private List<String> getCompilerArgs(Project project, SourceSet sourceSet) {
JavaCompile javaCompile = getJavaCompileTask(project, sourceSet);
if (javaCompile != null) {
return javaCompile.getOptions().getCompilerArgs();
}

return Collections.emptyList();
}

private Set<String> getClasspathConfigurationNames(SourceSet sourceSet) {
Set<String> configurationNames = new HashSet<>();
configurationNames.add(sourceSet.getCompileClasspathConfigurationName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.Set;

Expand Down Expand Up @@ -49,6 +50,8 @@ public class DefaultGradleSourceSet implements GradleSourceSet, Serializable {

private String targetCompatibility;

private List<String> compilerArgs;

private Set<GradleModuleDependency> moduleDependencies;

private Set<GradleProjectDependency> projectDependencies;
Expand Down Expand Up @@ -191,6 +194,14 @@ public void setTargetCompatibility(String targetCompatibility) {
this.targetCompatibility = targetCompatibility;
}

public List<String> getCompilerArgs() {
return compilerArgs;
}

public void setCompilerArgs(List<String> compilerArgs) {
this.compilerArgs = compilerArgs;
}

public Set<GradleModuleDependency> getModuleDependencies() {
return moduleDependencies;
}
Expand All @@ -210,10 +221,11 @@ public void setProjectDependencies(Set<GradleProjectDependency> projectDependenc
@Override
public int hashCode() {
return Objects.hash(projectName, projectPath, projectDir,
rootDir, sourceSetName, classesTaskName, sourceDirs, generatedSourceDirs,
sourceOutputDir, resourceDirs, resourceOutputDir,
javaHome, javaVersion, gradleVersion, sourceCompatibility,
targetCompatibility, moduleDependencies, projectDependencies);
rootDir, sourceSetName, classesTaskName, sourceDirs,
generatedSourceDirs, sourceOutputDir, resourceDirs,
resourceOutputDir, javaHome, javaVersion, gradleVersion,
sourceCompatibility, targetCompatibility, compilerArgs,
moduleDependencies, projectDependencies);
}

@Override
Expand Down Expand Up @@ -244,6 +256,7 @@ public boolean equals(Object obj) {
&& Objects.equals(gradleVersion, other.gradleVersion)
&& Objects.equals(sourceCompatibility, other.sourceCompatibility)
&& Objects.equals(targetCompatibility, other.targetCompatibility)
&& Objects.equals(compilerArgs, other.compilerArgs)
&& Objects.equals(moduleDependencies, other.moduleDependencies)
&& Objects.equals(projectDependencies, other.projectDependencies);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void testModelBuilder() throws IOException {
assertNotNull(gradleSourceSet.getJavaVersion());
assertNotNull(gradleSourceSet.getSourceCompatibility());
assertNotNull(gradleSourceSet.getTargetCompatibility());
assertNotNull(gradleSourceSet.getCompilerArgs());
assertNotNull(gradleSourceSet.getGradleVersion());
assertNotNull(gradleSourceSet.getModuleDependencies());
assertTrue(gradleSourceSet.getModuleDependencies().stream().anyMatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import ch.epfl.scala.bsp4j.InitializeBuildResult;
import ch.epfl.scala.bsp4j.InverseSourcesParams;
import ch.epfl.scala.bsp4j.InverseSourcesResult;
import ch.epfl.scala.bsp4j.JavaBuildServer;
import ch.epfl.scala.bsp4j.JavacOptionsParams;
import ch.epfl.scala.bsp4j.JavacOptionsResult;
import ch.epfl.scala.bsp4j.OutputPathsParams;
import ch.epfl.scala.bsp4j.OutputPathsResult;
import ch.epfl.scala.bsp4j.ResourcesParams;
Expand All @@ -50,7 +53,7 @@
/**
* The implementation of the Build Server Protocol.
*/
public class GradleBuildServer implements BuildServer {
public class GradleBuildServer implements BuildServer, JavaBuildServer {

private LifecycleService lifecycleService;

Expand Down Expand Up @@ -163,6 +166,12 @@ public CompletableFuture<DependencyModulesResult> buildTargetDependencyModules(
buildTargetService.getBuildTargetDependencyModules(params));
}

@Override
public CompletableFuture<JavacOptionsResult> buildTargetJavacOptions(JavacOptionsParams params) {
return handleRequest("buildTarget/javacOptions", cc ->
buildTargetService.getBuildTargetrJavacOptions(params));
}

private void handleNotification(String methodName, Runnable runnable, boolean async) {
LogEntity entity = new LogEntity.Builder()
.operationName(escapeMethodName(methodName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -30,6 +31,9 @@
import ch.epfl.scala.bsp4j.DependencyModulesItem;
import ch.epfl.scala.bsp4j.DependencyModulesParams;
import ch.epfl.scala.bsp4j.DependencyModulesResult;
import ch.epfl.scala.bsp4j.JavacOptionsItem;
import ch.epfl.scala.bsp4j.JavacOptionsParams;
import ch.epfl.scala.bsp4j.JavacOptionsResult;
import ch.epfl.scala.bsp4j.MavenDependencyModule;
import ch.epfl.scala.bsp4j.MavenDependencyModuleArtifact;
import ch.epfl.scala.bsp4j.OutputPathItem;
Expand Down Expand Up @@ -234,6 +238,30 @@ public CompileResult compile(CompileParams params) {
return result;
}

/**
* Get the compiler options.
*/
public JavacOptionsResult getBuildTargetrJavacOptions(JavacOptionsParams params) {
List<JavacOptionsItem> items = new ArrayList<>();
for (BuildTargetIdentifier btId : params.getTargets()) {
GradleBuildTarget target = buildTargetManager.getGradleBuildTarget(btId);
if (target == null) {
LOGGER.warning("Skip javac options collection for the build target: " + btId.getUri()
+ ". Because it cannot be found in the cache.");
continue;
}

GradleSourceSet sourceSet = target.getSourceSet();
items.add(new JavacOptionsItem(
btId,
sourceSet.getCompilerArgs(),
Collections.emptyList(), // classpath, not support currently
"" // classDirectory, not support currently
));
}
return new JavacOptionsResult(items);
}

/**
* Group the build targets by the project root directory,
* projects with the same root directory can run their tasks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
Expand All @@ -29,6 +30,8 @@
import ch.epfl.scala.bsp4j.DependencyModule;
import ch.epfl.scala.bsp4j.DependencyModulesParams;
import ch.epfl.scala.bsp4j.DependencyModulesResult;
import ch.epfl.scala.bsp4j.JavacOptionsParams;
import ch.epfl.scala.bsp4j.JavacOptionsResult;
import ch.epfl.scala.bsp4j.MavenDependencyModule;
import ch.epfl.scala.bsp4j.MavenDependencyModuleArtifact;
import ch.epfl.scala.bsp4j.OutputPathsParams;
Expand Down Expand Up @@ -212,4 +215,27 @@ public String getClassifier() {
MavenDependencyModuleArtifact artifact = module.getArtifacts().get(0);
assertEquals("sources", artifact.getClassifier());
}

@Test
void testGetJavacOptions() {
GradleBuildTarget gradleBuildTarget = mock(GradleBuildTarget.class);
when(buildTargetManager.getGradleBuildTarget(any())).thenReturn(gradleBuildTarget);

GradleSourceSet gradleSourceSet = mock(GradleSourceSet.class);
when(gradleBuildTarget.getSourceSet()).thenReturn(gradleSourceSet);

List<String> compilerArgs = new ArrayList<>();

compilerArgs.add("--add-opens");
compilerArgs.add("java.base/java.lang=ALL-UNNAMED");
when(gradleSourceSet.getCompilerArgs()).thenReturn(compilerArgs);

BuildTargetService buildTargetService = new BuildTargetService(buildTargetManager,
preferenceManager);
JavacOptionsResult javacOptions = buildTargetService.getBuildTargetrJavacOptions(
new JavacOptionsParams(Arrays.asList(new BuildTargetIdentifier("test"))));

assertEquals(1, javacOptions.getItems().size());
assertEquals(2, javacOptions.getItems().get(0).getOptions().size());
}
}

0 comments on commit 7eaf009

Please sign in to comment.