cargo icon indicating copy to clipboard operation
cargo copied to clipboard

Support setting target `runner` in the `[profile]` settings

Open masriomarm opened this issue 1 year ago • 6 comments

Problem

I need to have different runners based on release/debug build for now [target.'cfg(debug_assertions)'.runner] is always active even if --print=cfg shows no debug_assertions. I'm aware of the docs mentioning to not try to match on debug_assertions

Steps

1 - having .cargo/config.toml, this is based cargo generate --git https://github.com/rust-embedded/cortex-m-quickstar following the embedded book. I was trying to have different runners

[target.thumbv7m-none-eabi]
# uncomment this to make `cargo run` execute programs on QEMU
# runner = "qemu-system-arm -S -gdb tcp::3333 -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
runner = """
qemu-system-arm
-gdb tcp::3333
-cpu cortex-m3
-machine lm3s6965evb
-nographic
-serial mon:stdio
-semihosting-config enable=on,target=native
-kernel
"""

[target.'cfg(debug_assertions)']
runner = """
qemu-system-arm
-S
-gdb tcp::3333
-cpu cortex-m3
-machine lm3s6965evb
-nographic
-serial mon:stdio
-semihosting-config enable=on,target=native
-kernel
"""

[target.'cfg(not(debug_assertions))']
runner = """
qemu-system-arm
-gdb tcp::3333
-cpu cortex-m3
-machine lm3s6965evb
-nographic
-serial mon:stdio
-semihosting-config enable=on,target=native
-kernel
"""

[target.'cfg(all(target_arch = "arm", target_os = "none"))']
# uncomment ONE of these three option to make `cargo run` start a GDB session
# which option to pick depends on your system
# runner = "arm-none-eabi-gdb -q -x openocd.gdb"
# runner = "gdb-multiarch -q -x openocd.gdb"
# runner = "gdb -q -x openocd.gdb"
rustflags = [
  # Previously, the linker arguments --nmagic and -Tlink.x were set here.
  # They are now set by build.rs instead. The linker argument can still
  # only be set here, if a custom linker is needed.

  # By default, the LLD linker is used, which is shipped with the Rust
  # toolchain. If you run into problems with LLD, you can switch to the
  # GNU linker by uncommenting this line:
  # "-C", "linker=arm-none-eabi-ld",

  # If you need to link to pre-compiled C libraries provided by a C toolchain
  # use GCC as the linker by uncommenting the three lines below:
  # "-C", "linker=arm-none-eabi-gcc",
  # "-C", "link-arg=-Wl,-Tlink.x",
  # "-C", "link-arg=-nostartfiles",
  # "--print=cfg"
]

[build]
# Pick ONE of these default compilation targets
# target = "thumbv6m-none-eabi"        # Cortex-M0 and Cortex-M0+
target = "thumbv7m-none-eabi"        # Cortex-M3
# target = "thumbv7em-none-eabi"       # Cortex-M4 and Cortex-M7 (no FPU)
# target = "thumbv7em-none-eabihf"     # Cortex-M4F and Cortex-M7F (with FPU)
# target = "thumbv8m.base-none-eabi"   # Cortex-M23
# target = "thumbv8m.main-none-eabi"   # Cortex-M33 (no FPU)
# target = "thumbv8m.main-none-eabihf" # Cortex-M33 (with FPU)

2 - cargo run

Finished dev [unoptimized + debuginfo] target(s) in 0.06s Running qemu-system-arm -gdb 'tcp::3333' -cpu cortex-m3 -machine lm3s6965evb -nographic -serial 'mon:stdio' -semihosting-config enable=on,target=native -kernel target/thumbv7m-none-eabi/debug/app

3 - cargo run --release

Finished release [optimized + debuginfo] target(s) in 0.05s Running qemu-system-arm -gdb 'tcp::3333' -cpu cortex-m3 -machine lm3s6965evb -nographic -serial 'mon:stdio' -semihosting-config enable=on,target=native -kernel target/thumbv7m-none-eabi/release/app

4 - There's a missing -S which is the difference.

Possible Solution(s)

add runner to profile flags

Notes

No response

Version

cargo 1.77.1 (e52e36006 2024-03-26)
release: 1.77.1
commit-hash: e52e360061cacbbeac79f7f1215a7a90b6f08442
commit-date: 2024-03-26
host: x86_64-unknown-linux-gnu
libgit2: 1.7.2 (sys:0.18.2 vendored)
libcurl: 8.5.0-DEV (sys:0.4.70+curl-8.5.0 vendored ssl:OpenSSL/1.1.1w)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Fedora 39.0.0 [64-bit]

masriomarm avatar Apr 11 '24 14:04 masriomarm

Looks like you have a set of different configurations that need to be switched between. Instead of adding more configs to [profile] table, I might recommend using cargo --config <path/to/other-config.toml> to switch to a specific separate config.toml. It may look like:

├── .cargo/
│  ├── config.toml # base shared config
│  ├── thumbv7em-none-eabi-release.toml
│  └── thumbv6m-none-eabi-release.toml
├── Cargo.toml
└── src/

weihanglo avatar Apr 11 '24 16:04 weihanglo

You could also configure [alias] to make it easier to switch with less typing.

weihanglo avatar Apr 11 '24 16:04 weihanglo

why do you need a different runner at all, @masriomarm ? is it only because of the location of the binary? there is a 5 years old flag in unstable, --out-dir which may help: #6790

soloturn avatar Apr 27 '24 05:04 soloturn

@soloturn Thanks, but that won't help. I need different runner to to change qemu call arguments. one to halt target at beginning, and another just run to completion.

masriomarm avatar May 06 '24 04:05 masriomarm

I'm also trying to figure out how to change the runner based on the output directory. The use case is the runner command needs to be passed paths to build output files.

From: The Rust on ESP Book

If you want to use espflash, you can specify an appropriate bootloader and partition table using --bootloader and --partition-table. You can find the bootloader in target/<your MCU's target folder>//bootloader.bin and partition table in target/<your MCU's target folder>//partition-table.bin

One potential solution might be to perform environment variable substitution on the command string and provide $PROFILE and $TARGET to the environment. The extra complexity to implement the substitution logic is probably not worth it for just this use case, but if $PROFILE were provided to the runner a wrapper script could be written.

Currently the closest thing a wrapper script has to work with is $OUT_DIR but it has to parse/manipulate the value.

tones111 avatar Jun 06 '24 02:06 tones111