rakefile 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. # Copyright (c) 2008-2022 the Urho3D project
  2. # License: MIT
  3. task default: :build
  4. desc 'Show info for the specified target platform'
  5. task :info, [:kind] => [:init] do |_, args|
  6. case args[:kind]
  7. when 'build_tree'
  8. print build_tree
  9. when 'install_dir'
  10. print install_dir
  11. else
  12. abort %q{Please specify the type of info requested: 'build_tree', 'install_dir'}
  13. end
  14. end
  15. desc 'Invoke CMake to configure and generate a build tree'
  16. task :cmake => [:init] do
  17. if ENV['CI']
  18. system 'cmake --version' or abort 'Failed to find CMake'
  19. if ENV['USE_CCACHE'] && ENV['GITHUB_EVENT_NAME'] != 'repository_dispatch' && /\[cache clear\]/ =~ `git log --format=%B -n1 2>/dev/null`
  20. system 'bash', '-c', 'rm -rf ~/.{ccache,gradle}' or abort 'Failed to clear the build cache'
  21. puts "CMake cache has been cleared"
  22. end
  23. end
  24. next if ENV['PLATFORM'] == 'android' || (Dir.exists?(build_tree) and not ARGV.include?('cmake'))
  25. ['CMAKE_INSTALL_PREFIX', 'URHO3D_HOME'].each { |var|
  26. if ENV[var] == 'system'
  27. ENV.delete(var)
  28. elsif !ENV[var]
  29. ENV[var] = install_dir if var == 'CMAKE_INSTALL_PREFIX' || Dir.exists?(install_dir)
  30. end
  31. }
  32. script = "script/cmake_#{ENV['GENERATOR']}#{ENV['OS'] ? '.bat' : '.sh'}"
  33. build_options = /linux|macOS|win/ =~ ENV['PLATFORM'] ? '' : "-D #{ENV['PLATFORM'].upcase}=1"
  34. File.readlines('script/.build-options').each { |var|
  35. var.chomp!
  36. build_options = "#{build_options} -D #{var}=#{ENV[var]}" if ENV[var]
  37. }
  38. system %Q{#{script} "#{build_tree}" #{build_options}} or abort
  39. end
  40. desc 'Clean the build tree'
  41. task :clean => [:init] do
  42. if ENV['PLATFORM'] == 'android'
  43. Rake::Task[:gradle].invoke('clean')
  44. next
  45. end
  46. system build_target('clean') or abort
  47. end
  48. desc 'Build the software'
  49. task :build, [:target] => [:cmake] do |_, args|
  50. system "ccache -z" if ENV['USE_CCACHE']
  51. if ENV['PLATFORM'] == 'android'
  52. system "echo '!which java';which java" # Where is Java executable
  53. system "echo '!java -version';java -version" # Print Java veriosn
  54. system "echo '!env';env" # Print all environment variables
  55. system "echo '!ls -a $ANDROID_HOME';ls -a $ANDROID_HOME"
  56. system "echo '!ls -a $ANDROID_HOME/platforms';ls -a $ANDROID_HOME/platforms"
  57. system "echo '!ls -a $ANDROID_HOME/ndk';ls -a $ANDROID_HOME/ndk"
  58. system "echo '!ls -a $ANDROID_HOME/build-tools';ls -a $ANDROID_HOME/build-tools"
  59. system "echo '!ls -a $ANDROID_HOME/cmdline-tools';ls -a $ANDROID_HOME/cmdline-tools"
  60. system "echo '!ls -a $ANDROID_HOME/cmake';ls -a $ANDROID_HOME/cmake"
  61. #system "rm -r $ANDROID_HOME/.temp"
  62. #system "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager 'build-tools;30.0.2'"
  63. Rake::Task[:gradle].invoke('build -x test')
  64. system "ccache -s" if ENV['USE_CCACHE']
  65. next
  66. end
  67. filter = ''
  68. case ENV['GENERATOR']
  69. when 'xcode'
  70. concurrent = '' # Assume xcodebuild will do the right things without the '-jobs'
  71. filter = '|xcpretty -c && exit ${PIPESTATUS[0]}' if system('xcpretty -v >/dev/null 2>&1')
  72. when 'vs'
  73. concurrent = '/maxCpuCount'
  74. else
  75. concurrent = "-j #{$max_jobs}"
  76. filter = "2>#{lint_err_file}" if ENV['URHO3D_LINT']
  77. end
  78. system "#{build_target(args[:target])} -- #{concurrent} #{ENV['BUILD_PARAMS']} #{filter}" or abort
  79. system "ccache -s" if ENV['USE_CCACHE']
  80. end
  81. desc 'Test the software'
  82. task :test => [:init] do
  83. if ENV['PLATFORM'] == 'android'
  84. Rake::Task[:gradle].invoke('test')
  85. next
  86. elsif ENV['URHO3D_LINT'] == '1'
  87. Rake::Task[:lint].invoke
  88. next
  89. elsif ENV['URHO3D_STYLE'] == '1'
  90. Rake::Task[:style].invoke
  91. next
  92. end
  93. wrapper = ENV['CI'] && ENV['PLATFORM'] == 'linux' ? 'xvfb-run' : ''
  94. test = /xcode|vs/ =~ ENV['GENERATOR'] ? 'RUN_TESTS' : 'test'
  95. system build_target(test, wrapper) or abort
  96. end
  97. desc 'Generate documentation'
  98. task :doc => [:init] do
  99. if ENV['PLATFORM'] == 'android'
  100. Rake::Task[:gradle].invoke('documentationZip')
  101. next
  102. end
  103. system build_target('doc') or abort
  104. end
  105. desc 'Install the software'
  106. task :install, [:dest_dir] => [:init] do |_, args|
  107. if ENV['PLATFORM'] == 'android'
  108. Rake::Task[:gradle].invoke('publishToMavenLocal')
  109. next
  110. end
  111. wrapper = args[:dest_dir] && !ENV['OS'] ? "DESTDIR=#{verify_path(args[:dest_dir], true)}" : ''
  112. system build_target('install', wrapper) or abort
  113. end
  114. desc 'Package build artifact'
  115. task :package => [:init] do
  116. next if ENV['PLATFORM'] == 'android'
  117. wrapper = /linux|rpi|arm/ =~ ENV['PLATFORM'] && ENV['URHO3D_64BIT'] == '0' ? 'setarch i686' : ''
  118. system build_target('package', wrapper) or abort
  119. end
  120. desc 'Publish build artifact'
  121. task :publish => [:init] do
  122. if ENV['PLATFORM'] == 'android'
  123. Rake::Task[:gradle].invoke("publish #{/refs\/tags\// =~ ENV['GITHUB_REF'] ? 'bintrayUpload' : ''}")
  124. next
  125. end
  126. Rake::Task[$publish_task.to_sym].invoke if $publish_task
  127. end
  128. desc 'Create a new project'
  129. task :new, [:name, :parent_dir, :use_copy] => [:init] do |_, args|
  130. abort 'The "new" task can only be invoked in the Urho3D project root!' unless first_match(/^# (Urho3D)$/, 'README.md')
  131. args.with_defaults(:name => 'UrhoApp', :parent_dir => '~/projects', :use_copy => false)
  132. name = args[:name]
  133. parent_dir = verify_path(args[:parent_dir])
  134. dir = "#{parent_dir}/#{name}"
  135. use_copy = args[:use_copy] || dockerized?
  136. abort "The directory '#{dir}' already exists!" if Dir.exists?(dir)
  137. puts "Creating a new project in #{dir}..."
  138. func = FileUtils.method(use_copy ? :cp_r : :ln_s)
  139. source_tree(name).split("\n").each do |it|
  140. dirname, basename = /\// =~ it.split(/\s*<-\s*/).first ? it.match(/([^\s<]+)\/(.+)/).captures : ['.', it]
  141. FileUtils.mkdir_p("#{dir}/#{dirname}")
  142. if (matched = basename.match(/(?<basename>\S+)\s*<-\s*:(?<symbol>\S+)/))
  143. File.write("#{dir}/#{dirname}/#{matched[:basename]}", method(matched[:symbol].to_sym).call(name))
  144. elsif (matched = basename.match(/(?<basename>\S+)\s*<-\s*(?<dirname>\S+)/))
  145. func.call(verify_path("#{matched[:dirname]}/#{matched[:basename]}"), "#{dir}/#{dirname}")
  146. else
  147. func.call(verify_path(it), "#{dir}/#{dirname}")
  148. end
  149. end
  150. puts "Done!"
  151. end
  152. ### Internal tasks ###
  153. task :check_license do
  154. commit = 0
  155. # Automatically bump copyright when crossing a new year
  156. if /2008-([0-9]{4}) the Urho3D project/.match(File.read('rakefile'))[1].to_i != Time.now.year
  157. system %Q{
  158. git config user.name #{ENV['PUBLISHER_NAME']} && \\
  159. git config user.email #{ENV['PUBLISHER_EMAIL']} && \\
  160. git add #{bump_copyright_year.join ' '} && \\
  161. git commit -qm 'GH Actions: Bump copyright year to #{Time.now.year}.\n[cache clear]'
  162. } or abort "Failed to commit copyright year update"
  163. commit = 1
  164. end
  165. # TODO: Check and merge any new 3rd-party license into 'Source/ThirdParty/LICENSES'
  166. puts "::set-output name=commit::#{commit}"
  167. end
  168. task :ci do
  169. ENV['URHO3D_PCH'] = '0' if ENV['PLATFORM'] == 'linux-gcc' # TODO - PCH causes cache miss on initial build for Linux/GCC, why?
  170. platform_modifier = /(.+?)-(.+)/.match(ENV['PLATFORM'])
  171. if platform_modifier
  172. ENV['PLATFORM'] = platform_modifier[1]
  173. ENV['MODIFIER'] = platform_modifier[2]
  174. end
  175. case ENV['HOST']
  176. when 'linux'
  177. ENV['URHO3D_DEPLOYMENT_TARGET'] = 'generic' if /linux|mingw/ =~ ENV['PLATFORM']
  178. if ENV['MODIFIER'] == 'clang'
  179. ENV['CC'] = 'clang'
  180. ENV['CXX'] = 'clang++'
  181. end
  182. when 'windows'
  183. if ENV['MODIFIER'] == 'gcc'
  184. ENV['URHO3D_DEPLOYMENT_TARGET'] = 'generic'
  185. ENV['GENERATOR'] = 'mingw'
  186. end
  187. else
  188. # Do nothing
  189. end
  190. ENV['BUILD_TREE'] = 'build/ci'
  191. ENV['CMAKE_BUILD_TYPE'] = ENV['BUILD_TYPE'] == 'dbg' ? 'Debug' : 'Release' if /dbg|rel/ =~ ENV['BUILD_TYPE']
  192. case ENV['GRAPHICS_API']
  193. when 'DX11'
  194. ENV['URHO3D_D3D11'] = '1'
  195. when 'DX9'
  196. ENV['URHO3D_OPENGL'] = '0' # Need to make this explicit because 'MINGW' default to use OpenGL otherwise
  197. when 'OpenGL'
  198. ENV['URHO3D_OPENGL'] = '1'
  199. else
  200. # Do nothing
  201. end
  202. case ENV['PLATFORM']
  203. when 'web'
  204. ENV['EMSCRIPTEN_SHARE_DATA'] = '1'
  205. $max_jobs = 1 if ENV['BUILD_TYPE'] == 'dbg'
  206. $publish_task = 'ci_publish_web'
  207. else
  208. # Do nothing
  209. end
  210. ENV['URHO3D_LIB_TYPE'] = ENV['LIB_TYPE'].upcase if /static|shared/ =~ ENV['LIB_TYPE']
  211. ENV['URHO3D_TESTING'] = '1' if /linux|macOS|win/ =~ ENV['PLATFORM']
  212. ENV['URHO3D_LINT'] = '1' if ENV['MODIFIER'] == 'clang-tidy'
  213. ENV['URHO3D_STYLE'] = '1' if ENV['MODIFIER'] == 'clang-format'
  214. # Enable all the bells and whistles
  215. %w[URHO3D_DATABASE_SQLITE URHO3D_EXTRAS].each { |it| ENV[it] = '1' }
  216. end
  217. task :ci_publish_web do
  218. require 'json'
  219. system 'git clone --depth 1 -q https://github.com/urho3d/urho3d.github.io.git build/urho3d.github.io' or abort 'Failed to clone urho3d/urho3d.github.io'
  220. system "rsync -a --delete --exclude tool --exclude *.pak --exclude index.md ../engine_build/bin/ build/urho3d.github.io/samples" or abort 'Failed to rsync Web samples'
  221. Dir.chdir('build/urho3d.github.io/samples') {
  222. next unless system 'git diff --quiet Urho3D.js.data'
  223. uuid = `git diff --color=never --word-diff-regex='\\w+' --word-diff=porcelain Urho3D.js`.split.grep(/^[+-]\w+-/).map { |it| it[0] = ''; it }
  224. system %Q(ruby -i.bak -pe "gsub '#{uuid.last}', '#{uuid.first}'" Urho3D.js) or abort 'Failed to substitute UUID'
  225. if system 'git diff --quiet Urho3D.js'
  226. File.unlink 'Urho3D.js.bak'
  227. Dir['*.js'].each { |file| system %Q(ruby -i -pe "gsub '#{uuid.last}', '#{uuid.first}'" #{file}) }
  228. else
  229. File.rename 'Urho3D.js.bak', 'Urho3D.js'
  230. end
  231. }
  232. web = {'samples' => {}}
  233. Dir.chdir('build/urho3d.github.io/samples') { web['samples']['Native'] = Dir['*.html'].sort }
  234. web['player'] = web['samples']['Native'].pop # Assume the last sample after sorting is the Urho3DPlayer.html
  235. {'AngelScript' => 'Scripts', 'Lua' => 'LuaScripts'}.each { |lang, subdir|
  236. Dir.chdir("bin/Data/#{subdir}") {
  237. script_samples = Dir['[0-9]*'].sort
  238. deleted_samples = [] # Delete samples that do not have their native counterpart
  239. script_samples.each { |sample| deleted_samples.push sample unless web['samples']['Native'].include? "#{sample.split('.').first}.html" }
  240. web['samples'][lang] = (script_samples - deleted_samples).map { |sample| "#{subdir}/#{sample}" }
  241. }
  242. }
  243. File.open('build/urho3d.github.io/_data/web.json', 'w') { |file| file.puts web.to_json }
  244. system %Q{
  245. cd build/urho3d.github.io && \\
  246. git config user.name #{ENV['PUBLISHER_NAME']} && \\
  247. git config user.email #{ENV['PUBLISHER_EMAIL']} && \\
  248. git remote set-url --push origin https://#{ENV['PUBLISHER_TOKEN']}@github.com/urho3d/urho3d.github.io.git && \\
  249. git add -A . && \\
  250. ( git commit -qm "GH Actions: Web samples update at #{Time.now.utc}.\n\nCommit: https://github.com/#{ENV['GITHUB_REPOSITORY']}/commit/#{ENV['GITHUB_SHA']}\n\nMessage: #{`git log --format=%B -n 1`}" || true) && \\
  251. git push -q >/dev/null 2>&1
  252. } or abort 'Failed to update Web samples'
  253. end
  254. task :gradle, [:task] do |_, args|
  255. system "#{ENV['OS'] ? 'gradlew.bat' : './gradlew'} #{args[:task]} #{ENV['CI'] ? '--console plain' : ''}" or abort
  256. end
  257. task :init do
  258. next if $max_jobs
  259. Rake::Task[:ci].invoke if ENV['CI']
  260. case build_host
  261. when /linux/
  262. $max_jobs = `grep -c processor /proc/cpuinfo`.chomp unless $max_jobs
  263. ENV['GENERATOR'] = 'generic' unless ENV['GENERATOR']
  264. unless ENV['PLATFORM']
  265. if /x86/ =~ `uname -m`
  266. ENV['PLATFORM'] = 'linux'
  267. elsif Dir.exists?('/opt/vc')
  268. ENV['PLATFORM'] = 'rpi'
  269. else
  270. ENV['PLATFORM'] = 'arm'
  271. end
  272. end
  273. when /darwin|macOS/
  274. $max_jobs = `sysctl -n hw.logicalcpu`.chomp unless $max_jobs
  275. ENV['GENERATOR'] = 'xcode' unless ENV['GENERATOR']
  276. ENV['PLATFORM'] = 'macOS' unless ENV['PLATFORM']
  277. when /win32|mingw|mswin|windows/
  278. unless $max_jobs
  279. require 'win32ole'
  280. WIN32OLE.connect('winmgmts://').ExecQuery("select NumberOfLogicalProcessors from Win32_ComputerSystem").each { |it|
  281. $max_jobs = it.NumberOfLogicalProcessors
  282. }
  283. end
  284. ENV['GENERATOR'] = 'vs' unless ENV['GENERATOR']
  285. ENV['PLATFORM'] = 'win' unless ENV['PLATFORM']
  286. else
  287. abort "Unsupported host system: #{build_host}"
  288. end
  289. # The 'ARCH' env-var, when set, has higher precedence than the 'URHO3D_64BIT' env-var
  290. ENV['URHO3D_64BIT'] = ENV['ARCH'] == '32' ? '0' : '1' if /32|64/ =~ ENV['ARCH']
  291. end
  292. task :lint do
  293. lint_err = File.read(lint_err_file)
  294. puts lint_err
  295. # TODO: Tighten the check by failing the job later
  296. # abort 'Failed to pass linter checks' unless lint_err.empty?
  297. # puts 'Passed the linter checks'
  298. end
  299. task :style do
  300. system 'bash', '-c', %q{
  301. git diff --name-only HEAD~ -- Source \
  302. |grep -v ThirdParty \
  303. |grep -P '\.(?:c|cpp|h|hpp)' \
  304. |xargs clang-format -n -Werror 2>&1 \
  305. |tee build/clang-format.out \
  306. && exit ${PIPESTATUS[3]}
  307. } or abort 'Failed to pass style checks'
  308. puts 'Passed the style checks'
  309. end
  310. task :source_checksum do
  311. require 'digest'
  312. sha256_final = Digest::SHA256.new
  313. sha256_iter = Digest::SHA256
  314. Dir['Source/**/*.{c,h}*'].each { |it| sha256_final << sha256_iter.file(it).hexdigest }
  315. puts "::set-output name=hexdigest::#{sha256_final.hexdigest}"
  316. end
  317. task :update_dot_files do
  318. system 'bash', '-c', %q{
  319. perl -ne 'undef $/; print $1 if /(Build Option.*?(?=\n\n))/s' Docs/GettingStarted.dox \
  320. |tail -n +3 |cut -d'|' -f2 |tr -d [:blank:] >script/.build-options && \
  321. echo URHO3D_LINT >>script/.build-options && \
  322. cat script/.build-options <(perl -ne 'while (/([A-Z_]+):.+?/g) {print "$1\n"}' .github/workflows/main.yml) \
  323. <(perl -ne 'while (/ENV\[\x27(\w+)\x27\]/g) {print "$1\n"}' rakefile) \
  324. <(perl -ne 'while (/System.getenv\\("(\w+)"\\)/g) {print "$1\n"}' android/urho3d-lib/build.gradle.kts) \
  325. |sort |uniq |grep -Ev '^(HOME|PATH)$' >script/.env-file
  326. } or abort 'Failed to update dot files'
  327. if /schedule|workflow_dispatch/ =~ ENV['GITHUB_EVENT_NAME']
  328. system %Q{
  329. git config user.name #{ENV['PUBLISHER_NAME']} && \\
  330. git config user.email #{ENV['PUBLISHER_EMAIL']} && \\
  331. git add script/.build-options script/.env-file && \\
  332. commit=0 && \\
  333. if git commit -qm 'GH Actions: Update dot files for DBE.'; then commit=1; fi && \\
  334. echo ::set-output name=commit::$commit
  335. } or abort "Failed to commit dot files update"
  336. end
  337. end
  338. ### Internal methods ###
  339. def build_host
  340. ENV['HOST'] || RUBY_PLATFORM
  341. end
  342. def build_tree
  343. ENV['BUILD_TREE'] || "build/#{dockerized? ? 'dockerized-' : ''}#{default_path}"
  344. end
  345. def build_config
  346. /xcode|vs/ =~ ENV['GENERATOR'] ? "--config #{ENV.fetch('CONFIG', 'Release')}" : ''
  347. end
  348. def build_target(tgt, wrapper = '')
  349. %Q{#{wrapper} cmake --build "#{build_tree}" #{build_config} #{tgt ? "--target #{tgt}" : ''}}
  350. end
  351. def bump_copyright_year(regex = '2008-[0-9]{4} the Urho3D project')
  352. begin
  353. copyrighted = `git grep -El '#{regex}'`.split
  354. copyrighted.each { |filename|
  355. replaced_content = File.read(filename).gsub(/#{regex}/, regex.gsub('[0-9]{4}', Time.now.year.to_s))
  356. File.open(filename, 'w') { |file| file.puts replaced_content }
  357. }
  358. return copyrighted
  359. rescue
  360. abort 'Failed to bump copyright year'
  361. end
  362. end
  363. def default_path
  364. "#{ENV['PLATFORM'].downcase}" \
  365. "#{ENV['CC'] ? "-#{ENV['CC']}" : ''}" \
  366. "#{ENV['GENERATOR'] && /generic|xcode|vs/ =~ ENV['GENERATOR'] ? '' : "-#{ENV['GENERATOR']}"}"
  367. end
  368. def dockerized?
  369. File.exists?('/entrypoint.sh')
  370. end
  371. def install_dir
  372. "#{Dir.home}/.urho3d/install/#{default_path}"
  373. end
  374. def lint_err_file
  375. 'build/clang-tidy.out'
  376. end
  377. def verify_path(path, auto_create = false)
  378. require 'pathname'
  379. begin
  380. expanded_path = File.expand_path(path)
  381. FileUtils.mkdir_p(expanded_path) if (auto_create && !Dir.exists?(expanded_path))
  382. Pathname.new(expanded_path).realdirpath.to_s
  383. rescue
  384. abort "The specified path '#{path}' is invalid!"
  385. end
  386. end
  387. def first_match(regex, from)
  388. begin
  389. if from.instance_of?(Array)
  390. array = from
  391. else
  392. array = File.exists?(from) ? File.readlines(from) : from.split("\n")
  393. end
  394. array.grep(regex).first.match(regex).captures.first
  395. rescue
  396. nil
  397. end
  398. end
  399. def source_tree(name)
  400. <<-EOF
  401. bin/CoreData
  402. bin/Data/Materials/Mushroom.xml
  403. bin/Data/Models/Mushroom.mdl
  404. bin/Data/Music/Ninja Gods.ogg
  405. bin/Data/Textures/Mushroom.dds
  406. bin/Data/Textures/UrhoIcon.icns
  407. bin/Data/Textures/UrhoIcon.png
  408. cmake
  409. gradle
  410. script
  411. app/src/main/cpp/#{name}.cpp <- :urho_app_cpp
  412. app/src/main/cpp/#{name}.h <- :urho_app_h
  413. app/src/main/java/io/urho3d/#{name.downcase}/MainActivity.kt <- :main_activity_kt
  414. #{Dir.chdir('android/launcher-app') { Dir['src/main/res/{drawable,mipmap}*'].map { |it| "app/#{it} <- android/launcher-app/src/main/res" }.join("\n") }}
  415. app/src/main/res/values/strings.xml <- :strings_xml
  416. app/src/main/AndroidManifest.xml <- :android_manifest_xml
  417. app/build.gradle.kts <- :app_build_gradle_kts
  418. app/CMakeLists.txt <- :app_cmake_lists_txt
  419. app/proguard-rules.pro <- android/launcher-app
  420. build.gradle.kts <- :root_build_gradle_kts
  421. CMakeLists.txt <- :root_cmake_lists_txt
  422. gradle.properties
  423. gradlew
  424. gradlew.bat
  425. rakefile
  426. settings.gradle.kts <- :settings_gradle_kts
  427. .clang-format
  428. .clang-tidy
  429. .gitattributes <- :gitattributes
  430. .gitignore <- :gitignore
  431. EOF
  432. end
  433. def settings_gradle_kts(name)
  434. <<-EOF
  435. rootProject.name = "#{name}"
  436. include(":app")
  437. EOF
  438. end
  439. def app_build_gradle_kts(name)
  440. template = File.readlines('android/launcher-app/build.gradle.kts')
  441. sdk_version = first_match(/compileSdkVersion\((\d+)\)/, template)
  442. min_sdk_version = first_match(/minSdkVersion\((\d+)\)/, template)
  443. aar_version = ENV['CI'] && ENV['PLATFORM'] != 'android' ? 'unknown' : # Skip using gradle all together when on CI, unless for Android build
  444. first_match(/AAR version: (.+)/, `#{ENV['OS'] ? 'gradlew.bat' : './gradlew'} aarVersion 2>#{ENV['OS'] ? 'null' : '/dev/null'}`)
  445. type = ENV.fetch('URHO3D_LIB_TYPE', 'STATIC').downcase
  446. <<-EOF
  447. plugins {
  448. id("com.android.application")
  449. kotlin("android")
  450. kotlin("android.extensions")
  451. }
  452. val kotlinVersion: String by ext
  453. val ndkSideBySideVersion: String by ext
  454. val cmakeVersion: String by ext
  455. val buildStagingDir: String by ext
  456. android {
  457. ndkVersion = ndkSideBySideVersion
  458. compileSdkVersion(#{sdk_version})
  459. defaultConfig {
  460. minSdkVersion(#{min_sdk_version})
  461. targetSdkVersion(#{sdk_version})
  462. applicationId = "io.urho3d.#{name}"
  463. versionCode = 1
  464. versionName = "1.0"
  465. testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
  466. externalNativeBuild {
  467. cmake {
  468. arguments.apply {
  469. System.getenv("ANDROID_CCACHE")?.let { add("-D ANDROID_CCACHE=$it") }
  470. add("-D JNI_DIR=${project.file(buildStagingDir)}")
  471. // Pass along matching env-vars as CMake build options
  472. addAll(project.file("../script/.build-options")
  473. .readLines()
  474. .mapNotNull { variable -> System.getenv(variable)?.let { "-D $variable=$it" } }
  475. )
  476. }
  477. }
  478. }
  479. splits {
  480. abi {
  481. isEnable = project.hasProperty("ANDROID_ABI")
  482. reset()
  483. include(
  484. *(project.findProperty("ANDROID_ABI") as String? ?: "")
  485. .split(',')
  486. .toTypedArray()
  487. )
  488. }
  489. }
  490. }
  491. buildTypes {
  492. named("release") {
  493. isMinifyEnabled = false
  494. proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
  495. }
  496. }
  497. lintOptions {
  498. isAbortOnError = false
  499. }
  500. externalNativeBuild {
  501. cmake {
  502. version = cmakeVersion
  503. path = project.file("../CMakeLists.txt")
  504. setBuildStagingDirectory(buildStagingDir)
  505. }
  506. }
  507. sourceSets {
  508. named("main") {
  509. assets.srcDir(project.file("../bin"))
  510. }
  511. }
  512. }
  513. val urhoReleaseImpl by configurations.creating { isCanBeResolved = true }
  514. configurations.releaseImplementation.get().extendsFrom(urhoReleaseImpl)
  515. val urhoDebugImpl by configurations.creating { isCanBeResolved = true }
  516. configurations.debugImplementation.get().extendsFrom(urhoDebugImpl)
  517. dependencies {
  518. urhoReleaseImpl("io.urho3d:urho3d-lib-#{type}:#{aar_version}")
  519. urhoDebugImpl("io.urho3d:urho3d-lib-#{type}-debug:#{aar_version}")
  520. implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar", "*.aar"))))
  521. implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion")
  522. implementation("androidx.core:core-ktx:#{first_match(/"androidx.core:core-ktx:(.+)"/, template)}")
  523. implementation("androidx.appcompat:appcompat:#{first_match(/"androidx.appcompat:appcompat:(.+)"/, template)}")
  524. implementation("androidx.constraintlayout:constraintlayout:#{first_match(/"androidx.constraintlayout:constraintlayout:(.+)"/, template)}")
  525. testImplementation("junit:junit:#{first_match(/"junit:junit:(.+)"/, template)}")
  526. androidTestImplementation("androidx.test:runner:#{first_match(/"androidx.test:runner:(.+)"/, template)}")
  527. androidTestImplementation("androidx.test.espresso:espresso-core:#{first_match(/"androidx.test.espresso:espresso-core:(.+)"/, template)}")
  528. }
  529. afterEvaluate {
  530. android.buildTypes.forEach { buildType ->
  531. val config = buildType.name.capitalize()
  532. val unzipTaskName = "unzipJni$config"
  533. tasks {
  534. "generateJsonModel$config" {
  535. dependsOn(unzipTaskName)
  536. }
  537. register<Copy>(unzipTaskName) {
  538. val aar = configurations["urho${config}Impl"].resolve().first { it.name.startsWith("urho3d-lib") }
  539. from(zipTree(aar))
  540. include("urho3d/**")
  541. into(android.externalNativeBuild.cmake.buildStagingDirectory)
  542. }
  543. }
  544. }
  545. }
  546. tasks {
  547. register<Delete>("cleanAll") {
  548. dependsOn("clean")
  549. delete = setOf(android.externalNativeBuild.cmake.buildStagingDirectory)
  550. }
  551. }
  552. EOF
  553. end
  554. def root_build_gradle_kts(_)
  555. template = File.readlines('build.gradle.kts')
  556. <<-EOF
  557. buildscript {
  558. extra["kotlinVersion"] = "#{first_match(/extra\["kotlinVersion"\] = "(.+)"/, template)}"
  559. val kotlinVersion: String by extra
  560. repositories {
  561. google()
  562. jcenter()
  563. }
  564. dependencies {
  565. classpath("com.android.tools.build:gradle:#{first_match(/"com.android.tools.build:gradle:(.+)"/, template)}")
  566. classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
  567. }
  568. }
  569. val kotlinVersion: String by ext
  570. allprojects {
  571. repositories {
  572. google()
  573. jcenter()
  574. // Remove below two repos if you are only using released AAR from JCenter
  575. mavenLocal()
  576. if (System.getenv("GITHUB_ACTOR") != null && System.getenv("GITHUB_TOKEN") != null) {
  577. maven {
  578. name = "GitHubPackages"
  579. url = uri("https://maven.pkg.github.com/urho3d/Urho3D")
  580. credentials {
  581. username = System.getenv("GITHUB_ACTOR")
  582. password = System.getenv("GITHUB_TOKEN")
  583. }
  584. }
  585. }
  586. }
  587. buildscript {
  588. ext {
  589. set("kotlinVersion", kotlinVersion)
  590. set("ndkSideBySideVersion", "#{first_match(/set\("ndkSideBySideVersion", "(.+)"\)/, template)}")
  591. set("cmakeVersion", "#{first_match(/set\("cmakeVersion", "(.+)"\)/, template)}")
  592. set("buildStagingDir", "#{first_match(/set\("buildStagingDir", "(.+)"\)/, template)}")
  593. }
  594. }
  595. }
  596. tasks {
  597. wrapper {
  598. distributionType = Wrapper.DistributionType.ALL
  599. }
  600. "prepareKotlinBuildScriptModel" {
  601. listOf("Debug", "Release").forEach {
  602. dependsOn(":app:unzipJni$it")
  603. }
  604. }
  605. register<Delete>("clean") {
  606. // Clean the build artifacts generated by the Gradle build system only, but keep the buildDir
  607. rootProject.buildDir.listFiles { _, name -> name == "intermediates" || name == "kotlin" }?.let {
  608. delete = it.toSet()
  609. }
  610. }
  611. register<Delete>("cleanAll") {
  612. dependsOn("clean")
  613. }
  614. }
  615. EOF
  616. end
  617. def strings_xml(name)
  618. <<-EOF
  619. <resources>
  620. <string name="app_name">#{name}</string>
  621. </resources>
  622. EOF
  623. end
  624. def android_manifest_xml(name)
  625. <<-EOF
  626. <?xml version="1.0" encoding="utf-8"?>
  627. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  628. package="io.urho3d.#{name.downcase}">
  629. <application
  630. android:allowBackup="true"
  631. android:hardwareAccelerated="true"
  632. android:icon="@mipmap/ic_launcher"
  633. android:label="@string/app_name"
  634. android:roundIcon="@mipmap/ic_launcher_round"
  635. android:supportsRtl="true">
  636. <activity
  637. android:name=".MainActivity"
  638. android:configChanges="keyboardHidden|orientation|screenSize"
  639. android:screenOrientation="landscape"
  640. android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
  641. <intent-filter>
  642. <action android:name="android.intent.action.MAIN"/>
  643. <category android:name="android.intent.category.LAUNCHER"/>
  644. </intent-filter>
  645. </activity>
  646. </application>
  647. </manifest>
  648. EOF
  649. end
  650. def main_activity_kt(name)
  651. <<-EOF
  652. package io.urho3d.#{name.downcase}
  653. import io.urho3d.UrhoActivity
  654. class MainActivity : UrhoActivity()
  655. EOF
  656. end
  657. def app_cmake_lists_txt(name)
  658. <<-EOF
  659. set(TARGET_NAME #{name})
  660. define_source_files(GLOB_CPP_PATTERNS src/main/cpp/*.cpp GLOB_H_PATTERNS src/main/cpp/*.h RECURSE GROUP)
  661. setup_main_executable()
  662. setup_test()
  663. EOF
  664. end
  665. def root_cmake_lists_txt(name)
  666. <<-EOF
  667. cmake_minimum_required(VERSION #{first_match(/cmake_minimum_required\s*\(VERSION (.+)\)/, 'CMakeLists.txt')})
  668. project(#{name})
  669. set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules)
  670. include(UrhoCommon)
  671. add_subdirectory(app)
  672. EOF
  673. end
  674. def urho_app_h(name)
  675. <<-EOF
  676. #pragma once
  677. #include <Urho3D/Urho3DAll.h>
  678. class #{name} : public Application
  679. {
  680. URHO3D_OBJECT(#{name}, Application);
  681. public:
  682. explicit #{name}(Context* context);
  683. void Start() override;
  684. private:
  685. SharedPtr<Scene> scene_;
  686. };
  687. EOF
  688. end
  689. def urho_app_cpp(name)
  690. <<-EOF
  691. #include "#{name}.h"
  692. URHO3D_DEFINE_APPLICATION_MAIN(#{name})
  693. #{name}::#{name}(Context* context) : Application(context) {}
  694. void #{name}::Start()
  695. {
  696. auto* cache = GetSubsystem<ResourceCache>();
  697. auto* graphics = GetSubsystem<Graphics>();
  698. graphics->SetWindowIcon(cache->GetResource<Image>("Textures/UrhoIcon.png"));
  699. graphics->SetWindowTitle("#{name}");
  700. scene_ = new Scene(context_);
  701. scene_->CreateComponent<Octree>();
  702. Node* objectNode = scene_->CreateChild();
  703. auto* object = objectNode->CreateComponent<StaticModel>();
  704. object->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl"));
  705. object->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml"));
  706. auto* sound = scene_->CreateComponent<SoundSource>();
  707. sound->SetSoundType(SOUND_MUSIC);
  708. auto* music = cache->GetResource<Sound>("Music/Ninja Gods.ogg");
  709. music->SetLooped(true);
  710. sound->Play(music);
  711. Node* lightNode = scene_->CreateChild();
  712. auto* light = lightNode->CreateComponent<Light>();
  713. light->SetLightType(LIGHT_DIRECTIONAL);
  714. lightNode->SetDirection(Vector3(0.6f, -1.f, 0.8f));
  715. Node* cameraNode = scene_->CreateChild();
  716. auto* camera = cameraNode->CreateComponent<Camera>();
  717. cameraNode->SetPosition(Vector3(0.f, 0.3f, -3.f));
  718. GetSubsystem<Renderer>()->SetViewport(0, new Viewport(context_, scene_, camera));
  719. SubscribeToEvent(E_KEYUP, [&](StringHash, VariantMap&) { engine_->Exit(); });
  720. SubscribeToEvent(E_UPDATE, [=](StringHash, VariantMap& eventData) {
  721. objectNode->Yaw(eventData[Update::P_TIMESTEP].GetFloat());
  722. });
  723. }
  724. EOF
  725. end
  726. def gitattributes(_)
  727. <<-EOF
  728. *.h linguist-language=C++
  729. EOF
  730. end
  731. def gitignore(_)
  732. <<-EOF
  733. # Code::Blocks project settings
  734. /*.cbp
  735. # Codelite project settings
  736. /*.project
  737. /*.workspace
  738. # Gradle project settings
  739. /local.properties
  740. .gradle/
  741. build/
  742. .cxx/
  743. # JetBrains IDE project settings
  744. /.idea/
  745. /cmake-build-*/
  746. *.iml
  747. # KDevelop project settings
  748. /*.kdev?
  749. # Qt Creator project settings
  750. /CMakeLists.txt.user
  751. # Visual Studio project settings
  752. /CMakeSettings.json
  753. /.vs/
  754. /out/
  755. # Misc.
  756. *~
  757. *.swp
  758. .DS_Store
  759. *.log
  760. *.bak
  761. Thumbs.db
  762. .directory
  763. EOF
  764. end
  765. # Load custom rake scripts
  766. Dir['.rake/*.rake'].each { |r| load r }
  767. # vi: set ts=2 sw=2 expandtab: