build.gradle 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. apply plugin: 'cpp'
  2. import java.nio.file.Paths
  3. String bulletSrcPath = bulletFolder + '/src'
  4. if (!hasProperty('mainClass')) {
  5. ext.mainClass = ''
  6. }
  7. dependencies {
  8. compile project(':jme3-bullet')
  9. }
  10. clean { dependsOn 'cleanHeaders', 'cleanUnzipped' }
  11. // clean up auto-generated C++ headers
  12. task cleanHeaders(type: Delete) {
  13. delete fileTree(dir: 'src/native/cpp', include: 'com_jme3_bullet_*.h')
  14. }
  15. // clean up unzipped files
  16. task cleanUnzipped(type: Delete) {
  17. delete bulletFolder
  18. }
  19. // clean up the downloaded archive
  20. task cleanZipFile(type: Delete) {
  21. delete bulletZipFile
  22. }
  23. model {
  24. components {
  25. bulletjme(NativeLibrarySpec) {
  26. targetPlatform 'Windows64'
  27. targetPlatform 'Windows32'
  28. targetPlatform 'Mac64'
  29. targetPlatform 'Mac32'
  30. targetPlatform 'Linux64'
  31. targetPlatform 'Linux32'
  32. sources {
  33. cpp {
  34. source {
  35. srcDir 'src/native/cpp'
  36. srcDir bulletSrcPath
  37. exclude 'Bullet3Collision/**'
  38. exclude 'Bullet3Dynamics/**'
  39. exclude 'Bullet3Geometry/**'
  40. exclude 'Bullet3OpenCL/**'
  41. exclude 'Bullet3Serialize/**'
  42. include '**/*.cpp'
  43. }
  44. exportedHeaders {
  45. srcDir 'src/native/cpp'
  46. srcDir bulletSrcPath
  47. exclude 'Bullet3Collision/**'
  48. exclude 'Bullet3Dynamics/**'
  49. exclude 'Bullet3Geometry/**'
  50. exclude 'Bullet3OpenCL/**'
  51. exclude 'Bullet3Serialize/**'
  52. include '**/*.h'
  53. }
  54. }
  55. }
  56. }
  57. }
  58. binaries {
  59. withType(SharedLibraryBinarySpec) {
  60. def projectPath = project.projectDir.absolutePath
  61. def javaHome = org.gradle.internal.jvm.Jvm.current().javaHome
  62. def os = targetPlatform.operatingSystem.name
  63. def arch = targetPlatform.architecture.name
  64. def fileName = sharedLibraryFile.name
  65. // Gradle decided to change underscores to dashes - fix that.
  66. arch = arch.replaceAll('-', '_')
  67. // For all binaries that can't be built on the current system
  68. if (buildNativeProjects != "true") {
  69. buildable = false
  70. }
  71. if (!buildable) {
  72. if (sharedLibraryFile.exists()) {
  73. // Add binary to jar file if the binary exists in the build folder already,
  74. // e.g. when the build of jme3-bullet-native has been run on a virtual box
  75. // and the project hasn't been cleaned yet.
  76. jar.into("native/${os}/${arch}") {
  77. from sharedLibraryFile
  78. }
  79. } else {
  80. // Get from libs folder if no fresh build is available in the build folder and add to jar file
  81. def precompiledFile = Paths.get(projectPath, 'libs', 'native', os, arch, fileName).toFile()
  82. if (precompiledFile.exists()) {
  83. jar.into("native/${os}/${arch}") {
  84. from precompiledFile
  85. }
  86. }
  87. }
  88. return
  89. }
  90. cppCompiler.define('BT_NO_PROFILE')
  91. if (toolChain in VisualCpp) {
  92. cppCompiler.args "/I$javaHome\\include"
  93. } else{
  94. cppCompiler.args '-I', "$javaHome/include"
  95. }
  96. if (os == "osx") {
  97. cppCompiler.args '-I', "$javaHome/include/darwin"
  98. cppCompiler.args "-O3"
  99. cppCompiler.args "-U_FORTIFY_SOURCE"
  100. } else if (os == "linux") {
  101. cppCompiler.args "-fvisibility=hidden"
  102. cppCompiler.args '-I', "$javaHome/include/linux"
  103. cppCompiler.args "-fPIC"
  104. cppCompiler.args "-O3"
  105. cppCompiler.args "-U_FORTIFY_SOURCE"
  106. cppCompiler.args "-fpermissive"
  107. linker.args "-fvisibility=hidden"
  108. } else if (os == "windows") {
  109. if (toolChain in Gcc) {
  110. if (toolChain.name.startsWith('mingw')) {
  111. cppCompiler.args '-I', "$projectDir/src/native/cpp/fake_win32"
  112. } else {
  113. cppCompiler.args '-I', "$javaHome/include/win32"
  114. }
  115. cppCompiler.args "-fpermissive"
  116. cppCompiler.args "-static"
  117. cppCompiler.args "-O3"
  118. cppCompiler.args "-U_FORTIFY_SOURCE"
  119. linker.args "-static"
  120. linker.args "-Wl,--exclude-all-symbols"
  121. }
  122. else if (toolChain in VisualCpp) {
  123. cppCompiler.args "/I$javaHome\\include\\win32"
  124. }
  125. cppCompiler.define('WIN32')
  126. }
  127. tasks.all {
  128. dependsOn unzipBulletIfNeeded
  129. dependsOn ':jme3-bullet:compileJava'
  130. }
  131. // Add output to jar file
  132. jar.into("native/${os}/${arch}") {
  133. from sharedLibraryFile
  134. }
  135. // Add depend on build
  136. jar.dependsOn tasks
  137. // Add output to libs folder
  138. task "copyBinaryToLibs${targetPlatform.name}"(type: Copy, dependsOn: tasks) {
  139. from sharedLibraryFile
  140. into "libs/native/${os}/${arch}"
  141. }
  142. // Add depend on copy
  143. jar.dependsOn("copyBinaryToLibs${targetPlatform.name}")
  144. }
  145. withType(StaticLibraryBinarySpec) {
  146. buildable = false
  147. }
  148. }
  149. platforms {
  150. Windows32 {
  151. architecture "x86"
  152. operatingSystem "windows"
  153. }
  154. Windows64 {
  155. architecture "x86_64"
  156. operatingSystem "windows"
  157. }
  158. Mac32 {
  159. architecture "x86"
  160. operatingSystem "osx"
  161. }
  162. Mac64 {
  163. architecture "x86_64"
  164. operatingSystem "osx"
  165. }
  166. Linux32 {
  167. architecture "x86"
  168. operatingSystem "linux"
  169. }
  170. Linux64 {
  171. architecture "x86_64"
  172. operatingSystem "linux"
  173. }
  174. }
  175. }
  176. // Java source sets for IDE access and source jar bundling / mavenization
  177. sourceSets {
  178. main {
  179. java {
  180. srcDir 'src/native/cpp'
  181. }
  182. }
  183. }
  184. task downloadBullet(type: MyDownload) {
  185. sourceUrl = bulletUrl
  186. target = file(bulletZipFile)
  187. }
  188. task unzipBullet(type: Copy) {
  189. from zipTree(bulletZipFile)
  190. into file('.')
  191. }
  192. unzipBullet.dependsOn {
  193. if (!file(bulletZipFile).exists()) {
  194. downloadBullet
  195. }
  196. }
  197. task unzipBulletIfNeeded {
  198. }
  199. unzipBulletIfNeeded.dependsOn {
  200. if (buildNativeProjects == "true") {
  201. unzipBullet
  202. }
  203. }
  204. // Helper class to wrap ant download task
  205. class MyDownload extends DefaultTask {
  206. @Input
  207. String sourceUrl
  208. @OutputFile
  209. File target
  210. @TaskAction
  211. void download() {
  212. ant.get(src: sourceUrl, dest: target)
  213. }
  214. }