crown-android.rb 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_PIXELS_PER_METER 32
  37. #define CE_MAX_TEXTURE_UNITS 8
  38. #define CE_MAX_TEXTURES 32
  39. #define CE_MAX_RENDER_TARGETS 32
  40. #define CE_MAX_VERTEX_BUFFERS 1024
  41. #define CE_MAX_INDEX_BUFFERS 1024
  42. #define CE_MAX_SHADERS 512
  43. #define CE_MAX_GPU_PROGRAMS 512
  44. #define CE_MAX_UNIFORMS 128
  45. #define CE_TRANSIENT_VERTEX_BUFFER_SIZE 6 * 1024 * 1024 // In bytes
  46. #define CE_TRANSIENT_INDEX_BUFFER_SIZE 2 * 1024 * 1024 // In bytes
  47. #define CE_MAX_UNIFORM_NAME_LENGTH 64 // Including NUL character
  48. #define CE_MAX_WORLDS 1024
  49. #define CE_MAX_UNITS 65000 // Per world
  50. #define CE_MAX_CAMERAS 16 // Per world
  51. #define CE_MAX_ACTORS 1024 // Per world
  52. #define CE_MAX_CONTROLLERS 16 // Per world
  53. #define CE_MAX_TRIGGERS 1024 // Per world
  54. #define CE_MAX_JOINTS 512 // Per world
  55. #define CE_MAX_SOUND_INSTANCES 64 // Per world
  56. #define CE_MAX_RAYCASTS 8 // Per World
  57. #define CE_MAX_RAY_INTERSECTIONS 16
  58. #define CE_MAX_CAMERA_COMPONENTS 16 // Per unit
  59. #define CE_MAX_MESH_COMPONENTS 16 // Per unit
  60. #define CE_MAX_SPRITE_COMPONENTS 16 // Per unit
  61. #define CE_MAX_ACTOR_COMPONENTS 16 // Per unit
  62. #define CE_MAX_MATERIAL_COMPONENTS 16 // Per unit
  63. #define CE_MAX_CONSOLE_CLIENTS 32
  64. #define CE_MAX_GUI_RECTS 64 // Per Gui
  65. #define CE_MAX_GUI_TRIANGLES 64 // Per Gui
  66. #define CE_MAX_GUI_IMAGES 64 // Per Gui
  67. #define CE_MAX_GUI_TEXTS 64 // Per Gui
  68. #define CE_MAX_DEBUG_LINES 2 * 1024 // Per DebugLine
  69. #define CE_MAX_LUA_VECTOR2 4096
  70. #define CE_MAX_LUA_VECTOR3 4096
  71. #define CE_MAX_LUA_MATRIX4X4 4096
  72. #define CE_MAX_LUA_QUATERNION 4096
  73. "
  74. $application_mk =
  75. "
  76. APP_PLATFORM := android-10
  77. APP_STL := gnustl_static
  78. APP_ABI := armeabi-v7a
  79. "
  80. # Commands
  81. $android_create = "android create project"
  82. $android_update = "android update project"
  83. $activity = "CrownActivity"
  84. $package = "crown.android"
  85. # Paths
  86. $engine_src = "../engine/."
  87. $android_src = "../engine/os/android/java/."
  88. $android_manifest = "../engine/os/android/AndroidManifest.xml"
  89. $luajit = "../third/ARMv7/luajit"
  90. $oggvorbis = "../third/ARMv7/oggvorbis"
  91. $physx = "../third/ARMv7/physx"
  92. #------------------------------------------------------------------------------
  93. def validate_command_line(args)
  94. if args.length < 8
  95. return false
  96. end
  97. if args[0] != "--build"
  98. return false
  99. end
  100. if args[2] != "--target"
  101. return false
  102. end
  103. if args[4] != "--name"
  104. return false
  105. end
  106. if args[6] != "--path"
  107. return false
  108. end
  109. return true
  110. end
  111. #------------------------------------------------------------------------------
  112. def parse_command_line(args)
  113. banner = "Usage: crown-android.rb --build BUILD --target TARGET --name NAME --path PATH [--res RES]\n"
  114. if not validate_command_line(args)
  115. print banner
  116. exit
  117. end
  118. options = OpenStruct.new
  119. OptionParser.new do |opts|
  120. opts.banner = banner
  121. opts.on("-b", "--build BUILD", "Crown build") do |b|
  122. options.build = b
  123. end
  124. opts.on("-t", "--target TARGET", "Android target") do |t|
  125. options.target = t
  126. end
  127. opts.on("-n", "--name NAME", "Android project name") do |n|
  128. options.name = n
  129. end
  130. opts.on("-p", "--path PATH", "Android project path") do |p|
  131. options.path = p
  132. end
  133. opts.on("-r", "--res RES", "Android project compiled resources") do |r|
  134. options.res = r
  135. end
  136. opts.on_tail("-h", "--help", "Show this message") do
  137. puts opts
  138. exit
  139. end
  140. end.parse!(args)
  141. return options
  142. end
  143. #------------------------------------------------------------------------------
  144. def generate_config_h(build, dest)
  145. if build == "debug"
  146. $config_h << "#define CROWN_DEBUG"
  147. elsif build == "development"
  148. $config_h << "#define CROWN_DEVELOPMENT"
  149. elsif build == "release"
  150. $config_h << "#define CROWN_RELEASE"
  151. end
  152. f = File.new(dest, File::WRONLY|File::CREAT|File::TRUNC, 0644)
  153. f.write($config_h)
  154. f.close()
  155. end
  156. #------------------------------------------------------------------------------
  157. def generate_application_mk(target, dest)
  158. f = File.new(dest, File::WRONLY|File::CREAT|File::TRUNC, 0644)
  159. f.write($application_mk)
  160. f.write("APP_APPLICATION := " + target)
  161. f.close()
  162. end
  163. #------------------------------------------------------------------------------
  164. def create_android_project(target, name, path)
  165. engine_dest = path + "/jni"
  166. android_dest = path + "/src/crown/android"
  167. assets_dest = path + "/assets"
  168. # Creates path if not exists
  169. if not Dir.exists?(path)
  170. print "Creating directory " + path + "..."
  171. FileUtils.mkdir_p(path)
  172. print "OK!\n"
  173. end
  174. # Project path is not empty
  175. if not Dir["#{path}/."].empty?
  176. print "Cleaning directory " + path + "..."
  177. FileUtils.rm_rf("#{path}/.", :secure => true)
  178. print "OK!\n"
  179. end
  180. # Creates android project
  181. print "Creating android project...\n"
  182. system($android_create + " --target " + target + " --name " + name + " --path " + path + " --activity " + $activity + " --package " + $package)
  183. print "OK!\n"
  184. # if jni dir does not exists, create it!
  185. if not Dir.exists?(engine_dest)
  186. print "Creating directory " + engine_dest + "..."
  187. FileUtils.mkdir_p(engine_dest)
  188. print "OK!\n"
  189. end
  190. # if assets dir does not exists, create it!
  191. if not Dir.exists?(assets_dest)
  192. print "Creating directory " + assets_dest + "..."
  193. FileUtils.mkdir_p(assets_dest)
  194. print "OK!\n"
  195. end
  196. end
  197. #------------------------------------------------------------------------------
  198. def fill_android_project(build, target, res, path)
  199. engine_dest = path + "/jni"
  200. android_dest = path + "/src/crown/android"
  201. resources_dest = path + "/assets"
  202. print "Filling Android project..."
  203. # Copy Engine files
  204. FileUtils.cp_r($engine_src, engine_dest, :remove_destination => true)
  205. FileUtils.cp_r($engine_src + "/../third", engine_dest, :remove_destination => true)
  206. # Generate android Config.h
  207. generate_config_h(build, engine_dest + "/Config.h")
  208. # Generate Application.mk
  209. generate_application_mk(target, engine_dest + "/Application.mk")
  210. # Copy luajit lib
  211. FileUtils.cp($luajit + "/lib/libluajit-5.1.so.2.0.2", engine_dest + "/libluajit-5.1.so")
  212. # Copy oggvorbis lib
  213. FileUtils.cp($oggvorbis + "/lib/libogg.a", engine_dest + "/libogg.a")
  214. FileUtils.cp($oggvorbis + "/lib/libvorbis.a", engine_dest + "/libvorbis.a")
  215. # Copy physx lib
  216. FileUtils.cp($physx + "/lib/libPhysX3.a", engine_dest)
  217. FileUtils.cp($physx + "/lib/libSimulationController.a", engine_dest)
  218. FileUtils.cp($physx + "/lib/libLowLevel.a", engine_dest)
  219. FileUtils.cp($physx + "/lib/libLowLevelCloth.a", engine_dest)
  220. FileUtils.cp($physx + "/lib/libPxTask.a", engine_dest)
  221. FileUtils.cp($physx + "/lib/libPhysXProfileSDK.a", engine_dest)
  222. FileUtils.cp($physx + "/lib/libPhysX3Extensions.a", engine_dest)
  223. FileUtils.cp($physx + "/lib/libSceneQuery.a", engine_dest)
  224. FileUtils.cp($physx + "/lib/libPhysX3Common.a", engine_dest)
  225. FileUtils.cp($physx + "/lib/libPhysX3CharacterKinematic.a", engine_dest)
  226. FileUtils.cp($physx + "/lib/libPhysX3Vehicle.a", engine_dest)
  227. FileUtils.cp($physx + "/lib/libPhysX3Cooking.a", engine_dest)
  228. FileUtils.cp($physx + "/lib/libPvdRuntime.a", engine_dest)
  229. # Copy java files
  230. FileUtils.cp_r(Dir.glob($android_src), android_dest, :remove_destination => true)
  231. # Copy android manifest
  232. FileUtils.cp($android_manifest, path)
  233. if build == "release"
  234. FileUtils.cp_r(res + "/.", resources_dest)
  235. end
  236. print "OK!\n"
  237. end
  238. #------------------------------------------------------------------------------
  239. def build_android_project(path)
  240. print "Building...\n"
  241. # Move to root directory of Android project
  242. Dir.chdir(path)
  243. # Build libraries
  244. if not system("ndk-build")
  245. print "Critical error: Unable to build crown libraries"
  246. return
  247. end
  248. # Build apk
  249. # FIXME: it's convenient to build apk in debug mode because we avoid apk signature.
  250. # In a near future, we'll manage the build process in release mode
  251. # N.B: you can build crown in release mode and run 'ant debug' for achieving the same result of a final product
  252. if not system("ant debug")
  253. print "Critical error: Unable to build crown project"
  254. return
  255. else
  256. print "Done!\n"
  257. end
  258. end
  259. #------------------------------------------------------------------------------
  260. opts = parse_command_line(ARGV)
  261. create_android_project(opts.target, opts.name, opts.path)
  262. fill_android_project(opts.build, opts.target, opts.res, opts.path)
  263. build_android_project(opts.path)