build.gradle 7.0 KB

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