-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathbuild_sdk.py
More file actions
1056 lines (930 loc) · 34.5 KB
/
build_sdk.py
File metadata and controls
1056 lines (930 loc) · 34.5 KB
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2021, Breakaway Consulting Pty. Ltd.
# SPDX-License-Identifier: BSD-2-Clause
"""The SDK build script.
# Why Python (and not make, or something else)?
We call out to Make, but having this top-level driver script
is useful.
There are just a lot of things that are much easier in Python
than in make.
"""
from argparse import ArgumentParser
import copy
from os import popen, system, environ
import shutil
from pathlib import Path
from dataclasses import dataclass
from sys import executable
from tarfile import open as tar_open, TarInfo
import platform as host_platform
from enum import IntEnum
import json
import subprocess
from typing import Any, Dict, Union, List, Tuple, Optional
NAME = "microkit"
ENV_BIN_DIR = Path(executable).parent
MICROKIT_EPOCH = 1616367257
TRIPLE_AARCH64 = "aarch64-none-elf"
TRIPLE_RISCV = "riscv64-unknown-elf"
# TODO: this won't work for LLVM, to fix later
TRIPLE_X86_64 = "x86_64-linux-gnu"
KERNEL_CONFIG_TYPE = Union[bool, str]
KERNEL_OPTIONS = Dict[str, Union[bool, str]]
DEFAULT_X86_NUM_CPUS = 16
DEFAULT_KERNEL_OPTIONS = {
"KernelIsMCS": True,
"KernelRootCNodeSizeBits": "17",
# Thread local storage is painful and annoying to configure.
# We'd really rather NOT use thread local storage (especially
# considering we never have more than one thread in a VSpace)
#
# Turning off this feature removes the __thread attribute on
# __sel4_ipc_buffer and makes it a true global.
"LibSel4UseThreadLocals": False,
}
DEFAULT_KERNEL_OPTIONS_AARCH64 = {
"KernelArmExportPCNTUser": True,
"KernelArmHypervisorSupport": True,
"KernelArmVtimerUpdateVOffset": False,
"KernelAllowSMCCalls": True,
} | DEFAULT_KERNEL_OPTIONS
DEFAULT_KERNEL_OPTIONS_RISCV64 = DEFAULT_KERNEL_OPTIONS
DEFAULT_KERNEL_OPTIONS_X86_64 = {
"KernelPlatform": "pc99",
"KernelX86MicroArch": "generic",
# See https://github.com/seL4/microkit/issues/418 for details.
"KernelIOMMU": False,
} | DEFAULT_KERNEL_OPTIONS
class KernelArch(IntEnum):
AARCH64 = 1
RISCV64 = 2
X86_64 = 3
def target_triple(self) -> str:
if self == KernelArch.AARCH64:
return TRIPLE_AARCH64
elif self == KernelArch.RISCV64:
return TRIPLE_RISCV
elif self == KernelArch.X86_64:
return TRIPLE_X86_64
else:
raise Exception(f"Unsupported toolchain architecture '{self}'")
def rust_toolchain(self) -> str:
if self == KernelArch.AARCH64:
return f"aarch64-unknown-none"
elif self == KernelArch.RISCV64:
return f"riscv64gc-unknown-none-elf"
elif self == KernelArch.X86_64:
return f"x86_64-unknown-none"
else:
raise Exception(f"Unsupported toolchain target triple '{self}'")
def is_riscv(self) -> bool:
return self == KernelArch.RISCV64
def is_arm(self) -> bool:
return self == KernelArch.AARCH64
def is_x86(self) -> bool:
return self == KernelArch.X86_64
def to_str(self) -> str:
if self == KernelArch.AARCH64:
return "aarch64"
elif self == KernelArch.RISCV64:
return "riscv64"
elif self == KernelArch.X86_64:
return "x86_64"
else:
raise Exception(f"Unsupported arch {self}")
def as_kernel_arch_config(self) -> tuple[str, str]:
return ("KernelSel4Arch", self.to_str())
KERNEL_OPTIONS_ARCH = Dict[KernelArch, KERNEL_OPTIONS]
@dataclass
class BoardInfo:
name: str
arch: KernelArch
gcc_cpu: Optional[str]
loader_link_address: Optional[int]
kernel_options: KERNEL_OPTIONS
smp_cores: Optional[int] = None
@dataclass
class ConfigInfo:
name: str
debug: bool
kernel_options: KERNEL_OPTIONS
kernel_options_arch: KERNEL_OPTIONS_ARCH
# There are some references to paths we need to make that are relative
# to the kernel source, since the kernel source is supplied by the user,
# we can't hard-code it.
# This is necessary for particular kernel options such as, KernelCustomDTS
# and KernelCustomDTSOverlay.
@dataclass
class KernelPath:
path: str
SUPPORTED_BOARDS = (
BoardInfo(
name="kria_k26",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a53",
loader_link_address=0x40000000,
kernel_options={
"KernelPlatform": "zynqmp",
"KernelARMPlatform": "zcu102",
"KernelCustomDTSOverlay": Path("custom_dts/overlay-zynqmp-kria-k26.dts"),
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="tqma8xqp1gb",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a35",
loader_link_address=0x90000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "tqma8xqp1gb",
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="zcu102",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a53",
loader_link_address=0x40000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "zynqmp",
"KernelARMPlatform": "zcu102",
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="maaxboard",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a53",
loader_link_address=0x50000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "maaxboard",
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="imx8mm_evk",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a53",
loader_link_address=0x41000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "imx8mm-evk",
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="imx8mp_evk",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a53",
loader_link_address=0x41000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "imx8mp-evk",
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="imx8mq_evk",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a53",
loader_link_address=0x41000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "imx8mq-evk",
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="imx8mp_iotgate",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a53",
loader_link_address=0x50000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "imx8mp-evk",
"KernelCustomDTS": Path("custom_dts/iot-gate.dts"),
"KernelCustomDTSOverlay": KernelPath(path="src/plat/imx8m-evk/overlay-imx8mp-evk.dts"),
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="odroidc2",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a53",
loader_link_address=0x20000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "odroidc2",
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="odroidc4",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a55",
loader_link_address=0x20000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "odroidc4",
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="ultra96v2",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a53",
loader_link_address=0x40000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "zynqmp",
"KernelARMPlatform": "ultra96v2",
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="qemu_virt_aarch64",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a53",
loader_link_address=0x70000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "qemu-arm-virt",
"QEMU_MEMORY": "2048",
# There is no peripheral timer, so we use the ARM
# architectural timer
"KernelArmExportPTMRUser": True,
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="qemu_virt_riscv64",
arch=KernelArch.RISCV64,
gcc_cpu=None,
loader_link_address=0x90000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "qemu-riscv-virt",
"QEMU_MEMORY": "2048",
} | DEFAULT_KERNEL_OPTIONS_RISCV64,
),
BoardInfo(
name="rpi4b_1gb",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a72",
loader_link_address=0x10000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "bcm2711",
"RPI4_MEMORY": 1024,
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="rpi4b_2gb",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a72",
loader_link_address=0x10000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "bcm2711",
"RPI4_MEMORY": 2048,
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="rpi4b_4gb",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a72",
loader_link_address=0x10000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "bcm2711",
"RPI4_MEMORY": 4096,
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="rpi4b_8gb",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a72",
loader_link_address=0x10000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "bcm2711",
"RPI4_MEMORY": 8192,
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="rockpro64",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a53",
loader_link_address=0x30000000,
# ROCKPRO64 has 4 Cortex-A53 cores and 2 Cortex-A72 cores,
# we always run on the Cortex-A53s.
smp_cores=4,
kernel_options={
"KernelPlatform": "rockpro64",
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="rock3b",
arch=KernelArch.AARCH64,
gcc_cpu="cortex-a55",
loader_link_address=0x30000000,
kernel_options={
"KernelPlatform": "rk3568",
} | DEFAULT_KERNEL_OPTIONS_AARCH64,
),
BoardInfo(
name="hifive_p550",
arch=KernelArch.RISCV64,
gcc_cpu=None,
loader_link_address=0x90000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "hifive-p550",
} | DEFAULT_KERNEL_OPTIONS_RISCV64,
),
BoardInfo(
name="star64",
arch=KernelArch.RISCV64,
gcc_cpu=None,
loader_link_address=0x60000000,
smp_cores=4,
kernel_options={
"KernelPlatform": "star64",
} | DEFAULT_KERNEL_OPTIONS_RISCV64,
),
BoardInfo(
name="ariane",
arch=KernelArch.RISCV64,
gcc_cpu=None,
loader_link_address=0x90000000,
kernel_options={
"KernelPlatform": "ariane",
} | DEFAULT_KERNEL_OPTIONS_RISCV64,
),
BoardInfo(
name="cheshire",
arch=KernelArch.RISCV64,
gcc_cpu=None,
loader_link_address=0x90000000,
kernel_options={
"KernelPlatform": "cheshire",
} | DEFAULT_KERNEL_OPTIONS_RISCV64,
),
BoardInfo(
name="serengeti",
arch=KernelArch.RISCV64,
gcc_cpu=None,
loader_link_address=0x90000000,
kernel_options={
"KernelPlatform": "cheshire",
} | DEFAULT_KERNEL_OPTIONS_RISCV64,
),
BoardInfo(
name="x86_64_generic",
arch=KernelArch.X86_64,
gcc_cpu="generic",
loader_link_address=None,
smp_cores=DEFAULT_X86_NUM_CPUS,
kernel_options={
"KernelSupportPCID": False,
"KernelVTX": False,
} | DEFAULT_KERNEL_OPTIONS_X86_64,
),
BoardInfo(
name="x86_64_generic_vtx",
arch=KernelArch.X86_64,
gcc_cpu="generic",
loader_link_address=None,
smp_cores=DEFAULT_X86_NUM_CPUS,
kernel_options={
"KernelSupportPCID": False,
"KernelVTX": True,
"KernelX86_64VTX64BitGuests": True,
} | DEFAULT_KERNEL_OPTIONS_X86_64,
),
)
# These then get elaborated into smp-release, smp-benchmark, and smp-debug
SUPPORTED_CONFIGS = (
ConfigInfo(
name="release",
debug=False,
kernel_options={},
kernel_options_arch={},
),
ConfigInfo(
name="debug",
debug=True,
kernel_options={
"KernelDebugBuild": True,
"KernelPrinting": True,
"KernelVerificationBuild": False
},
kernel_options_arch={
KernelArch.AARCH64: {
"HardwareDebugAPI": True,
},
KernelArch.X86_64: {
"HardwareDebugAPI": True,
}
},
),
ConfigInfo(
name="benchmark",
debug=False,
kernel_options={
"KernelDebugBuild": False,
"KernelVerificationBuild": False,
"KernelBenchmarks": "track_utilisation",
"KernelSignalFastpath": True,
},
kernel_options_arch={
KernelArch.AARCH64: {
"KernelArmExportPMUUser": True,
},
KernelArch.X86_64: {
"KernelExportPMCUser": True,
"KernelX86DangerousMSR": True,
}
},
),
)
EXAMPLES = {
"hello": Path("example/hello"),
"ethernet": Path("example/ethernet"),
"passive_server": Path("example/passive_server"),
"hierarchy": Path("example/hierarchy"),
"timer": Path("example/timer"),
}
def elaborate_all_board_configs(board: BoardInfo) -> list[ConfigInfo]:
elaborated_configs = list(SUPPORTED_CONFIGS)
if board.smp_cores is not None:
for config in SUPPORTED_CONFIGS:
config = copy.deepcopy(config)
config.name = f"smp-{config.name}"
config.kernel_options |= {
"KernelMaxNumNodes": str(board.smp_cores),
}
elaborated_configs.append(config)
return elaborated_configs
def tar_filter(tarinfo: TarInfo) -> TarInfo:
"""This is used to change the tarinfo when created the .tar.gz archive.
This ensures the tar file does not leak information from the build environment.
"""
# Force uid/gid
tarinfo.uid = tarinfo.gid = 0
tarinfo.uname = tarinfo.gname = "microkit"
# This is unlikely to be set, but force it anyway
tarinfo.pax_headers = {}
tarinfo.mtime = MICROKIT_EPOCH
assert tarinfo.isfile() or tarinfo.isdir()
# Set the permissions properly
if tarinfo.isdir():
tarinfo.mode = tarinfo.mode & ~0o777 | 0o744
if tarinfo.isfile():
if "/bin/" in tarinfo.name:
# Assume everything in bin should be executable.
tarinfo.mode = tarinfo.mode & ~0o777 | 0o755
else:
tarinfo.mode = tarinfo.mode & ~0o777 | 0o644
return tarinfo
SUPPORTED_HOST_TARGETS = {
"linux-x86-64": "x86_64-unknown-linux-musl",
"linux-aarch64": "aarch64-unknown-linux-musl",
"macos-aarch64": "aarch64-apple-darwin",
"macos-x86-64": "x86_64-apple-darwin",
}
def get_tool_target_triple() -> str:
host_system = host_platform.system()
if host_system == "Linux":
host_arch = host_platform.machine()
if host_arch == "x86_64":
return SUPPORTED_HOST_TARGETS["linux-x86-64"]
elif host_arch == "aarch64":
return SUPPORTED_HOST_TARGETS["linux-aarch64"]
else:
raise Exception(f"Unexpected Linux architecture: {host_arch}")
elif host_system == "Darwin":
host_arch = host_platform.machine()
if host_arch == "x86_64":
return SUPPORTED_HOST_TARGETS["macos-x86-64"]
elif host_arch == "arm64":
return SUPPORTED_HOST_TARGETS["macos-aarch64"]
else:
raise Exception(f"Unexpected Darwin architecture: {host_arch}")
else:
raise Exception(f"The platform \"{host_system}\" is not supported")
def test_tool() -> None:
r = system(
f"cargo test -p microkit-tool"
)
assert r == 0
def build_tool(tool_target: Path, target_triple: str) -> None:
r = system(
f"cargo build --release --locked --target {target_triple} -p microkit-tool"
)
assert r == 0
tool_output = f"./target/{target_triple}/release/microkit"
shutil.copy(tool_output, tool_target)
tool_target.chmod(0o755)
def build_sel4(
sel4_dir: Path,
tool_dir: Path,
sdk_dir: Path,
build_dir: Path,
board: BoardInfo,
config: ConfigInfo,
llvm: bool
):
"""Build seL4"""
build_dir = build_dir / board.name / config.name / "sel4"
build_dir.mkdir(exist_ok=True, parents=True)
sel4_install_dir = build_dir / "install"
sel4_build_dir = build_dir / "build"
sel4_install_dir.mkdir(exist_ok=True, parents=True)
sel4_build_dir.mkdir(exist_ok=True, parents=True)
print(f"Building sel4: {sel4_dir=} {sdk_dir=} {build_dir=} {board=} {config=}")
config_args = [
*board.kernel_options.items(),
*config.kernel_options.items(),
board.arch.as_kernel_arch_config(),
]
if config.kernel_options_arch is not None:
if board.arch in config.kernel_options_arch:
config_args += config.kernel_options_arch[board.arch].items()
config_strs = []
for arg, val in sorted(config_args):
if isinstance(val, bool):
str_val = "ON" if val else "OFF"
elif isinstance(val, KernelPath):
str_val = f"{sel4_dir.absolute()}/{val.path}"
elif isinstance(val, Path):
str_val = Path(__file__).parent / val
else:
str_val = str(val)
s = f"-D{arg}={str_val}"
config_strs.append(s)
config_str = " ".join(config_strs)
target_triple = f"{board.arch.target_triple()}"
cmd = (
f"cmake -GNinja -DCMAKE_INSTALL_PREFIX={sel4_install_dir.absolute()} "
f" -DPYTHON3={executable} "
f" {config_str} "
f"-S {sel4_dir.absolute()} -B {sel4_build_dir.absolute()}")
if llvm:
cmd += f" -DTRIPLE={target_triple}"
else:
cmd += f" -DCROSS_COMPILER_PREFIX={target_triple}-"
r = system(cmd)
if r != 0:
raise Exception(f"Error configuring sel4: cmd={cmd}")
cmd = f"cmake --build {sel4_build_dir.absolute()}"
r = system(cmd)
if r != 0:
raise Exception(f"Error building sel4: cmd={cmd}")
cmd = f"cmake --install {sel4_build_dir.absolute()}"
r = system(cmd)
if r != 0:
raise Exception(f"Error installing sel4: cmd={cmd}")
elf = sel4_install_dir / "bin" / "kernel.elf"
elf64_dest = (
sdk_dir / "board" / board.name / config.name / "elf" / "sel4.elf"
)
elf64_dest.unlink(missing_ok=True)
shutil.copy(elf, elf64_dest)
# Make output read-only
elf64_dest.chmod(0o744)
# qemu-system-x86_64 -kernel option only accepts a 32-bit elf32-i386 kernel image. Since seL4's
# build process produces a 64-bit ELF, we must convert the output into a 32-bit ELF container
# with the same code and data, allowing QEMU to load it via -kernel.
# Otherwise, you get this "qemu-system-x86_64: Cannot load x86-64 image, give a 32bit one."
if board.arch.is_x86():
elf32_dest = (
sdk_dir / "board" / board.name / config.name / "elf" / "sel4_32.elf"
)
objcopy_arg = f"-O elf32-i386 {str(elf64_dest)} {str(elf32_dest)}"
if llvm:
cmd = f"llvm-objcopy {objcopy_arg}"
else:
cmd = f"{board.arch.target_triple()}-objcopy {objcopy_arg}"
r = system(cmd)
if r != 0:
raise Exception(f"Error creating 32-bit sel4 image: cmd={cmd}")
elf32_dest.chmod(0o744)
invocations_all = sel4_build_dir / "generated" / "invocations_all.json"
dest = (sdk_dir / "board" / board.name / config.name / "invocations_all.json")
dest.unlink(missing_ok=True)
shutil.copy(invocations_all, dest)
dest.chmod(0o744)
include_dir = sdk_dir / "board" / board.name / config.name / "include"
for source in ("kernel_Config", "libsel4", "libsel4/sel4_Config", "libsel4/autoconf"):
source_dir = sel4_install_dir / source / "include"
for p in source_dir.rglob("*"):
if not p.is_file():
continue
rel = p.relative_to(source_dir)
dest = include_dir / rel
dest.parent.mkdir(exist_ok=True, parents=True)
dest.unlink(missing_ok=True)
shutil.copy(p, dest)
dest.chmod(0o744)
if not board.arch.is_x86():
# only non-x86 platforms have this file to describe memory regions
platform_gen = sel4_build_dir / "gen_headers" / "plat" / "machine" / "platform_gen.json"
dest = sdk_dir / "board" / board.name / config.name / "platform_gen.json"
dest.unlink(missing_ok=True)
shutil.copy(platform_gen, dest)
dest.chmod(0o744)
# Use the preprocessor to convert the seL4 object size constants to readable JSON
# for the tool.
object_sizes_header = tool_dir / "object_sizes.h"
dest = sdk_dir / "board" / board.name / config.name / "object_sizes.json"
preprocessor = "clang" if (llvm) else f"{target_triple}-cpp"
preprocess_cmd = [
preprocessor,
"-E",
"-P",
f"-I{include_dir}",
object_sizes_header,
]
r = subprocess.run(preprocess_cmd, capture_output=True)
if r.returncode != 0:
raise Exception(f"Failed creating object_sizes.json: cmd={preprocess_cmd}")
preprocessor_out = r.stdout.decode("utf-8")
object_sizes = []
for l in preprocessor_out.split("\n"):
# Preprocessor emits commented lines etc that we want to ignore
if ": " in l:
assert len(l.split(": ")) == 2
obj_name, size = l.split(": ")
object_sizes.append((obj_name, int(size)))
with open(dest, "w") as out_file:
json.dump(dict(object_sizes), out_file)
dest.chmod(0o744)
def build_elf_component(
component_name: str,
sdk_dir: Path,
build_dir: Path,
board: BoardInfo,
config: ConfigInfo,
llvm: bool,
defines: List[Tuple[str, str]],
) -> None:
"""Build a specific ELF component.
Right now this is either the loader or the monitor
"""
sel4_dir = sdk_dir / "board" / board.name / config.name
build_dir = build_dir / board.name / config.name / component_name
build_dir.mkdir(exist_ok=True, parents=True)
target_triple = f"{board.arch.target_triple()}"
defines_str = " ".join(f"{k}={v}" for k, v in defines)
defines_str += f" ARCH={board.arch.to_str()} BOARD={board.name} BUILD_DIR={build_dir.absolute()} SEL4_SDK={sel4_dir.absolute()} TARGET_TRIPLE={target_triple} LLVM={llvm}"
if board.gcc_cpu is not None:
defines_str += f" GCC_CPU={board.gcc_cpu}"
r = system(
f"{defines_str} make -C {component_name} all"
)
if r != 0:
raise Exception(
f"Error building: {component_name} for board: {board.name} config: {config.name}"
)
elf = build_dir / f"{component_name}.elf"
dest = (
sdk_dir / "board" / board.name / config.name / "elf" / f"{component_name}.elf"
)
dest.unlink(missing_ok=True)
shutil.copy(elf, dest)
# Make output read-only
dest.chmod(0o744)
def build_doc(sdk_dir: Path):
output = sdk_dir / "doc" / "microkit_user_manual.pdf"
environ["TEXINPUTS"] = "style:"
r = system(f'cd docs && pandoc manual.md -o ../{output}')
assert r == 0
def build_lib_component(
component_name: str,
sdk_dir: Path,
build_dir: Path,
board: BoardInfo,
config: ConfigInfo,
llvm: bool
) -> None:
"""Build a specific library component.
Right now this is just libmicrokit.a
"""
sel4_dir = sdk_dir / "board" / board.name / config.name
build_dir = build_dir / board.name / config.name / component_name
build_dir.mkdir(exist_ok=True, parents=True)
target_triple = f"{board.arch.target_triple()}"
defines_str = f" ARCH={board.arch.to_str()} BUILD_DIR={build_dir.absolute()} SEL4_SDK={sel4_dir.absolute()} TARGET_TRIPLE={target_triple} LLVM={llvm}"
if board.gcc_cpu is not None:
defines_str += f" GCC_CPU={board.gcc_cpu}"
r = system(
f"{defines_str} make -C {component_name}"
)
if r != 0:
raise Exception(
f"Error building: {component_name} for board: {board.name} config: {config.name}"
)
lib = build_dir / f"{component_name}.a"
lib_dir = sdk_dir / "board" / board.name / config.name / "lib"
dest = lib_dir / f"{component_name}.a"
dest.unlink(missing_ok=True)
shutil.copy(lib, dest)
# Make output read-only
dest.chmod(0o744)
link_script = Path(component_name) / "microkit.ld"
dest = lib_dir / "microkit.ld"
dest.unlink(missing_ok=True)
shutil.copy(link_script, dest)
# Make output read-only
dest.chmod(0o744)
include_dir = sdk_dir / "board" / board.name / config.name / "include"
source_dir = Path(component_name) / "include"
for p in source_dir.rglob("*"):
if not p.is_file():
continue
rel = p.relative_to(source_dir)
dest = include_dir / rel
dest.parent.mkdir(exist_ok=True, parents=True)
dest.unlink(missing_ok=True)
shutil.copy(p, dest)
dest.chmod(0o744)
def build_initialiser(
component_name: str,
sdk_dir: Path,
build_dir: Path,
board: BoardInfo,
config: ConfigInfo,
) -> None:
sel4_src_dir = build_dir / board.name / config.name / "sel4" / "install"
cargo_target = board.arch.rust_toolchain()
dest = (
sdk_dir / "board" / board.name / config.name / "elf" / f"{component_name}.elf"
)
# To save on build times, we share a single 'build target' dir for the component,
# this means many initialiser dependencies do not have to be rebuilt unless something
# with the seL4 headers changes or the target architecture.
rust_target_dir = build_dir / component_name
component_build_dir = build_dir / board.name / config.name / component_name
component_build_dir.mkdir(exist_ok=True, parents=True)
r = system(f"""
RUSTC_BOOTSTRAP=1 \
SEL4_PREFIX={sel4_src_dir.absolute()} \
cargo build \
--target {cargo_target} \
--locked \
--target-dir {rust_target_dir} \
--release \
-p initialiser
""")
if r != 0:
raise Exception(
f"Error building: {component_name} for board: {board.name} config: {config.name}"
)
dest.unlink(missing_ok=True)
capdl_init_elf = rust_target_dir / cargo_target / "release" / "initialiser"
shutil.copy(capdl_init_elf, dest)
# Make output read-only
dest.chmod(0o744)
def main() -> None:
parser = ArgumentParser()
parser.add_argument("--sel4", type=Path, required=True)
parser.add_argument("--tool-target-triple", default=get_tool_target_triple(), help="Compile the Microkit tool for this target triple")
parser.add_argument("--llvm", action="store_true", help="Cross-compile seL4 and Microkit's run-time targets with LLVM")
parser.add_argument("--boards", metavar="BOARDS", help="Comma-separated list of boards to support. When absent, all boards are supported.")
parser.add_argument("--configs", metavar="CONFIGS", help="Comma-separated list of configurations to support. When absent, all configurations are supported.")
parser.add_argument("--skip-tool", action="store_true", help="Tool will not be built")
parser.add_argument("--skip-run-time", action="store_true", help="Run-time targets will not be built")
parser.add_argument("--skip-sel4", action="store_true", help="seL4 will not be built")
parser.add_argument("--skip-initialiser", action="store_true", help="Initialiser will not be built")
parser.add_argument("--skip-docs", action="store_true", help="Docs will not be built")
parser.add_argument("--skip-tar", action="store_true", help="SDK and source tarballs will not be built")
parser.add_argument("--release-packaging", action="store_true", help="All SDKs for distribution will be produced")
# Read from the version file as unless someone has specified
# a version, that is the source of truth
with open("VERSION", "r") as f:
default_version = f.read().strip()
parser.add_argument("--version", default=default_version, help="SDK version")
for arch in KernelArch:
arch_str = arch.name.lower()
parser.add_argument(f"--gcc-toolchain-prefix-{arch_str}", default=arch.target_triple(), help=f"GCC toolchain prefix when compiling for {arch_str}, e.g {arch_str}-none-elf")
args = parser.parse_args()
global TRIPLE_AARCH64
global TRIPLE_RISCV
global TRIPLE_X86_64
TRIPLE_AARCH64 = args.gcc_toolchain_prefix_aarch64
TRIPLE_RISCV = args.gcc_toolchain_prefix_riscv64
TRIPLE_X86_64 = args.gcc_toolchain_prefix_x86_64
version = args.version
if args.boards is not None:
supported_board_names = frozenset(board.name for board in SUPPORTED_BOARDS)
selected_board_names = frozenset(args.boards.split(","))
for board_name in selected_board_names:
if board_name not in supported_board_names:
raise Exception(f"Trying to build a board: {board_name} that does not exist in supported list.")
selected_boards = [board for board in SUPPORTED_BOARDS if board.name in selected_board_names]
else:
selected_boards = SUPPORTED_BOARDS
build_goals: list[tuple[BoardInfo, list[ConfigInfo]]] = []
for board in selected_boards:
elaborated_configs = elaborate_all_board_configs(board)
if args.configs is not None:
elaborated_config_names = frozenset(config.name for config in elaborated_configs)
selected_config_names = frozenset(args.configs.split(","))
if invalid_config_names := selected_config_names.difference(elaborated_config_names):
raise Exception(
"You asked for invalid config(s) '{}' but board '{}' only supports config(s) '{}'"
.format(", ".join(invalid_config_names), board.name, ", ".join(elaborated_config_names))
)
elaborated_configs = [config for config in elaborated_configs if config.name in selected_config_names]
build_goals.append((board, elaborated_configs))
sel4_dir = args.sel4.expanduser()
if not sel4_dir.exists():
raise Exception(f"sel4_dir: {sel4_dir} does not exist")
tool_dir = Path("tool/microkit")
sdk_dir = Path("release") / f"{NAME}-sdk-{version}"
tar_file = Path("release") / f"{NAME}-sdk-{version}.tar.gz"
source_tar_file = Path("release") / f"{NAME}-source-{version}.tar.gz"
dir_structure = [
sdk_dir / "bin",
sdk_dir / "board",
]
if not args.skip_docs:
dir_structure.append(sdk_dir / "doc")
for (board, configs) in build_goals:
board_dir = sdk_dir / "board" / board.name
dir_structure.append(board_dir)
for config in configs:
config_dir = board_dir / config.name
dir_structure.append(config_dir)
dir_structure += [
config_dir / "include",
config_dir / "lib",
config_dir / "elf",
]
for dr in dir_structure:
dr.mkdir(exist_ok=True, parents=True)
with open(sdk_dir / "VERSION", "w+") as f:
f.write(version + "\n")
shutil.copy(Path("LICENSE.md"), sdk_dir)
licenses_dir = Path("LICENSES")
licenses_dest_dir = sdk_dir / "LICENSES"
for p in licenses_dir.rglob("*"):
if not p.is_file():
continue
rel = p.relative_to(licenses_dir)
dest = licenses_dest_dir / rel
dest.parent.mkdir(exist_ok=True, parents=True)
dest.unlink(missing_ok=True)
shutil.copy(p, dest)
dest.chmod(0o744)
if not args.skip_tool:
tool_target = sdk_dir / "bin" / "microkit"
test_tool()
build_tool(tool_target, args.tool_target_triple)
if not args.skip_docs:
build_doc(sdk_dir)
if not args.skip_run_time:
build_dir = Path("build")
for (board, configs) in build_goals:
for config in configs:
if not args.skip_sel4:
build_sel4(sel4_dir, tool_dir, sdk_dir, build_dir, board, config, args.llvm)