Rust交叉编译笔记

感谢张同学投稿!也欢迎其它同学踊跃投稿!下面直接上正文。

原文链接在此:http://note.youdao.com/share/?id=650a41814923803f55e0f626869363af&type=note

===============================================


https://github.com/joerg-krause/rust-cross-libs 交叉编译rust库方法

https://github.com/elebihan/buildroot 支持rust的buildroot,需要切换分支

http://www.elebihan.com/ 一个关于buildroot支持rust的BLOG,推荐

http://www.elebihan.com/posts/how-to-add-a-buildroot-package-for-a-cargo-crate.html 如何添加一个pkg包for rust

https://users.rust-lang.org/t/rust-support-in-buildroot-wip/5543 buildroot即将支持rust

https://github.com/japaric/rust-on-openwrt

https://github.com/japaric/rust-cross

https://github.com/japaric/rust-on-openwrt/blob/master/scripts/remove-mno-compact-eh-flag.patch 交叉编译补丁 mk

https://users.rust-lang.org/t/solved-error-compiling-hello-world-program/4996  rust使用arm-poky-linux编译出错


http://blog.csdn.net/bywayboy/article/details/39673295

llvm/include/llvm/IR/Attributes.h:69:38: fatal error: llvm/IR/Attributes.inc: No such file or directory
     #include "llvm/IR/Attributes.inc"


http://reviews.llvm.org/D16940

git clone https://github.com/elebihan/buildroot 切换支持rust的分支

git branch -a 显示所有分支

git checkout -b feature_rust origin/feature/rust --track

git checkout -b rework_cargo origin/rework/cargo --track

git checkout -b feature/rust-v5 origin/feature/rust-v5 --track 目前最新的

$ rustc --print target-list | pr -tw100 --columns 3

-C target-feature=+neon to rustc

cargo rustc --target=$TRIPLE --release -- -C link-args=-s
[profile.release]
lto = true

-C prefer-dynamic 使用动态库链接方式

-C lto

Use Link Time Optimization (LTO)

链接时间优化


-C link-args=-s 使用strip

$ cargo sysroot --target $custom target/sysroot
$ RUSTFLAGS='--sysroot target/sysroot/debug' cargo build --target $custom
cargo rustc --release -- -C link-args=-static

 rustc -Vv 得来当前git hash
$ rustc -Vv
rustc 1.8.0-nightly (3c9442fc5 2016-02-04)
binary: rustc
commit-hash: 3c9442fc503fe397b8d3495d5a7f9e599ad63cf6commit-date: 2016-02-04
host: x86_64-unknown-linux-gnu
release: 1.8.0-nightly
rustc_commit_hash=3c9442fc503fe397b8d3495d5a7f9e599ad63cf6

git checkout $rustc_commit_hash
 git rev-parse HEAD
# In the 'build' directory
$ cp -r \
    $host/stage2/lib/rustlib/$target
    $(rustc --print sysroot)/lib/rustlib
export RUSTFLAGS=xxx 传递参数给rustc
可以设置本工程目录下的 .cargo/config 默认代替 ${HOME}/.carg/config
cat < .cargo/config
[target.arm-buildroot-linux-gnueabihf]
linker = "arm-buildroot-linux-gnueabihf-gcc"
[build]
rustflags = ["-C", "prefer-dynamic"]
EOF
手工编译时,修改src/bootstrap/bootstrap.py中的下载境像地址
还有get-stage0.py文件

https://mirrors.ustc.edu.cn/rust-static

--- get-stage0.py       2016-07-21 13:42:34.306071491 +0800

+++ /tmp/get-stage0.py  2016-07-21 13:42:17.098690811 +0800

@@ -33,7 +33,7 @@


filename_base = 'rustc-' + channel + '-' + triple

filename = filename_base + '.tar.gz'

-    url = 'https://mirrors.ustc.edu.cn/rust-static/dist/' + date + '/' + filename

+    url = 'https://static.rust-lang.org/dist/' + date + '/' + filename




./configure --prefix=/usr --disable-docs --release-channel=stable --target=armv7-buildroot-linux-gnueabihf --disable-jemalloc

0?wx_fmt=png



./install.sh --prefix=$(rustc --print sysroot)



0?wx_fmt=png


./rust_git/configure --target=armv7-buildroot-linux-gnueabihf  \

--disable-codegen-tests \

--disable-valgrind \

--disable-valgrind-rpass \

--jemalloc-root="/usr/lib" \

--enable-rpath                    \

--disable-docs                    \

--disable-manage-submodules       \

--disable-debug                   \

--enable-optimize                 \

--enable-optimize-cxx             \

--llvm-root=${llvmdir}            \

--enable-optimize-tests           \

--release-channel=nightly          \



有点乱,重新总结一下

上面的方法编译一直卡在llvm编译出错这步,提示如下

llvm/include/llvm/IR/Attributes.h:69:38: fatal error: llvm/IR/Attributes.inc: No such file or directory
     #include "llvm/IR/Attributes.inc"

参考别人提交的补丁后,手工添加后llvm能顺利通过编译,自己做了一个armv7-buildroot的target mk文件,编译过程发现找不到该target,通过查看源代码发现还需要在需要在target目录下的mod.rs中添加宏引用才能找到新加的target,安装到系统后,设置rustc路径为PATH的第一个优级先路径后,cargo即可自动找到自己制作的编译器,cargo build --verbose --target=armv7-buildroot-gnueabihf 即可编译hello world工程了,

重新编译后可以在imx6上运行hello world


https://github.com/joerg-krause/rust-cross-libs 后来才发现这个工程,实现的是armv5te的编译,适合用s3c2416, imx287之类的arm9芯片,根据作者的代码修改支持armv7也就是cortex-a9系列的imx6,编译成功但还未在板子上运行,里面吸取了两点,第一点是通过制作target的json后不需要修改target下的mod.rs文件即可编译出自己的target,注意还是需要在mk/cfg/目录下添加$triple.mk,其实查看源代码后发现, target下有判断用户是否指定target.json的地方。另一点是之前遇到yocto需要传递--sysroot指定gcc的sysroot,但一直不知道rust如何向gcc传参数,后来发现作者通过新建一个Wrapper的脚本,在脚本里调到 armv7-buildroot-gnueabihf-gcc --sysroot=XXX就可以顺利的传递CFLAGS参数了,具体方法查找该项目的readme.md


https://github.com/elebihan/buildroot  切换到rust-v5的分支,里面会看到有个rust-target-gen一个py写的json生成器,即可生成target的json


重新使用跳过编译llvm直接编译Rust出错, 应该是宿主机上的llvm与rust源代码中的llvm不兼容导致的

test/x86_64-unknown-linux-gnu/stage0/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_llvm-c8005792.so: undefined reference to `ffi_type_float'

test/x86_64-unknown-linux-gnu/stage0/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_llvm-c8005792.so: undefined reference to `ffi_type_sint64'

test/x86_64-unknown-linux-gnu/stage0/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_llvm-c8005792.so: undefined reference to `ffi_type_pointer'

test/x86_64-unknown-linux-gnu/stage0/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_llvm-c8005792.so: undefined reference to `ffi_type_sint32'

test/x86_64-unknown-linux-gnu/stage0/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_llvm-c8005792.so: undefined reference to `ffi_type_void'

https://github.com/rust-lang/rust/issues/34486 有人提交issue

In-tree, we configure LLVM without -lffi so maybe there's a discrepancy when we encounter an LLVM with it compiled in.





你可能感兴趣的:(Rust交叉编译笔记)