Skip to content

Commit

Permalink
refactor: move uniffi generated code into a separate library
Browse files Browse the repository at this point in the history
By moving the code into its own library we can control the visibility by importing
it into our public facing library as an implementation dependency. This makes
our wrapper API the only visible API to the library consumer.
  • Loading branch information
typfel committed Nov 22, 2024
1 parent 2b86a54 commit 24383f7
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 80 deletions.
1 change: 1 addition & 0 deletions .github/workflows/publish-android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
- name: Publish package
run: |
cd crypto-ffi/bindings
./gradlew uniffi-android:publishAllPublicationsToMavenCentral --no-configuration-cache
./gradlew android:publishAllPublicationsToMavenCentral --no-configuration-cache
env:
ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.SONATYPE_PASSWORD }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/publish-jvm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ jobs:
- name: Publish package
run: |
cd crypto-ffi/bindings
./gradlew :uniffi-jvm:publishAllPublicationsToMavenCentral --no-configuration-cache
./gradlew :jvm:publishAllPublicationsToMavenCentral --no-configuration-cache
env:
ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.SONATYPE_PASSWORD }}
Expand Down
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ DerivedData/
.exrc

# Kotlin
crypto-ffi/bindings/jvm/src/main/kotlin/com/wire/crypto/CoreCrypto.kt
crypto-ffi/bindings/jvm/src/main/kotlin/uniffi/core_crypto/core_crypto.kt
crypto-ffi/bindings/android/src/main/kotlin/com/wire/crypto/CoreCrypto.kt
crypto-ffi/bindings/android/src/main/kotlin/uniffi/core_crypto/core_crypto.kt
crypto-ffi/bindings/uniffi-jvm/src
crypto-ffi/bindings/uniffi-android/src

# Test databases leftovers
*.edb
Expand Down
12 changes: 4 additions & 8 deletions crypto-ffi/Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,17 @@ args = [
"generate",
"--config", "uniffi-android.toml",
"--language", "kotlin",
"--out-dir", "./bindings/android/src/main/kotlin/",
"--out-dir", "./bindings/uniffi-android/src/main/kotlin/",
"--library", "../target/release/libcore_crypto_ffi.${LIBRARY_EXTENSION}"
]

[tasks.ffi-kotlin-android]
dependencies = ["compile-ffi-kotlin-android"]
script = '''
mv ./bindings/android/src/main/kotlin/com/wire/crypto/core_crypto_ffi.kt ./bindings/android/src/main/kotlin/com/wire/crypto/CoreCrypto.kt
perl -i \
-pe 's/\bCryptoException\b/CryptoError/g;' \
-pe 's/\bE2eIdentityException\b/E2eIdentityError/g;' \
./bindings/android/src/main/kotlin/uniffi/core_crypto/core_crypto.kt
./bindings/uniffi-android/src/main/kotlin/uniffi/core_crypto/core_crypto.kt
'''

[tasks.compile-ffi-kotlin-jvm]
Expand All @@ -177,19 +175,17 @@ args = [
"--bin", "uniffi-bindgen",
"generate",
"--language", "kotlin",
"--out-dir", "./bindings/jvm/src/main/kotlin/",
"--out-dir", "./bindings/uniffi-jvm/src/main/kotlin/",
"--library", "../target/release/libcore_crypto_ffi.${LIBRARY_EXTENSION}"
]

[tasks.ffi-kotlin-jvm]
dependencies = ["compile-ffi-kotlin-jvm"]
script = '''
mv ./bindings/jvm/src/main/kotlin/com/wire/crypto/core_crypto_ffi.kt ./bindings/jvm/src/main/kotlin/com/wire/crypto/CoreCrypto.kt
perl -i \
-pe 's/\bCryptoException\b/CryptoError/g;' \
-pe 's/\bE2eIdentityException\b/E2eIdentityError/g;' \
./bindings/jvm/src/main/kotlin/uniffi/core_crypto/core_crypto.kt
./bindings/uniffi-jvm/src/main/kotlin/uniffi/core_crypto/core_crypto.kt
'''

[tasks.ffi]
Expand Down
37 changes: 1 addition & 36 deletions crypto-ffi/bindings/android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ val copyBindings by tasks.register<Copy>("copyBindings") {
group = "uniffi"
from(kotlinSources)
include("**/*")
exclude("**/CoreCrypto.kt", "**/core_crypto.kt")
into(generatedDir)
}

dependencies {
implementation(project(":uniffi-android"))
implementation(platform(kotlin("bom")))
implementation(platform(libs.coroutines.bom))
implementation(kotlin("stdlib-jdk7"))
implementation("${libs.jna.get()}@aar")
implementation(libs.appCompat)
implementation(libs.ktx.core)
implementation(libs.coroutines.core)
Expand Down Expand Up @@ -59,31 +58,6 @@ android {
}
}

val processedResourcesDir = buildDir.resolve("processedResources")

fun registerCopyJvmBinaryTask(target: String, jniTarget: String, include: String = "*.so"): TaskProvider<Copy> =
tasks.register<Copy>("copy-${target}") {
group = "uniffi"
from(projectDir.resolve("../../../target/${target}/release"))
include(include)
into(processedResourcesDir.resolve(jniTarget))
}

val copyBinariesTasks = listOf(
registerCopyJvmBinaryTask("aarch64-linux-android", "arm64-v8a"),
registerCopyJvmBinaryTask("armv7-linux-androideabi", "armeabi-v7a"),
registerCopyJvmBinaryTask("x86_64-linux-android", "x86_64")
)

project.afterEvaluate {
tasks.getByName("mergeReleaseJniLibFolders") { dependsOn(copyBinariesTasks) }
tasks.getByName("mergeDebugJniLibFolders") { dependsOn(copyBinariesTasks) }
}

tasks.withType<ProcessResources> {
dependsOn(copyBinariesTasks)
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
dependsOn(copyBindings)
}
Expand All @@ -92,11 +66,6 @@ tasks.withType<Jar> {
dependsOn(copyBindings)
}

tasks.withType<Test> {
enabled = false // FIXME: find a way to do this at some point
dependsOn(copyBinariesTasks)
}

kotlin.sourceSets.getByName("main").apply {
kotlin.srcDir(generatedDir.resolve("main"))
}
Expand All @@ -105,10 +74,6 @@ kotlin.sourceSets.getByName("androidTest").apply {
kotlin.srcDir(generatedDir.resolve("test"))
}

android.sourceSets.getByName("main").apply {
jniLibs.srcDir(processedResourcesDir)
}

// Allows skipping signing jars published to 'MavenLocal' repository
tasks.withType<Sign>().configureEach {
if (System.getenv("CI") == null) { // i.e. not in Github Action runner
Expand Down
4 changes: 2 additions & 2 deletions crypto-ffi/bindings/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
kotlin = "1.9.0"
app-compat = "1.6.1"
coroutines = "1.7.3"
jna = "5.13.0"
jna = "5.14.0"
ktx-core = "1.10.1"
slf4j = "2.0.7"
assertj = "3.24.2"
espresso = "3.5.1"
android-junit = "1.1.5"
android-logback = "2.0.0"
android-tools = "8.0.0"
android-tools = "8.1.1"
sdk-compile = "34"
sdk-min = "26"
gradle = "8.2.1"
Expand Down
30 changes: 1 addition & 29 deletions crypto-ffi/bindings/jvm/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent

plugins {
kotlin("jvm")
id("java-library")
Expand All @@ -13,38 +10,13 @@ java {
}

dependencies {
implementation(platform(kotlin("bom")))
implementation(platform(libs.coroutines.bom))
implementation(kotlin("stdlib-jdk7"))
implementation(libs.jna)
implementation(libs.coroutines.core)
implementation(project(":uniffi-jvm"))
testImplementation(kotlin("test"))
testImplementation(libs.coroutines.test)
testImplementation(libs.assertj.core)
}

val processedResourcesDir = buildDir.resolve("processedResources")

fun registerCopyJvmBinaryTask(target: String, jniTarget: String, include: String = "*.so"): TaskProvider<Copy> =
tasks.register<Copy>("copy-${target}") {
group = "uniffi"
from(projectDir.resolve("../../../target/${target}/release"))
include(include)
into(processedResourcesDir.resolve(jniTarget))
}

val copyBinariesTasks = listOf(
registerCopyJvmBinaryTask("x86_64-unknown-linux-gnu", "linux-x86-64"),
registerCopyJvmBinaryTask("aarch64-apple-darwin", "darwin-aarch64", "*.dylib"),
registerCopyJvmBinaryTask("x86_64-apple-darwin", "darwin-x86-64", "*.dylib"),
)

tasks.withType<ProcessResources> { dependsOn(copyBinariesTasks) }

tasks.withType<Test> { dependsOn(copyBinariesTasks) }

sourceSets { main { resources { srcDir(processedResourcesDir) } } }

// Allows skipping signing jars published to 'MavenLocal' repository
project.afterEvaluate {
tasks.named("signMavenPublication").configure {
Expand Down
2 changes: 1 addition & 1 deletion crypto-ffi/bindings/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ pluginManagement {
}
}

include("jvm", "android")
include("jvm", "android", "uniffi-jvm", "uniffi-android")
80 changes: 80 additions & 0 deletions crypto-ffi/bindings/uniffi-android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
plugins {
id("com.android.library")
kotlin("android")
id("com.vanniktech.maven.publish")
}

dependencies {
implementation(platform(kotlin("bom")))
implementation(platform(libs.coroutines.bom))
implementation(kotlin("stdlib-jdk7"))
implementation("${libs.jna.get()}@aar")
implementation(libs.appCompat)
implementation(libs.ktx.core)
implementation(libs.coroutines.core)
implementation(libs.slf4j)
testImplementation(kotlin("test"))
testImplementation(libs.android.logback)
testImplementation(libs.android.junit)
testImplementation(libs.espresso)
testImplementation(libs.coroutines.test)
testImplementation(libs.assertj.core)
}

android {
namespace = "com.wire.crypto"
compileSdk = libs.versions.sdk.compile.get().toInt()
defaultConfig {
minSdk = libs.versions.sdk.min.get().toInt()
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(file("proguard-android-optimize.txt"), file("proguard-rules.pro"))
}
}
}

val processedResourcesDir = buildDir.resolve("processedResources")

fun registerCopyJvmBinaryTask(target: String, jniTarget: String, include: String = "*.so"): TaskProvider<Copy> =
tasks.register<Copy>("copy-${target}") {
group = "uniffi"
from(projectDir.resolve("../../../target/${target}/release"))
include(include)
into(processedResourcesDir.resolve(jniTarget))
}

val copyBinariesTasks = listOf(
registerCopyJvmBinaryTask("aarch64-linux-android", "arm64-v8a"),
registerCopyJvmBinaryTask("armv7-linux-androideabi", "armeabi-v7a"),
registerCopyJvmBinaryTask("x86_64-linux-android", "x86_64")
)

project.afterEvaluate {
tasks.getByName("mergeReleaseJniLibFolders") { dependsOn(copyBinariesTasks) }
tasks.getByName("mergeDebugJniLibFolders") { dependsOn(copyBinariesTasks) }
}

tasks.withType<ProcessResources> {
dependsOn(copyBinariesTasks)
}

android.sourceSets.getByName("main").apply {
jniLibs.srcDir(processedResourcesDir)
}

// Allows skipping signing jars published to 'MavenLocal' repository
tasks.withType<Sign>().configureEach {
if (System.getenv("CI") == null) { // i.e. not in Github Action runner
enabled = false
}
}
1 change: 1 addition & 0 deletions crypto-ffi/bindings/uniffi-android/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POM_ARTIFACT_ID=core-crypto-uniffi-android
46 changes: 46 additions & 0 deletions crypto-ffi/bindings/uniffi-jvm/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
plugins {
kotlin("jvm")
id("java-library")
id("com.vanniktech.maven.publish")
}

dependencies {
implementation(platform(kotlin("bom")))
implementation(platform(libs.coroutines.bom))
implementation(kotlin("stdlib-jdk7"))
implementation(libs.jna)
implementation(libs.coroutines.core)
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
}

val processedResourcesDir = buildDir.resolve("processedResources")

fun registerCopyJvmBinaryTask(target: String, jniTarget: String, include: String = "*.so"): TaskProvider<Copy> =
tasks.register<Copy>("copy-${target}") {
group = "uniffi"
from(projectDir.resolve("../../../target/${target}/release"))
include(include)
into(processedResourcesDir.resolve(jniTarget))
}

val copyBinariesTasks = listOf(
registerCopyJvmBinaryTask("x86_64-unknown-linux-gnu", "linux-x86-64"),
registerCopyJvmBinaryTask("aarch64-apple-darwin", "darwin-aarch64", "*.dylib"),
registerCopyJvmBinaryTask("x86_64-apple-darwin", "darwin-x86-64", "*.dylib"),
)

tasks.withType<ProcessResources> { dependsOn(copyBinariesTasks) }

tasks.withType<Test> { dependsOn(copyBinariesTasks) }

sourceSets { main { resources { srcDir(processedResourcesDir) } } }

// Allows skipping signing jars published to 'MavenLocal' repository
project.afterEvaluate {
tasks.named("signMavenPublication").configure {
if (System.getenv("CI") == null) { // i.e. not in Github Action runner
enabled = false
}
}
}
1 change: 1 addition & 0 deletions crypto-ffi/bindings/uniffi-jvm/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POM_ARTIFACT_ID=core-crypto-uniffi-jvm

0 comments on commit 24383f7

Please sign in to comment.