crown-android.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. $android_create = "android create project"
  28. $android_update = "android update project"
  29. $activity = "CrownActivity"
  30. $package = "crown.android"
  31. $engine_src = "../engine/."
  32. $android_src = "../engine/os/android/java/."
  33. $config_src = "../engine/os/android/Config.h"
  34. $manifest = "../engine/os/android/AndroidManifest.xml"
  35. $luajit = "../engine/third/ARMv7/luajit"
  36. $oggvorbis = "../engine/third/ARMv7/oggvorbis"
  37. #------------------------------------------------------------------------------
  38. def validate_command_line(args)
  39. if args.length != 8
  40. return false
  41. end
  42. if args[0] != "--target"
  43. return false
  44. end
  45. if args[2] != "--name"
  46. return false
  47. end
  48. if args[4] != "--res"
  49. return false
  50. end
  51. if args[6] != "--path"
  52. return false
  53. end
  54. return true
  55. end
  56. #------------------------------------------------------------------------------
  57. def parse_command_line(args)
  58. banner = "Usage: crown-android.rb --target <android-target> --name <project-name> --path <project-path>\n"
  59. if not validate_command_line(args)
  60. print banner
  61. exit
  62. end
  63. options = OpenStruct.new
  64. OptionParser.new do |opts|
  65. opts.banner = banner
  66. opts.on("-t", "--target TARGET", "Android target") do |t|
  67. options.target = t
  68. end
  69. opts.on("-n", "--name NAME", "Android project name") do |n|
  70. options.name = n
  71. end
  72. opts.on("-r", "--res RES", "Android project compiled resources") do |r|
  73. options.res = r
  74. end
  75. opts.on("-p", "--path PATH", "Android project path") do |p|
  76. options.path = p
  77. end
  78. opts.on_tail("-h", "--help", "Show this message") do
  79. puts opts
  80. exit
  81. end
  82. end.parse!(args)
  83. return options
  84. end
  85. #------------------------------------------------------------------------------
  86. def create_android_project(target, name, path)
  87. engine_dest = path + "/jni"
  88. android_dest = path + "/src/crown/android"
  89. assets_dest = path + "/assets"
  90. # Creates path if not exists
  91. if not Dir.exists?(path)
  92. FileUtils.mkdir_p(path)
  93. end
  94. # Project path is empty
  95. if Dir[path + "/*"].empty?
  96. # Creates android project
  97. system($android_create + " --target " + target + " --name " + name + " --path " + path +
  98. " --activity " + $activity + " --package " + $package)
  99. else
  100. # Updates android project
  101. system($android_update + " --target " + target + " --name " + name + " --path " + path)
  102. end
  103. # if jni dir does not exists, create it!
  104. if not Dir.exists?(engine_dest)
  105. FileUtils.mkdir_p(engine_dest)
  106. print "Created directory " + engine_dest + "\n"
  107. end
  108. # if assets dir does not exists, create it!
  109. if not Dir.exists?(assets_dest)
  110. FileUtils.mkdir_p(assets_dest)
  111. print "Created directory " + assets_dest + "\n"
  112. end
  113. end
  114. #------------------------------------------------------------------------------
  115. def fill_android_project(res, path)
  116. engine_dest = path + "/jni"
  117. android_dest = path + "/src/crown/android"
  118. resources_dest = path + "/assets"
  119. # Copy Engine files
  120. FileUtils.cp_r($engine_src, engine_dest, :remove_destination => true)
  121. print "Copied Engine to " + engine_dest + "\n"
  122. # Copy android Config.h
  123. FileUtils.cp($config_src, engine_dest)
  124. print "Copied Config.h to " + engine_dest + "\n"
  125. # Copy luajit lib
  126. FileUtils.cp($luajit + "/lib/libluajit-5.1.so.2.0.2", engine_dest + "/libluajit-5.1.so")
  127. print "Copied luajit lib to " + engine_dest + "\n"
  128. # Copy oggvorbis lib
  129. FileUtils.cp($oggvorbis + "/lib/libogg.a", engine_dest + "/libogg.a")
  130. FileUtils.cp($oggvorbis + "/lib/libvorbis.a", engine_dest + "/libvorbis.a")
  131. print "Copied oggvorbis libs to " + engine_dest + "\n"
  132. # Copy Java files
  133. FileUtils.cp_r(Dir.glob($android_src), android_dest, :remove_destination => true)
  134. print "Copied Java files to " + android_dest + "\n"
  135. # Copy Android Manifest
  136. FileUtils.cp($manifest, path)
  137. print "Copied Android Manifest to " + path + "\n"
  138. #Copy resources
  139. FileUtils.cp_r(res + "/.", resources_dest, :remove_destination => true)
  140. print "Resources copied to " + resources_dest + "\n"
  141. end
  142. #------------------------------------------------------------------------------
  143. def build_android_project(path)
  144. # Move to root directory of Android project
  145. Dir.chdir(path)
  146. # Build libraries
  147. if not system("ndk-build")
  148. print "Critical error: Unable to build crown libraries"
  149. return
  150. end
  151. # Build apk
  152. if not system("ant debug")
  153. print "Critical error: Unable to build crown project"
  154. return
  155. end
  156. end
  157. #------------------------------------------------------------------------------
  158. opts = parse_command_line(ARGV)
  159. create_android_project(opts.target, opts.name, opts.path)
  160. fill_android_project(opts.res, opts.path)
  161. build_android_project(opts.path)