forked from mobiruby/BridgeSupport
-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.rb
201 lines (171 loc) · 5.91 KB
/
build.rb
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
version = RUBY_VERSION.sub(/^(\d+\.\d+)(\..*)?$$/, "\\1")
$:.unshift("./DSTROOT/System/Library/BridgeSupport/ruby-#{version}")
require_relative 'gen_bridge_metadata'
require 'pathname'
require 'fileutils'
require 'digest/sha1'
require 'stringio'
require 'thread'
include FileUtils
SLF = '/System/Library/Frameworks'
SLPF = '/System/Library/PrivateFrameworks'
STDOUT.sync = true
WORKNARGS = 5
dstroot = (ENV['DSTROOT'] or 'DSTROOT')
HOST_VERSION = `/usr/bin/sw_vers -productVersion`.strip
SDK_VERSION = HOST_VERSION.match(/(\d+\.\d+)/)[0]
def measure(something)
elapsed = Time.now
yield
$stderr.puts " #{something} (#{Time.now - elapsed} seconds)"
end
def work(fname, path, is_private, out_dir, out_file)
is_private = (is_private == 'true')
# We have work!
$stderr.puts "Generating BridgeSupport metadata for: #{fname} ..."
elapsed = Time.now
# Create a new generator object, configure it accordingly for a first 32-bit pass.
gen = BridgeSupportGenerator.new
gen.frameworks << path
gen.emulate_ppc = false # PPC is not supported anymore since SnowLeopard
# exceptions = "exceptions-#{RUBY_PLATFORM}/#{fname}.xml"
# if File.exist?(exceptions)
# gen.exception_paths << exceptions
# else
# exceptions = "exceptions/#{fname}.xml"
# if File.exist?(exceptions)
# gen.exception_paths << exceptions
# end
# end
a = SDK_VERSION.scan(/(\d+)\.(\d+)/)[0]
major = a[0].to_i
minor = a[1].to_i
if major <= 10 && minor <= 9
sdk_version_headers = "#{major}#{minor}0"
else
sdk_version_headers = "#{major}#{minor}00"
end
gen.private = true if is_private
gen.compiler_flags = "-I. -isysroot / -mmacosx-version-min=#{SDK_VERSION} -DTARGET_OS_MAC=1 -D__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=#{sdk_version_headers} -framework #{fname}"
measure('Parse 32 and 64-bit') { gen.parse(true, true) }
measure('Write final metadata') do
mkdir_p(out_dir)
gen.out_file = out_file
gen.write
end
# Validate.
measure('Validate XML') do
unless system("xmllint --dtdvalid ./BridgeSupport.dtd --noout #{out_file}")
$stderr.puts "Error: `#{out_file}' doesn't validate against BridgeSupport.dtd"
exit 1
end
end
# Generate inline dynamic library if required.
if gen.has_inline_functions?
measure('Generate dylib file') do
gen.generate_format = BridgeSupportGenerator::FORMAT_DYLIB
gen.out_file = "#{out_dir}/#{fname}.dylib"
gen.write
end
end
gen.cleanup
$stderr.puts "Done (#{Time.now - elapsed} seconds)."
end
if ARGV.length == (WORKNARGS + 1) && ARGV[0] == '-'
work(*ARGV[1..-1])
exit
end
puts "Environment is: #{ENV.inspect}"
frameworks = {}
Dir.glob(File.join(SLF, '**', '*.framework')).each do |path|
name = File.basename(path, '.framework')
# Ignore frameworks that do not respect basic layout.
if File.exist?(File.join(path, name)) and File.exist?(File.join(path, 'Headers'))
frameworks[name] = path
end
end
# The generator currently fails on these frameworks:
#
# IOKit - architecture-dependent headers (like arm) fail to parse
# MacRuby - requires -fobjc-gc-only and -lauto
# QuickTime - deprecated and 32-bit only
# vecLib - clang-110 abort when objc type encoding of vector types
ignore_list = %w{IOKit vecLib}
ignore_list.each { |x| frameworks.delete(x) }
also_gen_private_metadata = []
# If '--update-exceptions' is passed to the script, do a pass on all
# exception files and generate new templates.
if ARGV.include?('--update-exceptions')
$stderr.puts "Updating exceptions..."
frameworks.sort.each do |fname, path|
exception = "exceptions/#{fname}.xml"
next unless File.exist?(exception)
out_file = exception + '.new'
next if File.exist?(out_file)
$stderr.puts "Updating #{exception}"
gen = BridgeSupportGenerator.new
gen.frameworks << path
gen.exception_paths << exception
gen.generate_format = BridgeSupportGenerator::FORMAT_TEMPLATE
gen.private = path.include?(SLPF)
gen.out_file = out_file
begin
gen.parse(true, true)
gen.write
system("xmllint -format #{out_file} > #{exception}")
rm out_file
rescue
end
end
$stderr.puts "Done."
exit 0
end
frameworks.delete_if { |fname, path| !ARGV.include?(fname) } unless ARGV.empty?
$workq = []
frameworks.sort { |ary, ary2|
# Sort the frameworks by dependency order, using a naive-but-working algorithm.
deps = [ary, ary2].map { |a|
BridgeSupportGenerator.dependencies_of_framework(a[1]).map { |fpath|
BridgeSupportGenerator.dependencies_of_framework(fpath)
}.flatten
}
len1 = deps[0].reject { |x| deps[1].include?(x) }.length
len2 = deps[1].reject { |x| deps[0].include?(x) }.length
len1 <=> len2
}.each do |fname, path|
dir = Pathname.new("#{path}/Versions/Current/Resources").realpath.to_s + '/BridgeSupport'
is_private = also_gen_private_metadata.include?(fname)
file = "#{dir}/#{fname}#{is_private ? 'Private' : ''}.bridgesupport"
# Check if the bridge support file isn't already in the DSTROOT.
out_dir = File.join(dstroot, dir)
out_file = File.join(dstroot, file)
next if File.exist?(out_file) and File.size(out_file) > 0
$workq << [fname, path, is_private, out_dir, out_file]
end
$workmutex = Mutex.new
$stdoutmutex = Mutex.new
def run
work = ''
loop do
$workmutex.synchronize { work = $workq.shift }
break if work.nil?
StringIO.open do |sio|
IO.popen( "ruby #{$0} - '#{work.join("' '")}' 2>&1", 'r' ) do |io|
begin
io.each { |line| sio << line }
rescue Errno::EIO
end
end
#check exit status
$stderr.puts "\e[1;31m" + "![ERROR] #{work[0]} exit status=#{$?.exitstatus}" + "\e[0m" unless $?.success?
$stdoutmutex.synchronize { STDOUT.write(sio.string) }
end
end
end
Thread.abort_on_exception = true
ncpu = `sysctl -n hw.ncpu`.chomp.to_i
raise "ncpu = #{ncpu}" unless ncpu > 0
puts "ncpu = #{ncpu}"
threads = []
ncpu.times { |n| threads << Thread.new { run } }
ncpu.times { |n| threads[n].join }