Skip to content

Commit 4756039

Browse files
WhyNotHugoptrcnull
authored andcommitted
main/rust: Add support for UEFI targets
Enable support for compiling for UEFI targets. This includes compiling binaries such as bootloaders, stubs for EFI bundles and similar utilities. Specifically, I used this functionality to finish work a minimal EFI stub, candyboot: https://git.sr.ht/~whynothugo/candyboot The UEFI target is only supported by LLVM. GCC cannot produce native EFI binaries. When compiling for a UEFI target, GCC produces ELF binaries and uses GNU-EFI as a stub loader to run them inside EFI environments. This approach does not work when compiling Rust. The profiler_builtins crate fails to build for the UEFI target; it is a C component which requires a UEFI specific sysroot to compile. During coordination with upstream, it came up these compiler built-ins could not be disabled on a per-target basis. This is now implemented upstream, and available in Rust stable since 1.86.0. See: rust-lang/rust#135326
1 parent 8ae689a commit 4756039

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

main/rust/APKBUILD

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ depends="gcc musl-dev"
3434
makedepends_build="
3535
!libgit2-dev
3636
cargo-bootstrap>=$_bootver
37+
clang$_llvmver
3738
coreutils
3839
curl-dev
3940
file
@@ -90,6 +91,11 @@ subpackages="cargo"
9091
rustc-dev:_rustc_dev
9192
rustfmt
9293
"
94+
[ -z "$BOOTSTRAP" ] && case "$CTARGET" in
95+
aarch64*|x86_64*)
96+
subpackages="$subpackages $pkgname-uefi"
97+
;;
98+
esac
9399
source="https://static.rust-lang.org/dist/rustc-$pkgver-src.tar.xz
94100
alpine-move-py-scripts-to-share.patch
95101
alpine-target.patch
@@ -101,6 +107,7 @@ source="https://static.rust-lang.org/dist/rustc-$pkgver-src.tar.xz
101107
need-ssp_nonshared.patch
102108
no-export-ld-library-path.patch
103109
system-wasm-ld.patch
110+
system-uefi-ld.patch
104111
check-rustc
105112
"
106113
builddir="$srcdir/rustc-$pkgver-src"
@@ -255,6 +262,14 @@ build() {
255262
--set=target.wasm32-wasip2.wasi-root=/usr/share/wasi-sysroot
256263
'
257264
fi
265+
local _uefi_target=${CTARGET%%-*}-unknown-uefi
266+
if subpackage_types_has uefi; then
267+
targets="$targets,$_uefi_target"
268+
# Too many jobs uses too much memory and fails with OOM error.
269+
# This compounds when building this extra target.
270+
cores=$(nproc)
271+
JOBS=$((cores / 3 + 1))
272+
fi
258273

259274
# ./configure will not run when config.toml exists,
260275
# so assume we are restarting the build with ./x.py
@@ -295,6 +310,10 @@ build() {
295310
--set="target.$_target.cxx=${CROSS_COMPILE}c++" \
296311
--set="target.$_target.ar=${CROSS_COMPILE}ar" \
297312
--set="target.$_target.linker=${CROSS_COMPILE}cc" \
313+
--set="target.$_uefi_target.crt-static=true" \
314+
--set="target.$_uefi_target.cc=clang-$_llvmver" \
315+
--set="target.$_uefi_target.profiler=false" \
316+
--set="target.$_uefi_target.optimized-compiler-builtins=false" \
298317
--set="target.$_build.musl-root=/usr" \
299318
--set="target.$_build.crt-static=false" \
300319
--set="target.$_build.cc=cc" \
@@ -380,6 +399,9 @@ package() {
380399
_unpack_build_dist rust-std-wasm32-wasip1-threads "$pkgdir/usr" &
381400
_unpack_build_dist rust-std-wasm32-wasip2 "$pkgdir/usr" &
382401
fi
402+
if subpackage_types_has uefi; then
403+
_unpack_build_dist rust-std-${_target%%-*}-unknown-uefi "$pkgdir/usr" &
404+
fi
383405
wait
384406

385407
# Prevent rustc-dev from providing the same *.so files,
@@ -447,6 +469,13 @@ wasm() {
447469
amove usr/lib/rustlib/wasm32-*
448470
}
449471

472+
uefi() {
473+
pkgdesc="UEFI targets for rust"
474+
depends="rust=$pkgver-r$pkgrel lld"
475+
476+
amove usr/lib/rustlib/*-uefi
477+
}
478+
450479
analysis() {
451480
pkgdesc="Compiler analysis data for the Rust standard library"
452481
depends="$pkgname=$pkgver-r$pkgrel"
@@ -549,5 +578,6 @@ b283d31cc8d6559aa639d44f776e97c2a1e0f23c717b6784e549861b881555bd467fa6023180866a
549578
7c8644f5aa9138d72ef8d9d5bddc90d2767df171f92518bb5376f8ee676a62305533cb6ac045e323d6191d808f43c43cd741e8d2f09f70b41f1ef48283817e17 need-ssp_nonshared.patch
550579
5f2aca58a64eaa24fbcf894f8e6532fad69ba25c9e3e915fa5045de179b72053c12c7f1e87213b8bcde61acb94fa35ec79e45429cf444a62b5de81dc4c239123 no-export-ld-library-path.patch
551580
0a6ac7d297a49973f420c8bb678def4ceb69f74d16c542d654d37d93d55e3349acf0143a99b9aa4c39c546a5fcc92be9fead6b94e668254cdb834048f54af75a system-wasm-ld.patch
581+
f3ddb29ef6055eae2112836242123eb2d2643662827a38c789d608526dc15201fe6b2e1f32a4023b86a89831497045910ca5b0f0b831e78d080169c3ef60abd5 system-uefi-ld.patch
552582
c31fdfe8a9b3411576c75da46645cf0465b9053000a2ab49cf9b2f2733f679d6d33acbf236d67a20e14935d094a685453b7f1840180249f39d610fd0902c3125 check-rustc
553583
"

main/rust/system-uefi-ld.patch

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
diff --git a/compiler/rustc_target/src/spec/targets/aarch64_unknown_uefi.rs b/compiler/rustc_target/src/spec/targets/aarch64_unknown_uefi.rs
2+
index 9656024ddaa..787db13e382 100644
3+
--- a/compiler/rustc_target/src/spec/targets/aarch64_unknown_uefi.rs
4+
+++ b/compiler/rustc_target/src/spec/targets/aarch64_unknown_uefi.rs
5+
@@ -10,6 +10,7 @@ pub(crate) fn target() -> Target {
6+
base.add_pre_link_args(LinkerFlavor::Msvc(Lld::No), &["/machine:arm64"]);
7+
base.features = "+v8a".into();
8+
9+
+ base.linker = Some("lld".into());
10+
Target {
11+
llvm_target: "aarch64-unknown-windows".into(),
12+
metadata: crate::spec::TargetMetadata {
13+
diff --git a/compiler/rustc_target/src/spec/targets/x86_64_unknown_uefi.rs b/compiler/rustc_target/src/spec/targets/x86_64_unknown_uefi.rs
14+
index a11a79ff41a..d4900fbf536 100644
15+
--- a/compiler/rustc_target/src/spec/targets/x86_64_unknown_uefi.rs
16+
+++ b/compiler/rustc_target/src/spec/targets/x86_64_unknown_uefi.rs
17+
@@ -27,6 +27,7 @@ pub(crate) fn target() -> Target {
18+
// arguments, thus giving you access to full MMX/SSE acceleration.
19+
base.features = "-mmx,-sse,+soft-float".into();
20+
21+
+ base.linker = Some("lld".into());
22+
Target {
23+
llvm_target: "x86_64-unknown-windows".into(),
24+
metadata: crate::spec::TargetMetadata {

0 commit comments

Comments
 (0)