Fix embedding static libraries by cinterop
Fix 'staticLibraries' '.def' file option and '-staticLibrary' cinterop flag. #KT-39396 Fixed.
This commit is contained in:
committed by
GitHub
parent
7cad336013
commit
e279adaa14
+3
-1
@@ -33,7 +33,8 @@ fun createInteropLibrary(
|
|||||||
manifest: Properties,
|
manifest: Properties,
|
||||||
dependencies: List<KotlinLibrary>,
|
dependencies: List<KotlinLibrary>,
|
||||||
nopack: Boolean,
|
nopack: Boolean,
|
||||||
shortName: String?
|
shortName: String?,
|
||||||
|
staticLibraries: List<String>
|
||||||
) {
|
) {
|
||||||
val version = KotlinLibraryVersioning(
|
val version = KotlinLibraryVersioning(
|
||||||
libraryVersion = null,
|
libraryVersion = null,
|
||||||
@@ -58,6 +59,7 @@ fun createInteropLibrary(
|
|||||||
nativeBitcodeFiles.forEach(this::addNativeBitcode)
|
nativeBitcodeFiles.forEach(this::addNativeBitcode)
|
||||||
addManifestAddend(manifest)
|
addManifestAddend(manifest)
|
||||||
addLinkDependencies(dependencies)
|
addLinkDependencies(dependencies)
|
||||||
|
staticLibraries.forEach(this::addIncludedBinary)
|
||||||
commit()
|
commit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -377,7 +377,8 @@ private fun processCLib(flavorName: String, cinteropArguments: CInteropArguments
|
|||||||
manifest = def.manifestAddendProperties,
|
manifest = def.manifestAddendProperties,
|
||||||
dependencies = stdlibDependency + imports.requiredLibraries.toList(),
|
dependencies = stdlibDependency + imports.requiredLibraries.toList(),
|
||||||
nopack = cinteropArguments.nopack,
|
nopack = cinteropArguments.nopack,
|
||||||
shortName = cinteropArguments.shortModuleName
|
shortName = cinteropArguments.shortModuleName,
|
||||||
|
staticLibraries = staticLibraries
|
||||||
)
|
)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3506,6 +3506,28 @@ createInterop("auxiliaryCppSources") {
|
|||||||
it.extraOpts "-Xsource-compiler-option", "-std=c++17"
|
it.extraOpts "-Xsource-compiler-option", "-std=c++17"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createInterop("embedStaticLibraries") {
|
||||||
|
it.defFile 'interop/embedStaticLibraries/embedStaticLibraries.def'
|
||||||
|
it.headers "$projectDir/interop/embedStaticLibraries/embedStaticLibraries.h"
|
||||||
|
|
||||||
|
// Note: also hardcoded in def file.
|
||||||
|
final String libDir = "$buildDir/embedStaticLibraries/"
|
||||||
|
|
||||||
|
it.getByTarget(target.name).doFirst {
|
||||||
|
1.upto(4) {
|
||||||
|
UtilsKt.buildStaticLibrary(
|
||||||
|
project,
|
||||||
|
[file("$projectDir/interop/embedStaticLibraries/${it}.c")],
|
||||||
|
file("$libDir/${it}.a"),
|
||||||
|
file("$libDir/${it}.objs"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
it.extraOpts '-staticLibrary', "$libDir/1.a"
|
||||||
|
it.extraOpts '-staticLibrary', "$libDir/2.a"
|
||||||
|
}
|
||||||
|
|
||||||
if (PlatformInfo.isAppleTarget(project)) {
|
if (PlatformInfo.isAppleTarget(project)) {
|
||||||
createInterop("objcSmoke") {
|
createInterop("objcSmoke") {
|
||||||
it.defFile 'interop/objc/objcSmoke.def'
|
it.defFile 'interop/objc/objcSmoke.def'
|
||||||
@@ -3719,6 +3741,12 @@ interopTest("interop_auxiliarySources") {
|
|||||||
interop = 'auxiliaryCppSources'
|
interop = 'auxiliaryCppSources'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interopTest("interop_embedStaticLibraries") {
|
||||||
|
disabled = (project.testTarget == 'wasm32') // No interop for wasm yet.
|
||||||
|
source = "interop/embedStaticLibraries/main.kt"
|
||||||
|
interop = 'embedStaticLibraries'
|
||||||
|
}
|
||||||
|
|
||||||
interopTest("interop_values") {
|
interopTest("interop_values") {
|
||||||
disabled = (project.testTarget == 'wasm32') // No interop for wasm yet.
|
disabled = (project.testTarget == 'wasm32') // No interop for wasm yet.
|
||||||
source = "interop/basics/values.kt"
|
source = "interop/basics/values.kt"
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
int get1() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
int get2() {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
int get3() {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
int get4() {
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
staticLibraries = \
|
||||||
|
backend.native/tests/build/embedStaticLibraries/3.a \
|
||||||
|
backend.native/tests/build/embedStaticLibraries/4.a
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
int get1(void);
|
||||||
|
int get2(void);
|
||||||
|
int get3(void);
|
||||||
|
int get4(void);
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import kotlin.test.*
|
||||||
|
import embedStaticLibraries.*
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
assertEquals(1, get1())
|
||||||
|
assertEquals(2, get2())
|
||||||
|
assertEquals(3, get3())
|
||||||
|
assertEquals(4, get4())
|
||||||
|
}
|
||||||
@@ -319,4 +319,30 @@ fun Project.mergeManifestsByTargets(source: File, destination: File) {
|
|||||||
destinationProperties[KLIB_PROPERTY_NATIVE_TARGETS] = mergedNativeTargets.joinToString(" ")
|
destinationProperties[KLIB_PROPERTY_NATIVE_TARGETS] = mergedNativeTargets.joinToString(" ")
|
||||||
|
|
||||||
destinationFile.saveProperties(destinationProperties)
|
destinationFile.saveProperties(destinationProperties)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Project.buildStaticLibrary(cSources: Collection<File>, output: File, objDir: File) {
|
||||||
|
delete(objDir)
|
||||||
|
delete(output)
|
||||||
|
|
||||||
|
val platform = platformManager.platform(testTarget)
|
||||||
|
|
||||||
|
objDir.mkdirs()
|
||||||
|
exec {
|
||||||
|
it.commandLine(platform.clang.clangC(
|
||||||
|
"-c",
|
||||||
|
*cSources.map { it.absolutePath }.toTypedArray()
|
||||||
|
))
|
||||||
|
it.workingDir(objDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
output.parentFile.mkdirs()
|
||||||
|
exec {
|
||||||
|
it.commandLine(
|
||||||
|
"${platform.configurables.absoluteLlvmHome}/bin/llvm-ar",
|
||||||
|
"-rc",
|
||||||
|
output,
|
||||||
|
*fileTree(objDir).files.toTypedArray()
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user