Skip to content

Commit

Permalink
Added support for automatically consolidating compile options that ar…
Browse files Browse the repository at this point in the history
…e named differently but serve the same purpose
  • Loading branch information
gbevin committed Oct 14, 2024
1 parent 3e9b252 commit c6c9d32
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
37 changes: 34 additions & 3 deletions src/main/java/rife/bld/operations/CompileOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
* @since 1.5
*/
public class CompileOperation extends AbstractOperation<CompileOperation> {
static final String COMPILE_OPTION_D = "-d";
static final String COMPILE_OPTION_CP = "-cp";
static final String COMPILE_OPTION_CLASS_PATH = "--class-path";
static final String COMPILE_OPTION_CLASSPATH = "--classpath";
static final String COMPILE_OPTION_P = "-p";
static final String COMPILE_OPTION_MODULE_PATH = "--module-path";

private File buildMainDirectory_;
private File buildTestDirectory_;
private final List<String> compileMainClasspath_ = new ArrayList<>();
Expand Down Expand Up @@ -124,19 +131,33 @@ protected void executeBuildSources(List<String> classpath, List<String> modulePa
try (var file_manager = compiler.getStandardFileManager(null, null, null)) {
var compilation_units = file_manager.getJavaFileObjectsFromFiles(sources);
var diagnostics = new DiagnosticCollector<JavaFileObject>();
var options = new ArrayList<>(List.of("-d", destination.getAbsolutePath()));
var options = new ArrayList<>(List.of(COMPILE_OPTION_D, destination.getAbsolutePath()));

if (!classpath.isEmpty()) {
options.addAll(List.of("-cp", FileUtils.joinPaths(classpath)));
var class_path = FileUtils.joinPaths(classpath);
class_path = removeAndAppendCompileOptionPath(class_path, COMPILE_OPTION_CP);
class_path = removeAndAppendCompileOptionPath(class_path, COMPILE_OPTION_CLASS_PATH);
class_path = removeAndAppendCompileOptionPath(class_path, COMPILE_OPTION_CLASSPATH);

options.addAll(List.of(COMPILE_OPTION_CP, class_path));
}

if (!modulePath.isEmpty()) {
options.addAll(List.of("-p", FileUtils.joinPaths(modulePath)));
var module_path = FileUtils.joinPaths(modulePath);
module_path = removeAndAppendCompileOptionPath(module_path, COMPILE_OPTION_P);
module_path = removeAndAppendCompileOptionPath(module_path, COMPILE_OPTION_MODULE_PATH);

options.addAll(List.of(COMPILE_OPTION_P, module_path));
}

options.addAll(compileOptions());

var compilation_task = compiler.getTask(null, file_manager, diagnostics, options, null, compilation_units);
if (!compilation_task.call()) {
diagnostics_.addAll(diagnostics.getDiagnostics());
executeProcessDiagnostics(diagnostics);
}

var module_info_class = new File(destination, "module-info.class");
if (module_info_class.exists() && moduleMainClass() != null) {
var orig_bytes = FileUtils.readBytes(module_info_class);
Expand All @@ -146,6 +167,16 @@ protected void executeBuildSources(List<String> classpath, List<String> modulePa
}
}

private String removeAndAppendCompileOptionPath(String basePath, String option) {
var index = compileOptions_.indexOf(option);
if (index != -1 && index + 1 < compileOptions_.size() - 1) {
compileOptions_.remove(index);
return basePath + File.pathSeparator + compileOptions_.remove(index);
}

return basePath;
}

/**
* Part of the {@link #execute} operation, processes the compilation diagnostics.
*
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/rife/bld/operations/JavacOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.Arrays;
import java.util.List;

import static rife.bld.operations.CompileOperation.COMPILE_OPTION_MODULE_PATH;

/**
* Options for the standard javac tool.
*
Expand Down Expand Up @@ -410,7 +412,7 @@ public JavacOptions modulePath(String... paths) {
* @since 2.1
*/
public JavacOptions modulePathStrings(List<String> paths) {
add("--module-path");
add(COMPILE_OPTION_MODULE_PATH);
add(FileUtils.joinPaths(paths));
return this;
}
Expand Down

0 comments on commit c6c9d32

Please sign in to comment.