crown-android.rb 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. # Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  2. # Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. #
  4. # Permission is hereby granted, free of charge, to any person
  5. # obtaining a copy of this software and associated documentation
  6. # files (the "Software"), to deal in the Software without
  7. # restriction, including without limitation the rights to use,
  8. # copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following
  11. # conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be
  14. # included in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. # OTHER DEALINGS IN THE SOFTWARE.
  24. require 'optparse'
  25. require 'ostruct'
  26. require 'fileutils'
  27. $config_h =
  28. "
  29. #define CROWN_VERSION_MAJOR 0
  30. #define CROWN_VERSION_MINOR 1
  31. #define CROWN_VERSION_MICRO 13
  32. #define PRId64 \"lld\"
  33. #define PRIu64 \"llu\"
  34. #define PRIi64 \"lli\"
  35. #define PRIx64 \"llx\"
  36. #define CE_MAX_TEXTURE_UNITS 8
  37. #define CE_MAX_TEXTURES 32
  38. #define CE_MAX_RENDER_TARGETS 32
  39. #define CE_MAX_VERTEX_BUFFERS 1024
  40. #define CE_MAX_INDEX_BUFFERS 1024
  41. #define CE_MAX_SHADERS 512
  42. #define CE_MAX_GPU_PROGRAMS 512
  43. #define CE_MAX_UNIFORMS 128
  44. #define CE_TRANSIENT_VERTEX_BUFFER_SIZE 6 * 1024 * 1024 // In bytes
  45. #define CE_TRANSIENT_INDEX_BUFFER_SIZE 2 * 1024 * 1024 // In bytes
  46. #define CE_MAX_UNIFORM_NAME_LENGTH 64 // Including NUL character
  47. #define CE_MAX_WORLDS 1024
  48. #define CE_MAX_UNITS 65000 // Per world
  49. #define CE_MAX_CAMERAS 16 // Per world
  50. #define CE_MAX_ACTORS 1024 // Per world
  51. #define CE_MAX_CONTROLLERS 16 // Per world
  52. #define CE_MAX_TRIGGERS 1024 // Per world
  53. #define CE_MAX_JOINTS 512 // Per world
  54. #define CE_MAX_SOUND_INSTANCES 64 // Per world
  55. #define CE_MAX_RAYCASTS 8 // Per World
  56. #define CE_MAX_RAY_INTERSECTIONS 16
  57. #define CE_MAX_CAMERA_COMPONENTS 16 // Per unit
  58. #define CE_MAX_MESH_COMPONENTS 16 // Per unit
  59. #define CE_MAX_SPRITE_COMPONENTS 16 // Per unit
  60. #define CE_MAX_ACTOR_COMPONENTS 16 // Per unit
  61. #define CE_MAX_MATERIAL_COMPONENTS 16 // Per unit
  62. #define CE_MAX_CONSOLE_CLIENTS 32
  63. #define CE_MAX_GUI_RECTS 64 // Per Gui
  64. #define CE_MAX_GUI_TRIANGLES 64 // Per Gui
  65. #define CE_MAX_GUI_IMAGES 64 // Per Gui
  66. #define CE_MAX_GUI_TEXTS 64 // Per Gui
  67. #define CE_MAX_DEBUG_LINES 2 * 1024 // Per DebugLine
  68. #define CE_MAX_LUA_VECTOR2 4096
  69. #define CE_MAX_LUA_VECTOR3 4096
  70. #define CE_MAX_LUA_MATRIX4X4 4096
  71. #define CE_MAX_LUA_QUATERNION 4096
  72. "
  73. $application_mk =
  74. "
  75. APP_STL := gnustl_static
  76. APP_ABI := armeabi-v7a
  77. "
  78. # Commands
  79. $android_create = "android create project"
  80. $android_update = "android update project"
  81. $activity = "CrownActivity"
  82. $package = "crown.android"
  83. # Paths
  84. $engine_src = "../engine/."
  85. $android_src = "../engine/os/android/java/."
  86. $android_manifest = "../engine/os/android/AndroidManifest.xml"
  87. $luajit = "../engine/third/ARMv7/luajit"
  88. $oggvorbis = "../engine/third/ARMv7/oggvorbis"
  89. $physx = "../engine/third/ARMv7/physx"
  90. #------------------------------------------------------------------------------
  91. def validate_command_line(args)
  92. if args.length < 8
  93. return false
  94. end
  95. if args[0] != "--build"
  96. return false
  97. end
  98. if args[2] != "--target"
  99. return false
  100. end
  101. if args[4] != "--name"
  102. return false
  103. end
  104. if args[6] != "--path"
  105. return false
  106. end
  107. return true
  108. end
  109. #------------------------------------------------------------------------------
  110. def parse_command_line(args)
  111. banner = "Usage: crown-android.rb --build BUILD --target TARGET --name NAME --path PATH [--res RES]\n"
  112. if not validate_command_line(args)
  113. print banner
  114. exit
  115. end
  116. options = OpenStruct.new
  117. OptionParser.new do |opts|
  118. opts.banner = banner
  119. opts.on("-b", "--build BUILD", "Crown build") do |b|
  120. options.build = b
  121. end
  122. opts.on("-t", "--target TARGET", "Android target") do |t|
  123. options.target = t
  124. end
  125. opts.on("-n", "--name NAME", "Android project name") do |n|
  126. options.name = n
  127. end
  128. opts.on("-p", "--path PATH", "Android project path") do |p|
  129. options.path = p
  130. end
  131. opts.on("-r", "--res RES", "Android project compiled resources") do |r|
  132. options.res = r
  133. end
  134. opts.on_tail("-h", "--help", "Show this message") do
  135. puts opts
  136. exit
  137. end
  138. end.parse!(args)
  139. return options
  140. end
  141. #------------------------------------------------------------------------------
  142. def generate_config_h(build, dest)
  143. if build == "debug"
  144. $config_h << "#define CROWN_DEBUG"
  145. elsif build == "development"
  146. $config_h << "#define CROWN_DEVELOPMENT"
  147. elsif build == "release"
  148. $config_h << "#define CROWN_RELEASE"
  149. end
  150. f = File.new(dest, File::WRONLY|File::CREAT|File::TRUNC, 0644)
  151. f.write($config_h)
  152. f.close()
  153. end
  154. #------------------------------------------------------------------------------
  155. def generate_application_mk(target, dest)
  156. f = File.new(dest, File::WRONLY|File::CREAT|File::TRUNC, 0644)
  157. f.write($application_mk)
  158. f.write("APP_APPLICATION := " + target)
  159. f.close()
  160. end
  161. #------------------------------------------------------------------------------
  162. def create_android_project(target, name, path)
  163. engine_dest = path + "/jni"
  164. android_dest = path + "/src/crown/android"
  165. assets_dest = path + "/assets"
  166. # Creates path if not exists
  167. if not Dir.exists?(path)
  168. print "Creating directory " + path + "..."
  169. FileUtils.mkdir_p(path)
  170. print "OK!\n"
  171. end
  172. # Project path is not empty
  173. if not Dir["#{path}/."].empty?
  174. print "Cleaning directory " + path + "..."
  175. FileUtils.rm_rf("#{path}/.", :secure => true)
  176. print "OK!\n"
  177. end
  178. # Creates android project
  179. print "Creating android project...\n"
  180. system($android_create + " --target " + target + " --name " + name + " --path " + path + " --activity " + $activity + " --package " + $package)
  181. print "OK!\n"
  182. # if jni dir does not exists, create it!
  183. if not Dir.exists?(engine_dest)
  184. print "Creating directory " + engine_dest + "..."
  185. FileUtils.mkdir_p(engine_dest)
  186. print "OK!\n"
  187. end
  188. # if assets dir does not exists, create it!
  189. if not Dir.exists?(assets_dest)
  190. print "Creating directory " + assets_dest + "..."
  191. FileUtils.mkdir_p(assets_dest)
  192. print "OK!\n"
  193. end
  194. end
  195. #------------------------------------------------------------------------------
  196. def fill_android_project(build, target, res, path)
  197. engine_dest = path + "/jni"
  198. android_dest = path + "/src/crown/android"
  199. resources_dest = path + "/assets"
  200. print "Filling Android project..."
  201. # Copy Engine files
  202. FileUtils.cp_r($engine_src, engine_dest, :remove_destination => true)
  203. # Generate android Config.h
  204. generate_config_h(build, engine_dest + "/Config.h")
  205. # Generate Application.mk
  206. generate_application_mk(target, engine_dest + "/Application.mk")
  207. # Copy luajit lib
  208. FileUtils.cp($luajit + "/lib/libluajit-5.1.so.2.0.2", engine_dest + "/libluajit-5.1.so")
  209. # Copy oggvorbis lib
  210. FileUtils.cp($oggvorbis + "/lib/libogg.a", engine_dest + "/libogg.a")
  211. FileUtils.cp($oggvorbis + "/lib/libvorbis.a", engine_dest + "/libvorbis.a")
  212. # Copy physx lib
  213. FileUtils.cp($physx + "/lib/libPhysX3.a", engine_dest)
  214. FileUtils.cp($physx + "/lib/libSimulationController.a", engine_dest)
  215. FileUtils.cp($physx + "/lib/libLowLevel.a", engine_dest)
  216. FileUtils.cp($physx + "/lib/libLowLevelCloth.a", engine_dest)
  217. FileUtils.cp($physx + "/lib/libPxTask.a", engine_dest)
  218. FileUtils.cp($physx + "/lib/libPhysXProfileSDK.a", engine_dest)
  219. FileUtils.cp($physx + "/lib/libPhysX3Extensions.a", engine_dest)
  220. FileUtils.cp($physx + "/lib/libSceneQuery.a", engine_dest)
  221. FileUtils.cp($physx + "/lib/libPhysX3Common.a", engine_dest)
  222. FileUtils.cp($physx + "/lib/libPhysX3CharacterKinematic.a", engine_dest)
  223. FileUtils.cp($physx + "/lib/libPhysX3Vehicle.a", engine_dest)
  224. FileUtils.cp($physx + "/lib/libPhysX3Cooking.a", engine_dest)
  225. FileUtils.cp($physx + "/lib/libPvdRuntime.a", engine_dest)
  226. # Copy java files
  227. FileUtils.cp_r(Dir.glob($android_src), android_dest, :remove_destination => true)
  228. # Copy android manifest
  229. FileUtils.cp($android_manifest, path)
  230. if build == "release"
  231. FileUtils.cp_r(res + "/.", resources_dest)
  232. end
  233. print "OK!\n"
  234. end
  235. #------------------------------------------------------------------------------
  236. def build_android_project(path)
  237. print "Building...\n"
  238. # Move to root directory of Android project
  239. Dir.chdir(path)
  240. # Build libraries
  241. if not system("ndk-build")
  242. print "Critical error: Unable to build crown libraries"
  243. return
  244. end
  245. # Build apk
  246. # FIXME: it's convenient to build apk in debug mode because we avoid apk signature.
  247. # In a near future, we'll manage the build process in release mode
  248. # N.B: you can build crown in release mode and run 'ant debug' for achieving the same result of a final product
  249. if not system("ant debug")
  250. print "Critical error: Unable to build crown project"
  251. return
  252. else
  253. print "Done!\n"
  254. end
  255. end
  256. #------------------------------------------------------------------------------
  257. opts = parse_command_line(ARGV)
  258. create_android_project(opts.target, opts.name, opts.path)
  259. fill_android_project(opts.build, opts.target, opts.res, opts.path)
  260. build_android_project(opts.path)