Target
Use the rust language to develop application software on the MIPs (EL) + openwrt router platform.
Compile rustc
First, compile the source code of the rust compiler and generate the cross compiler rustc that supports the mipsel-Linux platform.
[Plain]View plaincopy
- ./Configure -- target = mipsel-Linux & make install
Note that during the compilation process, the MIPs (EL) + openwrt platform development kit SDK will be called, specifically, mipsel-Linux-GCC and mipsel-Linux-ar. However, the tool naming format in the SDK is mipsel-openwrt-Linux-uclibc-gcc/AR, which is different from that in the rust compiling script. A simple method is to create a symbolic link file:
[Plain]View plaincopy
- 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
Use the rust standard library std
Compile the source file histd. RS:
[CPP]View plaincopy
- FN main (){
- Println! ("Hi rust! (Uses std crate )");
- }
Compile histd:
[Plain]View plaincopy
- Rustc -- target = mipsel-Linux-C linker = mipsel-Linux-GCC histd. RS
The executable file histd under the target platform will be generated. The file size is 1,389,884 bytes, about 1.32 MB, which is quite large! Vro devices are almost unacceptable.
This is an executable file generated by static compilation without additional Runtime Library dependencies. Strictly speaking, it only depends on libc. So in the target system. Of course, if you do not need to output text to the console, you do not need libc. So.
Use the rust core library (without the standard library STD)
Compile the source file hicore. RS:
[CPP]View plaincopy
- #! [No_std]
- #! [Feature (lang_items)]
- Extern crate libc;
- Extern crate core;
- Use libc: puts;
- Use core: intrinsics: transmute;
- # [Start]
- FN start (_ argc: int, _ argv: * 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 {}
- }
Compile hicore. RS:
[Plain]View plaincopy
- Rustc -- target = mipsel-Linux-C linker = mipsel-Linux-GCC hicore. RS
The executable file hicore under the target platform will be generated. The file size is only 6556 bytes, which is quite small!
This is an executable file generated by static compilation without additional Runtime Library dependencies. Strictly speaking, it only depends on libc. So in the target system. Of course, if you do not need to output text to the console, you do not need libc. So.
No standard library STD, no core library CORE, pure streaking
Compile the source code hi. RS:
[CPP]View plaincopy
- #! [No_std]
- #! [Feature (lang_items)]
- #! [Feature (intrinsics)]
- # [LINK (name = "C")]
- Extern {
- FN puts (S: * const u8 );
- }
- # [Start]
- FN start (_ argc: int, _ argv: * 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;
- }
Compile hi. RS:
[Plain]View plaincopy
- Rustc -- target = mipsel-Linux-C linker = mipsel-Linux-GCC hi. RS
The executable file hi under the target platform will be generated. The file size is only 6504 bytes, which is quite small! It is smaller than the hicore that uses the core library core, but the difference is not big.
This is an executable file generated by static compilation without additional Runtime Library dependencies. Strictly speaking, it only depends on libc. So in the target system. Of course, if you do not need to output text to the console, you do not need libc. So.
Comparison of executable file sizes
Histd compiled using the rust standard library STD, hicore compiled using the rust core library CORE, and hicore generated without the standard library or core library compilation, the size of executable files of the three is compared as follows:
[Plain]View plaincopy
- -Rwxr-XR-x 1 liigo 1389884 20:20 histd
- -Rwxr-XR-x 1 liigo 6556 September 17 20:19 hicore
- -Rwxr-XR-x 1 liigo 6504 September 17 20:19 hi
The histd program compiled using the standard library STD is too large and is not suitable for embedded devices. It is eliminated first. The hicore program compiled using the core library CORE is very small and comparable to the hicore program that does not use any library. Since core of the core library is not used, the file size remains unchanged, and core of the core library can bring a lot of coding convenience, it is recommended to use core of the core library in the embedded platform.
The above is the situation of the mipsel-Linux platform. Below as a comparison, let's look at the x86_64-unknown-linux-gnu platform:
[Plain]View plaincopy
- -Rwxr-XR-x 1 liigo 1004136 20:16 histd
- -Rwxr-XR-x 1 liigo 8203 September 17 20:16 hicore
- -Rwxr-XR-x 1 liigo 8159 September 17 20:16 hi
After comparison, it is found that the executable files on the x86_64 platform are slightly larger than those on the mipsel platform, but the gap is not big, and the situation is similar.
Target triples)
Information Source: https://github.com/rust-lang/rust/blob/master/mk/platform.mk
[Plain]View plaincopy
- 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
Usage:
Compile rust itself:./configure -- target = triple1, triple2 & make install
Compile the rust program: rustc -- target = triple-C linker = triple-gcc
If necessary, create a symbolic link file, such as triple-gcc/triple-AR, for the SDK compilation tool of the Cross-compilation platform.
Development of rust applications in the openwrt router system of MIPS Platform