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

When cross compiling Rust to Android on Windows, Rust 1.69.0+ can't find the version script path. #113711

Open
moe-deer opened this issue Jul 15, 2023 · 11 comments
Labels
A-cross Area: Cross compilation A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-android Operating system: Android O-windows-msvc Toolchain: MSVC, Operating system: Windows regression-from-stable-to-stable Performance or correctness regression from one stable version to another.
Milestone

Comments

@moe-deer
Copy link

On Windows, when cross compiling Rust code to generate an Android .so library, using Rust versions 1.69.0 or newer can result in compile errors about the version script.
The error happens because the Rust compiler tries to pass a path to a version script to the linker, but that specified path does not actually exist on Windows.

I tried this code:

cargo build --release --target=aarch64-linux-android

I expected to see this happen:

Finished release [optimized] target(s) in 2.44s

Instead, this happened:

error: linking with `D:/NDK/android-ndk-r25c/toolchains/llvm/prebuilt/windows-x86_64/bin/aarch64-linux-android30-clang.cmd` failed: exit code: 255                                                                
  |
  = note: "D:/NDK/android-ndk-r25c/toolchains/llvm/prebuilt/windows-x86_64/bin/aarch64-linux-android30-clang.cmd" "-Wl,--version-script=C:\\Users\\Liste\\AppData\\Local\\Temp\\rustcPwRX2e\\list" "-Wl,--no-undefi
ned-version" "C:\\Users\\Liste\\AppData\\Local\\Temp\\rustcPwRX2e\\symbols.o" "E:\\idea_projects\\my_lib\\target\\aarch64-linux-android\\release\\deps\\my_lib.my_lib.d56b3372c49eacad-cgu.0.rcgu.o" "
-Wl,--as-needed" "-L" "E:\\idea_projects\\my_lib\\target\\aarch64-linux-android\\release\\deps" "-L" "E:\\idea_projects\\my_lib\\target\\release\\deps" "-L" "C:\\Users\\Liste\\.rustup\\toolchains\\stable-
x86_64-pc-windows-msvc\\lib\\rustlib\\aarch64-linux-android\\lib" "-Wl,-Bstatic" "C:\\Users\\Liste\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\aarch64-linux-android\\lib\\libcompiler_builtins-0
1f0855226f8e564.rlib" "-Wl,-Bdynamic" "-ldl" "-llog" "-lunwind" "-ldl" "-lm" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "C:\\Users\\Liste\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\a
arch64-linux-android\\lib" "-o" "E:\\idea_projects\\my_lib\\target\\aarch64-linux-android\\release\\deps\\libmy_lib.so" "-Wl,--gc-sections" "-shared" "-Wl,-z,relro,-z,now" "-Wl,--strip-all" "-nodefaultlibs"
  = note: --version-script=C:\Users\Liste\AppData\Local\Temp\rustcPwRX2e\list"" was unexpected at this time.


warning: `my_lib` (lib) generated 2 warnings                                                                                                                                                                     
error: could not compile `my_lib` (lib) due to previous error; 2 warnings emitted

Meta

rustc --version --verbose:

rustc 1.71.0 (8ede3aae2 2023-07-12)
binary: rustc
commit-hash: 8ede3aae28fe6e4d52b38157d7bfe0d3bceef225
commit-date: 2023-07-12
host: x86_64-pc-windows-msvc
release: 1.71.0
LLVM version: 16.0.5
@moe-deer moe-deer added the C-bug Category: This is a bug. label Jul 15, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 15, 2023
@jyn514 jyn514 added A-linkage Area: linking into static, shared libraries and binaries O-android Operating system: Android A-cross Area: Cross compilation O-windows-msvc Toolchain: MSVC, Operating system: Windows labels Jul 15, 2023
@jyn514
Copy link
Member

jyn514 commented Jul 15, 2023

you are using an MSVC host toolchain and the compiler is passing unix-style flags. do you see this same problem on windows-gnu or only on MSVC?

@Noratrieb Noratrieb removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 16, 2023
@moe-deer
Copy link
Author

I encountered this issue on both windows-gnu and MSVC. They work correctly in rust 1.68.2 version, but the problem appears in versions after that.

@jyn514 jyn514 added the regression-from-stable-to-stable Performance or correctness regression from one stable version to another. label Jul 16, 2023
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jul 16, 2023
@jyn514 jyn514 added this to the 1.69.0 milestone Jul 16, 2023
@jyn514 jyn514 added the E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc label Jul 16, 2023
@apiraino
Copy link
Contributor

apiraino commented Aug 1, 2023

@moe-deer Does it also happen with a new Rust project? Any chance you can provide a small bare Rust project template to reproduce the issue? Just to be sure I understand: is the correct Windows path to your Android NDK D:\\NDK\\android-ndk-r25c\\?

@jing332
Copy link

jing332 commented Aug 1, 2023

Version:

rustc 1.71.0 (8ede3aae2 2023-07-12)
binary: rustc
commit-hash: 8ede3aae28fe6e4d52b38157d7bfe0d3bceef225
commit-date: 2023-07-12
host: x86_64-pc-windows-msvc
release: 1.71.0
LLVM version: 16.0.5

This is because the cmd script cannot parse the parameter

Error: version-script=xx"" was unexpected at this time
image


When I remove the space, =, , it can be passed normally:

image

@knopp
Copy link

knopp commented Aug 3, 2023

Version:

rustc 1.71.0 (8ede3aae2 2023-07-12)
binary: rustc
commit-hash: 8ede3aae28fe6e4d52b38157d7bfe0d3bceef225
commit-date: 2023-07-12
host: x86_64-pc-windows-msvc
release: 1.71.0
LLVM version: 16.0.5

This is because the cmd script cannot parse the parameter

Error: version-script=xx"" was unexpected at this time image

When I remove the space, =, , it can be passed normally:

image

Neither "W, version-script=xx" nor "-Wversion-script" in your case would be proper linker flags. Linker flags are prefixed with -Wl,.

The problem is this line in the clang script:

if "%1" == "-cc1" goto :L

It escapes the first argument, but the argument is already escaped which causes problem in the condition and for some reason cmd unescapes and splits the argument and then complains that the second argument is unexpected.

@knopp
Copy link

knopp commented Aug 3, 2023

In other words, the invocation only works as long as first the linker argument doesn't have comma in it.

@knopp
Copy link

knopp commented Aug 7, 2023

This seems similar enough, just a different separator: android/ndk#1856.

@soupslurpr
Copy link

I am getting this exact error, and this workaround works android/ndk#1856 (comment)

@usernamety
Copy link

Has the problem been solved now?

@knopp
Copy link

knopp commented Nov 12, 2023

Has the problem been solved now?

This is not a Rust error. It is a problem with the CMD wrapper in Android SDK. I believe cargo-ndk is not calling the wrapper script anymore.

@apiraino apiraino removed E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Nov 13, 2023
@darcycui
Copy link

This seems similar enough, just a different separator: android/ndk#1856.

This solved my problem,thanks very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cross Area: Cross compilation A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-android Operating system: Android O-windows-msvc Toolchain: MSVC, Operating system: Windows regression-from-stable-to-stable Performance or correctness regression from one stable version to another.
Projects
None yet
Development

No branches or pull requests

10 participants