作者:Liigo(庄晓立)
日期:2014年9月17日
原创链接:http://blog.csdn.net/liigo/article/details/39347541
版权所有,转载请注明出处:http://blog.csdn.net/liigo
使用 Rust 语言,交叉编译开发 MIPS(el) + OpenWrt 路由器平台下的应用软件。
首先自行编译Rust编译器源代码,生成支持 mipsel-linux 平台的交叉编译器rustc
./configure --target=mipsel-linux && make && make install
注意编译过程中会调用 MIPS(el) + OpenWrt 平台的开发包SDK,具体来说就是 mipsel-linux-gcc 和 mipsel-linux-ar。但是SDK内的工具命名格式是 mipsel-openwrt-linux-uclibc-gcc/ar,跟Rust编译脚本说要求的不同。一个简单的做法是,创建符号软连接文件:
cd /path/to/openwrt/sdk/toolchain ln -s mipsel-openwrt-linux-uclibc-gcc mipsel-linux-gcc ln -s mipsel-openwrt-linux-uclibc-ar mipsel-linux-ar
编写源文件 histd.rs:
fn main() {
println!("Hi Rust! (uses std crate)");
}编译 histd:
rustc --target=mipsel-linux -C linker=mipsel-linux-gcc histd.rs
将生成目标平台下的可执行文件histd,文件尺寸是 1,389,884 字节,约 1.32 MB,相当的大!对于路由器设备而言,几乎是难以接受。
这是静态编译生成的可执行文件,没有额外的运行时库依赖。严格地说仅依赖目标系统内的libc.so,当然如果不需要向控制台输出文本,连libc.so也不需要。
编写源文件 hicore.rs:
#![no_std]
#![feature(lang_items)]
#![feature(intrinsics)]
extern crate libc;
extern crate core;
use libc::puts;
use core::intrinsics::transmute;
#[start]
fn start(_argc: int, _argv: *const *const u8) -> int {
let s = "Hi Rust! (uses core crate)\0"; // &str
unsafe {
let (s,_): (*const i8, uint) = transmute(s); // see core::raw::Slice
puts(s);
}
return 0;
}
#[lang = "stack_exhausted"] extern fn stack_exhausted() {}
#[lang = "eh_personality"] extern fn eh_personality() {}
#[lang = "begin_unwind"]
extern fn begin_unwind(_args: &core::fmt::Arguments,
_file: &str,
_line: uint) -> ! {
loop {}
}
编译 hicore.rs:
rustc --target=mipsel-linux -C linker=mipsel-linux-gcc hicore.rs
将生成目标平台下的可执行文件hicore,文件尺寸只有 6556 字节,相当的小!
这是静态编译生成的可执行文件,没有额外的运行时库依赖。严格地说仅依赖目标系统内的libc.so,当然如果不需要向控制台输出文本,连libc.so也不需要。
编写源代码 hi.rs:
#![no_std]
#![feature(lang_items)]
#![feature(intrinsics)]
#[link(name = "c")]
extern {
fn puts(s: *const u8);
}
#[start]
fn start(_argc: int, _argv: *const *const u8) -> int {
let s = "Hi Rust!\0"; // &str
unsafe {
let (s,_): (*const u8, uint) = transmute(s); // see core::raw::Slice
puts(s);
}
return 0;
}
#[lang = "stack_exhausted"] extern fn stack_exhausted() {}
#[lang = "eh_personality"] extern fn eh_personality() {}
extern "rust-intrinsic" {
fn transmute<T, U>(x: T) -> U;
}
编译 hi.rs:
rustc --target=mipsel-linux -C linker=mipsel-linux-gcc hi.rs将生成目标平台下的可执行文件hi,文件尺寸只有 6504 字节,相当的小!比使用核心库core的hicore还要小,但相差不大。
这是静态编译生成的可执行文件,没有额外的运行时库依赖。严格地说仅依赖目标系统内的libc.so,当然如果不需要向控制台输出文本,连libc.so也不需要。
使用Rust标准库std编译生成的histd,使用Rust核心库core编译生成的的hicore,和不用标准库也不用核心库编译生成的hi,这三者的可执行文件尺寸对比如下:
-rwxr-xr-x 1 liigo liigo 1389884 9月 17 20:20 histd -rwxr-xr-x 1 liigo liigo 6556 9月 17 20:19 hicore -rwxr-xr-x 1 liigo liigo 6504 9月 17 20:19 hi使用标准库std编译出来的程序histd太大,不适合嵌入式设备,首先被淘汰;使用核心库core编译出来的程序hicore很小,跟不使用任何库的hi不相上下。既然用不用核心库core,在文件尺寸上没有多大变化,而核心库core还能带来很多编码上的便利,故推荐在嵌入式平台内使用核心库core。
以上是 mipsel-linux 平台的情况。下面作为对比,我们再看一下 x86_64-unknown-linux-gnu 平台:
-rwxr-xr-x 1 liigo liigo 1004136 9月 17 20:16 histd -rwxr-xr-x 1 liigo liigo 8203 9月 17 20:16 hicore -rwxr-xr-x 1 liigo liigo 8159 9月 17 20:16 hi
对比后发现,x86_64 平台下的可执行文件总体上比 mipsel 平台略大,但差距也不大,情况也类似。
信息来源:https://github.com/rust-lang/rust/blob/master/mk/platform.mk
x86_64-unknown-linux-gnu i686-unknown-linux-gnu arm-apple-ios i386-apple-ios x86_64-apple-darwin i686-apple-darwin arm-linux-androideabi arm-unknown-linux-gnueabihf arm-unknown-linux-gnueabi mipsel-linux mips-unknown-linux-gnu i586-mingw32msvc i686-w64-mingw32 x86_64-w64-mingw32 x86_64-unknown-freebsd x86_64-pc-dragonfly-elf使用方法:
编译Rust本身:./configure --target=triple1,triple2 && make && make install
编译Rust程序:rustc --target=triple -C linker=triple-gcc
必要时创建对应交叉编译平台SDK编译工具的符号软连接文件 triple-gcc/triple-ar 等。
本文涉及到多个源代码文件已上传到Github:https://github.com/liigo/hirust ,作者Liigo。
MIPS平台OpenWrt路由器系统内的Rust应用程序开发
原文地址:http://blog.csdn.net/liigo/article/details/39347541