Browse Source

Build: Stub invisible modules to avoid compiler warnings

Some modules explicitly export only to darklaf.core, because
they are implementation modules. To avoid compiler warnings
we stub the module definition.
pull/307/head
Jannis Weis 3 years ago
parent
commit
59ca3502c8
No known key found for this signature in database
GPG Key ID: 7C9D8D4B558049AB
  1. 65
      buildSrc/src/main/kotlin/ModuleInfoCompilePlugin.kt
  2. 1
      compatibility/build.gradle.kts
  3. 6
      macos/build.gradle.kts
  4. 4
      windows/build.gradle.kts

65
buildSrc/src/main/kotlin/ModuleInfoCompilePlugin.kt

@ -14,17 +14,19 @@ import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.registering
import org.gradle.kotlin.dsl.withType
import org.gradle.process.JavaForkOptions
import java.io.File
import java.nio.file.Files
import java.util.regex.Pattern
class ExecParameters(
var addExports : MutableList<String> = mutableListOf(),
var addReads : MutableList<String> = mutableListOf(),
var addOpens : MutableList<String> = mutableListOf(),
var patchJUnit : Boolean = true,
var addExports: MutableList<String> = mutableListOf(),
var addReads: MutableList<String> = mutableListOf(),
var addOpens: MutableList<String> = mutableListOf(),
var patchJUnit: Boolean = true,
) {
internal val testPackagesOpens : MutableList<String> = mutableListOf()
internal val testPackagesOpens: MutableList<String> = mutableListOf()
fun openTestPackagesTo(vararg modules : String) {
fun openTestPackagesTo(vararg modules: String) {
testPackagesOpens.addAll(modules)
}
}
@ -35,19 +37,23 @@ open class ModuleInfoExtension {
lateinit var moduleName: String
val execParameters = ExecParameters()
fun modularExec(action : ExecParameters.() -> Unit) = execParameters.action()
internal val stubbedModules = mutableListOf<String>()
fun stubModule(moduleName: String) = stubbedModules.add(moduleName)
fun modularExec(action: ExecParameters.() -> Unit) = execParameters.action()
}
class ModuleInfoCompilePlugin : Plugin<Project> {
companion object {
private val MODULE_PATTERN : Pattern = Pattern.compile("""module\s+([^\s]+)\s*\{""")
private val MODULE_PATTERN: Pattern = Pattern.compile("""module\s+([^\s]+)\s*\{""")
}
override fun apply(target: Project) = target.run {
val infoExtension = target.extensions.create("moduleInfo", ModuleInfoExtension::class.java)
if (!JavaVersion.current().isJava9Compatible
|| project.findProperty("skipModuleInfo") in listOf("", "true")) return@run
|| project.findProperty("skipModuleInfo") in listOf("", "true")
) return@run
val moduleInfoFile = file("src/main/module/module-info.java")
if (moduleInfoFile.exists()) {
@ -72,17 +78,44 @@ class ModuleInfoCompilePlugin : Plugin<Project> {
}
}
private fun Project.setupModuleInfoCompilation(infoExtension : ModuleInfoExtension) {
private fun Project.setupModuleInfoCompilation(infoExtension: ModuleInfoExtension) {
val stubOutputDir = buildDir.resolve("generated/moduleInfoStubs")
fun String.stubModuleInfoPath() = stubOutputDir.resolve("$this/module-info.java")
val createModuleStubs by tasks.registering(JavaCompile::class) {
stubOutputDir.deleteRecursively()
Files.createDirectories(stubOutputDir.toPath())
infoExtension.stubbedModules.forEach {
source(it.stubModuleInfoPath().also { file ->
file.parentFile.mkdirs()
file.writeText("module $it {}")
})
}
classpath = files()
options.compilerArgs.addAll(listOf(
"--release", infoExtension.version.majorVersion,
"--module-source-path", stubOutputDir.absolutePath
))
destinationDirectory.set(buildDir.resolve("classes/moduleStubs"))
}
val compileJava = tasks.named<JavaCompile>("compileJava")
val compileModuleInfoJava by tasks.registering(JavaCompile::class) {
dependsOn(createModuleStubs)
val javaCompile = compileJava.get()
classpath = files()
source("src/main/module/module-info.java")
source(javaCompile.source)
destinationDirectory.set(buildDir.resolve("classes/module"))
check(infoExtension.version.isJava9Compatible)
options.compilerArgs.addAll(listOf("--module-path", javaCompile.classpath.asPath))
val separator = "${File.pathSeparatorChar}"
val modulePath = javaCompile.classpath.asPath + separator + infoExtension.stubbedModules
.joinToString(separator) { "${stubOutputDir.absolutePath}/$it/$it.jar" }
options.compilerArgs.addAll(listOf("--module-path", modulePath))
if (infoExtension.extraArgs.isNotEmpty()) {
options.compilerArgs.addAll(infoExtension.extraArgs)
sourceCompatibility = infoExtension.version.majorVersion
@ -108,12 +141,12 @@ class ModuleInfoCompilePlugin : Plugin<Project> {
}
}
private fun SourceDirectorySet.folders() : List<String> =
private fun SourceDirectorySet.folders(): List<String> =
this.asSequence().map { it.parentFile }.toSet().asSequence().map {
it.relativeTo(sourceDirectories.singleFile).toPath().joinToString(separator = ".")
}.filter { it.isNotEmpty() }.toList()
private fun JavaForkOptions.patchTestExecParams(project : Project, infoExtension: ModuleInfoExtension) {
private fun JavaForkOptions.patchTestExecParams(project: Project, infoExtension: ModuleInfoExtension) {
val sourceSets = project.sourceSets
val testSourceSet = sourceSets.test
val mainSourceSet = sourceSets.main
@ -154,12 +187,12 @@ class ModuleInfoCompilePlugin : Plugin<Project> {
}
}
private val Project.sourceSets : SourceSetContainer
private val Project.sourceSets: SourceSetContainer
get() = extensions.getByName("sourceSets") as SourceSetContainer
private val SourceSetContainer.test : SourceSet
private val SourceSetContainer.test: SourceSet
get() = named<SourceSet>("test").get()
private val SourceSetContainer.main : SourceSet
private val SourceSetContainer.main: SourceSet
get() = named<SourceSet>("main").get()
}

1
compatibility/build.gradle.kts

@ -12,6 +12,7 @@ configure<ModuleInfoExtension> {
extraArgs = listOf(
"--add-exports", "java.desktop/sun.awt=darklaf.compatibility"
)
stubModule("darklaf.core")
}
dependencies {

6
macos/build.gradle.kts

@ -1,5 +1,3 @@
plugins {
java
`module-info-compile`
@ -10,6 +8,10 @@ plugins {
`apple-m1-toolchain`
}
moduleInfo {
stubModule("darklaf.core")
}
val jnfConfig: Configuration by configurations.creating {
attributes {
attribute(Attribute.of("dev.nokee.architecture", String::class.java), "arm64")

4
windows/build.gradle.kts

@ -7,6 +7,10 @@ plugins {
`use-prebuilt-binaries`
}
moduleInfo {
stubModule("darklaf.core")
}
library {
dependencies {
jvmImplementation(projects.darklafNativeUtils)

Loading…
Cancel
Save