-
Notifications
You must be signed in to change notification settings - Fork 2
/
ECJPlugin.kt
100 lines (80 loc) · 4.54 KB
/
ECJPlugin.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright (c) 2022-2023 Leon Linhart
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.github.themrmilchmann.gradle.ecj.plugins
import io.github.themrmilchmann.gradle.ecj.ECJExtension
import io.github.themrmilchmann.gradle.ecj.internal.utils.*
import org.gradle.api.*
import org.gradle.api.plugins.*
import org.gradle.api.tasks.compile.*
import org.gradle.jvm.toolchain.*
import org.gradle.process.CommandLineArgumentProvider
public class ECJPlugin : Plugin<Project> {
internal companion object {
const val ECJ_CONFIGURATION_NAME = "ecj"
const val DEFAULT_DEPENDENCY_GROUP = "org.eclipse.jdt"
const val DEFAULT_DEPENDENCY_ARTIFACT = "ecj"
const val DEFAULT_DEPENDENCY_VERSION = "3.32.0"
const val MAIN = "org.eclipse.jdt.internal.compiler.batch.Main"
/* The version for which a toolchain is requested if the project's toolchain is not compatible. */
const val PREFERRED_JAVA_VERSION = 17
/* The version required to run ECJ. */
const val REQUIRED_JAVA_VERSION = 11
}
override fun apply(target: Project): Unit = applyTo(target) project@{
/*
* Make sure that the JavaPlugin is applied before this plugin, since we have to override some properties.
* (Configuration happens in the same order in which the configuration actions are added.)
*/
pluginManager.apply(JavaPlugin::class.java)
val ecjExtension = extensions.create("ecj", ECJExtension::class.java)
val ecjConfiguration = configurations.create(ECJ_CONFIGURATION_NAME) {
defaultDependencies {
val compilerGroupId = ecjExtension.compilerGroupId.orNull ?: error("ECJ compilerGroupId may not be null")
val compilerArtifactId = ecjExtension.compilerArtifactId.orNull ?: error("ECJ compilerArtifactId may not be null")
val compilerVersion = ecjExtension.compilerVersion.orNull ?: error("ECJ compilerVersion may not be null")
dependencies.add(target.dependencies.create("$compilerGroupId:$compilerArtifactId:$compilerVersion"))
}
}
val java = extensions.getByType(JavaPluginExtension::class.java)
val javaToolchains = extensions.getByType(JavaToolchainService::class.java)
tasks.withType(JavaCompile::class.java) {
/* Overwrite the javaCompiler to make sure that it is not inferred from the toolchain. */
javaCompiler.set(null as JavaCompiler?)
/* ECJ does not support generating JNI headers. Make sure the property is not used. */
options.headerOutputDirectory.set(this@project.provider { null })
afterEvaluate {
val javaLauncher = if (java.toolchain.languageVersion.orNull?.canCompileOrRun(REQUIRED_JAVA_VERSION) == true) {
javaToolchains.launcherFor(java.toolchain).orNull ?: error("Could not get launcher for toolchain: ${java.toolchain}")
} else {
javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(PREFERRED_JAVA_VERSION))
}.orNull ?: error("Could not provision launcher for Java $PREFERRED_JAVA_VERSION")
}
options.isFork = true
options.forkOptions.executable = javaLauncher.executablePath.asFile.absolutePath
options.forkOptions.jvmArgumentProviders.add(CommandLineArgumentProvider {
mutableListOf("-cp", ecjConfiguration.asPath, MAIN)
})
}
}
}
}