-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathci.rb
executable file
·594 lines (479 loc) · 12.6 KB
/
ci.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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#!/usr/bin/env ruby
require "bundler"
require "fileutils"
require "open3"
require "open-uri"
require "tmpdir"
class Qemu
# Version of QEMU to bundle
VERSION = "8.2.0"
# Map of canonicalized host architectures
ALIASES = {
aarch64: :arm64
}.freeze
# Interface to access which firmware to bundle for each QEMU architecture
class Architecture
def initialize(qemu)
@qemu = qemu
end
def name
self.class.name.split("::").last.downcase
end
def bundle
qemu_target_dir = File.join(architecture_directory, "bin")
FileUtils.mkdir_p qemu_target_dir
FileUtils.mkdir_p firmware_target_directory
bundle_uefi
FileUtils.cp File.join("qemu", "build", qemu_name), File.join(qemu_target_dir, "qemu")
bundle_firmware
execute "tar", "-C", architecture_directory, "-c", "-f", "#{qemu_name}-#{ci_runner.os_name}.tar", "."
end
def firmware_target_directory
File.join(architecture_directory, "share", "qemu")
end
def firmware_source_directory
@firmware_source_directory ||= File.join("qemu", "pc-bios")
end
private
attr_reader :qemu
def ci_runner
qemu.ci_runner
end
def target_directory
qemu.target_directory
end
def architecture_directory
File.join(target_directory, qemu_name)
end
def qemu_name
"qemu-system-#{name}"
end
def bundle_firmware
firmware_paths = firmwares.map { File.join(firmware_source_directory, _1) }
firmware_paths
.map { _1 + ".bz2" }
.filter { File.exist?(_1) }
.each { unpack_bzip2(_1) }
FileUtils.cp(firmware_paths, firmware_target_directory)
end
end
class X86_64 < Architecture
FIRMWARES = %w[
bios-256k.bin
efi-e1000.rom
efi-virtio.rom
kvmvapic.bin
vgabios-stdvga.bin
].freeze
private_constant :FIRMWARES
def firmwares
FIRMWARES
end
def bundle_uefi
ci_runner.bundle_uefi(firmware_target_directory)
end
end
class Arm64 < Architecture
FIRMWARES = %w[
efi-e1000.rom
efi-virtio.rom
].freeze
private_constant :FIRMWARES
def name
"aarch64"
end
def firmwares
FIRMWARES
end
def bundle_uefi
unpack_uefi
FileUtils.mkdir_p firmware_target_directory
FileUtils.cp(uefi_source_path, uefi_target_path)
File.open(uefi_target_path, File::RDWR) do |file|
file.truncate(file.read.bytes.rindex { _1 != 0 })
end
bundle_linaro_uefi
end
private
def uefi_target_path
@uefi_target_path ||= File.join(firmware_target_directory, "uefi.fd")
end
def uefi_source_path
@uefi_source_path ||= File.join(firmware_source_directory, "edk2-aarch64-code.fd")
end
def unpack_uefi
archive = uefi_source_path + ".bz2"
return unless File.exist?(archive)
FileUtils.rm_f uefi_source_path
unpack_bzip2 archive
end
def bundle_linaro_uefi
download_file(linaro_uefi_url, linaro_uefi_target_path)
end
def linaro_uefi_url
"https://releases.linaro.org/components/kernel/uefi-linaro/latest/release/qemu64/QEMU_EFI.fd"
end
def linaro_uefi_target_path
@linaro_uefi_target_path ||=
File.join(firmware_target_directory, "linaro_uefi.fd")
end
end
# Specifies which QEMU architectures to bundle for a given host architecture.
ENABLED_ARCHITECTURES = {
x86_64: [X86_64, Arm64],
arm64: [Arm64]
}.freeze
attr_reader :ci_runner
def initialize(ci_runner)
@ci_runner = ci_runner
end
def target_directory
"work/qemus"
end
def fetch
execute "curl", "-O", "https://download.qemu.org/qemu-#{VERSION}.tar.xz"
execute "xz -cd qemu-#{VERSION}.tar.xz | tar -xf -"
FileUtils.rm_rf ["qemu", "qemu-#{VERSION}.tar.xz"]
FileUtils.ln_s "qemu-#{VERSION}", "qemu"
end
def build
FileUtils.mkdir_p "qemu/build"
libslirp.build
Dir.chdir "qemu/build" do
target_list = enabled_architectures.map { "#{_1.name}-softmmu" }.join(",")
target_list_arg = "--target-list=" + target_list
args = %w[
--prefix=/tmp/cross-platform-actions
--disable-auth-pam
--disable-bochs
--disable-bsd-user
--disable-capstone
--disable-cfi-debug
--disable-curses
--disable-debug-info
--disable-debug-mutex
--disable-dmg
--disable-docs
--disable-gcrypt
--disable-gnutls
--disable-gtk
--disable-guest-agent
--disable-guest-agent-msi
--disable-libiscsi
--disable-libssh
--disable-libusb
--disable-linux-user
--disable-nettle
--disable-parallels
--disable-png
--disable-qcow1
--disable-qed
--disable-replication
--disable-sdl
--disable-sdl-image
--disable-smartcard
--disable-snappy
--disable-usb-redir
--disable-user
--disable-vde
--disable-vdi
--disable-vnc
--disable-vvfat
--disable-xen
--disable-lzo
--disable-zstd
--enable-lto
--enable-slirp
--enable-tools
].append(target_list_arg)
.concat(ci_runner.qemu_build_flags)
execute "../configure", *args, env: { LDFLAGS: ldflags }
execute "make qemu-img qemu-system-aarch64 qemu-system-x86_64"
execute "ls", "-lh"
end
ensure
libslirp.cleanup
end
def bundle
FileUtils.mkdir_p target_directory
enabled_architectures.each(&:bundle)
end
private
def ldflags
ci_runner.qemu_ldflags.concat(libslirp.ldflags).join(" ")
end
def libslirp
ci_runner.host.libslirp
end
def enabled_architectures
@enabled_architectures ||=
ci_runner.enabled_architectures.map { _1.new(self) }
end
end
class CIRunner
def run
host.install_prerequisite
qemu.fetch
qemu.build
bundle_resources
host.xhyve.bundle
qemu.bundle
end
def enabled_architectures
host.qemu.architectures
end
def bundle_uefi(firmware_target_dir)
host.bundle_uefi(firmware_target_dir)
end
def os_name
host.name
end
def qemu_build_flags
host.qemu.build_flags
end
def qemu_ldflags
host.qemu.ldflags
end
def host
@host ||= begin
os = Gem::Platform.local.os
host_map[os]&.new or raise "Unsupported operating system: #{os}"
end
end
private
def host_map
{
"darwin" => MacOS,
"linux" => Linux
}
end
def qemu
@qemu ||= Qemu.new(self)
end
class Host
def name
self.class.name.split("::").last.downcase
end
class Qemu
def architectures
::Qemu::ENABLED_ARCHITECTURES[architecture]
end
private
def architecture
@architecture ||= cpu.then { ::Qemu::ALIASES.fetch(_1, _1) }
end
def cpu
Gem::Platform.local.cpu.to_sym
end
end
end
class MacOS < Host
def qemu
@qemu ||= Qemu.new
end
def xhyve
@xhyve ||= begin
cls = Gem::Platform.local.cpu == "x86_64" ? Xhyve : XhyveNoop
cls.new(self)
end
end
def libslirp
@libslirp ||= Libslirp.new
end
def bundle_uefi(firmware_target_dir)
FileUtils.cp "OVMF.fd", File.join(firmware_target_dir, "uefi.fd")
end
def install_prerequisite
packages = %w[ninja pixman glib meson libslirp]
execute "brew", "install", *packages, env: { HOMEBREW_NO_INSTALL_CLEANUP: true }
patch_glib_python_codegen
end
# Python 3.12 doesn't have the distutils module.
# Remove when updating to a version of glib newer than 2.78.3.
def patch_glib_python_codegen
patch = <<~DIFF
diff --git a/gio/gdbus-2.0/codegen/utils.py b/gio/gdbus-2.0/codegen/utils.py
index 02046108dae49efb140c6438b03b80a73770d2c0..08f1ba9731d0582015ef9807eb739a3efa410e0d 100644
--- a/gio/gdbus-2.0/codegen/utils.py
+++ b/gio/gdbus-2.0/codegen/utils.py
@@ -19,7 +19,7 @@
#
# Author: David Zeuthen <[email protected]>
-import distutils.version
+import packaging.version
import os
import sys
@@ -166,4 +166,4 @@ def version_cmp_key(key):
v = str(key[0])
else:
v = "0"
- return (distutils.version.LooseVersion(v), key[1])
+ return (packaging.version.Version(v), key[1])
DIFF
Dir.chdir("/usr/local/Cellar/glib/2.78.3/share/glib-2.0") do
_, status = Open3.capture2("patch", "-p3", stdin_data: patch)
raise "Failed to execute 'patch' command" unless status.success?
end
execute "pip3", "install", "packaging"
end
class Qemu < Host::Qemu
def build_flags
[]
end
def ldflags
@ldflags ||= [
"-dead_strip",
"-framework", "Foundation",
"-framework", "Cocoa",
'-lffi',
"-liconv",
"-lresolv",
"#{brew_prefix}/opt/dtc/lib/libfdt.a",
"#{brew_prefix}/opt/gettext/lib/libintl.a",
"#{brew_prefix}/opt/glib/lib/libglib-2.0.a",
"#{brew_prefix}/opt/glib/lib/libgio-2.0.a",
"#{brew_prefix}/opt/glib/lib/libgmodule-2.0.a",
"#{brew_prefix}/opt/glib/lib/libgobject-2.0.a",
"#{brew_prefix}/opt/pixman/lib/libpixman-1.a",
"#{brew_prefix}/lib/libpcre2-8.a",
"#{brew_prefix}/lib/libslirp.a"
]
end
private
def brew_prefix
@brew_prefix ||= `brew --prefix`.strip
end
end
private_constant :Qemu
class Xhyve
attr_reader :host
def initialize(host)
@host = host
end
def bundle
# Reuse previously packaged Xhyve because: "xhyve has been disabled because it does not build"
# https://github.com/cross-platform-actions/resources/actions/runs/7292733675/job/19874361022#step:3:3225
download_file("https://github.com/cross-platform-actions/resources/releases/download/v0.9.1/xhyve-macos.tar", "xhyve-#{host.name}.tar")
end
end
private_constant :Xhyve
class Libslirp
def build
end
def ldflags
[]
end
def cleanup
end
end
private_constant :Libslirp
end
class Linux < Host
def qemu
@qemu ||= Qemu.new
end
def xhyve
@xhyve ||= XhyveNoop.new(self)
end
def libslirp
@libslirp ||= Libslirp.new
end
def install_prerequisite
packages = %w[
bash
curl
g++
gcc
glib-dev
glib-static
libblkid
libmount
make
meson
musl-dev
ninja
ovmf
perl
pixman-dev
pixman-static
pkgconf
python3
xz
zlib-static
]
execute "apk", "add", "--no-cache", *packages
end
def bundle_uefi(firmware_target_dir)
FileUtils.cp "/usr/share/OVMF/OVMF.fd", File.join(firmware_target_dir, "uefi.fd")
end
class Qemu < Host::Qemu
def build_flags
%w[--static]
end
def ldflags
["-s"]
end
end
private_constant :Qemu
class Libslirp
def build
Dir.chdir(temp_dir) do
fetch
_build
end
end
def ldflags
[File.join(target_path, "build", "libslirp.a")]
end
def cleanup
FileUtils.remove_entry(temp_dir)
end
private
def temp_dir
@temp_dir ||= Dir.mktmpdir
end
def target_path
@target_path ||= File.join(temp_dir, "libslirp-master")
end
def fetch
download_file("https://gitlab.com/qemu-project/libslirp/-/archive/master/libslirp-master.tar", "libslirp.tar")
execute "tar", "-xf", "libslirp.tar"
end
def _build
Dir.chdir(target_path) do
execute "meson", "setup", "-Ddefault_library=static", "build"
execute "ninja", "-C", "build", "install"
end
end
end
private_constant :Libslirp
end
class XhyveNoop
def initialize(_host)
end
def install
end
def bundle
end
end
private
def bundle_resources
FileUtils.mkdir_p "work"
FileUtils.cp_r "qemu/build/qemu-img", "work"
execute "tar", "-C", "work", "-c", "-f", "resources-#{host.name}.tar", "."
FileUtils.rm_r "work"
end
end
def execute(*args, env: {})
env = env.map { |k, v| [k.to_s, v.to_s] }.to_h
Kernel.system env, *args, exception: true
end
def download_file(url, destination)
URI.open(url) do |uri|
File.open(destination, 'w') { _1.write(uri.read) }
end
end
def unpack_bzip2(archive)
execute "bzip2", "-d", archive
end
CIRunner.new.run