rust: refactor build helper
authorTianling Shen <cnsztl@immortalwrt.org>
Sun, 19 Mar 2023 06:07:48 +0000 (14:07 +0800)
committerTianling Shen <cnsztl@gmail.com>
Thu, 23 Mar 2023 18:08:47 +0000 (02:08 +0800)
Added new RustBinPackage, RustBinHostBuild wrapper.
Added new RUST_PKG_FEATURES flag.
Moved CARGO_HOME to STAGING_DIR_HOSTPKG.
Overrode default Build/Compile and Host/Compile to Cargo build.

Signed-off-by: Tianling Shen <cnsztl@immortalwrt.org>
lang/rust/Makefile
lang/rust/rust-host-build.mk [new file with mode: 0644]
lang/rust/rust-host.mk [deleted file]
lang/rust/rust-package.mk
lang/rust/rust-values.mk [new file with mode: 0644]

index f01e75e0b5b338603ab06bc4f197f3f38648bb0d..1c47fbf76efa344433d9f4b98b337b4020f921c7 100644 (file)
@@ -20,9 +20,9 @@ PKG_LICENSE_FILES:=LICENSE-APACHE LICENSE-MIT
 HOST_BUILD_DEPENDS:=python3/host
 PKG_HOST_ONLY:=1
 
-include $(INCLUDE_DIR)/package.mk
 include $(INCLUDE_DIR)/host-build.mk
-include ./rust-host.mk
+include $(INCLUDE_DIR)/package.mk
+include ./rust-values.mk
 
 define Package/rust
   SECTION:=lang
diff --git a/lang/rust/rust-host-build.mk b/lang/rust/rust-host-build.mk
new file mode 100644 (file)
index 0000000..39cf1f2
--- /dev/null
@@ -0,0 +1,45 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Copyright (C) 2023 Luca Barbato and Donald Hoskins
+
+ifeq ($(origin RUST_INCLUDE_DIR),undefined)
+  RUST_INCLUDE_DIR:=$(dir $(lastword $(MAKEFILE_LIST)))
+endif
+include $(RUST_INCLUDE_DIR)/rust-values.mk
+
+# $(1) path to the package (optional)
+# $(2) additional arguments to cargo (optional)
+define Host/Compile/Cargo
+       ( \
+               cd $(HOST_BUILD_DIR) ; \
+               export PATH="$(CARGO_HOME)/bin:$(PATH)" ; \
+               CARGO_HOME=$(CARGO_HOME) \
+               CC=$(HOSTCC_NOCACHE) \
+               cargo install -v \
+                       --profile stripped \
+                       $(if $(RUST_PKG_FEATURES),--features "$(RUST_PKG_FEATURES)") \
+                       --root $(HOST_INSTALL_DIR) \
+                       --path "$(if $(strip $(1)),$(strip $(1)),.)" $(2) ; \
+       )
+endef
+
+define Host/Uninstall/Cargo
+       ( \
+               cd $(HOST_BUILD_DIR) ; \
+               export PATH="$(CARGO_HOME)/bin:$(PATH)" ; \
+               CARGO_HOME=$(CARGO_HOME) \
+               CC=$(HOSTCC_NOCACHE) \
+               cargo uninstall -v \
+                       --root $(HOST_INSTALL_DIR) || true ; \
+       )
+endef
+
+define RustBinHostBuild
+  define Host/Install
+       $(INSTALL_DIR) $(STAGING_DIR_HOSTPKG)/bin
+       $(INSTALL_BIN) $(HOST_INSTALL_DIR)/bin/* $(STAGING_DIR_HOSTPKG)/bin/
+  endef
+endef
+
+Host/Compile=$(call Host/Compile/Cargo)
+Host/Uninstall=$(call Host/Uninstall/Cargo)
diff --git a/lang/rust/rust-host.mk b/lang/rust/rust-host.mk
deleted file mode 100644 (file)
index acc93c5..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-# SPDX-License-Identifier: GPL-2.0-only
-#
-# Copyright (C) 2023 Luca Barbato and Donald Hoskins
-
-# Rust Environmental Vars
-CONFIG_HOST_SUFFIX:=$(word 4, $(subst -, ,$(GNU_HOST_NAME)))
-RUSTC_HOST_ARCH:=$(HOST_ARCH)-unknown-linux-$(CONFIG_HOST_SUFFIX)
-CARGO_HOME:=$(STAGING_DIR_HOST)/cargo
-
-# Support only a subset for now.
-RUST_ARCH_DEPENDS:=@(aarch64||arm||i386||i686||mips||mipsel||mips64||mips64el||mipsel||powerpc64||x86_64)
-
-# Common Build Flags
-RUST_BUILD_FLAGS = \
-  CARGO_HOME="$(CARGO_HOME)"
-
-# This adds the rust environmental variables to Make calls
-MAKE_FLAGS += $(RUST_BUILD_FLAGS)
-
-# Force linking of the SSP library
-ifdef CONFIG_PKG_CC_STACKPROTECTOR_REGULAR
-  ifeq ($(strip $(PKG_SSP)),1)
-    RUSTC_LDFLAGS += -lssp_nonshared
-  endif
-endif
-ifdef CONFIG_PKG_CC_STACKPROTECTOR_STRONG
-  ifeq ($(strip $(PKG_SSP)),1)
-    TARGET_CFLAGS += -lssp_nonshared
-  endif
-endif
-
-# mips64 openwrt has a specific targed in rustc
-ifeq ($(ARCH),mips64)
-  RUSTC_TARGET_ARCH:=$(REAL_GNU_TARGET_NAME)
-else
-  RUSTC_TARGET_ARCH:=$(subst openwrt,unknown,$(REAL_GNU_TARGET_NAME))
-endif
-
-RUSTC_TARGET_ARCH:=$(subst muslgnueabi,musleabi,$(RUSTC_TARGET_ARCH))
-
-ifeq ($(ARCH),i386)
-  RUSTC_TARGET_ARCH:=$(subst i486,i586,$(RUSTC_TARGET_ARCH))
-endif
-
-# ARM Logic
-ifeq ($(ARCH),arm)
-  ifeq ($(CONFIG_arm_v7),y)
-    RUSTC_TARGET_ARCH:=$(subst arm,armv7,$(RUSTC_TARGET_ARCH))
-  endif
-
-  ifeq ($(CONFIG_HAS_FPU),y)
-    RUSTC_TARGET_ARCH:=$(subst musleabi,musleabihf,$(RUSTC_TARGET_ARCH))
-  endif
-endif
-
-ifeq ($(ARCH),aarch64)
-    RUST_CFLAGS:=-mno-outline-atomics
-endif
index 54c2aa89ac5be0accc2e214562359f02f3229d74..4e39009cc03638db63995e44b4cf67a6f9fb29c9 100644 (file)
@@ -2,27 +2,49 @@
 #
 # Copyright (C) 2023 Luca Barbato and Donald Hoskins
 
-rust_mk_path:=$(dir $(lastword $(MAKEFILE_LIST)))
-include $(rust_mk_path)rust-host.mk
+# Variables (all optional) to be set in package Makefiles:
+#
+# RUST_PKG_FEATURES - list of options, default empty
+#
+#   Space or comma separated list of features to activate
+#
+#   e.g. RUST_PKG_FEATURES:=enable-foo,with-bar
 
-# $(1) path to the package
-# $(2) additional arguments to cargo
-define Host/Compile/Cargo
-       ( \
-               cd $(HOST_BUILD_DIR) ; \
-               export PATH="$(CARGO_HOME)/bin:$(PATH)" ; \
-               CARGO_HOME=$(CARGO_HOME) CC=$(HOSTCC) \
-                       cargo install -v --profile stripped --root $(HOST_INSTALL_DIR) --path "$(if $(strip $(1)),$(strip $(1)),.)" $(2) ; \
-       )
-endef
+ifeq ($(origin RUST_INCLUDE_DIR),undefined)
+  RUST_INCLUDE_DIR:=$(dir $(lastword $(MAKEFILE_LIST)))
+endif
+include $(RUST_INCLUDE_DIR)/rust-values.mk
+
+# Support only a subset for now.
+RUST_ARCH_DEPENDS:=@(aarch64||arm||i386||i686||mips||mipsel||mips64||mips64el||mipsel||powerpc64||x86_64)
 
-# $(1) path to the package
-# $(2) additional arguments to cargo
+# $(1) path to the package (optional)
+# $(2) additional arguments to cargo (optional)
 define Build/Compile/Cargo
        ( \
                cd $(PKG_BUILD_DIR) ; \
                export PATH="$(CARGO_HOME)/bin:$(PATH)" ; \
-               CARGO_HOME=$(CARGO_HOME) TARGET_CFLAGS="$(TARGET_CFLAGS) $(RUST_CFLAGS)" TARGET_CC=$(TARGET_CC_NOCACHE) CC=cc \
-                       cargo install -v --profile stripped --target $(RUSTC_TARGET_ARCH) --root $(PKG_INSTALL_DIR) --path "$(if $(strip $(1)),$(strip $(1)),.)" $(2) ; \
+               CARGO_HOME=$(CARGO_HOME) \
+               TARGET_CFLAGS="$(TARGET_CFLAGS) $(RUST_CFLAGS)" \
+               TARGET_CC=$(TARGET_CC_NOCACHE) \
+               CC=$(HOSTCC) \
+               cargo install -v \
+                       --profile stripped \
+                       --target $(RUSTC_TARGET_ARCH) \
+                       $(if $(strip $(RUST_PKG_FEATURES)),--features "$(strip $(RUST_PKG_FEATURES))") \
+                       --root $(PKG_INSTALL_DIR) \
+                       --path "$(if $(strip $(1)),$(strip $(1)),.)" \
+                       $(2) ; \
        )
 endef
+
+define RustBinPackage
+  ifndef Package/$(1)/install
+    define Package/$(1)/install
+       $$(INSTALL_DIR) $$(1)/usr/bin/
+       $$(INSTALL_BIN) $$(PKG_INSTALL_DIR)/bin/* $$(1)/usr/bin/
+    endef
+  endif
+endef
+
+Build/Compile=$(call Build/Compile/Cargo)
diff --git a/lang/rust/rust-values.mk b/lang/rust/rust-values.mk
new file mode 100644 (file)
index 0000000..7fcfcf2
--- /dev/null
@@ -0,0 +1,48 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Copyright (C) 2023 Luca Barbato and Donald Hoskins
+
+# Rust Environmental Vars
+CONFIG_HOST_SUFFIX:=$(word 4, $(subst -, ,$(GNU_HOST_NAME)))
+RUSTC_HOST_ARCH:=$(HOST_ARCH)-unknown-linux-$(CONFIG_HOST_SUFFIX)
+CARGO_HOME:=$(STAGING_DIR_HOSTPKG)/cargo
+
+# Force linking of the SSP library
+ifdef CONFIG_PKG_CC_STACKPROTECTOR_REGULAR
+  ifeq ($(strip $(PKG_SSP)),1)
+    RUSTC_LDFLAGS += -lssp_nonshared
+  endif
+endif
+ifdef CONFIG_PKG_CC_STACKPROTECTOR_STRONG
+  ifeq ($(strip $(PKG_SSP)),1)
+    TARGET_CFLAGS += -lssp_nonshared
+  endif
+endif
+
+# mips64 openwrt has a specific targed in rustc
+ifeq ($(ARCH),mips64)
+  RUSTC_TARGET_ARCH:=$(REAL_GNU_TARGET_NAME)
+else
+  RUSTC_TARGET_ARCH:=$(subst openwrt,unknown,$(REAL_GNU_TARGET_NAME))
+endif
+
+RUSTC_TARGET_ARCH:=$(subst muslgnueabi,musleabi,$(RUSTC_TARGET_ARCH))
+
+ifeq ($(ARCH),i386)
+  RUSTC_TARGET_ARCH:=$(subst i486,i586,$(RUSTC_TARGET_ARCH))
+endif
+
+# ARM Logic
+ifeq ($(ARCH),arm)
+  ifeq ($(CONFIG_arm_v7),y)
+    RUSTC_TARGET_ARCH:=$(subst arm,armv7,$(RUSTC_TARGET_ARCH))
+  endif
+
+  ifeq ($(CONFIG_HAS_FPU),y)
+    RUSTC_TARGET_ARCH:=$(subst musleabi,musleabihf,$(RUSTC_TARGET_ARCH))
+  endif
+endif
+
+ifeq ($(ARCH),aarch64)
+    RUST_CFLAGS:=-mno-outline-atomics
+endif