crown-android.rb 7.6 KB

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