openalsoft.gradle 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // OpenAL Soft r1.21.1
  2. // TODO: update URL to jMonkeyEngine fork once it's updated with latest kcat's changes
  3. String openALSoftUrl = 'https://github.com/kcat/openal-soft/archive/1.24.3.zip'
  4. String openALSoftZipFile = 'OpenALSoft.zip'
  5. // OpenAL Soft directory the download is extracted into
  6. // Typically, the downloaded OpenAL Soft zip file will extract to a directory
  7. // called "openal-soft"
  8. String openALSoftFolder = 'openal-soft-1.24.3'
  9. //Working directories for the ndk build.
  10. String openalsoftBuildDir = "${buildDir}" + File.separator + 'openalsoft'
  11. String openalsoftClassesBuildDir = "${buildDir}" + File.separator + 'openalsoft_classes'
  12. String openalsoftBuildJniDir = openalsoftBuildDir + File.separator + 'jni'
  13. String openalsoftBuildLibsDir = openalsoftBuildDir + File.separator + 'libs'
  14. //Pre-compiled libs directory
  15. def rootPath = rootProject.projectDir.absolutePath
  16. String openalsoftPreCompiledLibsDir = rootPath + File.separator + 'build' + File.separator + 'native' + File.separator + 'android' + File.separator + 'openalsoft'
  17. // jME Android Native source files path
  18. String openalsoftJmeAndroidPath = 'src/native/jme_openalsoft'
  19. String jmeHeaders = 'src/native/headers'
  20. // Download external source files if not available
  21. task downloadOpenALSoft(type: MyDownload) {
  22. sourceUrl = openALSoftUrl
  23. target = file(openalsoftBuildDir + File.separator + openALSoftZipFile)
  24. }
  25. // Unzip external source files
  26. task unzipOpenALSoft(type: Copy) {
  27. def zipFile = file(openalsoftBuildDir + File.separator + openALSoftZipFile)
  28. def outputDir = file(openalsoftBuildDir)
  29. from zipTree(zipFile)
  30. into outputDir
  31. }
  32. unzipOpenALSoft.dependsOn {
  33. def zipFilePath = openalsoftBuildDir + File.separator + openALSoftZipFile
  34. def zipFile = new File(zipFilePath)
  35. // println "zipFile path: " + zipFile.absolutePath
  36. // println "zipFile exists: " + zipFile.exists()
  37. if (!zipFile.exists()) {
  38. downloadOpenALSoft
  39. }
  40. }
  41. // Copy external source files to jni directory
  42. task copyOpenALSoft(type: Copy) {
  43. def sourceDir = file(openalsoftBuildDir + File.separator + openALSoftFolder)
  44. def outputDir = file(openalsoftBuildJniDir)
  45. // println "copyOpenALSoft sourceDir: " + sourceDir
  46. // println "copyOpenALSoft outputDir: " + outputDir
  47. from sourceDir
  48. into outputDir
  49. }
  50. copyOpenALSoft.dependsOn {
  51. def openALSoftUnzipDir = new File(openalsoftBuildDir + File.separator + openALSoftFolder)
  52. // println "openALSoftUnzipDir path: " + openALSoftUnzipDir.absolutePath
  53. // println "openALSoftUnzipDir exists: " + openALSoftUnzipDir.isDirectory()
  54. if (!openALSoftUnzipDir.isDirectory()) {
  55. unzipOpenALSoft
  56. }
  57. }
  58. // Copy JME Headers to jni directory
  59. task copyJmeHeadersOpenAL(type: Copy) {
  60. from file(jmeHeaders)
  61. into file(openalsoftBuildJniDir + File.separator + "headers")
  62. }
  63. // Copy jME Android native files to jni directory
  64. task copyJmeOpenALSoft(type: Copy, dependsOn: [copyOpenALSoft, copyJmeHeadersOpenAL]) {
  65. def sourceDir = file(openalsoftJmeAndroidPath)
  66. def outputDir = file(openalsoftBuildJniDir)
  67. // println "copyJmeOpenALSoft sourceDir: " + sourceDir
  68. // println "copyJmeOpenALSoft outputDir: " + outputDir
  69. from sourceDir
  70. into outputDir
  71. }
  72. // rootProject.ndkCommandPath must be set to your ndk-build wrapper or full ndk path
  73. def ndkPath = new File(rootProject.ndkCommandPath).getParent()
  74. def cmakeToolchain = "${ndkPath}/build/cmake/android.toolchain.cmake"
  75. // 1) list your ABIs here
  76. def openalAbis = [
  77. "armeabi-v7a",
  78. "arm64-v8a",
  79. "x86",
  80. "x86_64"
  81. ]
  82. // 2) for each ABI, register a configure/build pair
  83. openalAbis.each { abi ->
  84. // configure task
  85. tasks.register("configureOpenAlSoft_${abi}", Exec) {
  86. group = "external-native"
  87. description = "Generate CMake build files for OpenAL-Soft [$abi]"
  88. workingDir file("$openalsoftBuildDir/$openALSoftFolder")
  89. commandLine = [
  90. "cmake",
  91. "-S", ".",
  92. "-B", "cmake-build-${abi}",
  93. "-G", "Unix Makefiles", // or Ninja
  94. "-DCMAKE_TOOLCHAIN_FILE=${cmakeToolchain}",
  95. "-DANDROID_PLATFORM=android-21",
  96. "-DANDROID_ABI=${abi}",
  97. "-DCMAKE_BUILD_TYPE=Release",
  98. "-DALSOFT_UTILS=OFF",
  99. "-DALSOFT_EXAMPLES=OFF",
  100. "-DALSOFT_TESTS=OFF",
  101. "-DALSOFT_BACKEND_OPENSL=ON",
  102. '-DALSOFT_SHARED=OFF',
  103. '-DBUILD_SHARED_LIBS=OFF',
  104. '-DALSOFT_STATIC=ON',
  105. '-DLIBTYPE=STATIC',
  106. '-DCMAKE_CXX_FLAGS=-stdlib=libc++'
  107. ]
  108. dependsOn copyOpenALSoft
  109. }
  110. // build task
  111. tasks.register("buildOpenAlSoft_${abi}", Exec) {
  112. group = "external-native"
  113. description = "Compile OpenAL-Soft into libopenalsoft.a for [$abi]"
  114. dependsOn "configureOpenAlSoft_${abi}"
  115. workingDir file("$openalsoftBuildDir/$openALSoftFolder")
  116. commandLine = [
  117. "cmake",
  118. "--build", "cmake-build-${abi}",
  119. "--config", "Release"
  120. ]
  121. }
  122. }
  123. // 3) optional: aggregate tasks
  124. tasks.register("configureOpenAlSoftAll") {
  125. group = "external-native"
  126. description = "Configure OpenAL-Soft for all ABIs"
  127. dependsOn openalAbis.collect { "configureOpenAlSoft_${it}" }
  128. }
  129. tasks.register("buildOpenAlSoftAll") {
  130. group = "external-native"
  131. description = "Build OpenAL-Soft for all ABIs"
  132. dependsOn openalAbis.collect { "buildOpenAlSoft_${it}" }
  133. }
  134. task buildOpenAlSoftNativeLib(type: Exec) {
  135. group = "external-native"
  136. description = "Runs ndk-build on your JNI code, linking in the prebuilt OpenAL-Soft .a files"
  137. dependsOn copyJmeOpenALSoft, buildOpenAlSoftAll
  138. // where your Android.mk lives
  139. workingDir openalsoftBuildDir
  140. // call the NDK build script
  141. executable rootProject.ndkCommandPath
  142. // pass in all ABIs (so ndk-build will rebuild your shared .so for each one),
  143. // and pass in a custom var OPENALSOFT_BUILD_DIR so your Android.mk can find
  144. // the cmake-build-<ABI> folders.
  145. args(
  146. // let ndk-build know which ABIs to build for
  147. "APP_ABI=armeabi-v7a,arm64-v8a,x86,x86_64",
  148. // pass in the path to the CMake output root
  149. "OPENALSOFT_BUILD_ROOT=${openalsoftBuildDir}/${openALSoftFolder}",
  150. // parallel jobs
  151. "-j${Runtime.runtime.availableProcessors()}"
  152. )
  153. }
  154. task updatePreCompiledOpenAlSoftLibs(type: Copy, dependsOn: buildOpenAlSoftNativeLib) {
  155. def sourceDir = new File(openalsoftBuildLibsDir)
  156. def outputDir = new File(openalsoftPreCompiledLibsDir)
  157. // println "updatePreCompiledOpenAlSoftLibs sourceDir: " + sourceDir
  158. // println "updatePreCompiledOpenAlSoftLibs outputDir: " + outputDir
  159. from sourceDir
  160. into outputDir
  161. }
  162. // Copy pre-compiled libs to build directory (when not building new libs)
  163. task copyPreCompiledOpenAlSoftLibs(type: Copy) {
  164. def sourceDir = file(openalsoftPreCompiledLibsDir)
  165. def outputDir = file(openalsoftBuildLibsDir)
  166. // println "copyStbiJmeFiles sourceDir: " + sourceDir
  167. // println "copyStbiJmeFiles outputDir: " + outputDir
  168. from sourceDir
  169. into outputDir
  170. }
  171. if (skipPrebuildLibraries != "true" && buildNativeProjects != "true") {
  172. copyPreCompiledOpenAlSoftLibs.dependsOn(rootProject.extractPrebuiltNatives)
  173. }
  174. // ndkExists is a boolean from the build.gradle in the root project
  175. // buildNativeProjects is a string set to "true"
  176. if (ndkExists && buildNativeProjects == "true") {
  177. // build native libs and update stored pre-compiled libs to commit
  178. compileJava.dependsOn { updatePreCompiledOpenAlSoftLibs }
  179. } else {
  180. // use pre-compiled native libs (not building new ones)
  181. compileJava.dependsOn { copyPreCompiledOpenAlSoftLibs }
  182. }
  183. jar.into("lib") { from openalsoftBuildLibsDir }
  184. // Helper class to wrap ant download task
  185. class MyDownload extends DefaultTask {
  186. @Input
  187. String sourceUrl
  188. @OutputFile
  189. File target
  190. @TaskAction
  191. void download() {
  192. ant.get(src: sourceUrl, dest: target)
  193. }
  194. }