]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
build: Add embedded rust support
authorVlad-Andrei Badoiu <vlad_andrei.badoiu@upb.ro>
Sun, 27 Jun 2021 22:26:47 +0000 (01:26 +0300)
committerUnikraft <monkey@unikraft.io>
Wed, 4 Aug 2021 14:06:06 +0000 (14:06 +0000)
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 <vlad_andrei.badoiu@upb.ro>
Reviewed-by: Simon Kuenzer <simon.kuenzer@neclab.eu>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Pull-Request: #241

Makefile
Makefile.uk
support/build/Makefile.rules

index ce512cd506c99890e1091fcddc7d624a579c1894..5f0f622bf63d4786cf1f0d1e4a1f47f8c6343f0f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -319,6 +319,8 @@ CXXINCLUDES :=
 CXXINCLUDES-y :=
 GOCFLAGS :=
 GOCFLAGS-y :=
+RUSTCFLAGS :=
+RUSTCFLAGS-y :=
 GOCINCLUDES :=
 GOCINCLUDES-y :=
 DBGFLAGS :=
@@ -568,6 +570,13 @@ CC         := $(CONFIG_CROSS_COMPILE)$(CONFIG_COMPILER)
 CPP            := $(CC)
 CXX            := $(CPP)
 GOC            := $(CONFIG_CROSS_COMPILE)gccgo-7
+# We use rustc because the gcc frontend is experimental and missing features such
+# as borrowing checking
+ifneq ("$(origin LLVM_TARGET_ARCH)","undefined")
+RUSTC          := rustc --target=$(CONFIG_LLVM_TARGET_ARCH)
+else
+RUSTC          := rustc
+endif
 AS             := $(CC)
 AR             := $(CONFIG_CROSS_COMPILE)gcc-ar
 NM             := $(CONFIG_CROSS_COMPILE)gcc-nm
index b5520babee20b36949471e2e75674372a01b149e..1df4e3b57958ddc735710a7410c578084e0abe47 100644 (file)
@@ -21,6 +21,30 @@ CINCLUDES    += -I$(CONFIG_UK_BASE)/include
 CXXINCLUDES  += -I$(CONFIG_UK_BASE)/include
 GOCINCLUDES  += -I$(CONFIG_UK_BASE)/include
 
+RUSTCFLAGS-y   += --emit=obj --crate-type=rlib --edition=2018 \
+               -Cpanic=abort -Cembed-bitcode=n \
+               -Zbinary_dep_depinfo=y -Zsymbol-mangling-version=v0 \
+               -Cforce-unwind-tables=n -Ccodegen-units=1 \
+               -Dunsafe_op_in_unsafe_fn -Drust_2018_idioms
+
+
+RUSTCFLAGS-$(CONFIG_OPTIMIZE_NONE)         += -Copt-level="0"
+RUSTCFLAGS-$(CONFIG_OPTIMIZE_SIZE)         += -Copt-level="s"
+RUSTCFLAGS-$(CONFIG_OPTIMIZE_PERF)         += -Copt-level="2"
+
+RUSTCFLAGS-$(CONFIG_DEBUG_SYMBOLS_LVL0)     += -Cdebuginfo=0
+RUSTCFLAGS-$(CONFIG_DEBUG_SYMBOLS_LVL1)     += -Cdebuginfo=1
+RUSTCFLAGS-$(CONFIG_DEBUG_SYMBOLS_LVL2)     += -Cdebuginfo=2
+# NOTE: There is not level 3 in rustc
+RUSTCFLAGS-$(CONFIG_DEBUG_SYMBOLS_LVL3)     += -Cdebuginfo=2
+
+# NOTE: rustc supports LTO only with clang
+ifeq ($(call have_clang),y)
+RUSTCFLAGS-$(CONFIG_OPTIMIZE_LTO)      += -Clinker-plugin-lto
+else
+RUSTCFLAGS-y   += -Cembed-bitcode=n -Clto=n
+endif
+
 LIBLDFLAGS  += -nostdlib -Wl,-r -Wl,-d -Wl,--build-id=none -no-pie
 LIBLDFLAGS-$(call have_gcc)    += -nostdinc
 
index 5e8eeef2269a434f75bc5b4595851bcff4ee3670..4d47d0ebf55dda8ae929274e9b6bb3f3cde0fa82 100644 (file)
@@ -574,6 +574,24 @@ buildrule_CPP = $(call buildrule_cxx,$(1),$(2),$(3),$(4))
 buildrule_C   = $(call buildrule_cxx,$(1),$(2),$(3),$(4))
 buildrule_c$(plus)$(plus) = $(call buildrule_cxx,$(1),$(2),$(3),$(4))
 
+# NOTE: We are not using most of the flags such as COMPFLAGS due to incompatibilities between rustc and GCC.
+define buildrule_rs =
+$(4): $(2) | preprocess
+       $(call build_cmd,RUSTC,$(1),$(4),\
+               $(RUSTC) $$(RUSTCFLAGS) $$(RUSTCFLAGS-y) $$(RUSTCFLAGS_EXTRA) \
+                       $$($(call vprefix_lib,$(1),RUSTCFLAGS)) $$($(call vprefix_lib,$(1),RUSTCFLAGS-y)) \
+                       $$($(call vprefix_src,$(1),$(2),$(3),FLAGS)) $$($(call vprefix_src,$(1),$(2),$(3),FLAGS-y)) \
+                       --cfg '__LIBNAME__="$(1)"' --cfg '__BASENAME__="$(notdir $(2))"' $(if $(3),--cfg '__VARIANT__="$(3)"') \
+                       $(2) -o $(4)
+       )
+
+UK_SRCS-y += $(2)
+UK_DEPS-y += $(call out2dep,$(4))
+UK_OBJS-y += $(4)
+$(eval $(call vprefix_lib,$(1),OBJS-y) += $(4))
+$(eval $(call vprefix_lib,$(1),CLEAN-y) += $(call build_clean,$(4)) $(call out2dep,$(4)))
+endef
+
 define add_lds_to_plat =
 $(eval $(call uc,$(2))_LD_SCRIPT-y += $(1))
 endef