build.gradle 6.4 KB

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