Fix the use case of an old-style project depending on an MPP lib

* Setup the attributes for the resolvable and output configurations
This commit is contained in:
Sergey Igushkin
2018-08-02 03:10:31 +03:00
parent 3ff61f59a7
commit 3f60798848
9 changed files with 102 additions and 10 deletions
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.gradle
import org.jetbrains.kotlin.gradle.util.checkBytecodeContains
import org.jetbrains.kotlin.gradle.util.modify
import org.junit.Assert
import org.junit.Test
@@ -20,6 +21,7 @@ class NewMultiplatformIT : BaseGradleIT() {
fun testLibAndApp() {
val libProject = Project("sample-lib", gradleVersion, "new-mpp-lib-and-app")
val appProject = Project("sample-app", gradleVersion, "new-mpp-lib-and-app")
val oldStyleAppProject = Project("sample-old-style-app", gradleVersion, "new-mpp-lib-and-app")
with(libProject) {
build("publish") {
@@ -86,6 +88,22 @@ class NewMultiplatformIT : BaseGradleIT() {
checkAppBuild()
}
}
with(oldStyleAppProject) {
setupWorkingDir()
gradleBuildScript().appendText("\nallprojects { repositories { maven { url '$libLocalRepoUri' } } }")
build("assemble") {
assertSuccessful()
assertTasksExecuted(":app-js:compileKotlin2Js", ":app-jvm:compileKotlin")
val jvmClassFile = projectDir.resolve(kotlinClassesDir("app-jvm") + "com/example/app/JvmAppKt.class")
checkBytecodeContains(jvmClassFile, "CommonKt.id", "MainKt.expectedFun")
val jsCompiledFilePath = kotlinClassesDir("app-js") + "app-js.js"
assertFileContains(jsCompiledFilePath, "lib.expectedFun", "lib.id")
}
}
}
@Test
@@ -0,0 +1,5 @@
apply plugin: 'kotlin2js'
dependencies {
compile 'com.example:sample-lib:1.0'
}
@@ -0,0 +1,10 @@
package com.example.app
import com.example.lib.id
import com.example.lib.expectedFun
fun idUsage(x: Int): Int = id(x)
fun main(args: Array<String>) {
expectedFun()
}
@@ -0,0 +1,5 @@
apply plugin: 'kotlin'
dependencies {
compile 'com.example:sample-lib:1.0'
}
@@ -0,0 +1,10 @@
package com.example.app
import com.example.lib.id
import com.example.lib.expectedFun
fun idUsage(x: Int): Int = id(x)
fun main(args: Array<String>) {
expectedFun()
}
@@ -0,0 +1,18 @@
buildscript {
repositories {
mavenLocal()
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
mavenLocal()
jcenter()
// NB: Build of this module depends on 'sample-lib' publication. You need to add the local
// repository where 'sample-lib' artifacts are published.
}
}
@@ -0,0 +1,3 @@
enableFeaturePreview('GRADLE_METADATA')
include 'app-jvm', 'app-js'
@@ -346,11 +346,14 @@ internal abstract class AbstractKotlinPlugin(
override fun apply(project: Project) {
project.plugins.apply(JavaPlugin::class.java)
val target = (project.kotlinExtension as KotlinSingleJavaTargetExtension).target
configureTarget(
(project.kotlinExtension as KotlinSingleJavaTargetExtension).target,
target,
{ compilation -> buildSourceSetProcessor(project, compilation, kotlinPluginVersion) }
)
configureAttributes(target)
configureProjectGlobalSettings(project, kotlinPluginVersion)
registry.register(KotlinModelBuilder(kotlinPluginVersion))
}
@@ -422,7 +425,24 @@ internal abstract class AbstractKotlinPlugin(
}
}
fun configureSourceSetDefaults(
private fun configureAttributes(
kotlinTarget: KotlinWithJavaTarget
) {
val project = kotlinTarget.project
// Don't set the attributes for common module; otherwise their 'common' platform won't be compatible with the one in
// platform-specific modules
if (kotlinTarget.platformType != KotlinPlatformType.common) {
project.dependencies.attributesSchema.attribute(KotlinPlatformType.attribute)
project.configurations.getByName(kotlinTarget.apiElementsConfigurationName).usesPlatformOf(kotlinTarget)
project.configurations.getByName(kotlinTarget.runtimeElementsConfigurationName).usesPlatformOf(kotlinTarget)
kotlinTarget.compilations.all { compilation ->
KotlinTargetConfigurator.defineConfigurationsForCompilation(compilation, kotlinTarget, project.configurations)
}
}
}
private fun configureSourceSetDefaults(
kotlinTarget: KotlinTarget,
buildSourceSetProcessor: (KotlinCompilation) -> KotlinSourceSetProcessor<*>
) {
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinOnlyTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.disambiguateName
import org.jetbrains.kotlin.gradle.plugin.mpp.defaultSourceSetName
import org.jetbrains.kotlin.gradle.plugin.sources.getSourceSetHierarchy
import org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile
import org.jetbrains.kotlin.gradle.utils.isGradleVersionAtLeast
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
@@ -56,6 +57,14 @@ open class KotlinTargetConfigurator(
val project = platformTarget.project
val main = platformTarget.compilations.create(KotlinCompilation.MAIN_COMPILATION_NAME)
platformTarget.compilations.all {
buildOutputCleanupRegistry.registerOutputs(it.output)
it.compileDependencyFiles = project.configurations.maybeCreate(it.compileDependencyConfigurationName)
if (it is KotlinCompilationToRunnableFiles) {
it.runtimeDependencyFiles = project.configurations.maybeCreate(it.runtimeDependencyConfigurationName)
}
}
platformTarget.compilations.create(KotlinCompilation.TEST_COMPILATION_NAME).apply {
compileDependencyFiles = project.files(main.output, project.configurations.maybeCreate(compileDependencyConfigurationName))
@@ -64,9 +73,6 @@ open class KotlinTargetConfigurator(
}
}
platformTarget.compilations.all {
buildOutputCleanupRegistry.registerOutputs(it.output)
}
}
private fun <KotlinCompilationType: KotlinCompilation> configureCompilationDefaults(target: KotlinOnlyTarget<KotlinCompilationType>) {
@@ -80,7 +86,8 @@ open class KotlinTargetConfigurator(
}
if (compilation is KotlinCompilationWithResources) {
configureResourceProcessing(compilation, project.files())
val sourceSetHierarchy = compilation.kotlinSourceSets.flatMap { it.getSourceSetHierarchy() }.distinct()
configureResourceProcessing(compilation, project.files(Callable { sourceSetHierarchy.map { it.resources } }))
}
createLifecycleTask(compilation)
@@ -336,8 +343,6 @@ open class KotlinTargetConfigurator(
description = "Compile classpath for $compilation."
}
compilation.compileDependencyFiles = compileClasspathConfiguration
if (compilation is KotlinCompilationToRunnableFiles) {
val runtimeConfiguration = configurations.maybeCreate(compilation.deprecatedRuntimeConfigurationName).apply {
extendsFrom(compileConfiguration)
@@ -366,8 +371,6 @@ open class KotlinTargetConfigurator(
attributes.attribute(USAGE_ATTRIBUTE, compilation.target.project.usageByName(Usage.JAVA_RUNTIME))
description = "Runtime classpath of $compilation."
}
compilation.runtimeDependencyFiles = compilation.output.plus(runtimeClasspathConfiguration)
}
}