From 59ca3502c801f84484b4775023d076f62e529628 Mon Sep 17 00:00:00 2001 From: Jannis Weis <31143295+weisJ@users.noreply.github.com> Date: Sun, 9 Jan 2022 16:42:24 +0100 Subject: [PATCH] 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. --- .../main/kotlin/ModuleInfoCompilePlugin.kt | 65 ++++++++++++++----- compatibility/build.gradle.kts | 1 + macos/build.gradle.kts | 6 +- windows/build.gradle.kts | 4 ++ 4 files changed, 58 insertions(+), 18 deletions(-) diff --git a/buildSrc/src/main/kotlin/ModuleInfoCompilePlugin.kt b/buildSrc/src/main/kotlin/ModuleInfoCompilePlugin.kt index 9ce93295..eeeef553 100644 --- a/buildSrc/src/main/kotlin/ModuleInfoCompilePlugin.kt +++ b/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 = mutableListOf(), - var addReads : MutableList = mutableListOf(), - var addOpens : MutableList = mutableListOf(), - var patchJUnit : Boolean = true, + var addExports: MutableList = mutableListOf(), + var addReads: MutableList = mutableListOf(), + var addOpens: MutableList = mutableListOf(), + var patchJUnit: Boolean = true, ) { - internal val testPackagesOpens : MutableList = mutableListOf() + internal val testPackagesOpens: MutableList = 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() + + fun stubModule(moduleName: String) = stubbedModules.add(moduleName) + fun modularExec(action: ExecParameters.() -> Unit) = execParameters.action() } class ModuleInfoCompilePlugin : Plugin { 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 { } } - 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("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 { } } - private fun SourceDirectorySet.folders() : List = + private fun SourceDirectorySet.folders(): List = 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 { } } - 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("test").get() - private val SourceSetContainer.main : SourceSet + private val SourceSetContainer.main: SourceSet get() = named("main").get() } diff --git a/compatibility/build.gradle.kts b/compatibility/build.gradle.kts index aed72a66..20c62e1c 100644 --- a/compatibility/build.gradle.kts +++ b/compatibility/build.gradle.kts @@ -12,6 +12,7 @@ configure { extraArgs = listOf( "--add-exports", "java.desktop/sun.awt=darklaf.compatibility" ) + stubModule("darklaf.core") } dependencies { diff --git a/macos/build.gradle.kts b/macos/build.gradle.kts index 01799aa8..4437e4b3 100644 --- a/macos/build.gradle.kts +++ b/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") diff --git a/windows/build.gradle.kts b/windows/build.gradle.kts index afb90d25..6bc0868e 100644 --- a/windows/build.gradle.kts +++ b/windows/build.gradle.kts @@ -7,6 +7,10 @@ plugins { `use-prebuilt-binaries` } +moduleInfo { + stubModule("darklaf.core") +} + library { dependencies { jvmImplementation(projects.darklafNativeUtils)