我正在尝试按照Embeddonomicon中解释的步骤编译最小的#![no-std]
程序,但用于新体系结构上的新目标。
- 该架构在LLVM中作为实验目标("ARC")上行,但它没有被rustc使用,所以首先我在Rust附带的LLVM中启用了它,如下所示:运行
./x.py setup
然后更新config.toml:
[llvm]
download-ci-llvm = false
ninja = true
targets = "X86"
experimental-targets = "ARC"
- 然后我按照下面解释的步骤手动添加了对arch的支持(以这个提交为例):
- 创建rustc_target/src/abi/电话/arc.rs
- 更新rustc_llvm/src/lib.rs 等
- 然后我添加了目标文件
arc-pc-unknown-gnu.json
并使其通过RUST_TARGET_PATH
envvar可见:
{
"arch": "arc",
"cpu": "generic",
"abi": "eabi",
"c-enum-min-bits": 8,
"data-layout": "e-m:e-p:32:32-i64:32-f64:32-v64:32-v128:32-a:0:32-n32",
"eh-frame-header": false,
"emit-debug-gdb-scripts": false,
"executables": true,
"features": "",
"linker": "rust-lld",
"linker-flavor": "ld.lld",
"llvm-target": "arc-pc-unknown-gnu",
"max-atomic-width": 32,
"atomic-cas": false,
"panic-strategy": "abort",
"relocation-model": "static",
"target-pointer-width": "32"
}
- 编译器:
./x.py build -i --target=arc-pc-unknown-gnu library/core
。它成功地完成了,我可以看到arc-pc-unknown-gnu
目标的stage1
库 - 我认为这就足够了,但代码没有编译,因为以下问题:
$ rustc --emit=llvm-ir -o rust_main.ll -C panic=abort --target arc-pc-unknown-gnu src/main.rs
error[E0463]: can't find crate for `core`
|
= note: the `arc-pc-unknown-gnu` target may not be installed
= help: consider downloading the target with `rustup target add arc-pc-unknown-gnu`
= help: consider building the standard library from source with `cargo build -Zbuild-std`
error[E0463]: can't find crate for `compiler_builtins`
error[E0412]: cannot find type `PanicInfo` in this scope
--> src/main.rs:18:18
这很奇怪,因为在前面的步骤中,我应该为我的目标编译这些库…
- 然后我认为也许我需要使用
cargo build-std
重新构建libcore(虽然我不知道确切的原因,但有人在网上提到了这一点)?我试过了,但是现在有以下错误:
$ cargo build -Z build-std=core --target arc-pc-unknown-gnu
Compiling compiler_builtins v0.1.70
Compiling core v0.0.0 (/home/valeriyk/proj/rust-arc/1.60.0/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/src/rust/library/core)
error[E0463]: can't find crate for `std`
error: cannot find macro `println` in this scope
--> /home/valeriyk/.cargo/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.70/build.rs:88:9
|
88 | println!("cargo:rustc-cfg=kernel_user_helpers")
| ^^^^^^^
error: cannot find macro `println` in this scope
--> /home/valeriyk/.cargo/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.70/build.rs:78:9
|
78 | println!("cargo:rustc-cfg=thumb_1")
| ^^^^^^^
...
为什么libcore
需要std
?我只是想让它使用stage1 rustc交叉编译,然后在我的#![no_std]示例编译。如有任何指导,不胜感激。
我就是这样解决这个问题的:
-
使用正确的rust源代码。我从一个稳定的版本开始,它的libcore与最新的
compiler_builtins
不兼容。点击这里了解更多细节。然后我需要每晚升级到最新版本。 -
构建编译器时,不要要求在最后阶段构建libcore,而只构建rustc:
$ ./x.py build -i --stage=1 --target=arcv2-none-elf32 compiler/rustc
- 使用以下
.cargo/config.toml
,不要使用Xargo
或-Z build-std=core
。compiler-builtins-mem
的事情在这里解释。
[unstable]
build-std = [
"core",
"compiler_builtins"
]
build-std-features = ["compiler-builtins-mem"]
[build]
target = "arcv2-none-elf32"
- 用简单的
cargo build
构建代码。它编译得很好,虽然后来链接失败了——但那是另一回事。