cmake: don't add the C runtime library to the .obj file (when using MSVC)

Using /Zl, the obj files will no longer add a link requirement to the C
runtime libraries. Meanwhile, also add /NODEFAULTLIB for non-UWP MSVC
toolchains.

Because /Zl is a compile option, it can also be used when building a
static SDL3 library, and SDL3_test.
main
Anonymous Maarten 2023-11-09 00:20:34 +01:00
parent bea34c5380
commit 2e3f574f8f
1 changed files with 29 additions and 13 deletions

View File

@ -1932,8 +1932,8 @@ elseif(WINDOWS)
vccorlib$<$<CONFIG:Debug>:d>.lib vccorlib$<$<CONFIG:Debug>:d>.lib
msvcrt$<$<CONFIG:Debug>:d>.lib msvcrt$<$<CONFIG:Debug>:d>.lib
LINK_OPTIONS LINK_OPTIONS
-nodefaultlib:vccorlib$<$<CONFIG:Debug>:d> /nodefaultlib:vccorlib$<$<CONFIG:Debug>:d>
-nodefaultlib:msvcrt$<$<CONFIG:Debug>:d> /nodefaultlib:msvcrt$<$<CONFIG:Debug>:d>
) )
endif() endif()
@ -2988,6 +2988,24 @@ if(PS2)
sdl_compile_options(PRIVATE "-Wno-error=declaration-after-statement") sdl_compile_options(PRIVATE "-Wno-error=declaration-after-statement")
endif() endif()
if(NOT SDL_LIBC)
if(MSVC)
set(saved_CMAKE_TRY_COMPILE_TARGET_TYPE "${CMAKE_TRY_COMPILE_TARGET_TYPE}")
cmake_push_check_state(RESET)
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
check_c_compiler_flag("/Zl" COMPILER_SUPPORTS_Zl)
cmake_pop_check_state()
set(CMAKE_TRY_COMPILE_TARGET_TYPE "${saved_CMAKE_TRY_COMPILE_TARGET_TYPE}")
if(COMPILER_SUPPORTS_Zl)
# /Zl omits the default C runtime library name from the .obj file.
sdl_compile_options(PRIVATE "$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:/Zl>")
if(TARGET SDL3_test)
target_compile_options(SDL3_test PRIVATE "/Zl")
endif()
endif()
endif()
endif()
if(APPLE) if(APPLE)
get_property(sources TARGET SDL3-collector PROPERTY INTERFACE_SOURCES) get_property(sources TARGET SDL3-collector PROPERTY INTERFACE_SOURCES)
foreach(SOURCE_FILE IN LISTS sources) foreach(SOURCE_FILE IN LISTS sources)
@ -3058,9 +3076,15 @@ if(SDL_SHARED)
) )
endif() endif()
if(NOT SDL_LIBC) if(NOT SDL_LIBC)
if(MSVC AND SDL_CPU_X86) if(MSVC AND (NOT MSVC_CLANG AND NOT WINDOWS_STORE))
# FIXME: should be added for all architectures (missing symbols for ARM) # Don't try to link with the default set of libraries.
target_link_libraries(SDL3-shared PRIVATE "-nodefaultlib:MSVCRT") # Note: The clang toolset for Visual Studio does not support /NODEFAULTLIB.
target_link_options(SDL3-shared PRIVATE "/NODEFAULTLIB")
if(SDL_CPU_ARM32)
# linking to msvcrt.lib avoid unresolved external symbols
# (__rt_sdiv, __rt_udiv, __rt_sdiv64, _rt_udiv64, __dtou64, __u64tod, __i64tos)
target_link_libraries(SDL3-shared PRIVATE msvcrt.lib)
endif()
endif() endif()
if(HAS_Q_NO_USE_LIBIRC) if(HAS_Q_NO_USE_LIBIRC)
target_compile_options(SDL3-shared PRIVATE /Q_no-use-libirc) target_compile_options(SDL3-shared PRIVATE /Q_no-use-libirc)
@ -3095,14 +3119,6 @@ if(SDL_SHARED)
) )
endif() endif()
endif() endif()
# Note: The clang toolset for Visual Studio does not support /NODEFAULTLIB.
if(MSVC AND NOT SDL_LIBC AND NOT MSVC_CLANG AND NOT SDL_CPU_ARM32)
# Don't try to link with the default set of libraries.
if(NOT WINDOWS_STORE)
# FIXME: is this needed? "-nodefaultlib:MSVCRT" ia already added when SDL_LIBC is false
target_link_options(SDL3-shared PRIVATE "/NODEFAULTLIB")
endif()
endif()
target_link_libraries(SDL3-shared PRIVATE ${SDL_CMAKE_DEPENDS}) target_link_libraries(SDL3-shared PRIVATE ${SDL_CMAKE_DEPENDS})
target_include_directories(SDL3-shared target_include_directories(SDL3-shared
PRIVATE PRIVATE