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.
This commit is contained in:
Anonymous Maarten 2023-11-09 00:20:34 +01:00
parent bea34c5380
commit 2e3f574f8f

View File

@ -1932,8 +1932,8 @@ elseif(WINDOWS)
vccorlib$<$<CONFIG:Debug>:d>.lib
msvcrt$<$<CONFIG:Debug>:d>.lib
LINK_OPTIONS
-nodefaultlib:vccorlib$<$<CONFIG:Debug>:d>
-nodefaultlib:msvcrt$<$<CONFIG:Debug>:d>
/nodefaultlib:vccorlib$<$<CONFIG:Debug>:d>
/nodefaultlib:msvcrt$<$<CONFIG:Debug>:d>
)
endif()
@ -2988,6 +2988,24 @@ if(PS2)
sdl_compile_options(PRIVATE "-Wno-error=declaration-after-statement")
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)
get_property(sources TARGET SDL3-collector PROPERTY INTERFACE_SOURCES)
foreach(SOURCE_FILE IN LISTS sources)
@ -3058,9 +3076,15 @@ if(SDL_SHARED)
)
endif()
if(NOT SDL_LIBC)
if(MSVC AND SDL_CPU_X86)
# FIXME: should be added for all architectures (missing symbols for ARM)
target_link_libraries(SDL3-shared PRIVATE "-nodefaultlib:MSVCRT")
if(MSVC AND (NOT MSVC_CLANG AND NOT WINDOWS_STORE))
# Don't try to link with the default set of libraries.
# 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()
if(HAS_Q_NO_USE_LIBIRC)
target_compile_options(SDL3-shared PRIVATE /Q_no-use-libirc)
@ -3095,14 +3119,6 @@ if(SDL_SHARED)
)
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_include_directories(SDL3-shared
PRIVATE