Skip to content

per-package-target looks in project root for memory.x rather then crate specific directories #9537

Closed as not planned
@FrankvdStam

Description

@FrankvdStam

Unsure if bug or asking for a feature.

Problem

When using the new per-package-target feature in nightly builds, any linker scripts that you might need in a specific crate will not be found when using a workspace. The linker script is expected in root.

Given the following project structure and manifest files:

root (directory)
	cargo.toml
		
                [workspace]
		members = ["cli", "hardware"]
	
	cli (directory)
		cargo.toml
			[package]
			name = "cli"
			version = "0.1.0"
			authors = ["redacted"]
			edition = "2018"
		
		main.rs
			code omitted, not important
	
	hardware (directory)
		cargo.toml
			cargo-features = ["per-package-target"]
			
			[package]
			name = "hardware"
			version = "0.1.0"
			authors = ["redacted"]
			edition = "2018"
			forced-target = "thumbv7em-none-eabihf"

			[dependencies]
			cortex-m = "0.7.2"
			cortex-m-rt = "0.6.13"
			cortex-m-semihosting = "0.3.7"
			alloc-cortex-m = "0.4.1"
			stm32h7xx-hal = {version = "0.9.0", features = ["stm32h7a3","rt"]}
			embedded-hal = "0.2.5"

		memory.x
			script ommitted, contents not important
	
		.cargo (directory)
			config.toml
				[target.thumbv7em-none-eabihf]
				runner = 'arm-none-eabi-gdb'
				rustflags = [
					# LLD (shipped with the Rust toolchain) is used as the default linker
					"-C", "link-arg=-Tlink.x",
				]

				[build]
				target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)

cargo build will eventually fail during linking:

note: rust-lld: error: C:\projects\embedded\stm6502\target\thumbv7em-none-eabihf\debug\build\cortex-m-rt-3e6b4c679f86541e\out\link.x:23: cannot find linker script memory.x
          >>> INCLUDE memory.x
          >>>         ^

Even though the script is right there in the crate's subdirectory.

After copying memory.x to the root directory, cargo build runs without problems (except for all those warnings I should really look at)

I would expect the script (memory.x) in the crate directory itself to be picked up.
The way that this is setup, I wouldn't be able to target multiple microcontrollers requiring a different memory.x script from the same workspace. I can't specify the name either; "link-arg=-Tlink.x" just looks for memory.x and that's it. In order to target different microcontrollers, you'd have to swap the file in the root directory during build or some crazy shenanigans like that.

Steps

  1. Create above project structure or clone this minimal reproduction repository that I set up for this issue (names of directories slightly different): https://github.com/FrankvdStam/PerPackageTargetIssue
  2. Run cargo build, note linker error
  3. Move memory.x to project root
  4. Run cargo build, note linker error disappearing

Possible Solution(s)

Maybe the best would not be to just make it pick up memory.x from the specific crate, but allow users to explicitly tell cargo where to find memory.x in Cargo.toml - the first time I saw memory.x I was confused how/where it was used/what it was, an explicit setting might help users find the documentation for it on their own. Then again I have no idea as to the complexities of implementing any of this.

Notes

Output of cargo version:

cargo 1.54.0-nightly (e931e4796 2021-05-24)

Output of rustup show:

Default host: x86_64-pc-windows-gnu
rustup home:  C:\Users\Frank\.rustup

installed toolchains
--------------------

stable-x86_64-pc-windows-gnu
stable-x86_64-pc-windows-msvc
nightly-x86_64-pc-windows-gnu
nightly-x86_64-pc-windows-msvc (default)

installed targets for active toolchain
--------------------------------------

thumbv6m-none-eabi
thumbv7em-none-eabihf
thumbv7m-none-eabi
x86_64-pc-windows-msvc

active toolchain
----------------

nightly-x86_64-pc-windows-msvc (default)
rustc 1.54.0-nightly (dbe459ded 2021-06-02)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linker issues, dylib, cdylib, shared libraries, soA-rustflagsArea: rustflagsC-bugCategory: bugS-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.Z-per-package-targetNightly: per-package-target

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions