stb_image.gradle 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // stb_image url for download
  2. String stbiUrl = 'http://www.nothings.org/stb_image.c'
  3. String stbiDownloadTarget = 'stb_image.c'
  4. // stb_image is not downloaded. The single source file is included in the repo
  5. String stbiFolder = 'stb_image'
  6. //Working directories for the ndk build.
  7. String stbiBuildDir = "${buildDir}" + File.separator + 'stb_image'
  8. String stbiBuildJniDir = stbiBuildDir + File.separator + 'jni'
  9. String stbiBuildLibsDir = stbiBuildDir + File.separator + 'libs'
  10. //Pre-compiled libs directory
  11. String stbiPreCompiledLibsDir = 'libs' + File.separator + 'stb_image'
  12. // jME Android Native source files path
  13. String stbiJmeAndroidPath = 'src/native/jme_stbi'
  14. // Download external source files if not available
  15. task downloadStbImage(type: MyDownload) {
  16. sourceUrl = stbiUrl
  17. target = file(stbiFolder + File.separator + stbiDownloadTarget)
  18. }
  19. // Copy stb_image files to jni directory
  20. task copyStbiFiles(type: Copy) {
  21. def sourceDir = file(stbiFolder)
  22. def outputDir = file(stbiBuildJniDir)
  23. // println "copyStbiFiles sourceDir: " + sourceDir
  24. // println "copyStbiFiles outputDir: " + outputDir
  25. from sourceDir
  26. into outputDir
  27. }
  28. copyStbiFiles.dependsOn {
  29. def stbiFilePath = project.projectDir.absolutePath + stbiFolder + File.separator + stbiDownloadTarget
  30. def stbiFile = new File(stbiFilePath)
  31. // println "zipFile path: " + zipFile.absolutePath
  32. // println "zipFile exists: " + zipFile.exists()
  33. if (!stbiFile.exists()) {
  34. downloadStbImage
  35. }
  36. }
  37. // Copy jME Android native files to jni directory
  38. task copyStbiJmeFiles(type: Copy, dependsOn:copyStbiFiles) {
  39. def sourceDir = file(stbiJmeAndroidPath)
  40. def outputDir = file(stbiBuildJniDir)
  41. // println "copyStbiJmeFiles sourceDir: " + sourceDir
  42. // println "copyStbiJmeFiles outputDir: " + outputDir
  43. from sourceDir
  44. into outputDir
  45. }
  46. task generateStbiHeaders(dependsOn: copyStbiJmeFiles) << {
  47. String destDirPath = stbiBuildJniDir
  48. String classes = ""
  49. .concat("com.jme3.texture.plugins.AndroidNativeImageLoader, ")
  50. // println "stb_image classes = " + classes
  51. // println "stb_image destDir = " + destDir
  52. // println "stb_image classpath = " + project.projectClassPath
  53. ant.javah(
  54. classpath: project.projectClassPath,
  55. destdir: destDirPath,
  56. class: classes
  57. )
  58. }
  59. task buildStbiNativeLib(type: Exec, dependsOn: generateStbiHeaders) {
  60. // println "stb_image build dir: " + buildLibDir
  61. // println "ndkCommandPath: " + project.ndkCommandPath
  62. args 'TARGET_PLATFORM=android-9'
  63. workingDir stbiBuildDir
  64. executable rootProject.ndkCommandPath
  65. }
  66. task updatePreCompiledStbiLibs(type: Copy, dependsOn: buildStbiNativeLib) {
  67. def sourceDir = new File(stbiBuildLibsDir)
  68. def outputDir = new File(stbiPreCompiledLibsDir)
  69. // println "updatePreCompiledStbiLibs sourceDir: " + sourceDir
  70. // println "updatePreCompiledStbiLibs outputDir: " + outputDir
  71. from sourceDir
  72. into outputDir
  73. }
  74. // Copy pre-compiled libs to build directory (when not building new libs)
  75. task copyPreCompiledStbiLibs(type: Copy) {
  76. def sourceDir = file(stbiPreCompiledLibsDir)
  77. def outputDir = file(stbiBuildLibsDir)
  78. // println "copyStbiJmeFiles sourceDir: " + sourceDir
  79. // println "copyStbiJmeFiles outputDir: " + outputDir
  80. from sourceDir
  81. into outputDir
  82. }
  83. if (rootProject.ndkExists) {
  84. // build native libs and update stored pre-compiled libs to commit
  85. compileJava.dependsOn { updatePreCompiledStbiLibs }
  86. } else {
  87. // use pre-compiled native libs (not building new ones)
  88. compileJava.dependsOn { copyPreCompiledStbiLibs }
  89. }
  90. jar.into("lib") { from stbiBuildLibsDir }
  91. // Helper class to wrap ant dowload task
  92. class MyDownload extends DefaultTask {
  93. @Input
  94. String sourceUrl
  95. @OutputFile
  96. File target
  97. @TaskAction
  98. void download() {
  99. ant.get(src: sourceUrl, dest: target)
  100. }
  101. }