-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rb
executable file
·376 lines (262 loc) · 9.95 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
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
#!/usr/bin/env ruby
# coding: utf-8
# Copyright (C) 2013 by Hanna Reitz
#
# This file is part of µxoµcota.
#
# µxoµcota is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# µxoµcota is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with µxoµcota. If not, see <http://www.gnu.org/licenses/>.
require 'find'
require 'shellwords'
require 'trollop'
class Array
def drop!(n)
self.replace(self.drop(n))
end
end
def find(*paths, &block)
remaining = paths.to_a.select { |d| File.directory?(d) }.flatten
return nil if remaining.empty?
Find.find(*remaining, &block)
end
arch = `uname -m`.chomp
if arch == 'x86'
arch = 'i386'
elsif arch == 'x86_64'
arch = File.directory?('src/kernel/arch/x64') ? 'x64' : 'i386'
elsif arch[0..2] == 'arm'
arch = 'arm'
end
case arch
when 'i386', 'x64'
platform = 'pc'
when 'arm'
platform = 'rpi'
else
platform = 'default'
end
def image_types
Dir.entries('build/scripts').select { |e| e =~ /^[^-]*-[^-]*-.*\.rb$/ }.map { |e| /^([^-]*)-([^-]*)-(.*)$/.match(e.chomp('.rb')).to_a[1..3] }
end
def platform_output
arch_plat = Hash.new
image_types.each { |e| arch_plat[e[0]] = Array.new unless arch_plat[e[0]]; arch_plat[e[0]] << e[1] }
maxlen = 0
arch_plat.each_key { |a| maxlen = a.length if a.length > maxlen }
arch_plat.keys.map { |a| " %*s: #{arch_plat[a].uniq * ', '}" % [maxlen, a] } * "\n"
end
def image_type_output
hash = Hash.new
image_types.map { |e| [e[0..1] * '-', e[2]] }.each { |e| hash[e[0]] = Array.new unless hash[e[0]]; hash[e[0]] << e[1] }
maxlen = 0
hash.each_key { |ap| maxlen = ap.length if ap.length > maxlen }
hash.keys.map { |ap| " %*s: #{hash[ap].uniq * ', '}" % [maxlen, ap] } * "\n"
end
opts = Trollop::options do
version('µxoµcota build system 0.0.1 (c) 2012/13 Hanna Reitz')
banner <<-EOS
µxoµcota build system
Supported target architectures:
#{image_types.map { |e| e[0] }.uniq * ', '}
Supported target platforms:
#{platform_output}
Supported image types:
#{image_type_output}
Usage:
./build.rb [options] [target]
where [options] are:
EOS
opt(:arch, 'Target architecture', short: 'a', default: arch)
opt(:platform, 'Target platform', short: 'p', default: platform)
opt(:image, 'Image type to be built', short: 'i', default: 'default')
end
arch = opts[:arch]
platform = opts[:platform]
image = opts[:image]
Trollop::die('Unsupported architecture') unless File.directory?("src/kernel/arch/#{arch}")
Trollop::die('Unsupported platform') unless File.directory?("src/kernel/platform/#{platform}")
Trollop::die('Unsupported image type') unless File.file?("build/scripts/#{arch}-#{platform}-#{image}.rb")
target = ARGV[0] ? ARGV[0] : 'all'
Trollop::die('Unsupported target') unless ['clean', 'all'].include?(target)
exit 1 unless system('mkdir -p build/root/{bin,boot,dev,etc}') if target == 'all'
image_root = "#{Dir.pwd}/build/root"
exts = ['.c', '.asm', '.S', '.incbin']
$inc_dirs = ['common', "arch/#{arch}", "platform/#{platform}"].map { |d| "#{Dir.pwd}/src/include/#{d}" }
cores = `cat /proc/cpuinfo | grep -c 'processor[[:space:]]*:'`
cores = cores.to_i if cores
cores = 4 unless cores && (cores > 0)
threads = Array.new
['kernel', 'lib', 'progs'].each do |dir|
puts("——— #{dir} ———") unless target == 'clean'
pushed = Dir.pwd
Dir.chdir("src/#{dir}")
if target == 'clean'
if dir == 'lib'
system('rm -f obj/* crt/*.o lib*.a')
else
system('rm -f obj/*')
end
Dir.chdir(pushed)
next
end
pars = eval(IO.read("arch/#{arch}/build-vars.rb"))
objdir = "#{Dir.pwd}/obj"
exit 1 unless system("mkdir -p #{objdir.shellescape}")
progdir = { 'progs' => "#{Dir.pwd}/progs" }[dir]
exit 1 unless system("mkdir -p #{progdir.shellescape}") if progdir
exit 1 unless system('mkdir -p crt') if dir == 'lib'
subdirs = Array.new
find('common', "platform/#{platform}", "arch/#{arch}") do |path|
subdirs << File.dirname(path) if exts.include?(File.extname(path))
end
operations = Array.new
retirement = Array.new
retirement << '.' if dir == 'progs'
subdirs.uniq.each do |sd|
if dir == 'lib'
case File.basename(sd)
when 'cdi'
objprefix = 'cdi'
when 'math'
objprefix = 'm'
else
objprefix = 'c'
end
objprefix += "_#{sd.gsub('/', '__')}"
else
objprefix = sd.gsub('/', '__')
end
operations << sd
cdi = (dir == 'progs') && File.file?("#{sd}/.cdi")
cdipars = cdi ? eval(IO.read("#{sd}/.cdi")) : {}
[:cc, :asm, :objcp, :ld].each { |top| cdipars[top] = {} unless cdipars[top] }
if cdi
cdipars.each do |top, flags|
flags.each do |key, value|
next unless value.kind_of?(String)
cdipars[top][key].gsub!('${pwd}', File.expand_path(sd))
end
end
end
Dir.new(sd).each do |file|
ext = File.extname(file)
next unless exts.include?(ext)
if (dir == 'lib') && (File.basename(sd) == 'crt')
output = "#{Dir.pwd}/crt/#{File.basename(sd, ext)}.o"
else
output = "#{objdir}/#{objprefix}__#{file.gsub('.', '_')}.o"
end
next if File.file?(output) && (File.mtime("#{sd}/#{file}") < File.mtime(output))
case ext
when '.c'
operations << [['CC', file], "#{pars[:cc][:cmd]} #{pars[:cc][:flags]} #{cdipars[:cc][:flags]} -c #{"#{sd}/#{file}".shellescape} -o #{output.shellescape}"]
when '.asm', '.S'
operations << [['ASM', file], "#{(cdipars[:asm][:cmd] ? cdipars : pars)[:asm][:cmd]} #{pars[:asm][:flags]} #{cdipars[:asm][:flags]} #{"#{sd}/#{file}".shellescape} #{(cdipars[:asm][:out] ? cdipars : pars)[:asm][:out]} #{output.shellescape}"]
when '.incbin'
operations << [['OBJCP', file], "pushd #{sd.shellescape} &> /dev/null; #{pars[:objcp][:cmd]} #{pars[:objcp][:bin2elf]} #{cdipars[:objcp][:bin2elf]} #{file.shellescape} #{output.shellescape} && popd &> /dev/null"]
end
end
if dir == 'progs'
ret = [['LD', ">#{sd}"], "#{pars[:ld][:cmd]} #{pars[:ld][:flags]} #{cdipars[:ld][:flags]} #{objdir}/#{objprefix}__*.o ../lib/crt/*.o -L../lib -o #{"#{image_root}/bin/#{File.basename(sd)}".shellescape} -\\( -lc #{pars[:ld][:libs]} #{cdi ? '-lcdi' : ''} -\\)"]
retirement << ret
end
end
print_mtx = Mutex.new
[operations, retirement].each do |queue|
queue.each do |op|
if op.kind_of?(String)
print_mtx.lock()
puts("<<< #{op} >>>")
print_mtx.unlock()
next
end
while threads.size > cores
threads.keep_if { |thr| thr.alive? }
Thread.pass
end
threads << Thread.new(op) do |op|
print_mtx.lock()
puts('%-8s%s' % op[0])
print_mtx.unlock()
if (op[0][0] == 'ASM') && pars[:asm][:quiet]
outp = `#{op[1]} 2>&1`
unless $?.success?
print_mtx.lock()
STDERR.puts(outp)
print_mtx.unlock()
exit 1
end
else
exit 1 unless system(op[1])
end
end
end
threads.each do |thr|
thr.join()
end
threads = []
end
case dir
when 'kernel'
puts('<<< . >>>')
puts('%-8s%s' % ['LD', '>kernel'])
exit 1 unless system("#{pars[:ld][:cmd]} #{pars[:ld][:flags]} obj/*.o -o #{image_root.shellescape}/boot/kernel #{pars[:ld][:libs]}")
when 'lib'
puts('<<< . >>>')
[ 'c', 'm', 'cdi' ].each do |lib|
puts('%-8s%s' % ['AR', ">lib#{lib}.a"])
exit 1 unless system("#{pars[:ar][:cmd]} #{pars[:ar][:flags]} lib#{lib}.a obj/#{lib}_*.o")
end
end
Dir.chdir(pushed)
end
if target == 'clean'
exit 1 unless system("rm -rf #{image_root.shellescape}")
else
puts("——— blobs ———")
pushed = Dir.pwd
Dir.chdir("src/blobs")
subdirs = Array.new
find('common', "platform/#{platform}", "arch/#{arch}") do |path|
subdirs << path if File.directory?(path)
end
subdirs.uniq.each do |sd|
puts("<<< #{sd} >>>")
pathparts = sd.split('/')
if pathparts[0] == 'common'
pathparts.drop!(1)
else
pathparts.drop!(2)
end
if pathparts.empty?
image_dir = 'etc'
else
image_dir = pathparts.join('/')
puts('%-8s%s' % ['MKDIR', image_dir])
exit 1 unless system("mkdir -p #{"#{image_root}/#{image_dir}".shellescape}")
end
Dir.new(sd).each do |file|
next unless File.file?("#{sd}/#{file}")
puts('%-8s%s' % ['CP', file])
exit 1 unless system("cp #{"#{sd}/#{file}".shellescape} #{"#{image_root}/#{image_dir}/#{file}".shellescape}")
end
end
Dir.chdir(pushed)
end
exit 1 unless system("mkdir -p build/images")
if target == 'all'
puts("——— image ———")
exit 1 unless system("build/scripts/#{arch}-#{platform}-#{image}.rb")
else
system("rm -rf build/images/* build/root build/root-unstripped")
end