Skip to content

Commit

Permalink
Make it configurable which native libs to copy when building (instead…
Browse files Browse the repository at this point in the history
… of always copying all of them)
  • Loading branch information
Christian Melchior committed Jan 15, 2024
1 parent fcefa8a commit c4706cb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,11 @@ def genAndStashSwigJNI() {
stash includes: 'packages/jni-swig-stub/build/generated/sources/jni/realmc.cpp,packages/jni-swig-stub/build/generated/sources/jni/realmc.h', name: 'swig_jni'
}
def runBuild() {
def buildJvmAbiFlag = "-Prealm.kotlin.copyNativeJvmLibs=false"
def buildJvmAbiFlag = "-Prealm.kotlin.copyNativeJvmLibs="
if (shouldBuildJvmABIs()) {
unstash name: 'linux_so_file'
unstash name: 'win_dll'
buildJvmAbiFlag = "-Prealm.kotlin.copyNativeJvmLibs=true"
buildJvmAbiFlag = "-Prealm.kotlin.copyNativeJvmLibs=windows,linux" // Macos is built in-place
}

withCredentials([
Expand Down
61 changes: 38 additions & 23 deletions packages/cinterop/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import org.jetbrains.kotlin.konan.target.KonanTarget
import java.lang.IllegalArgumentException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
Expand Down Expand Up @@ -413,31 +414,45 @@ val buildJVMSharedLibs: TaskProvider<Task> by tasks.registering {
* mostly useful on CI.
*/
val copyJVMSharedLibs: TaskProvider<Task> by tasks.registering {
val copyJvmABIs = project.hasProperty("realm.kotlin.copyNativeJvmLibs") && project.property("realm.kotlin.copyNativeJvmLibs") == "true"

val copyJvmABIs = project.hasProperty("realm.kotlin.copyNativeJvmLibs")
&& (project.property("realm.kotlin.copyNativeJvmLibs") as String).isNotEmpty()
logger.info("Copy native Realm JVM libraries: $copyJvmABIs")
if (copyJvmABIs) {
// copy MacOS pre-built binaries
project.file("$buildDir/realmMacOsBuild/librealmc.dylib")
.copyTo(project.file("$jvmJniPath/macos/librealmc.dylib"), overwrite = true)
genHashFile(platform = "macos", prefix = "lib", suffix = ".dylib")

// copy Linux pre-built binaries
project.file("$buildDir/realmLinuxBuild/librealmc.so")
.copyTo(project.file("$jvmJniPath/linux/librealmc.so"), overwrite = true)
genHashFile(platform = "linux", prefix = "lib", suffix = ".so")

// copy Window pre-built binaries
project.file("$buildDir/realmWindowsBuild/Release/realmc.dll")
.copyTo(project.file("$jvmJniPath/windows/realmc.dll"), overwrite = true)
genHashFile(platform = "windows", prefix = "", suffix = ".dll")

// Register copied libraries as output
outputs.file(project.file("$jvmJniPath/macos/librealmc.dylib"))
outputs.file(project.file("$jvmJniPath/macos/dynamic_libraries.properties"))
outputs.file(project.file("$jvmJniPath/linux/librealmc.so"))
outputs.file(project.file("$jvmJniPath/linux/dynamic_libraries.properties"))
outputs.file(project.file("$jvmJniPath/windows/realmc.dll"))
outputs.file(project.file("$jvmJniPath/windows/dynamic_libraries.properties"))
val archs = (project.property("realm.kotlin.copyNativeJvmLibs") as String)
.split(",")
.map { it.trim() }
.map { it.toLowerCase() }

archs.forEach { arch ->
when(arch) {
"linux" -> {
// copy Linux pre-built binaries
project.file("$buildDir/realmLinuxBuild/librealmc.so")
.copyTo(project.file("$jvmJniPath/linux/librealmc.so"), overwrite = true)
genHashFile(platform = "linux", prefix = "lib", suffix = ".so")
outputs.file(project.file("$jvmJniPath/linux/librealmc.so"))
outputs.file(project.file("$jvmJniPath/linux/dynamic_libraries.properties"))
}
"macos" -> {
// copy MacOS pre-built binaries
project.file("$buildDir/realmMacOsBuild/librealmc.dylib")
.copyTo(project.file("$jvmJniPath/macos/librealmc.dylib"), overwrite = true)
genHashFile(platform = "macos", prefix = "lib", suffix = ".dylib")
outputs.file(project.file("$jvmJniPath/macos/librealmc.dylib"))
outputs.file(project.file("$jvmJniPath/macos/dynamic_libraries.properties"))
}
"windows" -> {
// copy Window pre-built binaries
project.file("$buildDir/realmWindowsBuild/Release/realmc.dll")
.copyTo(project.file("$jvmJniPath/windows/realmc.dll"), overwrite = true)
genHashFile(platform = "windows", prefix = "", suffix = ".dll")
outputs.file(project.file("$jvmJniPath/windows/realmc.dll"))
outputs.file(project.file("$jvmJniPath/windows/dynamic_libraries.properties"))
}
else -> throw IllegalArgumentException("Unsupported platfor for realm.kotlin.copyNativeJvmLibs: $arch")
}
}
}
}

Expand Down
18 changes: 11 additions & 7 deletions packages/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,18 @@ realm.kotlin.mainHost=true
# when only building docs or compiler/gradle plugins.
realm.kotlin.buildRealmCore=true

# Whether or not to copy pre-built JVM native files into place, making them
# ready to run or package the final JVM JARs.
# Comma-seperated list of pre-built JVM native files that should be copied into place, making them
# ready to run or package into the final JVM JARs.
#
# The prebuilt files must be placed in this location for this to work:
# - MacOS: <root>/packages/cinterop/build/realmMacOsBuild/librealmc.dylib
# - Linux: <root>/packages/cinterop/build/realmWindowsBuild/librealmc.so
# - Windows: <root>/packages/cinterop/build/realmWindowsBuild/Release/realmc.dll
realm.kotlin.copyNativeJvmLibs=false
# If the list is empty, no files are copied, and must instead be built locally.
#
# The following options are allowed and will copy the prebuilt file if it is placed in the defined location.
# If a platform is enabled, but the file doesn't exist, then the build will crash.
#
# - macos: <root>/packages/cinterop/build/realmMacOsBuild/librealmc.dylib
# - linux: <root>/packages/cinterop/build/realmWindowsBuild/librealmc.so
# - windows: <root>/packages/cinterop/build/realmWindowsBuild/Release/realmc.dll
realm.kotlin.copyNativeJvmLibs=

# See https://kotlinlang.org/docs/mpp-publish-lib.html#publish-an-android-library
# Allow the default dependency name to match the client debug build type. Otherwise the client project has to
Expand Down

0 comments on commit c4706cb

Please sign in to comment.