Skip to content

rust: Uses wrong target architecture for proc-macro targets when cross-compiling #11702

Closed
@sdroege

Description

@sdroege

Describe the bug

meson currently always uses the host architecture for building proc-macro targets, which is wrong. proc-macro targets are loaded and run by the compiler during compilation so must be compiled for the build architecture.

Also in extension this means that all dependencies of the proc-macro target also must be compiled for the build architecture, and with this it can happen that dependencies have to be compiled for both the build and host architecture (if they're used by targets for both).

To Reproduce

Try cross-compiling test cases/rust/18 proc-macro and see how it fails:

[1/2] rustc --target aarch64-unknown-linux-gnu -C linker=aarch64-linux-gnu-gcc --color=always --crate-type proc-macro -g -C relocation-model=pic --crate-name proc_macro_examples --emit dep-info=proc_macro_examples.d --emit link -o libproc_macro_examples.so -C prefer-dynamic ../proc.rs
[2/2] rustc --target aarch64-unknown-linux-gnu -C linker=aarch64-linux-gnu-gcc --color=always --crate-type bin -g --crate-name main --emit dep-info=main.d --emit link -o main --extern proc_macro_examples=libproc_macro_examples.so -L . -C prefer-dynamic -C 'link-arg=-Wl,-rpath,$ORIGIN/:/home/slomo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' -C 'link-arg=-Wl,-rpath-link,/home/slomo/Projects/meson/test cases/rust/18 proc-macro/build-cross/:/home/slomo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' ../use.rs
FAILED: main 
rustc --target aarch64-unknown-linux-gnu -C linker=aarch64-linux-gnu-gcc --color=always --crate-type bin -g --crate-name main --emit dep-info=main.d --emit link -o main --extern proc_macro_examples=libproc_macro_examples.so -L . -C prefer-dynamic -C 'link-arg=-Wl,-rpath,$ORIGIN/:/home/slomo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' -C 'link-arg=-Wl,-rpath-link,/home/slomo/Projects/meson/test cases/rust/18 proc-macro/build-cross/:/home/slomo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' ../use.rs
error[E0463]: can't find crate for `proc_macro_examples`
 --> ../use.rs:1:1
  |
1 | extern crate proc_macro_examples;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate

error: cannot determine resolution for the macro `make_answer`
 --> ../use.rs:4:1
  |
4 | make_answer!();
  | ^^^^^^^^^^^
  |
  = note: import resolution is stuck, try simplifying macro imports

error[E0425]: cannot find function `answer` in this scope
 --> ../use.rs:7:20
  |
7 |     assert_eq!(42, answer());
  |                    ^^^^^^ not found in this scope

error: aborting due to 3 previous errors

When running the commands manually and selecting the correct target for the proc-macro it builds fine:

rustc --color=always --crate-type proc-macro -g -C relocation-model=pic --crate-name proc_macro_examples --emit dep-info=proc_macro_examples.d --emit link -o libproc_macro_examples.so -C prefer-dynamic ../proc.rs
rustc --target aarch64-unknown-linux-gnu -C linker=aarch64-linux-gnu-gcc --color=always --crate-type bin -g --crate-name main --emit dep-info=main.d --emit link -o main --extern proc_macro_examples=libproc_macro_examples.so -L . -C prefer-dynamic -C 'link-arg=-Wl,-rpath,$ORIGIN/:/home/slomo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' -C 'link-arg=-Wl,-rpath-link,/home/slomo/Projects/meson/test cases/rust/18 proc-macro/build-cross/:/home/slomo/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' ../use.rs

CC @dcbaker

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions