build.gradle.kts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import com.android.build.gradle.internal.tasks.factory.dependsOn
  2. import utils.getGitVersion
  3. plugins {
  4. alias(libs.plugins.java.library)
  5. alias(libs.plugins.java.testFixtures)
  6. alias(libs.plugins.kotlin.jvm)
  7. alias(libs.plugins.kotlin.serialization)
  8. alias(libs.plugins.mavenPublish)
  9. }
  10. dependencies {
  11. implementation(project(":common"))
  12. api(libs.kotlinx.coroutines.core)
  13. api(libs.libphonenumber)
  14. api(libs.androidx.annotation)
  15. api(libs.protobuf.kotlin.lite)
  16. implementation(libs.slf4j.api)
  17. implementation(libs.eddsa)
  18. implementation(libs.kotlinx.coroutines.android)
  19. implementation(libs.kotlinx.serialization.json)
  20. implementation(libs.jna)
  21. compileOnly(libs.compose.annotation)
  22. testImplementation(libs.junit)
  23. testImplementation(libs.mockk)
  24. testImplementation(libs.kotlinx.coroutines.test)
  25. testImplementation(libs.kotlin.test)
  26. testImplementation(project(":test-helpers"))
  27. testFixturesImplementation(project(":common"))
  28. }
  29. sourceSets {
  30. assert(file("./protocol/src/common.proto").exists()) {
  31. "Error: Git protobuf submodule missing. Please run `git submodule update --init`.\n"
  32. }
  33. main {
  34. java.srcDir("./build/generated/source/proto/main/java")
  35. java.srcDir("./build/generated/source/proto/main/kotlin")
  36. java.srcDir("./build/generated/source/libthreema")
  37. }
  38. }
  39. tasks.withType<Test> {
  40. // Necessary to load the dynamic libthreema library in unit tests
  41. systemProperty("jna.library.path", "${project.projectDir}/libthreema/target/release")
  42. useJUnitPlatform()
  43. }
  44. afterEvaluate {
  45. val bindingsDirectory = "../build/generated/source/libthreema"
  46. // Define the task to generate libthreema library (only used to generate bindings for it)
  47. val generateLibthreema = tasks.register<Exec>("generateLibthreema") {
  48. workingDir("${project.projectDir}/libthreema")
  49. commandLine("cargo", "build", "-F", "uniffi", "-p", "libthreema", "--release", "--locked")
  50. }
  51. // Define the task to generate the uniffi bindings for libthreema
  52. val uniffiBindings = tasks.register("generateUniFFIBindings") {
  53. dependsOn(generateLibthreema)
  54. doLast {
  55. // It seems that the uniffi package generates a "*.so" file on linux and a "*.dylib" on mac
  56. // while using the cargo build command from the gradle task above ("generateLibthreema").
  57. val uniffiLibraryFilePathPrefix = "${project.projectDir}/libthreema/target/release/liblibthreema"
  58. val uniffiLibraryFile = file("$uniffiLibraryFilePathPrefix.so")
  59. .takeIf { it.exists() }
  60. ?: file("$uniffiLibraryFilePathPrefix.dylib")
  61. assert(uniffiLibraryFile.exists()) {
  62. "Error: Missing pre-generated uniffy library file in libthreema/target/*/ directory.\n"
  63. }
  64. val processBuilder = ProcessBuilder(
  65. "cargo",
  66. "run",
  67. "-p",
  68. "uniffi-bindgen",
  69. "generate",
  70. "--library",
  71. uniffiLibraryFile.path,
  72. "--language",
  73. "kotlin",
  74. "--out-dir",
  75. bindingsDirectory,
  76. "--no-format",
  77. )
  78. processBuilder.directory(file("${project.projectDir}/libthreema"))
  79. processBuilder.start().waitFor()
  80. }
  81. }
  82. tasks["compileKotlin"].dependsOn(uniffiBindings)
  83. }
  84. publishing {
  85. publications {
  86. register<MavenPublication>("library") {
  87. from(components["java"])
  88. version = getGitVersion()
  89. }
  90. }
  91. repositories {
  92. maven {
  93. url = run {
  94. val apiV4Url = System.getenv("CI_API_V4_URL")
  95. val projectId = System.getenv("CI_PROJECT_ID")
  96. uri("$apiV4Url/projects/$projectId/packages/maven")
  97. }
  98. name = "Gitlab"
  99. credentials(HttpHeaderCredentials::class.java) {
  100. name = "Job-Token"
  101. value = System.getenv("CI_JOB_TOKEN")
  102. }
  103. authentication {
  104. create<HttpHeaderAuthentication>("header")
  105. }
  106. }
  107. }
  108. }
  109. tasks.register<Exec>("compileProto") {
  110. group = "build"
  111. description = "generate class bindings from protobuf files in the 'protocol/src' directory"
  112. workingDir(project.projectDir)
  113. commandLine("./compile-proto.sh")
  114. }
  115. tasks.compileKotlin.dependsOn("compileProto")
  116. tasks.register<Exec>("libthreemaCleanUp") {
  117. workingDir("${project.projectDir}/libthreema")
  118. commandLine("cargo", "clean")
  119. }
  120. tasks.clean.dependsOn("libthreemaCleanUp")
  121. java {
  122. sourceCompatibility = JavaVersion.VERSION_11
  123. targetCompatibility = JavaVersion.VERSION_11
  124. }
  125. kotlin {
  126. compilerOptions {
  127. jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_11
  128. }
  129. }