crown-android.rb 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 1024
  42. #define CE_MAX_GPU_PROGRAMS 1024
  43. #define CE_MAX_UNIFORMS 128
  44. #define CE_TRANSIENT_VERTEX_BUFFER_SIZE 2 * 1024 // In bytes
  45. #define CE_TRANSIENT_INDEX_BUFFER_SIZE 2 * 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 1024 // 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. "
  56. $application_mk =
  57. "
  58. APP_STL := gnustl_static
  59. APP_ABI := armeabi-v7a
  60. "
  61. # Commands
  62. $android_create = "android create project"
  63. $android_update = "android update project"
  64. $activity = "CrownActivity"
  65. $package = "crown.android"
  66. # Paths
  67. $engine_src = "../engine/."
  68. $android_src = "../engine/os/android/java/."
  69. $android_manifest = "../engine/os/android/AndroidManifest.xml"
  70. $luajit = "../engine/third/ARMv7/luajit"
  71. $oggvorbis = "../engine/third/ARMv7/oggvorbis"
  72. $physx = "../engine/third/ARMv7/physx"
  73. #------------------------------------------------------------------------------
  74. def validate_command_line(args)
  75. if args.length != 8
  76. return false
  77. end
  78. if args[0] != "--build"
  79. return false
  80. end
  81. if args[2] != "--target"
  82. return false
  83. end
  84. if args[4] != "--name"
  85. return false
  86. end
  87. if args[6] != "--path"
  88. return false
  89. end
  90. return true
  91. end
  92. #------------------------------------------------------------------------------
  93. def parse_command_line(args)
  94. banner = "Usage: crown-android.rb --build <crown-build> --target <android-target> --name <project-name> --path <project-path>\n"
  95. if not validate_command_line(args)
  96. print banner
  97. exit
  98. end
  99. options = OpenStruct.new
  100. OptionParser.new do |opts|
  101. opts.banner = banner
  102. opts.on("-b", "--build BUILD", "Crown build") do |b|
  103. options.build = b
  104. end
  105. opts.on("-t", "--target TARGET", "Android target") do |t|
  106. options.target = t
  107. end
  108. opts.on("-n", "--name NAME", "Android project name") do |n|
  109. options.name = n
  110. end
  111. opts.on("-r", "--res RES", "Android project compiled resources") do |r|
  112. options.res = r
  113. end
  114. opts.on("-p", "--path PATH", "Android project path") do |p|
  115. options.path = p
  116. end
  117. opts.on_tail("-h", "--help", "Show this message") do
  118. puts opts
  119. exit
  120. end
  121. end.parse!(args)
  122. return options
  123. end
  124. #------------------------------------------------------------------------------
  125. def generate_config_h(build, dest)
  126. if build == "debug"
  127. $config_h << "#define CROWN_DEBUG"
  128. elsif build == "development"
  129. $config_h << "#define CROWN_DEVELOPMENT"
  130. elsif build == "release"
  131. $config_h << "#define CROWN_RELEASE"
  132. end
  133. f = File.new(dest, File::WRONLY|File::CREAT|File::TRUNC, 0644)
  134. f.write($config_h)
  135. f.close()
  136. end
  137. #------------------------------------------------------------------------------
  138. def generate_application_mk(target, dest)
  139. f = File.new(dest, File::WRONLY|File::CREAT|File::TRUNC, 0644)
  140. f.write($application_mk)
  141. f.write("APP_APPLICATION := " + target)
  142. f.close()
  143. end
  144. #------------------------------------------------------------------------------
  145. def create_android_project(target, name, path)
  146. engine_dest = path + "/jni"
  147. android_dest = path + "/src/crown/android"
  148. assets_dest = path + "/assets"
  149. # Creates path if not exists
  150. if not Dir.exists?(path)
  151. FileUtils.mkdir_p(path)
  152. end
  153. # Project path is empty
  154. if Dir[path + "/*"].empty?
  155. # Creates android project
  156. system($android_create + " --target " + target + " --name " + name + " --path " + path +
  157. " --activity " + $activity + " --package " + $package)
  158. else
  159. # Updates android project
  160. system($android_update + " --target " + target + " --name " + name + " --path " + path)
  161. end
  162. # if jni dir does not exists, create it!
  163. if not Dir.exists?(engine_dest)
  164. FileUtils.mkdir_p(engine_dest)
  165. print "Created directory " + engine_dest + "\n"
  166. end
  167. # if assets dir does not exists, create it!
  168. if not Dir.exists?(assets_dest)
  169. FileUtils.mkdir_p(assets_dest)
  170. print "Created directory " + assets_dest + "\n"
  171. end
  172. end
  173. #------------------------------------------------------------------------------
  174. def fill_android_project(build, target, res, path)
  175. engine_dest = path + "/jni"
  176. android_dest = path + "/src/crown/android"
  177. resources_dest = path + "/assets"
  178. # Copy Engine files
  179. FileUtils.cp_r($engine_src, engine_dest, :remove_destination => true)
  180. # Generate android Config.h
  181. generate_config_h(build, engine_dest + "/Config.h")
  182. # Generate Application.mk
  183. generate_application_mk(target, engine_dest + "/Application.mk")
  184. # Copy luajit lib
  185. FileUtils.cp($luajit + "/lib/libluajit-5.1.so.2.0.2", engine_dest + "/libluajit-5.1.so")
  186. # Copy oggvorbis lib
  187. FileUtils.cp($oggvorbis + "/lib/libogg.a", engine_dest + "/libogg.a")
  188. FileUtils.cp($oggvorbis + "/lib/libvorbis.a", engine_dest + "/libvorbis.a")
  189. # Copy physx lib
  190. FileUtils.cp($physx + "/lib/libPhysX3.a", engine_dest)
  191. FileUtils.cp($physx + "/lib/libSimulationController.a", engine_dest)
  192. FileUtils.cp($physx + "/lib/libLowLevel.a", engine_dest)
  193. FileUtils.cp($physx + "/lib/libLowLevelCloth.a", engine_dest)
  194. FileUtils.cp($physx + "/lib/libPxTask.a", engine_dest)
  195. FileUtils.cp($physx + "/lib/libPhysXProfileSDK.a", engine_dest)
  196. FileUtils.cp($physx + "/lib/libPhysX3Extensions.a", engine_dest)
  197. FileUtils.cp($physx + "/lib/libSceneQuery.a", engine_dest)
  198. FileUtils.cp($physx + "/lib/libPhysX3Common.a", engine_dest)
  199. FileUtils.cp($physx + "/lib/libPhysX3CharacterKinematic.a", engine_dest)
  200. FileUtils.cp($physx + "/lib/libPhysX3Vehicle.a", engine_dest)
  201. FileUtils.cp($physx + "/lib/libPhysX3Cooking.a", engine_dest)
  202. FileUtils.cp($physx + "/lib/libPvdRuntime.a", engine_dest)
  203. # Copy java files
  204. FileUtils.cp_r(Dir.glob($android_src), android_dest, :remove_destination => true)
  205. # Copy android manifest
  206. FileUtils.cp($android_manifest, path)
  207. end
  208. #------------------------------------------------------------------------------
  209. def build_android_project(path)
  210. # Move to root directory of Android project
  211. Dir.chdir(path)
  212. # Build libraries
  213. if not system("ndk-build")
  214. print "Critical error: Unable to build crown libraries"
  215. return
  216. end
  217. # Build apk
  218. if not system("ant debug")
  219. print "Critical error: Unable to build crown project"
  220. return
  221. end
  222. end
  223. #------------------------------------------------------------------------------
  224. opts = parse_command_line(ARGV)
  225. create_android_project(opts.target, opts.name, opts.path)
  226. fill_android_project(opts.build, opts.target, opts.res, opts.path)
  227. build_android_project(opts.path)