将AVR程序与货物联系起来

时间:2017-04-30 03:49:39

标签: linker rust avr rust-cargo

我有一个Rust项目,我目前正在手工编译和链接:

rustc --target=avr-atmel-none src/main.rs  --emit=obj -o _build/main.rs.o -C opt-level=3
avr-gcc -Os -Wl,--gc-sections -mmcu=atmega328p -o _build/image.elf _build/main.rs.o 
avr-objcopy -Oihex -R.eeprom _build/image.elf _build/image.hex

我想用Cargo自动化这个,所以我首先将avr-gcc设置为链接器,将以下内容添加到.cargo/config

[build]
target = "avr-atmel-none"

[target.avr-atmel-none]
linker = "avr-gcc"

然而,似乎cargo将一些额外的参数传递给avr-gcc无法处理的链接器:

11:47:10 [cactus@galaxy interrupt-bug]$ cargo build --release
   Compiling hello-avr v0.1.0 (file:///home/cactus/prog/rust/avr/interrupt-bug)
error: linking with `avr-gcc` failed: exit code: 1
  |
  = note: "avr-gcc" "-Wl,--as-needed" "-L" "/home/cactus/prog/rust/rust-avr/build/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/avr-atmel-none/lib" "/home/cactus/prog/rust/avr/interrupt-bug/target/avr-atmel-none/release/deps/hello_avr-8bce8eb24807f5a8.0.o" "-o" "/home/cactus/prog/rust/avr/interrupt-bug/target/avr-atmel-none/release/deps/hello_avr-8bce8eb24807f5a8" "-Wl,--gc-sections" "-pie" "-Wl,-O1" "-nodefaultlibs" "-L" "/home/cactus/prog/rust/avr/interrupt-bug/target/avr-atmel-none/release/deps" "-L" "/home/cactus/prog/rust/avr/interrupt-bug/target/release/deps" "-L" "/home/cactus/prog/rust/rust-avr/build/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/avr-atmel-none/lib"
  = note: /usr/lib/gcc/avr/4.8.2/../../../avr/bin/ld: -pie not supported
          collect2: error: ld returned 1 exit status

如何从avr-gcc调用中删除这些额外的参数?此外,有没有办法将第三步(即avr-objcopy调用)整合到货物工作流程中?

1 个答案:

答案 0 :(得分:1)

警告:avr-rust上的发展可以礼貌地说是前沿。很可能一天工作的东西可能不是下一个,所以这样的答案可能会很快过时。我们欢迎该项目的所有贡献者,以帮助其更有用。

您需要指定目标JSON文件和完整的链接器参数集。以下是我的旧项目中的一个示例(某些确切的值现在可能不正确):

{
  "llvm-target": "avr-atmel-none",
  "target-endian": "little",
  "target-pointer-width": "16",
  "os": "none",
  "target-env": "gnu",
  "target-vendor": "unknown",
  "arch": "avr",
  "data-layout": "e-p:16:16:16-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-n8",

  "executables": true,

  "linker": "avr-gcc",
  "linker-flavor": "gcc",
  "pre-link-args": {
    "gcc": ["-mmcu=atmega328p", "-nostartfiles", "../interrupt_vector.S"]
  },
  "exe-suffix": ".elf",
  "post-link-args": {
    "gcc": ["-Wl,--no-gc-sections"]
  },

  "no-compiler-rt": true
}

有关完整示例,请参阅my example repository。这个项目曾经工作过(参见我的blog series)。我最近更新了它,所以它编译反对avr-rust的主分支,但没有在真实设备上测试编译的代码。

Cargo post build scripts的开放式RFC,但它似乎不太可能合并。我继续使用Makefile。 xargo可能是另一种选择。还有关于可以创建货物子命令的隆隆声。