使用Rust为OpenWRT开发应用.

最近阅读了 liigo大侠 的文章 MIPS平台OpenWrt路由器系统内的Rust应用程序开发, 受益颇深, 于是产生了 将 Rust 集成到OpenWRT软件包的冲动。经过一两天的尝试,终于初步取得成功. 下面分享下具体的过程.


第一步:安装依赖项

apt-get install gyp bzip2

第二步:修改Python软件包

首先假定您在OpenWRT的目录下,
进入到 package/feeds/packages/python/patches/
打开 110-enable-zlib.patch
用如下内容覆盖

---
 Modules/Setup.dist |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)


--- a/Modules/Setup.dist
+++ b/Modules/Setup.dist
@@ -460,7 +460,9 @@ GLHACK=-Dclear=__GLclear
 # Andrew Kuchling's zlib module.
 # This require zlib 1.1.3 (or later).
 # See http://www.gzip.org/zlib/
-#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
+zlib zlibmodule.c -lz
+bz2 bz2module.c -lbz2  
+thread threadmodule.c -lpthread


 # Interface to the Expat XML parser
 #

  打开package/feeds/packages/python/Makefile 文件,找到 define Host/Configure 节,将里面的 --without-threads 修改成 --with-threads
  最后执行 make package/feeds/packages/python/host/install V=s.


最后 在package目录下创建一个 rust目录。并新建一个Makefile

include $(TOPDIR)/rules.mk

PKG_NAME:=rust
PKG_VERSION:=0.12.0-nightly
PKG_RELEASE:=1

PKG_SOURCE_PROTO:=git
PKG_SOURCE_VERSION:=master
PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)

PKG_SOURCE_URL:=https://github.com/rust-lang/$(PKG_NAME).git
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz

PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)/

PKG_BUILD_PARALLEL:=1
HOST_BUILD_PARALLEL:=1
HOST_BUILD_DEPENDS:= python/host python/bzip2/host


include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/host-build.mk

define Package/rust
  SUBMENU:=RUST
  CATEGORY:=Languages
  TITLE:=The Rust Programming Language
  URL:=http://www.rust-lang.org/
endef

define Package/rust/description
  Rust is a systems programming language that runs blazingly fast, prevents almost all crashes*, and eliminates data races. 
endef



define Build/Configure
	cd $(PKG_BUILD_DIR) &&	./configure \
		$(CONFIGURE_VARS)
endef

define Build/Compile
	$(MAKE) -C $(PKG_BUILD_DIR)
endef

define Build/InstallDev

endef

define Package/rust/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/lua $(1)/usr/bin/
endef

RUST_HOST_NAME:=$(shell echo $(GNU_HOST_NAME) | awk 'BEGIN{FS="-"} {print $$1"-unknown-"$$2"-"$$3}')

HOST_CONFIGURE_VARS = \
	--target=$(REAL_GNU_TARGET_NAME) \
	--host=$(RUST_HOST_NAME) \
	--build=$(RUST_HOST_NAME) \
	--prefix=$(STAGING_DIR_HOST) \
	--disable-jemalloc \
	--disable-docs
	

define Host/Configure
	CC=$(HOSTCC_NOCACHE)
	cd $(HOST_BUILD_DIR) &&	./configure \
		$(HOST_CONFIGURE_VARS)
endef

define Host/Compile
	( \
		cd $(HOST_BUILD_DIR) && $(MAKE) \
	 )
endef

define Host/Install
	(cd $(HOST_BUILD_DIR) && $(MAKE) install)
endef

$(eval $(call HostBuild))
$(eval $(call BuildPackage,rust))

针对目标平台制作补丁,并将补丁放到 package/rust/patches 目录下

以我的路由器为例: target 为 mipsel-openwrt-linux-uclibc, 我生成的补丁为:  001-append-mipsel-linux.patch

diff -uNr ./a/mk/platform.mk ./b/mk/platform.mk
--- a/mk/platform.mk
+++ b/mk/platform.mk
@@ -404,6 +404,36 @@
 RUSTC_FLAGS_mipsel-linux := -C target-cpu=mips32 -C target-feature="+mips32,+o32"
 
 
+# mipsel-openwrt-linux-uclibc configuration
+CC_mipsel-openwrt-linux-uclibc=mipsel-openwrt-linux-uclibc-gcc
+CXX_mipsel-openwrt-linux-uclibc=mipsel-openwrt-linux-uclibc-g++
+CPP_mipsel-openwrt-linux-uclibc=mipsel-openwrt-linux-uclibc-gcc
+AR_mipsel-openwrt-linux-uclibc=mipsel-openwrt-linux-uclibc-ar
+CFG_LIB_NAME_mipsel-openwrt-linux-uclibc=lib$(1).so
+CFG_STATIC_LIB_NAME_mipsel-openwrt-linux-uclibc=lib$(1).a
+CFG_LIB_GLOB_mipsel-openwrt-linux-uclibc=lib$(1)-*.so
+CFG_LIB_DSYM_GLOB_mipsel-openwrt-linux-uclibc=lib$(1)-*.dylib.dSYM
+CFG_CFLAGS_mipsel-openwrt-linux-uclibc := -mips32 -mabi=32 $(CFLAGS)
+CFG_GCCISH_CFLAGS_mipsel-openwrt-linux-uclibc := -Wall -g -fPIC -mips32 -mabi=32 $(CFLAGS)
+CFG_GCCISH_CXXFLAGS_mipsel-openwrt-linux-uclibc := -fno-rtti $(CXXFLAGS)
+CFG_GCCISH_LINK_FLAGS_mipsel-openwrt-linux-uclibc := -shared -fPIC -g -mips32
+CFG_GCCISH_DEF_FLAG_mipsel-openwrt-linux-uclibc := -Wl,--export-dynamic,--dynamic-list=
+CFG_GCCISH_PRE_LIB_FLAGS_mipsel-openwrt-linux-uclibc := -Wl,-whole-archive
+CFG_GCCISH_POST_LIB_FLAGS_mipsel-openwrt-linux-uclibc := -Wl,-no-whole-archive
+CFG_DEF_SUFFIX_mipsel-openwrt-linux-uclibc := .linux.def
+CFG_LLC_FLAGS_mipsel-openwrt-linux-uclibc :=
+CFG_INSTALL_NAME_mipsel-openwrt-linux-uclibc =
+CFG_LIBUV_LINK_FLAGS_mipsel-openwrt-linux-uclibc =
+CFG_EXE_SUFFIX_mipsel-openwrt-linux-uclibc :=
+CFG_WINDOWSY_mipsel-openwrt-linux-uclibc :=
+CFG_UNIXY_mipsel-openwrt-linux-uclibc := 1
+CFG_PATH_MUNGE_mipsel-openwrt-linux-uclibc := true
+CFG_LDPATH_mipsel-openwrt-linux-uclibc :=
+CFG_RUN_mipsel-openwrt-linux-uclibc=
+CFG_RUN_TARG_mipsel-openwrt-linux-uclibc=
+RUSTC_FLAGS_mipsel-openwrt-linux-uclibc := -C target-cpu=mips32 -C target-feature="+mips32,+o32"
+
+
 # mips-unknown-linux-gnu configuration
 CC_mips-unknown-linux-gnu=mips-linux-gnu-gcc
 CXX_mips-unknown-linux-gnu=mips-linux-gnu-g++

最后执行

 make package/rust/host/install V=s 进入漫长的等待吧。

如果不出意外,一切成功后,您就可以在OpenWRT里创建Rust语言编写的软件包了。


上述代码和项目都已经放到了github。

位置: https://github.com/bywayboy/openwrt-packages/tree/master/rust

hellorust 是一个rust编写的测试项目

位置:https://github.com/bywayboy/openwrt-packages/tree/master/hellorust




没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试