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 override Windows runtime for rust tests #116900

Closed
xclaesse opened this issue Oct 18, 2023 · 8 comments
Closed

Cannot override Windows runtime for rust tests #116900

xclaesse opened this issue Oct 18, 2023 · 8 comments

Comments

@xclaesse
Copy link

xclaesse commented Oct 18, 2023

Related to #39016.

Meson has an option to select different vscrt, but rustc always uses msvcrt.lib. To work around that issue, Meson adds -l static=msvcrtd.lib when we need use debug crt. This seems to work fine, unless a rust test program is built with rustc --test. In that case I get that linker issue:

[2/6] "rustc" "-C" "linker=link" "--color=always" "-C" "debug-assertions=yes" "-C" "overflow-checks=no" "--crate-type" "bin" "-C" "link-arg=/release" "-C" "link-arg=/nologo" "-g" "--crate-name" "rust_test_from_static" "--emit" "dep-info=rust_test_from_static.d" "--emit" "link=rust_test_from_static.exe" "--out-dir" "rust_test_from_static.exe.p" "--test" "-l" "static=msvcrtd" "../test cases/rust/9 unit tests/test.rs"
FAILED: rust_test_from_static.exe 
"rustc" "-C" "linker=link" "--color=always" "-C" "debug-assertions=yes" "-C" "overflow-checks=no" "--crate-type" "bin" "-C" "link-arg=/release" "-C" "link-arg=/nologo" "-g" "--crate-name" "rust_test_from_static" "--emit" "dep-info=rust_test_from_static.d" "--emit" "link=rust_test_from_static.exe" "--out-dir" "rust_test_from_static.exe.p" "--test" "-l" "static=msvcrtd" "../test cases/rust/9 unit tests/test.rs"
error: linking with `link` failed: exit code: 1120
  |
  = note: "link" "/NOLOGO" "C:\\Users\\VSSADM~1\\AppData\\Local\\Temp\\rustc6j3F90\\symbols.o" "rust_test_from_static.exe.p\\rust_test_from_static.rust_test_from_static.3a99c7044bb59086-cgu.0.rcgu.o" "rust_test_from_static.exe.p\\rust_test_from_static.56vms91uq00rjhzp.rcgu.o" "/LIBPATH:C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "/WHOLEARCHIVE:msvcrtd.lib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libtest-a2072d90d499fe6f.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libgetopts-0d6237423cc36bee.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libunicode_width-4f110db62cbab4e9.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_std-8292a214a8faae0b.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd-6d80dd4621b609ca.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libpanic_unwind-28e98873675f19a9.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_demangle-4de870115f3f458f.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd_detect-ffc9fccfd2140b69.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libhashbrown-8a83799ae3c10d1a.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_alloc-fe19ee773224879a.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libminiz_oxide-654d17813ba7bea6.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libadler-f8fe6b4f80df7ee8.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libunwind-33080a7794fe111b.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcfg_if-927908925e399197.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liblibc-bb1b1a6951ff0244.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc-e2d4eb855c49fe9e.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_core-1dfd0cc5ad1fd4f1.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcore-09e8922577df7322.rlib" "C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcompiler_builtins-d1b73685f2fdc914.rlib" "kernel32.lib" "kernel32.lib" "advapi32.lib" "bcrypt.lib" "kernel32.lib" "ntdll.lib" "userenv.lib" "ws2_32.lib" "kernel32.lib" "ws2_32.lib" "kernel32.lib" "msvcrt.lib" "/NXCOMPAT" "/LIBPATH:C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "/OUT:rust_test_from_static.exe" "/OPT:REF,NOICF" "/DEBUG" "/NATVIS:C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libcore.natvis" "/NATVIS:C:\\Users\\VssAdministrator\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libstd.natvis" "/release" "/nologo"
  = note: LINK : warning LNK4031: no subsystem specified; CONSOLE assumed
          LINK : warning LNK4067: ambiguous entry point; selected 'mainCRTStartup'
          LINK : warning LNK4286: symbol '_HUGE' defined in 'msvcrtd.lib(huge.obj)' is imported by 'msvcrtd.lib(HUGE.obj)'
          msvcrtd.lib(_argc.obj) : error LNK2001: unresolved external symbol __imp___argc
          msvcrtd.lib(_argc.obj) : error LNK2001: unresolved external symbol __argc_dll
          msvcrtd.lib(_argv.obj) : error LNK2001: unresolved external symbol __imp___argv
          msvcrtd.lib(_argv.obj) : error LNK2001: unresolved external symbol __argv_dll
          msvcrtd.lib(commode.obj) : error LNK2001: unresolved external symbol __imp__commode
          msvcrtd.lib(commode.obj) : error LNK2001: unresolved external symbol _commode_dll
          msvcrtd.lib(daylight.obj) : error LNK2001: unresolved external symbol __imp__daylight
          msvcrtd.lib(daylight.obj) : error LNK2001: unresolved external symbol _daylight_dll
          msvcrtd.lib(environ.obj) : error LNK2001: unresolved external symbol __imp__environ
          msvcrtd.lib(environ.obj) : error LNK2001: unresolved external symbol _environ_dll
          msvcrtd.lib(fmode.obj) : error LNK2001: unresolved external symbol __imp__fmode
          msvcrtd.lib(fmode.obj) : error LNK2001: unresolved external symbol _fmode_dll
          msvcrtd.lib(environ.obj) : error LNK2001: unresolved external symbol _environ_dll
          msvcrtd.lib(fmode.obj) : error LNK2001: unresolved external symbol __imp__fmode
          msvcrtd.lib(fmode.obj) : error LNK2001: unresolved external symbol _fmode_dll
          msvcrtd.lib(HUGE.obj) : error LNK2001: unresolved external symbol _HUGE_dll
          msvcrtd.lib(osver.obj) : error LNK2001: unresolved external symbol __imp__osver
          msvcrtd.lib(osver.obj) : error LNK2001: unresolved external symbol _osver_dll
          msvcrtd.lib(pctype.obj) : error LNK2001: unresolved external symbol __imp__pctype
          msvcrtd.lib(pctype.obj) : error LNK2001: unresolved external symbol _pctype_dll
          msvcrtd.lib(pwctype.obj) : error LNK2001: unresolved external symbol __imp__pwctype
          msvcrtd.lib(pwctype.obj) : error LNK2001: unresolved external symbol _pwctype_dll
          msvcrtd.lib(pgmptr.obj) : error LNK2001: unresolved external symbol __imp__pgmptr
          msvcrtd.lib(pgmptr.obj) : error LNK2001: unresolved external symbol _pgmptr_dll
          msvcrtd.lib(sys_nerr.obj) : error LNK2001: unresolved external symbol __imp__sys_nerr
          msvcrtd.lib(sys_nerr.obj) : error LNK2001: unresolved external symbol _sys_nerr_dll
          msvcrtd.lib(timezone.obj) : error LNK2001: unresolved external symbol __imp__timezone
          msvcrtd.lib(timezone.obj) : error LNK2001: unresolved external symbol _timezone_dll
          msvcrtd.lib(winver.obj) : error LNK2001: unresolved external symbol __imp__winver
          msvcrtd.lib(winver.obj) : error LNK2001: unresolved external symbol _winver_dll
          msvcrtd.lib(winmajor.obj) : error LNK2001: unresolved external symbol __imp__winmajor
          msvcrtd.lib(winmajor.obj) : error LNK2001: unresolved external symbol _winmajor_dll
          msvcrtd.lib(winminor.obj) : error LNK2001: unresolved external symbol __imp__winminor
          msvcrtd.lib(winminor.obj) : error LNK2001: unresolved external symbol _winminor_dll
          msvcrtd.lib(ctype.obj) : error LNK2001: unresolved external symbol __imp__ctype
          msvcrtd.lib(ctype.obj) : error LNK2001: unresolved external symbol _ctype
          msvcrtd.lib(tzname.obj) : error LNK2001: unresolved external symbol __imp__tzname
          msvcrtd.lib(tzname.obj) : error LNK2001: unresolved external symbol _tzname
          msvcrtd.lib(mbcurmax.obj) : error LNK2001: unresolved external symbol __imp___mb_cur_max
          msvcrtd.lib(mbcurmax.obj) : error LNK2001: unresolved external symbol __mb_cur_max_dll
          msvcrtd.lib(syserlst.obj) : error LNK2001: unresolved external symbol __imp__sys_errlist
          msvcrtd.lib(syserlst.obj) : error LNK2001: unresolved external symbol _sys_errlist

Any idea what causes that? Any workaround possible?

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 18, 2023
@ChrisDenton
Copy link
Member

I think this is the same issue as the one you link tbh. Sure, there are workarounds which may work in some situations but without a proper fix this issue will keep cropping up in various forms.

My personal opinion is that the first step should be to move linking of the CRT into the compiler (for historical reasons it's currently hardcoded in the libc crate). Then we can use /DEFAULTLIB to make it come after explicitly set libraries and this would also allow disabling it with /NODEFAULTLIB. This is more or less how the msvc C/C++ compiler works.

The next step would be to have some way to properly coordinate using the debug CRT or other CRT. This matters for crates like cc which may be used to build libs independently of rustc but need to know which CRT to use.

@xclaesse
Copy link
Author

I think this is the same issue as the one you link tbh.

Feel free to close as dup, yes. The thing is the workaround proposed there is to add -l static=msvcrtd.lib which actually works fine for regular rust programs that links to C/C++ libraries that have been build with debug crt. But when the program is generated by rustc --test it suddenly fails and I'm not sure to understand why :/

@ChrisDenton
Copy link
Member

ChrisDenton commented Oct 18, 2023

If I look at the link arguments, "/WHOLEARCHIVE:msvcrtd.lib" looks weird to me. Try using -l dylib=msvcrtd and see if that works.

@xclaesse
Copy link
Author

@ChrisDenton oh, good point, that's really weird! For regular programs -l static=msvcrtd is passed to linker as msvcrtd.lib, but for tests it's passed as /WHOLEARCHIVE:msvcrtd.lib... that makes no sense... You're right, -l dylib=msvcrtd fix the problem, but that sounds weird as well since it's a static lib...

@ChrisDenton
Copy link
Member

I mean, msvc doesn't really make much of a distinction. But if you want to be explicitly static then try:

-l static:-whole-archive=msvcrtd

@xclaesse
Copy link
Author

I mean, msvc doesn't really make much of a distinction

Fair enough.

Old rust version used to do whole archive by default I think. Then +whole-archive got added and it stopped doing wholearchive by default. This seems broken in the ---test case, that's a Rustc bug right?

@ChrisDenton
Copy link
Member

I'd think so. It might be a good idea to open an issue about that specifically. Someone who knows more about the test harness than me can chime in. My suspicion would be that it was indeed just an oversight but perhaps there's a reason for it I'm not aware of.

@xclaesse
Copy link
Author

@ChrisDenton reported #116910. Thanks a lot for your help.

@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 19, 2023
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

4 participants