2
0
Fork 0
mirror of https://github.com/ethauvin/bld.git synced 2025-04-25 00:07:12 -07:00

Added support for automatically consolidating compile options that are named differently but serve the same purpose

This commit is contained in:
Geert Bevin 2024-10-14 09:07:24 -04:00
parent 3e9b252c51
commit c6c9d32c35
2 changed files with 37 additions and 4 deletions

View file

@ -23,6 +23,13 @@ import java.util.List;
* @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<>();
@ -124,19 +131,33 @@ public class CompileOperation extends AbstractOperation<CompileOperation> {
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);
@ -146,6 +167,16 @@ public class CompileOperation extends AbstractOperation<CompileOperation> {
}
}
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.
*

View file

@ -14,6 +14,8 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static rife.bld.operations.CompileOperation.COMPILE_OPTION_MODULE_PATH;
/**
* Options for the standard javac tool.
*
@ -410,7 +412,7 @@ public class JavacOptions extends ArrayList<String> {
* @since 2.1
*/
public JavacOptions modulePathStrings(List<String> paths) {
add("--module-path");
add(COMPILE_OPTION_MODULE_PATH);
add(FileUtils.joinPaths(paths));
return this;
}