Skip to content

Commit 825b115

Browse files
vladandrewunikraft-bot
authored andcommitted
build: Add embedded rust support
This patch adds embedded rust support. Only the core crate is available. Internal libraies may be written in rust now. We're using rustc as a rust compiler. The compilation flags are different from GCC and thus we introduce new flags for rust sources such as as a -Copt_lvl="2". LTO is available only when using the clang toolchain for C/C++ sources. DCE is available and done at linking. The sources should have #![no_std] and a panic handler, e.g: ``` use core::panic::PanicInfo; fn panic(_info: &PanicInfo) -> ! { loop {} } ``` Signed-off-by: Vlad-Andrei Badoiu <[email protected]> Reviewed-by: Simon Kuenzer <[email protected]> Tested-by: Unikraft CI <[email protected]> GitHub-Pull-Request: unikraft#241
1 parent 823852b commit 825b115

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ CXXINCLUDES :=
319319
CXXINCLUDES-y :=
320320
GOCFLAGS :=
321321
GOCFLAGS-y :=
322+
RUSTCFLAGS :=
323+
RUSTCFLAGS-y :=
322324
GOCINCLUDES :=
323325
GOCINCLUDES-y :=
324326
DBGFLAGS :=
@@ -568,6 +570,13 @@ CC := $(CONFIG_CROSS_COMPILE)$(CONFIG_COMPILER)
568570
CPP := $(CC)
569571
CXX := $(CPP)
570572
GOC := $(CONFIG_CROSS_COMPILE)gccgo-7
573+
# We use rustc because the gcc frontend is experimental and missing features such
574+
# as borrowing checking
575+
ifneq ("$(origin LLVM_TARGET_ARCH)","undefined")
576+
RUSTC := rustc --target=$(CONFIG_LLVM_TARGET_ARCH)
577+
else
578+
RUSTC := rustc
579+
endif
571580
AS := $(CC)
572581
AR := $(CONFIG_CROSS_COMPILE)gcc-ar
573582
NM := $(CONFIG_CROSS_COMPILE)gcc-nm

Makefile.uk

+24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ CINCLUDES += -I$(CONFIG_UK_BASE)/include
2121
CXXINCLUDES += -I$(CONFIG_UK_BASE)/include
2222
GOCINCLUDES += -I$(CONFIG_UK_BASE)/include
2323

24+
RUSTCFLAGS-y += --emit=obj --crate-type=rlib --edition=2018 \
25+
-Cpanic=abort -Cembed-bitcode=n \
26+
-Zbinary_dep_depinfo=y -Zsymbol-mangling-version=v0 \
27+
-Cforce-unwind-tables=n -Ccodegen-units=1 \
28+
-Dunsafe_op_in_unsafe_fn -Drust_2018_idioms
29+
30+
31+
RUSTCFLAGS-$(CONFIG_OPTIMIZE_NONE) += -Copt-level="0"
32+
RUSTCFLAGS-$(CONFIG_OPTIMIZE_SIZE) += -Copt-level="s"
33+
RUSTCFLAGS-$(CONFIG_OPTIMIZE_PERF) += -Copt-level="2"
34+
35+
RUSTCFLAGS-$(CONFIG_DEBUG_SYMBOLS_LVL0) += -Cdebuginfo=0
36+
RUSTCFLAGS-$(CONFIG_DEBUG_SYMBOLS_LVL1) += -Cdebuginfo=1
37+
RUSTCFLAGS-$(CONFIG_DEBUG_SYMBOLS_LVL2) += -Cdebuginfo=2
38+
# NOTE: There is not level 3 in rustc
39+
RUSTCFLAGS-$(CONFIG_DEBUG_SYMBOLS_LVL3) += -Cdebuginfo=2
40+
41+
# NOTE: rustc supports LTO only with clang
42+
ifeq ($(call have_clang),y)
43+
RUSTCFLAGS-$(CONFIG_OPTIMIZE_LTO) += -Clinker-plugin-lto
44+
else
45+
RUSTCFLAGS-y += -Cembed-bitcode=n -Clto=n
46+
endif
47+
2448
LIBLDFLAGS += -nostdlib -Wl,-r -Wl,-d -Wl,--build-id=none -no-pie
2549
LIBLDFLAGS-$(call have_gcc) += -nostdinc
2650

support/build/Makefile.rules

+18
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,24 @@ buildrule_CPP = $(call buildrule_cxx,$(1),$(2),$(3),$(4))
574574
buildrule_C = $(call buildrule_cxx,$(1),$(2),$(3),$(4))
575575
buildrule_c$(plus)$(plus) = $(call buildrule_cxx,$(1),$(2),$(3),$(4))
576576

577+
# NOTE: We are not using most of the flags such as COMPFLAGS due to incompatibilities between rustc and GCC.
578+
define buildrule_rs =
579+
$(4): $(2) | preprocess
580+
$(call build_cmd,RUSTC,$(1),$(4),\
581+
$(RUSTC) $$(RUSTCFLAGS) $$(RUSTCFLAGS-y) $$(RUSTCFLAGS_EXTRA) \
582+
$$($(call vprefix_lib,$(1),RUSTCFLAGS)) $$($(call vprefix_lib,$(1),RUSTCFLAGS-y)) \
583+
$$($(call vprefix_src,$(1),$(2),$(3),FLAGS)) $$($(call vprefix_src,$(1),$(2),$(3),FLAGS-y)) \
584+
--cfg '__LIBNAME__="$(1)"' --cfg '__BASENAME__="$(notdir $(2))"' $(if $(3),--cfg '__VARIANT__="$(3)"') \
585+
$(2) -o $(4)
586+
)
587+
588+
UK_SRCS-y += $(2)
589+
UK_DEPS-y += $(call out2dep,$(4))
590+
UK_OBJS-y += $(4)
591+
$(eval $(call vprefix_lib,$(1),OBJS-y) += $(4))
592+
$(eval $(call vprefix_lib,$(1),CLEAN-y) += $(call build_clean,$(4)) $(call out2dep,$(4)))
593+
endef
594+
577595
define add_lds_to_plat =
578596
$(eval $(call uc,$(2))_LD_SCRIPT-y += $(1))
579597
endef

0 commit comments

Comments
 (0)