From 88e86cbdd46732738141ffeba57ad71183cb53c4 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Wed, 12 Sep 2018 17:23:00 +0300 Subject: [PATCH] [gradle-plugin] Support custom entry points (#1996) --- GRADLE_PLUGIN.md | 3 ++ .../experimental/KotlinNativeComponent.kt | 4 +++ .../internal/AbstractKotlinNativeComponent.kt | 3 ++ .../experimental/tasks/KotlinNativeCompile.kt | 6 ++++ .../test/kotlin/ExperimentalPluginTests.kt | 35 ++++++++++++++++++- 5 files changed, 50 insertions(+), 1 deletion(-) diff --git a/GRADLE_PLUGIN.md b/GRADLE_PLUGIN.md index 7763b7427e9..637033631db 100644 --- a/GRADLE_PLUGIN.md +++ b/GRADLE_PLUGIN.md @@ -914,6 +914,9 @@ components.main { // Set up output kinds outputKinds = [EXECUTABLE, KLIBRARY, FRAMEWORK, DYNAMIC, STATIC] + + // Specify custom entry point for executables + entryPoint = "org.test.myMain" // Target-specific options target('linux_x64') { diff --git a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/experimental/KotlinNativeComponent.kt b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/experimental/KotlinNativeComponent.kt index 930ae306cad..c1bd19922a4 100644 --- a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/experimental/KotlinNativeComponent.kt +++ b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/experimental/KotlinNativeComponent.kt @@ -106,6 +106,10 @@ interface KotlinNativeComponent: ComponentWithBinaries, ComponentWithDependencie val publishJavadoc: Boolean val publishSources: Boolean + + /** Allows setting custom entry point for executables */ + var entryPoint: String? + fun entryPoint(value: String) // endregion } diff --git a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/experimental/internal/AbstractKotlinNativeComponent.kt b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/experimental/internal/AbstractKotlinNativeComponent.kt index b6f6f291cff..fc83c894919 100644 --- a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/experimental/internal/AbstractKotlinNativeComponent.kt +++ b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/experimental/internal/AbstractKotlinNativeComponent.kt @@ -155,5 +155,8 @@ abstract class AbstractKotlinNativeComponent @Inject constructor( action.execute(dependencies) } + override var entryPoint: String? = null + override fun entryPoint(value: String) { entryPoint = value } + // endregion } \ No newline at end of file diff --git a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/experimental/tasks/KotlinNativeCompile.kt b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/experimental/tasks/KotlinNativeCompile.kt index f794a277b7b..bd893586ae1 100644 --- a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/experimental/tasks/KotlinNativeCompile.kt +++ b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/experimental/tasks/KotlinNativeCompile.kt @@ -23,6 +23,7 @@ import org.gradle.api.file.FileSystemLocation import org.gradle.api.provider.Provider import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFiles +import org.gradle.api.tasks.Optional import org.gradle.api.tasks.TaskAction import org.jetbrains.kotlin.gradle.plugin.* import org.jetbrains.kotlin.gradle.plugin.experimental.internal.AbstractKotlinNativeBinary @@ -59,6 +60,9 @@ open class KotlinNativeCompile @Inject constructor(internal val binary: Abstract val linkerOpts: List @Input get() = binary.linkerOpts + val entryPoint: String? + @Optional @Input get() = binary.component.entryPoint + val outputFile: File get() = outputLocationProvider.get().asFile @@ -113,6 +117,8 @@ open class KotlinNativeCompile @Inject constructor(internal val binary: Abstract add("-Xmulti-platform") + addArgIfNotNull("-entry", entryPoint) + addAll(additionalCompilerOptions) libraries.files.forEach {library -> diff --git a/tools/kotlin-native-gradle-plugin/src/test/kotlin/ExperimentalPluginTests.kt b/tools/kotlin-native-gradle-plugin/src/test/kotlin/ExperimentalPluginTests.kt index 83b8a96ba2f..6dd4a39f457 100644 --- a/tools/kotlin-native-gradle-plugin/src/test/kotlin/ExperimentalPluginTests.kt +++ b/tools/kotlin-native-gradle-plugin/src/test/kotlin/ExperimentalPluginTests.kt @@ -730,6 +730,39 @@ class ExperimentalPluginTests { } } + @Test + fun `Plugin should support custom entry points`() { + val hostSuffix = CompilerOutputKind.PROGRAM.suffix(HostManager.host) + val exePath = "build/exe/main/release/entry-point$hostSuffix" + val project = KonanProject.createEmpty(projectDirectory).apply { + buildFile.writeText(""" + plugins { id 'kotlin-native' } + + components.main { + outputKinds = [EXECUTABLE] + entryPoint = 'org.test.myMain' + } + + task run(type: Exec) { + commandLine file('$exePath').absolutePath + } + """.trimIndent()) + settingsFile.appendText("rootProject.name = 'entry-point'") + + generateSrcFile("main.kt", """ + package org.test + + fun myMain(args: Array) { + println("myMain called!") + } + """.trimIndent()) + } + project.createRunner().withArguments(":assemble").build() + assertFileExists(exePath) + val result = project.createRunner().withArguments(":run").build() + assertTrue(result.output.contains("myMain called!")) + } + @Test fun `Plugin should support the konan tooling model`() { withProject { @@ -793,7 +826,7 @@ class ExperimentalPluginTests { val outputRoot = if (kind == OutputKind.EXECUTABLE) "exe" else "lib" val outputFile = file( "build/$outputRoot/main/$buildType/${kind.name.toLowerCase()}/${target.name}/" + - "testProject${kind.compilerOutputKind.suffix(target)}" + "testProject${kind.compilerOutputKind.suffix(target)}" ) assertEquals(outputFile, artifact.file) }