[CMake][Win] Conditionally select DLL CRT or static CRT
[WebKit-https.git] / Source / cmake / OptionsMSVC.cmake
1 add_compile_options(
2     /wd4018 /wd4068 /wd4099 /wd4100 /wd4127 /wd4138 /wd4146 /wd4180 /wd4189
3     /wd4201 /wd4206 /wd4244 /wd4251 /wd4267 /wd4275 /wd4288 /wd4291 /wd4305
4     /wd4309 /wd4344 /wd4355 /wd4389 /wd4396 /wd4456 /wd4457 /wd4458 /wd4459
5     /wd4481 /wd4503 /wd4505 /wd4510 /wd4512 /wd4530 /wd4610 /wd4611 /wd4646
6     /wd4702 /wd4706 /wd4722 /wd4800 /wd4819 /wd4951 /wd4952 /wd4996 /wd6011
7     /wd6031 /wd6211 /wd6246 /wd6255 /wd6387
8 )
9
10 # Create pdb files for debugging purposes, also for Release builds
11 add_compile_options(/Zi /GS)
12
13 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUG /OPT:ICF /OPT:REF")
14 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG /OPT:ICF /OPT:REF")
15
16 # We do not use exceptions
17 add_definitions(-D_HAS_EXCEPTIONS=0)
18 add_compile_options(/EHa- /EHc- /EHs- /fp:except-)
19
20 # We have some very large object files that have to be linked
21 add_compile_options(/analyze- /bigobj)
22
23 # Use CRT security features
24 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
25 if (NOT COMPILER_IS_CLANG_CL)
26     add_definitions(-D_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1)
27 endif ()
28
29 # Turn off certain link features
30 add_compile_options(/Gy- /openmp- /GF-)
31
32 # Specify the source code encoding
33 add_compile_options(/utf-8 /validate-charset)
34
35 if (${CMAKE_BUILD_TYPE} MATCHES "Debug")
36     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /OPT:NOREF /OPT:NOICF")
37     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /OPT:NOREF /OPT:NOICF")
38
39     # To debug linking time issues, uncomment the following three lines:
40     #add_compile_options(/Bv)
41     #set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /VERBOSE /VERBOSE:INCR /TIME")
42     #set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /VERBOSE /VERBOSE:INCR /TIME")
43 elseif (${CMAKE_BUILD_TYPE} MATCHES "Release")
44     add_compile_options(/Oy-)
45 endif ()
46
47 if (NOT ${CMAKE_GENERATOR} MATCHES "Ninja")
48     link_directories("${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_BUILD_TYPE}")
49     add_definitions(/MP)
50 endif ()
51 if (NOT ${CMAKE_CXX_FLAGS} STREQUAL "")
52     string(REGEX REPLACE "(/EH[a-z]+) " "\\1- " CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) # Disable C++ exceptions
53     string(REGEX REPLACE "/EHsc$" "/EHs- /EHc- " CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) # Disable C++ exceptions
54     string(REGEX REPLACE "/GR " "/GR- " CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) # Disable RTTI
55     string(REGEX REPLACE "/W3" "/W4" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) # Warnings are important
56 endif ()
57
58 if (MSVC_STATIC_RUNTIME)
59     message(STATUS "Using multithreaded, static version of the run-time library")
60     set(MSVC_RUNTIME_COMPILE_FLAG "/MT")
61     set(MSVC_RUNTIME_LINKER_FLAGS "/NODEFAULTLIB:MSVCRT /NODEFAULTLIB:MSVCRTD")
62 else ()
63     message(STATUS "Using multithreaded, dynamic version of the run-time library")
64     set(MSVC_RUNTIME_COMPILE_FLAG "/MD")
65     # No linker flags are required
66 endif ()
67
68 foreach (flag_var
69     CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
70     CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
71     CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
72     CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
73     # Use the multithreaded static runtime library instead of the default DLL runtime.
74     string(REGEX REPLACE "/MD" "${MSVC_RUNTIME_COMPILE_FLAG}" ${flag_var} "${${flag_var}}")
75
76     # No debug runtime, even in debug builds.
77     if (NOT DEBUG_SUFFIX)
78         string(REGEX REPLACE "${MSVC_RUNTIME_COMPILE_FLAG}d" "${MSVC_RUNTIME_COMPILE_FLAG}" ${flag_var} "${${flag_var}}")
79         string(REGEX REPLACE "/D_DEBUG" "" ${flag_var} "${${flag_var}}")
80     endif ()
81 endforeach ()
82
83 # Make sure incremental linking is turned off, as it creates unacceptably long link times.
84 string(REPLACE "INCREMENTAL:YES" "INCREMENTAL:NO" replace_CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS})
85 set(CMAKE_SHARED_LINKER_FLAGS "${replace_CMAKE_SHARED_LINKER_FLAGS} /INCREMENTAL:NO")
86 string(REPLACE "INCREMENTAL:YES" "INCREMENTAL:NO" replace_CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS})
87 set(CMAKE_EXE_LINKER_FLAGS "${replace_CMAKE_EXE_LINKER_FLAGS} /INCREMENTAL:NO")
88
89 string(REPLACE "INCREMENTAL:YES" "INCREMENTAL:NO" replace_CMAKE_SHARED_LINKER_FLAGS_DEBUG ${CMAKE_SHARED_LINKER_FLAGS_DEBUG})
90 set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${replace_CMAKE_SHARED_LINKER_FLAGS_DEBUG} /INCREMENTAL:NO")
91 string(REPLACE "INCREMENTAL:YES" "INCREMENTAL:NO" replace_CMAKE_EXE_LINKER_FLAGS_DEBUG ${CMAKE_EXE_LINKER_FLAGS_DEBUG})
92 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${replace_CMAKE_EXE_LINKER_FLAGS_DEBUG} /INCREMENTAL:NO")
93
94 string(REPLACE "INCREMENTAL:YES" "INCREMENTAL:NO" replace_CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO ${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO})
95 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${replace_CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /INCREMENTAL:NO")
96 string(REPLACE "INCREMENTAL:YES" "INCREMENTAL:NO" replace_CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO ${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO})
97 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${replace_CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /INCREMENTAL:NO")
98
99 if (COMPILER_IS_CLANG_CL)
100     # FIXME: We need to set the msc-version above the one it defaults to
101     # when using clang-cl with VS2015. This might be unnecessary when moving to
102     # VS2017 as part of https://bugs.webkit.org/show_bug.cgi?id=172412
103     add_compile_options(-fmsc-version=190023918)
104
105     # FIXME: Building with clang-cl seemed to fail with 128 bit int support
106     set(HAVE_INT128_T OFF)
107     list(REMOVE_ITEM _WEBKIT_CONFIG_FILE_VARIABLES HAVE_INT128_T)
108 endif ()