Skip to content

Commit

Permalink
Merge pull request #25346 from glefloch/fix/2811
Browse files Browse the repository at this point in the history
Allow setting kotlinc compiler arguments in dev task/mojo
  • Loading branch information
aloubyansky authored May 13, 2022
2 parents c8a7267 + 5d25483 commit 035447b
Show file tree
Hide file tree
Showing 22 changed files with 324 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import io.quarkus.paths.PathCollection;
Expand All @@ -17,6 +18,10 @@ public interface CompilationProvider extends Closeable {

Set<String> handledExtensions();

default String getProviderKey() {
return getClass().getName();
}

default Set<String> handledSourcePaths() {
return Collections.emptySet();
}
Expand All @@ -38,7 +43,7 @@ class Context {
private final File sourceDirectory;
private final File outputDirectory;
private final Charset sourceEncoding;
private final List<String> compilerOptions;
private final Map<String, Set<String>> compilerOptions;
private final String releaseJavaVersion;
private final String sourceJavaVersion;
private final String targetJvmVersion;
Expand All @@ -52,7 +57,7 @@ public Context(
File sourceDirectory,
File outputDirectory,
String sourceEncoding,
List<String> compilerOptions,
Map<String, Set<String>> compilerOptions,
String releaseJavaVersion,
String sourceJavaVersion,
String targetJvmVersion,
Expand All @@ -64,7 +69,7 @@ public Context(
this.sourceDirectory = sourceDirectory;
this.outputDirectory = outputDirectory;
this.sourceEncoding = sourceEncoding == null ? StandardCharsets.UTF_8 : Charset.forName(sourceEncoding);
this.compilerOptions = compilerOptions == null ? new ArrayList<String>() : compilerOptions;
this.compilerOptions = compilerOptions == null ? new HashMap<>() : compilerOptions;
this.releaseJavaVersion = releaseJavaVersion;
this.sourceJavaVersion = sourceJavaVersion;
this.targetJvmVersion = targetJvmVersion;
Expand Down Expand Up @@ -96,8 +101,8 @@ public Charset getSourceEncoding() {
return sourceEncoding;
}

public List<String> getCompilerOptions() {
return compilerOptions;
public Set<String> getCompilerOptions(String key) {
return compilerOptions.getOrDefault(key, Collections.emptySet());
}

public String getReleaseJavaVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -50,7 +51,7 @@ public class DevModeContext implements Serializable {
// args of the main-method
private String[] args;

private List<String> compilerOptions;
private Map<String, Set<String>> compilerOptions;
private String releaseJavaVersion;
private String sourceJavaVersion;
private String targetJvmVersion;
Expand Down Expand Up @@ -138,11 +139,11 @@ public void setAbortOnFailedStart(boolean abortOnFailedStart) {
this.abortOnFailedStart = abortOnFailedStart;
}

public List<String> getCompilerOptions() {
public Map<String, Set<String>> getCompilerOptions() {
return compilerOptions;
}

public void setCompilerOptions(List<String> compilerOptions) {
public void setCompilerOptions(Map<String, Set<String>> compilerOptions) {
this.compilerOptions = compilerOptions;
}

Expand Down Expand Up @@ -451,6 +452,6 @@ public boolean isEnablePreview() {
if (compilerOptions == null) {
return false;
}
return compilerOptions.contains(ENABLE_PREVIEW_FLAG);
return compilerOptions.getOrDefault("java", Collections.emptySet()).contains(ENABLE_PREVIEW_FLAG);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class JavaCompilationProvider implements CompilationProvider {
StandardJavaFileManager fileManager;
DiagnosticCollector<JavaFileObject> fileManagerDiagnostics;

@Override
public String getProviderKey() {
return "java";
}

@Override
public Set<String> handledExtensions() {
return Collections.singleton(".java");
Expand All @@ -64,7 +69,7 @@ public void compile(Set<File> filesToCompile, Context context) {
fileManager.setLocation(StandardLocation.CLASS_PATH, context.getClasspath());
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(context.getOutputDirectory()));

CompilerFlags compilerFlags = new CompilerFlags(COMPILER_OPTIONS, context.getCompilerOptions(),
CompilerFlags compilerFlags = new CompilerFlags(COMPILER_OPTIONS, context.getCompilerOptions(getProviderKey()),
context.getReleaseJavaVersion(), context.getSourceJavaVersion(), context.getTargetJvmVersion());

Iterable<? extends JavaFileObject> sources = fileManager.getJavaFileObjectsFromFiles(filesToCompile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class QuarkusCompiler implements Closeable {
private static final Logger log = Logger.getLogger(QuarkusCompiler.class);
private static final Pattern WHITESPACE_PATTERN = Pattern.compile(" ");

private static final String JAVA_COMPILER_KEY = "java";
private static final String KOTLIN_COMPILER_KEY = "kotlin";

private final List<CompilationProvider> compilationProviders;
/**
* map of compilation contexts to source directories
Expand Down Expand Up @@ -157,6 +160,7 @@ public void setupSourceCompilationContext(DevModeContext context, Set<File> clas
+ "'. It is advised that this module be compiled before launching dev mode");
return;
}

for (Path sourcePath : compilationUnit.getSourcePaths()) {
final String srcPathStr = sourcePath.toString();
if (this.compilationContexts.containsKey(srcPathStr)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,19 @@ public B sourceEncoding(String srcEncoding) {
return (B) this;
}

@SuppressWarnings("unchecked")
public B compilerOption(String option) {
compilerOptions.add(option);
public B compilerOptions(String name, List<String> options) {
compilerOptions.compute(name, (key, value) -> {
if (value == null) {
return new HashSet<>(options);
}
value.addAll(options);
return value;
});
return (B) this;
}

@SuppressWarnings("unchecked")
public B compilerOptions(List<String> options) {
compilerOptions.addAll(options);
public B compilerOptions(Map<String, Set<String>> options) {
compilerOptions.putAll(options);
return (B) this;
}

Expand Down Expand Up @@ -286,7 +290,7 @@ public R build() throws Exception {
private String applicationName;
private String applicationVersion;
private String sourceEncoding;
private List<String> compilerOptions = new ArrayList<>(0);
private Map<String, Set<String>> compilerOptions = new HashMap<>(1);
private List<String> compilerPluginArtifacts;
private List<String> compilerPluginOptions;
private String releaseJavaVersion;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.gradle.dsl;

import java.util.ArrayList;
import java.util.List;

public class CompilerOption {

private final String name;
private final List<String> opts = new ArrayList<>(0);

public CompilerOption(String name) {
this.name = name;
}

public CompilerOption args(List<String> options) {
opts.addAll(options);
return this;
}

public String getName() {
return name;
}

public List<String> getArgs() {
return opts;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.gradle.dsl;

import java.util.ArrayList;
import java.util.List;

public class CompilerOptions {

private final List<CompilerOption> compilerOptions = new ArrayList<>(1);

public CompilerOption compiler(String name) {
CompilerOption compilerOption = new CompilerOption(name);
compilerOptions.add(compilerOption);
return compilerOption;
}

public List<CompilerOption> getCompilerOptions() {
return compilerOptions;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.inject.Inject;

import org.apache.tools.ant.types.Commandline;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
Expand All @@ -33,6 +34,7 @@
import org.gradle.api.tasks.CompileClasspath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputDirectory;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.compile.JavaCompile;
Expand All @@ -52,6 +54,8 @@
import io.quarkus.deployment.dev.DevModeContext;
import io.quarkus.deployment.dev.DevModeMain;
import io.quarkus.deployment.dev.QuarkusDevModeLauncher;
import io.quarkus.gradle.dsl.CompilerOption;
import io.quarkus.gradle.dsl.CompilerOptions;
import io.quarkus.gradle.tooling.ToolingUtils;
import io.quarkus.maven.dependency.ArtifactKey;
import io.quarkus.maven.dependency.ResolvedDependency;
Expand Down Expand Up @@ -80,6 +84,8 @@ public class QuarkusDev extends QuarkusTask {

private List<String> compilerArgs = new LinkedList<>();

private CompilerOptions compilerOptions = new CompilerOptions();

private boolean shouldPropagateJavaCompilerArgs = true;

@Inject
Expand Down Expand Up @@ -192,6 +198,16 @@ public void setCompilerArgs(List<String> compilerArgs) {
this.compilerArgs = compilerArgs;
}

@Internal
public CompilerOptions getCompilerOptions() {
return this.compilerOptions;
}

public QuarkusDev compilerOptions(Action<CompilerOptions> action) {
action.execute(compilerOptions);
return this;
}

@TaskAction
public void startDev() {
if (!getSourceDir().isDirectory()) {
Expand Down Expand Up @@ -313,12 +329,16 @@ private QuarkusDevModeLauncher newLauncher() throws Exception {
builder.targetJavaVersion(javaPluginConvention.getTargetCompatibility().toString());
}

for (CompilerOption compilerOptions : compilerOptions.getCompilerOptions()) {
builder.compilerOptions(compilerOptions.getName(), compilerOptions.getArgs());
}

if (getCompilerArgs().isEmpty() && shouldPropagateJavaCompilerArgs) {
getJavaCompileTask()
.map(compileTask -> compileTask.getOptions().getCompilerArgs())
.ifPresent(builder::compilerOptions);
.ifPresent(args -> builder.compilerOptions("java", args));
} else {
builder.compilerOptions(getCompilerArgs());
builder.compilerOptions("java", getCompilerArgs());
}

modifyDevModeContext(builder);
Expand Down
18 changes: 16 additions & 2 deletions devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import io.quarkus.deployment.dev.DevModeMain;
import io.quarkus.deployment.dev.QuarkusDevModeLauncher;
import io.quarkus.maven.MavenDevModeLauncher.Builder;
import io.quarkus.maven.components.CompilerOptions;
import io.quarkus.maven.components.MavenVersionEnforcer;
import io.quarkus.maven.dependency.ArtifactKey;
import io.quarkus.maven.dependency.GACT;
Expand Down Expand Up @@ -305,6 +306,12 @@ public class DevMojo extends AbstractMojo {
@Parameter
private List<String> compilerArgs;

/**
* Additional compiler arguments
*/
@Parameter
private List<CompilerOptions> compilerOptions;

/**
* The --release argument to javac.
*/
Expand Down Expand Up @@ -957,11 +964,17 @@ private QuarkusDevModeLauncher newLauncher() throws Exception {

builder.sourceEncoding(getSourceEncoding());

if (compilerOptions != null) {
for (CompilerOptions compilerOption : compilerOptions) {
builder.compilerOptions(compilerOption.getName(), compilerOption.getArgs());
}
}

// Set compilation flags. Try the explicitly given configuration first. Otherwise,
// refer to the configuration of the Maven Compiler Plugin.
final Optional<Xpp3Dom> compilerPluginConfiguration = findCompilerPluginConfiguration();
if (compilerArgs != null) {
builder.compilerOptions(compilerArgs);
builder.compilerOptions("java", compilerArgs);
} else if (compilerPluginConfiguration.isPresent()) {
final Xpp3Dom compilerPluginArgsConfiguration = compilerPluginConfiguration.get().getChild("compilerArgs");
if (compilerPluginArgsConfiguration != null) {
Expand All @@ -974,9 +987,10 @@ private QuarkusDevModeLauncher newLauncher() throws Exception {
&& !compilerPluginArgsConfiguration.getValue().isEmpty()) {
compilerPluginArgs.add(compilerPluginArgsConfiguration.getValue().trim());
}
builder.compilerOptions(compilerPluginArgs);
builder.compilerOptions("java", compilerPluginArgs);
}
}

if (release != null) {
builder.releaseJavaVersion(release);
} else if (compilerPluginConfiguration.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.maven.components;

import java.util.ArrayList;
import java.util.List;

public class CompilerOptions {

private String name = null;
private List<String> args = new ArrayList<>();

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setArgs(List<String> options) {
this.args = options;
}

public List<String> getArgs() {
return args;
}

}
Loading

0 comments on commit 035447b

Please sign in to comment.