1/* 2 * Copyright 2014-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5project.ext.registerInteropAsSourceSetOutput = { interop, sourceSet -> 6 afterEvaluate { 7 def cinteropTask = tasks.named(interop.interopProcessingTaskName) 8 def cinteropKlib = cinteropTask.map { it.outputFile } 9 def fakeCinteropCompilation = kotlin.targets["metadata"].compilations[sourceSet.name] 10 def destination = fakeCinteropCompilation.compileKotlinTask.destinationDir 11 12 def tempDir = "$buildDir/tmp/${sourceSet.name}UnpackedInteropKlib" 13 14 def prepareKlibTaskProvider = tasks.register("prepare${sourceSet.name.capitalize()}InteropKlib", Sync) { 15 from(files(zipTree(cinteropKlib).matching { 16 exclude("targets/**", "default/targets/**") 17 }).builtBy(cinteropTask)) 18 19 into(tempDir) 20 21 doLast { 22 def manifest140 = file("$tempDir/default/manifest") 23 def manifest1371 = file("$tempDir/manifest") 24 def manifest = manifest140.exists() ? manifest140 : manifest1371 25 26 def lines = manifest.readLines() 27 def modifiedLines = lines.collect { line -> 28 line.startsWith("depends=") ? "depends=stdlib ${manifest == manifest140 ? 'org.jetbrains.kotlin.native.platform.posix' : 'posix'}" : 29 line.startsWith("native_targets=") ? "native_targets=" : 30 line 31 } 32 manifest.text = modifiedLines.join("\n") 33 } 34 } 35 36 def copyCinteropTaskProvider = tasks.register("copy${sourceSet.name.capitalize()}CinteropKlib", Zip) { 37 from(fileTree(tempDir).builtBy(prepareKlibTaskProvider)) 38 destinationDirectory.set(destination) 39 archiveFileName.set("${project.name}_${fakeCinteropCompilation.name}.klib") 40 dependsOn cinteropTask 41 } 42 43 fakeCinteropCompilation.output.classesDirs.from(files().builtBy(copyCinteropTaskProvider)) 44 45 kotlin.sourceSets.matching { 46 def visited = new HashSet() 47 def visit 48 visit = { s -> if (visited.add(s)) s.dependsOn.each { visit(it) } } 49 visit(it) 50 sourceSet in visited 51 }.all { 52 project.dependencies.add(implementationMetadataConfigurationName, files(cinteropKlib)) 53 } 54 } 55} 56