Skip to content

Commit

Permalink
Match Java homes canonically
Browse files Browse the repository at this point in the history
  • Loading branch information
alllex committed Feb 23, 2023
1 parent ba332d7 commit c11d14a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
Expand Up @@ -75,8 +75,6 @@
import java.util.List;
import java.util.concurrent.Callable;

import static com.google.common.base.Preconditions.checkState;

/**
* Compiles Java source files.
*
Expand Down Expand Up @@ -261,23 +259,23 @@ private void validateForkOptionsMatchToolchain() {

ForkOptions forkOptions = getOptions().getForkOptions();
File customJavaHome = forkOptions.getJavaHome();
checkState(
customJavaHome == null || customJavaHome.equals(toolchainJavaHome),
"Toolchain from `javaHome` property on `ForkOptions` does not match toolchain from `javaCompiler` property"
);
if (customJavaHome != null) {
JavaExecutableUtils.validateMatchingFiles(
customJavaHome, "Toolchain from `javaHome` property on `ForkOptions`",
toolchainJavaHome, "toolchain from `javaCompiler` property"
);
}

String customExecutablePath = forkOptions.getExecutable();
if (customExecutablePath == null) {
return;
if (customExecutablePath != null) {
// We do not match the custom executable against the compiler executable from the toolchain (javac),
// because the custom executable can be set to the path of another tool in the toolchain such as a launcher (java).
File customExecutableJavaHome = JavaExecutableUtils.resolveJavaHomeOfExecutable(customExecutablePath);
JavaExecutableUtils.validateMatchingFiles(
customExecutableJavaHome, "Toolchain from `executable` property on `ForkOptions`",
toolchainJavaHome, "toolchain from `javaCompiler` property"
);
}

// We do not match the custom executable against the compiler executable from the toolchain (javac),
// because the custom executable can be set to the path of another tool in the toolchain such as a launcher (java).
File customExecutableJavaHome = JavaExecutableUtils.resolveJavaHomeOfExecutable(customExecutablePath);
checkState(
customExecutableJavaHome.equals(toolchainJavaHome),
"Toolchain from `executable` property on `ForkOptions` does not match toolchain from `javaCompiler` property"
);
}

private boolean isToolchainCompatibleWithJava8() {
Expand Down
Expand Up @@ -61,17 +61,21 @@ public static void validateExecutable(@Nullable String executable, String execut
}

File executableFile = resolveExecutable(executable);
if (executableFile.equals(referenceFile)) {
validateMatchingFiles(executableFile, executableDescription, referenceFile, referenceDescription);
}

public static void validateMatchingFiles(File customFile, String customDescription, File referenceFile, String referenceDescription) {
if (customFile.equals(referenceFile)) {
return;
}

File canonicalExecutableFile = canonicalFile(executableFile);
File canonicalCustomFile = canonicalFile(customFile);
File canonicalReferenceFile = canonicalFile(referenceFile);
if (canonicalExecutableFile.equals(canonicalReferenceFile)) {
if (canonicalCustomFile.equals(canonicalReferenceFile)) {
return;
}

throw new IllegalStateException(executableDescription + " does not match " + referenceDescription + ".");
throw new IllegalStateException(customDescription + " does not match " + referenceDescription + ".");
}

private static File canonicalFile(File file) {
Expand Down

0 comments on commit c11d14a

Please sign in to comment.