[CLANG-CL] Initial commit that allows us to compile ReactOS with clang-cl.
[reactos.git] / sdk / cmake / Platform / Windows-MSVC.cmake
1
2 #=============================================================================
3 # Copyright 2001-2012 Kitware, Inc.
4 #
5 # Distributed under the OSI-approved BSD License (the "License");
6 # see accompanying file Copyright.txt for details.
7 #
8 # This software is distributed WITHOUT ANY WARRANTY; without even the
9 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 # See the License for more information.
11 #=============================================================================
12 # (To distribute this file outside of CMake, substitute the full
13 # License text for the above reference.)
14
15 # This module is shared by multiple languages; use include blocker.
16 if(__WINDOWS_MSVC)
17 return()
18 endif()
19 set(__WINDOWS_MSVC 1)
20
21 set(CMAKE_LIBRARY_PATH_FLAG "-LIBPATH:")
22 set(CMAKE_LINK_LIBRARY_FLAG "")
23 set(MSVC 1)
24
25 # hack: if a new cmake (which uses CMAKE_LINKER) runs on an old build tree
26 # (where link was hardcoded) and where CMAKE_LINKER isn't in the cache
27 # and still cmake didn't fail in CMakeFindBinUtils.cmake (because it isn't rerun)
28 # hardcode CMAKE_LINKER here to link, so it behaves as it did before, Alex
29 if(NOT DEFINED CMAKE_LINKER)
30 set(CMAKE_LINKER link)
31 endif()
32
33 if(CMAKE_VERBOSE_MAKEFILE)
34 set(CMAKE_CL_NOLOGO)
35 else()
36 set(CMAKE_CL_NOLOGO "/nologo")
37 endif()
38
39 set(WIN32 1)
40
41 if(CMAKE_SYSTEM_NAME MATCHES "WindowsCE")
42 set(CMAKE_CREATE_WIN32_EXE "/subsystem:windowsce /entry:WinMainCRTStartup")
43 set(CMAKE_CREATE_CONSOLE_EXE "/subsystem:windowsce /entry:mainACRTStartup")
44 set(WINCE 1)
45 else()
46 set(CMAKE_CREATE_WIN32_EXE "/subsystem:windows")
47 set(CMAKE_CREATE_CONSOLE_EXE "/subsystem:console")
48 endif()
49
50 if(CMAKE_GENERATOR MATCHES "Visual Studio 6")
51 set (CMAKE_NO_BUILD_TYPE 1)
52 endif()
53 if(NOT CMAKE_NO_BUILD_TYPE AND CMAKE_GENERATOR MATCHES "Visual Studio")
54 set (CMAKE_NO_BUILD_TYPE 1)
55 set (CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING
56 "Semicolon separated list of supported configuration types, only supports Debug, Release, MinSizeRel, and RelWithDebInfo, anything else will be ignored.")
57 mark_as_advanced(CMAKE_CONFIGURATION_TYPES)
58 endif()
59
60 # make sure to enable languages after setting configuration types
61 enable_language(RC)
62 if(CMAKE_VERSION VERSION_LESS 3.4.0)
63 set(CMAKE_COMPILE_RESOURCE "rc <FLAGS> /fo<OBJECT> <SOURCE>")
64 else()
65 set(CMAKE_COMPILE_RESOURCE "rc <INCLUDES> <FLAGS> /fo<OBJECT> <SOURCE>")
66 endif()
67
68 if("${CMAKE_GENERATOR}" MATCHES "Visual Studio")
69 set(MSVC_IDE 1)
70 else()
71 set(MSVC_IDE 0)
72 endif()
73
74 if(NOT MSVC_VERSION)
75 if(CMAKE_C_COMPILER_VERSION)
76 set(_compiler_version ${CMAKE_C_COMPILER_VERSION})
77 else()
78 set(_compiler_version ${CMAKE_CXX_COMPILER_VERSION})
79 endif()
80 if("${_compiler_version}" MATCHES "^([0-9]+)\\.([0-9]+)")
81 math(EXPR MSVC_VERSION "${CMAKE_MATCH_1}*100 + ${CMAKE_MATCH_2}")
82 else()
83 message(FATAL_ERROR "MSVC compiler version not detected properly: ${_compiler_version}")
84 endif()
85
86 set(MSVC10)
87 set(MSVC11)
88 set(MSVC12)
89 set(MSVC60)
90 set(MSVC70)
91 set(MSVC71)
92 set(MSVC80)
93 set(MSVC90)
94 set(CMAKE_COMPILER_2005)
95 set(CMAKE_COMPILER_SUPPORTS_PDBTYPE)
96 if(NOT "${_compiler_version}" VERSION_LESS 18)
97 set(MSVC12 1)
98 elseif(NOT "${_compiler_version}" VERSION_LESS 17)
99 set(MSVC11 1)
100 elseif(NOT "${_compiler_version}" VERSION_LESS 16)
101 set(MSVC10 1)
102 elseif(NOT "${_compiler_version}" VERSION_LESS 15)
103 set(MSVC90 1)
104 elseif(NOT "${_compiler_version}" VERSION_LESS 14)
105 set(MSVC80 1)
106 set(CMAKE_COMPILER_2005 1)
107 elseif(NOT "${_compiler_version}" VERSION_LESS 13.10)
108 set(MSVC71 1)
109 elseif(NOT "${_compiler_version}" VERSION_LESS 13)
110 set(MSVC70 1)
111 else()
112 set(MSVC60 1)
113 set(CMAKE_COMPILER_SUPPORTS_PDBTYPE 1)
114 endif()
115 endif()
116
117 if(MSVC_C_ARCHITECTURE_ID MATCHES 64 OR MSVC_CXX_ARCHITECTURE_ID MATCHES 64)
118 set(CMAKE_CL_64 1)
119 else()
120 set(CMAKE_CL_64 0)
121 endif()
122 if(CMAKE_FORCE_WIN64 OR CMAKE_FORCE_IA64)
123 set(CMAKE_CL_64 1)
124 endif()
125
126 if(MSVC_VERSION GREATER 1599)
127 set(MSVC_INCREMENTAL_DEFAULT ON)
128 endif()
129
130 # default to Debug builds
131 set(CMAKE_BUILD_TYPE_INIT Debug)
132
133 if(CMAKE_SYSTEM_NAME MATCHES "WindowsCE")
134 string(TOUPPER "${MSVC_C_ARCHITECTURE_ID}" _MSVC_C_ARCHITECTURE_ID_UPPER)
135 string(TOUPPER "${MSVC_CXX_ARCHITECTURE_ID}" _MSVC_CXX_ARCHITECTURE_ID_UPPER)
136
137 if("${CMAKE_SYSTEM_VERSION}" MATCHES "^([0-9]+)\\.([0-9]+)")
138 math(EXPR _CE_VERSION "${CMAKE_MATCH_1}*100 + ${CMAKE_MATCH_2}")
139 elseif("${CMAKE_SYSTEM_VERSION}" STREQUAL "")
140 set(_CE_VERSION "500")
141 else()
142 message(FATAL_ERROR "Invalid Windows CE version: ${CMAKE_SYSTEM_VERSION}")
143 endif()
144
145 set(_PLATFORM_DEFINES "/D_WIN32_WCE=0x${_CE_VERSION} /DUNDER_CE")
146 set(_PLATFORM_DEFINES_C " /D${MSVC_C_ARCHITECTURE_ID} /D_${_MSVC_C_ARCHITECTURE_ID_UPPER}_")
147 set(_PLATFORM_DEFINES_CXX " /D${MSVC_CXX_ARCHITECTURE_ID} /D_${_MSVC_CXX_ARCHITECTURE_ID_UPPER}_")
148
149 set(_RTC1 "")
150 set(_FLAGS_CXX " /GR /EHsc")
151 set(CMAKE_C_STANDARD_LIBRARIES_INIT "coredll.lib corelibc.lib ole32.lib oleaut32.lib uuid.lib commctrl.lib")
152 set(CMAKE_EXE_LINKER_FLAGS_INIT "${CMAKE_EXE_LINKER_FLAGS_INIT} /NODEFAULTLIB:libc.lib /NODEFAULTLIB:oldnames.lib")
153 else()
154 set(_PLATFORM_DEFINES "/DWIN32")
155
156 if(MSVC_VERSION GREATER 1310)
157 set(_RTC1 "/RTC1")
158 set(_FLAGS_CXX " /GR /EHsc")
159 set(CMAKE_C_STANDARD_LIBRARIES_INIT "")
160 else()
161 set(_RTC1 "/GZ")
162 set(_FLAGS_CXX " /GR /GX")
163 set(CMAKE_C_STANDARD_LIBRARIES_INIT "")
164 endif()
165 endif()
166
167 set(CMAKE_CXX_STANDARD_LIBRARIES_INIT "${CMAKE_C_STANDARD_LIBRARIES_INIT}")
168
169 # executable linker flags
170 set (CMAKE_LINK_DEF_FILE_FLAG "/DEF:")
171 # set the machine type
172 set(_MACHINE_ARCH_FLAG ${MSVC_C_ARCHITECTURE_ID})
173 if(NOT _MACHINE_ARCH_FLAG)
174 set(_MACHINE_ARCH_FLAG ${MSVC_CXX_ARCHITECTURE_ID})
175 endif()
176 if(CMAKE_SYSTEM_NAME MATCHES "WindowsCE")
177 if(_MACHINE_ARCH_FLAG MATCHES "ARM")
178 set(_MACHINE_ARCH_FLAG "THUMB")
179 elseif(_MACHINE_ARCH_FLAG MATCHES "SH")
180 set(_MACHINE_ARCH_FLAG "SH4")
181 endif()
182 endif()
183 set (CMAKE_EXE_LINKER_FLAGS_INIT
184 "${CMAKE_EXE_LINKER_FLAGS_INIT} /machine:${_MACHINE_ARCH_FLAG} /MANIFEST:NO")
185
186 # add /debug and /INCREMENTAL:YES to DEBUG and RELWITHDEBINFO also add pdbtype
187 # on versions that support it
188 set( MSVC_INCREMENTAL_YES_FLAG "")
189 if(NOT MSVC_INCREMENTAL_DEFAULT)
190 set( MSVC_INCREMENTAL_YES_FLAG "/INCREMENTAL:YES")
191 else()
192 set( MSVC_INCREMENTAL_YES_FLAG "/INCREMENTAL" )
193 endif()
194
195 if (CMAKE_COMPILER_SUPPORTS_PDBTYPE)
196 if(NOT USE_CLANG_CL)
197 set (CMAKE_EXE_LINKER_FLAGS_DEBUG_INIT "/debug /pdbtype:sept")
198 set (CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO_INIT "/debug /pdbtype:sept ${MSVC_INCREMENTAL_YES_FLAG}")
199 else()
200 set(CMAKE_EXE_LINKER_FLAGS_DEBUG_INIT "/debug")
201 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO_INIT "/debug")
202 endif()
203 else ()
204 set (CMAKE_EXE_LINKER_FLAGS_DEBUG_INIT "/debug")
205 set (CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO_INIT "/debug ${MSVC_INCREMENTAL_YES_FLAG}")
206 endif ()
207 # for release and minsize release default to no incremental linking
208 set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL_INIT "/INCREMENTAL:NO")
209 set(CMAKE_EXE_LINKER_FLAGS_RELEASE_INIT "/INCREMENTAL:NO")
210
211 # copy the EXE_LINKER flags to SHARED and MODULE linker flags
212 # shared linker flags
213 set (CMAKE_SHARED_LINKER_FLAGS_INIT ${CMAKE_EXE_LINKER_FLAGS_INIT})
214 set (CMAKE_SHARED_LINKER_FLAGS_DEBUG_INIT ${CMAKE_EXE_LINKER_FLAGS_DEBUG_INIT})
215 set (CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO_INIT ${CMAKE_EXE_LINKER_FLAGS_DEBUG_INIT})
216 set (CMAKE_SHARED_LINKER_FLAGS_RELEASE_INIT ${CMAKE_EXE_LINKER_FLAGS_RELEASE_INIT})
217 set (CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL_INIT ${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL_INIT})
218 # module linker flags
219 set (CMAKE_MODULE_LINKER_FLAGS_INIT ${CMAKE_SHARED_LINKER_FLAGS_INIT})
220 set (CMAKE_MODULE_LINKER_FLAGS_DEBUG_INIT ${CMAKE_SHARED_LINKER_FLAGS_DEBUG_INIT})
221 set (CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO_INIT ${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO_INIT})
222 set (CMAKE_MODULE_LINKER_FLAGS_RELEASE_INIT ${CMAKE_EXE_LINKER_FLAGS_RELEASE_INIT})
223 set (CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL_INIT ${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL_INIT})
224
225 macro(__windows_compiler_msvc lang)
226 if(NOT "${CMAKE_${lang}_COMPILER_VERSION}" VERSION_LESS 14)
227 # for 2005 make sure the manifest is put in the dll with mt
228 set(_CMAKE_VS_LINK_DLL "<CMAKE_COMMAND> -E vs_link_dll ")
229 set(_CMAKE_VS_LINK_EXE "<CMAKE_COMMAND> -E vs_link_exe ")
230 endif()
231 set(CMAKE_${lang}_CREATE_SHARED_LIBRARY "<CMAKE_LINKER> ${CMAKE_CL_NOLOGO} <OBJECTS> ${CMAKE_START_TEMP_FILE} /out:<TARGET> /pdb:<TARGET_PDB> /dll /version:<TARGET_VERSION_MAJOR>.<TARGET_VERSION_MINOR> <LINK_FLAGS> <LINK_LIBRARIES> ${CMAKE_END_TEMP_FILE}")
232
233 set(CMAKE_${lang}_CREATE_SHARED_MODULE ${CMAKE_${lang}_CREATE_SHARED_LIBRARY})
234 set(CMAKE_${lang}_CREATE_STATIC_LIBRARY "<CMAKE_LINKER> /lib ${CMAKE_CL_NOLOGO} <LINK_FLAGS> /out:<TARGET> <OBJECTS> ")
235
236 if(CMAKE_VERSION VERSION_LESS 3.4.0)
237 set(CMAKE_${lang}_COMPILE_OBJECT
238 "<CMAKE_${lang}_COMPILER> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO}${_COMPILE_${lang}} <FLAGS> <DEFINES> /Fo<OBJECT> /Fd<OBJECT_DIR>/ -c <SOURCE>${CMAKE_END_TEMP_FILE}")
239 set(CMAKE_${lang}_CREATE_PREPROCESSED_SOURCE
240 "<CMAKE_${lang}_COMPILER> > <PREPROCESSED_SOURCE> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO}${_COMPILE_${lang}} <FLAGS> <DEFINES> -E <SOURCE>${CMAKE_END_TEMP_FILE}")
241 set(CMAKE_${lang}_CREATE_ASSEMBLY_SOURCE
242 "<CMAKE_${lang}_COMPILER> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO}${_COMPILE_${lang}} <FLAGS> <DEFINES> /FoNUL /FAs /Fa<ASSEMBLY_SOURCE> /c <SOURCE>${CMAKE_END_TEMP_FILE}")
243 else()
244 set(CMAKE_${lang}_COMPILE_OBJECT
245 "<CMAKE_${lang}_COMPILER> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO}${_COMPILE_${lang}} <INCLUDES> <FLAGS> <DEFINES> /Fo<OBJECT> /Fd<OBJECT_DIR>/ -c <SOURCE>${CMAKE_END_TEMP_FILE}")
246 set(CMAKE_${lang}_CREATE_PREPROCESSED_SOURCE
247 "<CMAKE_${lang}_COMPILER> > <PREPROCESSED_SOURCE> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO}${_COMPILE_${lang}} <INCLUDES> <FLAGS> <DEFINES> -E <SOURCE>${CMAKE_END_TEMP_FILE}")
248 set(CMAKE_${lang}_CREATE_ASSEMBLY_SOURCE
249 "<CMAKE_${lang}_COMPILER> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO}${_COMPILE_${lang}} <INCLUDES> <FLAGS> <DEFINES> /FoNUL /FAs /Fa<ASSEMBLY_SOURCE> /c <SOURCE>${CMAKE_END_TEMP_FILE}")
250 endif()
251
252 set(CMAKE_${lang}_USE_RESPONSE_FILE_FOR_OBJECTS 1)
253 set(CMAKE_${lang}_LINK_EXECUTABLE
254 "${_CMAKE_VS_LINK_EXE}<CMAKE_LINKER> ${CMAKE_CL_NOLOGO} <OBJECTS> ${CMAKE_START_TEMP_FILE} /out:<TARGET> /pdb:<TARGET_PDB> /version:<TARGET_VERSION_MAJOR>.<TARGET_VERSION_MINOR> <CMAKE_${lang}_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES>${CMAKE_END_TEMP_FILE}")
255
256 set(CMAKE_${lang}_FLAGS_INIT "")
257 set(CMAKE_${lang}_FLAGS_DEBUG_INIT "")
258 set(CMAKE_${lang}_FLAGS_RELEASE_INIT "")
259 set(CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT "/MD /Zi /O2 /Ob1 /D NDEBUG")
260 set(CMAKE_${lang}_FLAGS_MINSIZEREL_INIT "/MD /O1 /Ob1 /D NDEBUG")
261 endmacro()