Skip to content

Commit 203f607

Browse files
committed
build: use the new -libc option
This sets up the build to link via the new `-libc` option so that we get the correct MSVC runtime library.
1 parent 7e67c02 commit 203f607

File tree

1 file changed

+20
-43
lines changed

1 file changed

+20
-43
lines changed

CMakeLists.txt

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@ cmake_minimum_required(VERSION 3.4.3)
44
list(APPEND CMAKE_MODULE_PATH
55
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
66

7+
# NOTE(compnerd) enable CMP0091 - select MSVC runtime based on
8+
# CMAKE_MSVC_RUNTIME_LIBRARY. Requires CMake 3.15 or newer
9+
if(POLICY CMP0091)
10+
cmake_policy(CMP0091 NEW)
11+
endif()
12+
713
project(Foundation
814
LANGUAGES
915
C)
1016
enable_testing()
1117

18+
# NOTE(compnerd) default to /MD or /MDd by default based on the configuration.
19+
# Cache the variable to allow the user to alter the configuration.
20+
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL" CACHE
21+
STRING "MSVC Runtime Library")
22+
1223
option(BUILD_SHARED_LIBS "build shared libraries" ON)
1324

1425
option(FOUNDATION_ENABLE_LIBDISPATCH "Enable GCD Support" YES)
@@ -73,6 +84,12 @@ endif()
7384
if(ENABLE_TESTING)
7485
set(swift_enable_testing -enable-testing)
7586
endif()
87+
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
88+
set(swift_libc_flags -libc;${CMAKE_MSVC_RUNTIME_LIBRARY})
89+
if(CMAKE_SYSTEM_NAME STREQUAL Debug)
90+
list(APPEND swift_libc_flags -Xcc;-D_DEBUG)
91+
endif()
92+
endif()
7693

7794
set(deployment_enable_libdispatch)
7895
set(libdispatch_cflags)
@@ -95,7 +112,6 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
95112
elseif(CMAKE_SYSTEM_NAME STREQUAL Windows)
96113
# FIXME(SR9138) Silence "locally defined symbol '…' imported in function '…'
97114
set(WORKAROUND_SR9138 -Xlinker;-ignore:4049;-Xlinker;-ignore:4217)
98-
set(WORKAROUND_SR9995 -Xlinker;-nodefaultlib:libcmt)
99115
endif()
100116

101117
# NOTE(compnerd) this is a horrible hack to work around the fact that we do not
@@ -111,35 +127,6 @@ foreach(library ${CoreFoundation_LINK_LIBRARIES})
111127
endif()
112128
endforeach()
113129

114-
# Windows uses 4 different variants of the C library that are normally
115-
# controlled by options to the compiler:
116-
#
117-
# Option | Flags | Runtime
118-
# --------+-----------------------+------------
119-
# /MTd | -D_DEBUG -D_MT | libcmtd.lib
120-
# /MT | -D_MT | libcmt.lib
121-
# /MDd | -D_DEBUG -D_DLL -D_MT | msvcrtd.lib
122-
# /MD | -D_DLL -D_MT | msvcrt.lib
123-
124-
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
125-
set(MSVCRT_C_FLAGS)
126-
set(MSVCRT_LINK_FLAGS)
127-
if(CMAKE_BUILD_TYPE STREQUAL Debug)
128-
list(APPEND MSVCRT_C_FLAGS -D_DEBUG)
129-
endif()
130-
# We currently do not support building in /MT or /MTd
131-
list(APPEND MSVCRT_C_FLAGS -D_MT)
132-
list(APPEND MSVCRT_C_FLAGS -D_DLL)
133-
# Ensure that we link against the correct CRT
134-
if(CMAKE_BUILD_TYPE STREQUAL Debug)
135-
list(APPEND MSVCRT_C_FLAGS -Xclang --dependent-lib=msvcrtd)
136-
list(APPEND MSVCRT_LINK_FLAGS -Xlinker -defaultlib:msvcrtd.lib)
137-
else()
138-
list(APPEND MSVCRT_C_FLAGS -Xclang --dependent-lib=msvcrt)
139-
list(APPEND MSVCRT_LINK_FLAGS -Xlinker -defaultlib:msvcrt.lib)
140-
endif()
141-
endif()
142-
143130
add_swift_library(Foundation
144131
MODULE_NAME
145132
Foundation
@@ -316,12 +303,10 @@ add_swift_library(Foundation
316303
TARGET
317304
${CMAKE_C_COMPILER_TARGET}
318305
CFLAGS
319-
${MSVCRT_C_FLAGS}
320306
${deployment_target}
321307
${deployment_enable_libdispatch}
322308
-F${CMAKE_CURRENT_BINARY_DIR}
323309
LINK_FLAGS
324-
${MSVCRT_LINK_FLAGS}
325310
${CoreFoundation_LIBRARIES}
326311
${CURL_LIBRARIES}
327312
${ICU_UC_LIBRARY} ${ICU_I18N_LIBRARY}
@@ -330,7 +315,6 @@ add_swift_library(Foundation
330315
$<TARGET_FILE:uuid>
331316
${Foundation_RPATH}
332317
${WORKAROUND_SR9138}
333-
${WORKAROUND_SR9995}
334318
$<$<PLATFORM_ID:Windows>:-lDbgHelp>
335319
$<$<PLATFORM_ID:Windows>:-lOle32>
336320
$<$<PLATFORM_ID:Windows>:-lShLwApi>
@@ -346,6 +330,7 @@ add_swift_library(Foundation
346330
${libdispatch_cflags}
347331
${swift_enable_testing}
348332
${swift_optimization_flags}
333+
${swift_libc_flags}
349334
DEPENDS
350335
uuid
351336
CoreFoundation
@@ -367,18 +352,15 @@ add_swift_executable(plutil
367352
SOURCES
368353
Tools/plutil/main.swift
369354
CFLAGS
370-
${MSVCRT_C_FLAGS}
371355
${deployment_target}
372356
${deployment_enable_libdispatch}
373357
-F${CMAKE_CURRENT_BINARY_DIR}
374358
LINK_FLAGS
375-
${MSVCRT_LINK_FLAGS}
376359
${libdispatch_ldflags}
377360
-L${CMAKE_CURRENT_BINARY_DIR}
378361
-lFoundation
379362
${Foundation_INTERFACE_LIBRARIES}
380363
${Foundation_RPATH}
381-
${WORKAROUND_SR9995}
382364
SWIFT_FLAGS
383365
-DDEPLOYMENT_RUNTIME_SWIFT
384366
${deployment_enable_libdispatch}
@@ -387,6 +369,7 @@ add_swift_executable(plutil
387369
${libdispatch_cflags}
388370
${swift_enable_testing}
389371
${swift_optimization_flags}
372+
${swift_libc_flags}
390373
DEPENDS
391374
uuid
392375
Foundation
@@ -395,17 +378,14 @@ add_swift_executable(plutil
395378
if(ENABLE_TESTING)
396379
add_swift_executable(xdgTestHelper
397380
CFLAGS
398-
${MSVCRT_C_FLAGS}
399381
${deployment_target}
400382
${deployment_enable_libdispatch}
401383
-F${CMAKE_CURRENT_BINARY_DIR}
402384
LINK_FLAGS
403-
${MSVCRT_LINK_FLAGS}
404385
${libdispatch_ldflags}
405386
-L${CMAKE_CURRENT_BINARY_DIR}
406387
-lFoundation
407388
${Foundation_INTERFACE_LIBRARIES}
408-
${WORKAROUND_SR9995}
409389
${XDG_TEST_HELPER_RPATH}
410390
SOURCES
411391
TestFoundation/xdgTestHelper/main.swift
@@ -517,21 +497,17 @@ if(ENABLE_TESTING)
517497
TestFoundation/TestXMLDocument.swift
518498
TestFoundation/TestXMLParser.swift
519499
CFLAGS
520-
${MSVCRT_C_FLAGS}
521500
${deployment_target}
522501
${deployment_enable_libdispatch}
523502
-F${CMAKE_CURRENT_BINARY_DIR}
524-
-D_DLL
525503
LINK_FLAGS
526-
${MSVCRT_LINK_FLAGS}
527504
${libdispatch_ldflags}
528505
-L${CMAKE_CURRENT_BINARY_DIR}
529506
-lFoundation
530507
${Foundation_INTERFACE_LIBRARIES}
531508
-L${FOUNDATION_PATH_TO_XCTEST_BUILD}
532509
-lXCTest
533510
${WORKAROUND_SR9138}
534-
${WORKAROUND_SR9995}
535511
$<$<PLATFORM_ID:Windows>:-lWS2_32>
536512
RESOURCES
537513
${CMAKE_SOURCE_DIR}/TestFoundation/Resources/Info.plist
@@ -565,6 +541,7 @@ if(ENABLE_TESTING)
565541
-I;${ICU_INCLUDE_DIR}
566542
${libdispatch_cflags}
567543
${swift_optimization_flags}
544+
${swift_libc_flags}
568545
DEPENDS
569546
Foundation
570547
CoreFoundation

0 commit comments

Comments
 (0)