-
Notifications
You must be signed in to change notification settings - Fork 18
/
Rakefile
466 lines (424 loc) · 19.2 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
require_relative 'parallel_builder'
COCOS2D_PATH = File.expand_path('ext/cocos2d-x')
XCODE_PATH = '/Applications/Xcode.app'
XCODE_IOS_SDK = '9.1'
XCODE_TVOS_SDK = '9.0'
XCODE_IOS_DEPLOYMENT_TARGET = '7.0'
XCODE_TVOS_DEPLOYMENT_TARGET = '9.0'
ANDROID_NDK_PATH = File.expand_path(ENV['RUBYMOTION_ANDROID_NDK'] || '~/.rubymotion-android/ndk')
ANDROID_SDK_PATH = File.expand_path(ENV['RUBYMOTION_ANDROID_SDK'] || '~/.rubymotion-android/sdk')
ANDROID_API = '16'
# if you need debug build, run `DEBUG=true rake build:all`.
EXTRA_CFLAGS = !!ENV['DEBUG'] ? '-g3' : ''
OPTZ_LEVEL = !!ENV['DEBUG'] ? '0' : '3'
BUILD_OPTIONS = {}
begin
# iOS
toolchain_bin = XCODE_PATH + '/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin'
cc = toolchain_bin + '/clang'
cxx = toolchain_bin + '/clang++'
cflags_gen = lambda do |platform_apple_str|
sdk_path = "#{XCODE_PATH}/Contents/Developer/Platforms/#{platform_apple_str}.platform/Developer/SDKs/#{platform_apple_str}.sdk"
cflags = "-isysroot \"#{sdk_path}\" -F#{sdk_path}/System/Library/Frameworks -fobjc-legacy-dispatch -fobjc-abi-version=2 "
case platform_apple_str
when 'iPhoneSimulator'
cflags << " -arch i386 -arch x86_64 #{EXTRA_CFLAGS} -O#{OPTZ_LEVEL} -mios-simulator-version-min=#{XCODE_IOS_DEPLOYMENT_TARGET} -DCC_TARGET_OS_IPHONE=1 -include platform/ios/cocos2d-prefix.pch"
when 'iPhoneOS'
cflags << " -arch armv7 -arch arm64 #{EXTRA_CFLAGS} -O#{OPTZ_LEVEL} -mios-version-min=#{XCODE_IOS_DEPLOYMENT_TARGET} -DCC_TARGET_OS_IPHONE=1 -include platform/ios/cocos2d-prefix.pch"
when 'AppleTVSimulator'
cflags << " -arch i386 -arch x86_64 #{EXTRA_CFLAGS} -O#{OPTZ_LEVEL} -mtvos-simulator-version-min=#{XCODE_TVOS_DEPLOYMENT_TARGET} -DCC_TARGET_OS_APPLETV=1 -include platform/tvos/cocos2d-prefix.pch"
when 'AppleTVOS'
cflags << " -arch arm64 -fembed-bitcode #{EXTRA_CFLAGS} -O#{OPTZ_LEVEL} -mtvos-version-min=#{XCODE_TVOS_DEPLOYMENT_TARGET} -DCC_TARGET_OS_APPLETV=1 -include platform/tvos/cocos2d-prefix.pch"
end
cflags
end
%w{iPhoneSimulator iPhoneOS AppleTVSimulator AppleTVOS}.each do |platform|
cflags = cflags_gen.call(platform)
BUILD_OPTIONS[platform] = { :cc => cc, :cxx => cxx, :cflags => cflags, :cxxflags => cflags + " -std=c++11 -Wno-shift-negative-value -Wno-inconsistent-missing-override" }
end
end
begin
toolchain_bin = File.join(ANDROID_NDK_PATH, 'toolchains/llvm/prebuilt/darwin-x86_64/bin')
# Android ARM
cflags = "-no-canonical-prefixes -target armv7a-none-linux-androideabi -march=armv7a -mfpu=vfpv3-d16 -marm #{EXTRA_CFLAGS} -gcc-toolchain \"#{ANDROID_NDK_PATH}/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64\" -mtune=xscale -MMD -MP -fpic -ffunction-sections -funwind-tables -fexceptions -fstack-protector -fno-strict-aliasing -fno-omit-frame-pointer -DANDROID -I\"#{ANDROID_NDK_PATH}/platforms/android-#{ANDROID_API}/arch-arm/usr/include\" -Wno-shift-negative-value -Wno-inconsistent-missing-override -Wno-macro-redefined -Wformat -Werror=format-security -DCC_TARGET_OS_ANDROID=1"
BUILD_OPTIONS['android-arm'] = { :cc => File.join(toolchain_bin, 'clang'), :cxx => File.join(toolchain_bin, 'clang++'), :cflags => cflags, :cxxflags => cflags + " -std=c++11 -I\"#{ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/include\" -I\"#{ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/include\" -I\"#{ANDROID_NDK_PATH}/sources/android/cpufeatures\"" }
# Android Intel
cflags = "-mno-sse -mno-mmx -no-canonical-prefixes -msoft-float -target i686-none-linux-android #{EXTRA_CFLAGS} -gcc-toolchain \"#{ANDROID_NDK_PATH}/toolchains/x86-4.9/prebuilt/darwin-x86_64\" -MMD -MP -fpic -ffunction-sections -funwind-tables -fexceptions -fstack-protector -fno-strict-aliasing -O0 -fno-omit-frame-pointer -DANDROID -I\"#{ANDROID_NDK_PATH}/platforms/android-#{ANDROID_API}/arch-x86/usr/include\" -Wno-shift-negative-value -Wno-inconsistent-missing-override -Wno-macro-redefined -Wformat -Werror=format-security -DCC_TARGET_OS_ANDROID=1"
BUILD_OPTIONS['android-x86'] = { :cc => File.join(toolchain_bin, 'clang'), :cxx => File.join(toolchain_bin, 'clang++'), :cflags => cflags, :cxxflags => cflags + " -std=c++11 -I\"#{ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/include\" -I\"#{ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/x86/include\" -I\"#{ANDROID_NDK_PATH}/sources/android/cpufeatures\"" }
end
def build_project(platforms, platform_code, build_dir)
build_dir = File.expand_path(build_dir)
objs_dir = File.join(build_dir, 'objs')
compile_obj = lambda do |src_path, add_flags, platform|
obj_path = File.join(objs_dir, platform, src_path + '.o')
obj_dirname = File.dirname(obj_path)
mkdir_p obj_dirname unless File.exist?(obj_dirname)
if !File.exist?(obj_path) or File.mtime(src_path) > File.mtime(obj_path)
platform_options = BUILD_OPTIONS[platform]
cc = platform_options[:cc]
cxx = platform_options[:cxx]
cflags = platform_options[:cflags]
cxxflags = platform_options[:cxxflags]
case File.extname(src_path)
when '.cpp', '.mm', '.cc'
sh "#{cxx} #{cxxflags} #{add_flags} -c #{src_path} -o \"#{obj_path}\""
when '.c', '.m'
sh "#{cc} #{cflags} #{add_flags} -c #{src_path} -o \"#{obj_path}\""
end
end
obj_path
end
objs = []
platforms.each do |platform|
file_pattern = platform_code == 'android' ? '*.{c,cc,cpp}' : '*.{c,cc,cpp,m,mm}'
cocos_platforms_pattern = case platform_code
when 'android'
'android'
when 'ios'
'apple,ios'
when 'tvos'
'apple,tvos'
end
i_platform_code = platform_code
i_platform_code = 'ios' if platform_code == 'tvos'
Dir.chdir(File.join(COCOS2D_PATH, 'cocos')) do
add_flags = "-I. -I.. -Ieditor-support -Iplatform -Ideprecated -I2d -Iui/UIEditBox -I../extensions -I../external -I../external/edtaa3func -I../external/tinyxml2 -I../external/ConvertUTF -I../external/unzip -I../external/curl/include/#{i_platform_code} -I../external/websockets/include/#{i_platform_code} -I../external/chipmunk/include/chipmunk -I../external/xxhash -I../external/png/include/#{i_platform_code} -I../external/tiff/include/#{i_platform_code} -I../external/jpeg/include/#{i_platform_code} -I../external/webp/include/#{i_platform_code} -I../external/freetype2/include/#{i_platform_code} -I../external/freetype2/include/#{i_platform_code}/freetype2"
if platform_code == 'android'
add_flags << " -I../external/freetype2/include/android/freetype2"
add_flags << " -Iplatform/android"
add_flags << " -Iaudio/include -Iaudio/android"
end
pats = %w{2d 3d base editor-support math navmesh network physics physics3d renderer storage ui deprecated}.map { |x| "#{x}/**/#{file_pattern}" }
pats << "#{file_pattern}"
pats << "audio/#{file_pattern}"
pats << "audio/{#{cocos_platforms_pattern}}/**/#{file_pattern}"
pats << "audio/ios/**/#{file_pattern}" if platform_code == 'tvos'
pats << "platform/#{file_pattern}"
pats << "platform/{#{cocos_platforms_pattern}}/**/#{file_pattern}"
files = pats.map { |x| Dir.glob(x) }.flatten.uniq
files.delete_if do |src_path|
skip = false
case platform_code
when 'ios', 'tvos'
(skip = true) if (src_path =~ /.*-android.cpp$/)
(skip = true) if (src_path =~ /.*-win32.cpp$/)
(skip = true) if (src_path =~ /.*-winrt.cpp$/)
(skip = true) if (src_path =~ /.*-linux.cpp$/)
(skip = true) if (src_path == 'ui/UIWebView.cpp')
(skip = true) if (src_path == 'network/WebSocket.cpp')
(skip = true) if (src_path == 'network/SocketIO.cpp')
(skip = true) if (src_path == 'network/HttpClient.cpp')
(skip = true) if (src_path == 'platform/CCThread.cpp' && platform_code == 'tvos')
(skip = true) if (src_path.include?('ui/UIEditBox/iOS') && platform_code == 'tvos')
when 'android'
end
skip
end
parallel = ParallelBuilder.new(compile_obj, platform, add_flags)
parallel.files = files
parallel.run
objs += parallel.objects
objs.flatten!
end
['external/bullet', 'external/xxhash', 'external/ConvertUTF', 'external/tinyxml2', 'external/unzip', 'external/edtaa3func', 'extensions/GUI/CCControlExtension', 'extensions/GUI/CCScrollView', 'external/clipper', 'external/poly2tri', 'extensions/Particle3D', 'external/recast'].each do |dir|
Dir.chdir(File.join(COCOS2D_PATH, dir)) do
add_flags = '-DUSE_FILE32API -I.'
base_headers = '-I' + (0...dir.split('/').size).to_a.map { '..' }.join('/')
add_flags << ' ' + base_headers
add_flags << ' ' + base_headers + '/cocos'
add_flags << ' ' + base_headers + '/cocos/platform'
add_flags << ' -I../../'
add_flags << ' -I../'
files = Dir.glob(File.join('**', file_pattern))
files.delete_if do |src_path|
skip = false
(skip = true) if src_path.include?('DX11')
(skip = true) if src_path.include?('OpenCL')
skip
end
parallel = ParallelBuilder.new(compile_obj, platform, add_flags)
parallel.files = files
parallel.run
objs += parallel.objects
objs.flatten!
end
end
Dir.chdir('src') do
add_flags = "-I. -Werror -I\"#{COCOS2D_PATH}/cocos\" -I\"#{COCOS2D_PATH}/cocos/audio/include\""
files = Dir.glob(file_pattern)
parallel = ParallelBuilder.new(compile_obj, platform, add_flags)
parallel.files = files
parallel.run
objs += parallel.objects
objs.flatten!
end
next unless platform_code == 'android'
toolchain_bin = "#{ANDROID_NDK_PATH}/toolchains/x86-4.9/prebuilt/darwin-x86_64/i686-linux-android/bin"
ar = toolchain_bin + "/ar"
ranlib = toolchain_bin + "/ranlib"
lib_dir = File.join(build_dir,
case platform
when 'android-x86'
'x86'
when 'android-arm'
'armeabi'
end)
lib = File.join(lib_dir, 'libmotion-cocos.a')
if !File.exist?(lib) or objs.any? { |x| File.mtime(x) > File.mtime(lib) }
rm_rf lib
mkdir_p lib_dir
sh "#{ar} rcu \"#{lib}\" #{objs.map{|x| "\"#{x}\""}.join(' ')}"
sh "#{ranlib} \"#{lib}\""
end
objs.clear
end
if platform_code != 'android'
ar = '/usr/bin/ar'
ranlib = '/usr/bin/ranlib'
lib_dir = build_dir
lib = File.join(lib_dir, 'libmotion-cocos.a')
if !File.exist?(lib) or objs.any? { |x| File.mtime(x) > File.mtime(lib) }
rm_rf lib
mkdir_p lib_dir
sh "#{ar} rcu \"#{lib}\" #{objs.map{|x| "\"#{x}\""}.join(' ')}"
sh "#{ranlib} \"#{lib}\""
end
end
prebuild_platforms = []
if platform_code == 'android'
prebuild_platforms += ['android/armeabi', 'android/x86']
else
prebuild_platforms += [platform_code]
end
prebuild_platforms.each do |prebuild_platform|
Dir.glob(File.join(COCOS2D_PATH, "external/**/prebuilt/#{prebuild_platform}/*.a")).each do |lib|
next if lib.include?('lua')
next if lib.include?('js_static')
next if lib.include?('websocket')
prebuild_build_dir = File.join(build_dir, prebuild_platform.include?('/') ? File.basename(prebuild_platform) : '')
lib_dest = File.join(prebuild_build_dir, File.basename(lib))
if !File.exist?(lib_dest) or File.mtime(lib) > File.mtime(lib_dest)
mkdir_p File.dirname(lib_dest)
install lib, lib_dest
end
end
end
if platform_code == 'android'
classes_dir = File.join(build_dir, 'classes')
Dir.chdir(File.join(COCOS2D_PATH, 'cocos/platform/android/java')) do
build_tools_dir = Dir.glob(ANDROID_SDK_PATH + '/build-tools/*').sort { |x, y| File.basename(x) <=> File.basename(y) }.max
aidl_tool = File.join(build_tools_dir, 'aidl')
aidl_files = Dir.glob('*/**/*.aidl')
aidl_files.each do |aidl_file|
sh "#{aidl_tool} #{aidl_file} #{File.dirname(aidl_file)}/#{File.basename(aidl_file, '.*')}.java"
end
java_src_files = Dir.glob('*/**/*.java')
jar_files = Dir.glob('*/**/*.jar')
android_jar = "#{ANDROID_SDK_PATH}/platforms/android-#{ANDROID_API}/android.jar"
mkdir_p classes_dir unless File.exist? classes_dir
sh "/usr/bin/javac -cp \"#{jar_files.join(';')}\" -d \"#{classes_dir}\" -bootclasspath \"#{android_jar}\" #{java_src_files.join(' ')}"
end
Dir.chdir(classes_dir) do
sh "/usr/bin/jar cvf ../motion-cocos.jar ."
end
end
end
namespace 'build' do
desc 'Setup development environment'
task 'setup' do
sh "git submodule update --init"
unless File.exist?("v3-deps-78.zip")
# Download .zip file from https://github.com/HipByte/cocos2d-x-3rd-party-libs-bin/releases/tag/RubyMotion-v3-deps-78
sh "curl 'https://dl.dropboxusercontent.com/u/15539999/motion-game/v3-deps-78.zip' -o v3-deps-78.zip"
end
sh "python ext/cocos2d-x/download-deps.py --remove-download no"
end
desc 'Build for Android'
task 'android' do
build_project(['android-arm', 'android-x86'], 'android', 'build/android')
end
desc 'Build for iOS simulator and device'
task 'ios' do
build_project(['iPhoneSimulator', 'iPhoneOS'], 'ios', 'build/ios')
end
desc 'Build for tvOS simulator and device'
task 'tvos' do
build_project(['AppleTVSimulator', 'AppleTVOS'], 'tvos', 'build/tvos')
end
desc 'Build everything'
task 'all' => ['build:ios', 'build:tvos', 'build:android']
end
desc 'Generate lib/motion-game/shortcuts.rb'
task 'gen_tasks_shortcuts' do
File.open('lib/motion-game/shortcuts.rb', 'w') do |io|
io.puts "# This file has been generated, do not edit by hand.\n"
io.puts "def invoke_rake(platform, task)"
io.puts " trace = Rake.application.options.trace == true"
io.puts " system \"/usr/bin/rake -r \\\"#\{File.dirname(__FILE__)\}/#\{platform\}.rb\\\" -f \\\"config/#\{platform\}.rb\\\" \\\"#\{task\}\\\" #\{trace ? '--trace' : ''\}\" or exit 1"
io.puts "end"
%w{ios tvos android}.each do |platform|
io.puts "namespace '#{platform}' do"
`/usr/bin/rake -I /Library/RubyMotion/lib -f /Library/RubyMotion/lib/motion/project/template/#{platform}.rb -T`.scan(/rake\s([^\s]+)\s+#\s([^\n]+)/).each do |plat_task, plat_desc|
io.puts " desc \"#{plat_desc}\""
io.puts " task \"#{plat_task}\" do"
io.puts " invoke_rake '#{platform}', '#{plat_task}'"
io.puts " end"
end
io.puts "end"
end
end
end
task 'archive' do
archive_dir = '/tmp/motion-game'
rm_rf archive_dir
mkdir_p archive_dir
files = []
files += Dir.glob('build/{ios,android}/**/*.{a,jar}')
files += Dir.glob('lib/**/*.rb')
files += Dir.glob('doc/**/*').reject { |x| File.directory?(x) }
files += Dir.glob('samples/**/*').reject { |x| x.include?('build') or File.directory?(x) }
files.each do |path|
dest = File.join(archive_dir, File.dirname(path))
mkdir_p dest
install path, dest
end
Dir.chdir(File.dirname(archive_dir)) do
rm_rf 'motion-game.zip'
sh "/usr/bin/zip -r motion-game.zip #{File.basename(archive_dir)}"
end
mv File.join(File.dirname(archive_dir), 'motion-game.zip'), '.'
end
class DocAPIGen
def initialize
@classes = []
end
def parse(path)
txt = File.read(path)
current_class = nil
current_node = nil
txt.lines.select { |x| x.start_with?('/// ') }.each do |line|
line = line[4..-1].rstrip
if md = line.match(/@class\s+(.+)/)
current_class = current_node = add_class(md[1])
elsif md = line.match(/@constant\s+(.+)/)
current_node = add_constant(current_class, md[1])
elsif md = line.match(/@method\s+(.+)/)
current_node = add_method(current_class, md[1])
elsif md = line.match(/@property\s+(.+)/)
current_node = add_property(current_class, md[1])
elsif md = line.match(/@property-readonly\s+(.+)/)
current_node = add_property(current_class, md[1], :reader)
elsif md = line.match(/@property-writeonly\s+(.+)/)
current_node = add_property(current_class, md[1], :writer)
elsif md = line.match(/@group\s+(.+)/)
current_node = add_group(current_class, md[1])
elsif md = line.match(/@endgroup/)
current_node = add_endgroup(current_class)
else
current_node[:doc] << line << "\n"
end
end
end
def to_ruby
require 'stringio'
io = StringIO.new
io.puts "# MG stands for Motion Game. All classes part of the framework are defined under that module."
io.puts "module MG"
io.puts "# The MG::Events module contains classes that represent events received from the game engine. You typically never instantiate these classes yourself."
io.puts "module Events; end"
@classes.each do |klass|
io.puts doc_comment(klass, 0)
io.puts "class #{klass[:def]}"
klass[:nodes].each do |node|
io.puts doc_comment(node, 2)
case node[:type]
when :constant
io.puts " #{node[:sel]}"
when :property
io.puts " attr_#{node[:mode]} :#{node[:sel]}"
when :cmethod
io.puts " def self.#{node[:sel]}; end"
when :imethod
io.puts " def #{node[:sel]}; end"
when :group
io.puts " # @group #{node[:name]}"
when :endgroup
io.puts " # @endgroup"
end
io.puts
end
io.puts "end"
end
io.puts "end"
io
end
private
def add_class(definition)
klass = { :def => definition, :doc => '', :nodes => [] }
@classes << klass
klass
end
def add_constant(klass, definition)
const = { :type => :constant, :sel => definition, :doc =>'' }
klass[:nodes] << const
const
end
def add_method(klass, definition)
method = { :sel => definition[1..-1], :doc => '' }
type = case definition[0]
when '.'
:cmethod
when '#'
:imethod
else
raise "expected method definition `#{definition}' to start with '.' or '#'"
end
method[:type] = type
klass[:nodes] << method
method
end
def add_property(klass, definition, mode = :accessor)
if definition[0] != '#'
raise "expected property definition `#{definition}' to start with '#'"
end
property = { :type => :property, :sel => definition[1..-1], :mode => mode, :doc => '' }
klass[:nodes] << property
property
end
def add_group(klass, definition)
group = { :type => :group, :name => definition, :doc => '' }
klass[:nodes] << group
group
end
def add_endgroup(klass)
group = { :type => :endgroup, :doc => '' }
klass[:nodes] << group
group
end
def doc_comment(node, level)
node[:doc].lines.map { |x| (' ' * level) + "# " + x }.join
end
end
desc 'Generate API reference'
task 'doc' do
# Generate documentation in 'doc' directory.
api_gen = DocAPIGen.new
Dir.glob('src/*.cpp').each { |path| api_gen.parse(path) }
rm_rf 'doc'
mkdir_p 'doc'
File.open('doc/API_reference.rb', 'w') { |io|
io.puts "# CAUTION :"
io.puts "# This file is automatically generated to creating documents."
io.puts "# Please not use this file for your app."
io.puts "#--------------------------------------------------------------"
io.puts ""
io.write(api_gen.to_ruby.string)
}
sh "yard doc -o ./doc doc/API_reference.rb"
end
desc 'Create motion-game.gem file'
task 'gem' do
Rake::Task[:"build:all"].invoke
Rake::Task[:doc].invoke
sh "gem build motion-game.gemspec"
end