rakefile 28 KB

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