Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find linker script memory.x: cargo workspaces #461

Open
tgross35 opened this issue Jan 25, 2023 · 3 comments
Open

Cannot find linker script memory.x: cargo workspaces #461

tgross35 opened this issue Jan 25, 2023 · 3 comments

Comments

@tgross35
Copy link
Contributor

Hello all,

Does anyone have a workaround for the following error:

cannot find linker script memory.x
          >>> INCLUDE memory.x
          >>>         ^

that arises when compiling packages within workspaces?

I've tried a few things like adding println!("cargo:rustc-link-arg=-T{}/memory.x", env!("CARGO_MANIFEST_DIR")); to my build.rs, but nothing has worked so far.

Whatever the solution is, it would likely be good to document it somewhere

@tgross35
Copy link
Contributor Author

Moving memory.x to the workspace root does work, but that of course isn't doable when you have >1 target within a workspace

@adamgreig
Copy link
Member

The usual thing is a build.rs script that copies your memory.x (or indeed creates it from scratch) into the output directory and adds that to the linker search path, e.g.: https://github.com/rust-embedded/cortex-m-quickstart/blob/master/build.rs

I guess you could also just put the crate path into the linker search path too, println!("cargo:rustc-link-search={}", env!("CARGO_MANIFEST_DIR"));, though I've not tried it.

@franksacco
Copy link

I had the same issue: I created a Cargo workspace with several binary and library packages and the linker was unable to find the memory.x files while building.

As @adamgreig pointed out, I created a build.rs file in each binary package with the following content:

use std::{env, error::Error, fs::File, io::prelude::Write, path::PathBuf};

fn main() -> Result<(), Box<dyn Error>> {
    // Make `memory.x` available to the linker.
    let out_dir = env::var("OUT_DIR")?;
    let out_dir = PathBuf::from(out_dir);

    let memory_x = include_bytes!("memory.x").as_ref();
    File::create(out_dir.join("memory.x"))?.write_all(memory_x)?;

    // Tell Cargo where to find the file.
    println!("cargo:rustc-link-search={}", out_dir.display());

    // Tell Cargo to rebuild if `memory.x` is updated.
    println!("cargo:rerun-if-changed=memory.x");

    Ok(())
}

This solved the problem for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants