SchrodingerZhu/snmalloc-rs

Failing static compilation on stable-x86_64-pc-windows-msvc

Count-Count opened this issue · 5 comments

Trying to build any crate which depends on sn-malloc and uses it as its global allocater with RUSTFLAGS=-C target-feature=+crt-static on Windows leads to a link-time error:

   Compiling snmalloc-windows-test v0.1.0 (C:\Users\xxx\Documents\dev\rustspikes\snmalloc-windows-test)
error: linking with `link.exe` failed: exit code: 1120
  |
  = note: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.26.28801\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "C:\\Users\\xxx\\Documents\\dev\\rustspikes\\snmalloc-windows-test\\target\\release\\deps\\snmalloc_windows_test.snmalloc_windows_test.uvh4yiuq-cgu.0.rcgu.o" "C:\\Users\\xxx\\Documents\\dev\\rustspikes\\snmalloc-windows-test\\target\\release\\deps\\snmalloc_windows_test.snmalloc_windows_test.uvh4yiuq-cgu.1.rcgu.o" "C:\\Users\\xxx\\Documents\\dev\\rustspikes\\snmalloc-windows-test\\target\\release\\deps\\snmalloc_windows_test.snmalloc_windows_test.uvh4yiuq-cgu.2.rcgu.o" "C:\\Users\\xxx\\Documents\\dev\\rustspikes\\snmalloc-windows-test\\target\\release\\deps\\snmalloc_windows_test.snmalloc_windows_test.uvh4yiuq-cgu.3.rcgu.o" "/OUT:C:\\Users\\xxx\\Documents\\dev\\rustspikes\\snmalloc-windows-test\\target\\release\\deps\\snmalloc_windows_test.exe" "C:\\Users\\xxx\\Documents\\dev\\rustspikes\\snmalloc-windows-test\\target\\release\\deps\\snmalloc_windows_test.1hv601l7hgil9fss.rcgu.o" "/OPT:REF,ICF" "/DEBUG" "/NATVIS:C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libcore.natvis" "/NATVIS:C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libstd.natvis" "/LIBPATH:C:\\Users\\xxx\\Documents\\dev\\rustspikes\\snmalloc-windows-test\\target\\release\\deps" "/LIBPATH:C:\\Users\\xxx\\Documents\\dev\\rustspikes\\snmalloc-windows-test\\target\\release\\build\\snmalloc-sys-edccc1af11933945\\out\\./build/Release" "/LIBPATH:C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "C:\\Users\\xxx\\Documents\\dev\\rustspikes\\snmalloc-windows-test\\target\\release\\deps\\libsnmalloc_rs-ac271706809acdd3.rlib" "C:\\Users\\xxx\\Documents\\dev\\rustspikes\\snmalloc-windows-test\\target\\release\\deps\\libsnmalloc_sys-c9c44a07abfb04ce.rlib" "C:\\Users\\xxx\\Documents\\dev\\rustspikes\\snmalloc-windows-test\\target\\release\\deps\\liblibc-dacd56e8d1ed66b8.rlib" "C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd-b3b7d2754e4f3c4a.rlib" "C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libpanic_unwind-ae2d0fd7efecc707.rlib" "C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_demangle-190d7f5796f574cb.rlib" "C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libhashbrown-f81dd2aa8d51e1ef.rlib" "C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_alloc-9a56cd7574396589.rlib" "C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libunwind-938cf2d7f7936554.rlib" "C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcfg_if-3ed683af0e96cdb9.rlib" "C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liblibc-24993eff3af321f7.rlib" "C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc-7b7f5b91dda770f4.rlib" "C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librustc_std_workspace_core-00274f5eadf75c2b.rlib" "C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcore-465f87485a6021ee.rlib" "C:\\Users\\xxx\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcompiler_builtins-d9775021cc0867d6.rlib" "snmallocshim-1mib-rust.lib" "mincore.lib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "libcmt.lib"
  = note: LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
          LINK : warning LNK4217: symbol '__acrt_iob_func' defined in 'libucrt.lib(_file.obj)' is imported by 'snmallocshim-1mib-rust.lib(rust.obj)' in function '"public: static void __cdecl snmalloc::PALWindows::error(char const * const)" (?error@PALWindows@snmalloc@@SAXQEBD@Z)'
          LINK : warning LNK4217: symbol 'fflush' defined in 'libucrt.lib(fflush.obj)' is imported by 'snmallocshim-1mib-rust.lib(rust.obj)' in function '"public: static void __cdecl snmalloc::PALWindows::error(char const * const)" (?error@PALWindows@snmalloc@@SAXQEBD@Z)'
          LINK : warning LNK4217: symbol 'abort' defined in 'libucrt.lib(abort.obj)' is imported by 'snmallocshim-1mib-rust.lib(rust.obj)' in function '"public: static void __cdecl snmalloc::PALWindows::error(char const * const)" (?error@PALWindows@snmalloc@@SAXQEBD@Z)'
          snmallocshim-1mib-rust.lib(rust.obj) : error LNK2019: unresolved external symbol __imp_puts referenced in function "public: static void __cdecl snmalloc::PALWindows::error(char const * const)" (?error@PALWindows@snmalloc@@SAXQEBD@Z)
          C:\Users\xxx\Documents\dev\rustspikes\snmalloc-windows-test\target\release\deps\snmalloc_windows_test.exe : fatal error LNK1120: 1 unresolved externals
          

error: aborting due to previous error

error: could not compile `snmalloc-windows-test`

To learn more, run the command again with --verbose.

@mjp41 any thought?

mjp41 commented

It looks like it has statically linked two versions of the CRT.

mjp41 commented

I think the issues is:

if cfg!(all(windows, target_env = "msvc")) {
cfg = cfg.define("CMAKE_CXX_FLAGS_RELEASE", "/MD /O2 /Ob2 /DNDEBUG /EHsc");
cfg = cfg.define("CMAKE_C_FLAGS_RELEASE", "/MD /O2 /Ob2 /DNDEBUG /EHsc");
}

It is specifying the /MD version of the library for linking. I assume

target-feature=+crt-static 

translates in to compiling with /MT. Can that feature flag be used in the snmalloc build? I.e. to decide whether to use /MT or /MD?

mjp41 commented

I think based on this:

After this change, the gcc-rs crate will be modified to check for the CARGO_CFG_TARGET_FEATURE
directive, and parse it into a list of enabled features. If the crt-static feature is not enabled it will
compile C code on the MSVC target with /MD, indicating dynamic linkage. Otherwise if the value
is static it will compile code with /MT, indicating static linkage. Because today the MSVC targets
use dynamic linkage and gcc-rs compiles C code with /MD, gcc-rs will remain forward and
backwards compatible with existing and future Rust MSVC toolchains until such time as the the
decision is made to change the MSVC toolchain to +crt-static by default.

We can just drop the /MD from the build, and it should do the right thing?