From c4706cb47797cc7822212d0519e786f4a630234a Mon Sep 17 00:00:00 2001 From: Christian Melchior Date: Mon, 15 Jan 2024 12:38:36 +0100 Subject: [PATCH] Make it configurable which native libs to copy when building (instead of always copying all of them) --- Jenkinsfile | 4 +- packages/cinterop/build.gradle.kts | 61 +++++++++++++++++++----------- packages/gradle.properties | 18 +++++---- 3 files changed, 51 insertions(+), 32 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 11cab8a119..2add8ccda0 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -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([ diff --git a/packages/cinterop/build.gradle.kts b/packages/cinterop/build.gradle.kts index ddde50796e..56a2d2154d 100644 --- a/packages/cinterop/build.gradle.kts +++ b/packages/cinterop/build.gradle.kts @@ -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 @@ -413,31 +414,45 @@ val buildJVMSharedLibs: TaskProvider by tasks.registering { * mostly useful on CI. */ val copyJVMSharedLibs: TaskProvider 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") + } + } } } diff --git a/packages/gradle.properties b/packages/gradle.properties index 4ba41d1156..dbcb870063 100644 --- a/packages/gradle.properties +++ b/packages/gradle.properties @@ -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: /packages/cinterop/build/realmMacOsBuild/librealmc.dylib -# - Linux: /packages/cinterop/build/realmWindowsBuild/librealmc.so -# - Windows: /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: /packages/cinterop/build/realmMacOsBuild/librealmc.dylib +# - linux: /packages/cinterop/build/realmWindowsBuild/librealmc.so +# - windows: /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