Sync with trunk r65656.
[reactos.git] / cmake / msvc.cmake
1
2 #if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
3 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
4 # no optimization
5 elseif(OPTIMIZE STREQUAL "1")
6 add_definitions(/O1)
7 elseif(OPTIMIZE STREQUAL "2")
8 add_definitions(/O2)
9 elseif(OPTIMIZE STREQUAL "3")
10 add_definitions(/Ot /Ox /GS-)
11 elseif(OPTIMIZE STREQUAL "4")
12 add_definitions(/Os /Ox /GS-)
13 elseif(OPTIMIZE STREQUAL "5")
14 add_definitions(/GF /Gy /Ob2 /Os /Ox /GS-)
15 endif()
16
17 if(ARCH STREQUAL "i386")
18 add_definitions(/DWIN32 /D_WINDOWS)
19 endif()
20
21 add_definitions(/Dinline=__inline /D__STDC__=1)
22
23 add_compile_flags("/X /GR- /EHs-c- /GS- /Zl /W3")
24
25 # HACK: for VS 11+ we need to explicitly disable SSE, which is off by
26 # default for older compilers. See CORE-6507
27 if(MSVC_VERSION GREATER 1699 AND ARCH STREQUAL "i386")
28 add_compile_flags("/arch:IA32")
29 endif ()
30
31 # VS 12+ requires /FS when used in parallel compilations
32 if(MSVC_VERSION GREATER 1799 AND NOT MSVC_IDE)
33 add_compile_flags("/FS")
34 endif ()
35
36 # Disable overly sensitive warnings as well as those that generally aren't
37 # useful to us.
38 # - TODO: C4018: signed/unsigned mismatch
39 # - C4244: implicit integer truncation
40 # - C4290: C++ exception specification ignored
41 #add_compile_flags("/wd4018 /wd4244 /wd4290")
42 add_compile_flags("/wd4290 /wd4244")
43
44 # The following warnings are treated as errors:
45 # - C4013: implicit function declaration
46 # - C4020: too many actual parameters
47 # - C4022: pointer type mismatch for parameter
48 # - TODO: C4028: formal parameter different from declaration
49 # - C4047: different level of indirection
50 # - TODO: C4090: different 'modifier' qualifiers (for C programs only;
51 # for C++ programs, the compiler error C2440 is issued)
52 # - C4098: void function returning a value
53 # - C4113: parameter lists differ
54 # - C4129: unrecognized escape sequence
55 # - TODO: C4133: incompatible types
56 # - C4163: 'identifier': not available as an intrinsic function
57 # - C4229: modifiers on data are ignored
58 # - C4700: uninitialized variable usage
59 # - C4603: macro is not defined or definition is different after precompiled header use
60 add_compile_flags("/we4013 /we4020 /we4022 /we4047 /we4098 /we4113 /we4129 /we4163 /we4229 /we4700 /we4603")
61
62 # Enable warnings above the default level, but don't treat them as errors:
63 # - C4115: named type definition in parentheses
64 add_compile_flags("/w14115")
65
66 # Debugging
67 #if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
68 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
69 if(NOT (_PREFAST_ OR _VS_ANALYZE_))
70 add_compile_flags("/Zi")
71 endif()
72 add_compile_flags("/Ob0 /Od")
73 #elseif(${CMAKE_BUILD_TYPE} STREQUAL "Release")
74 elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
75 add_compile_flags("/Ob2 /D NDEBUG")
76 endif()
77
78 if(MSVC_IDE)
79 add_compile_flags("/MP")
80 if(NOT DEFINED USE_FOLDER_STRUCTURE)
81 set(USE_FOLDER_STRUCTURE FALSE)
82 endif()
83 endif()
84
85 if(NOT DEFINED RUNTIME_CHECKS)
86 set(RUNTIME_CHECKS FALSE)
87 endif()
88
89 if(RUNTIME_CHECKS)
90 add_definitions(-D__RUNTIME_CHECKS__)
91 add_compile_flags("/RTC1")
92 endif()
93
94 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE")
95 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE")
96 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE")
97
98 if(CMAKE_DISABLE_NINJA_DEPSLOG)
99 set(cl_includes_flag "")
100 else()
101 set(cl_includes_flag "/showIncludes")
102 endif()
103
104 if(MSVC_IDE AND (CMAKE_VERSION MATCHES "ReactOS"))
105 # For VS builds we'll only have en-US in resource files
106 add_definitions(/DLANGUAGE_EN_US)
107 else()
108 # Only VS 10+ resource compiler supports /nologo
109 if(MSVC_VERSION GREATER 1599)
110 set(rc_nologo_flag "/nologo")
111 else()
112 set(rc_nologo_flag)
113 endif()
114 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${rc_nologo_flag} <FLAGS> <DEFINES> ${I18N_DEFS} /fo<OBJECT> <SOURCE>")
115 if(ARCH STREQUAL "arm")
116 set(CMAKE_ASM_COMPILE_OBJECT
117 "cl ${cl_includes_flag} /nologo /X /I${REACTOS_SOURCE_DIR}/include/asm /I${REACTOS_BINARY_DIR}/include/asm <FLAGS> <DEFINES> /D__ASM__ /D_USE_ML /EP /c <SOURCE> > <OBJECT>.tmp"
118 "<CMAKE_ASM_COMPILER> -nologo -o <OBJECT> <OBJECT>.tmp")
119 else()
120 set(CMAKE_ASM_COMPILE_OBJECT
121 "cl ${cl_includes_flag} /nologo /X /I${REACTOS_SOURCE_DIR}/include/asm /I${REACTOS_BINARY_DIR}/include/asm <FLAGS> <DEFINES> /D__ASM__ /D_USE_ML /EP /c <SOURCE> > <OBJECT>.tmp"
122 "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <OBJECT>.tmp")
123 endif()
124 endif()
125
126 if(_VS_ANALYZE_)
127 message("VS static analysis enabled!")
128 add_compile_flags("/analyze")
129 elseif(_PREFAST_)
130 message("PREFAST enabled!")
131 set(CMAKE_C_COMPILE_OBJECT "prefast <CMAKE_C_COMPILER> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO} <FLAGS> <DEFINES> /Fo<OBJECT> -c <SOURCE>${CMAKE_END_TEMP_FILE}"
132 "prefast LIST")
133 set(CMAKE_CXX_COMPILE_OBJECT "prefast <CMAKE_CXX_COMPILER> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO} <FLAGS> <DEFINES> /TP /Fo<OBJECT> -c <SOURCE>${CMAKE_END_TEMP_FILE}"
134 "prefast LIST")
135 set(CMAKE_C_LINK_EXECUTABLE
136 "<CMAKE_C_COMPILER> ${CMAKE_CL_NOLOGO} <OBJECTS> ${CMAKE_START_TEMP_FILE} <FLAGS> /Fe<TARGET> -link /implib:<TARGET_IMPLIB> /version:<TARGET_VERSION_MAJOR>.<TARGET_VERSION_MINOR> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES>${CMAKE_END_TEMP_FILE}")
137 set(CMAKE_CXX_LINK_EXECUTABLE
138 "<CMAKE_CXX_COMPILER> ${CMAKE_CL_NOLOGO} <OBJECTS> ${CMAKE_START_TEMP_FILE} <FLAGS> /Fe<TARGET> -link /implib:<TARGET_IMPLIB> /version:<TARGET_VERSION_MAJOR>.<TARGET_VERSION_MINOR> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES>${CMAKE_END_TEMP_FILE}")
139 endif()
140
141 set(CMAKE_RC_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
142 set(CMAKE_ASM_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
143 set(CMAKE_ASM_CREATE_STATIC_LIBRARY ${CMAKE_C_CREATE_STATIC_LIBRARY})
144
145 if(PCH)
146 macro(add_pch _target _pch _sources)
147
148 # Workaround for the MSVC toolchain (MSBUILD) /MP bug
149 set(_temp_gch ${CMAKE_CURRENT_BINARY_DIR}/${_target}.pch)
150 if(MSVC_IDE)
151 file(TO_NATIVE_PATH ${_temp_gch} _gch)
152 else()
153 set(_gch ${_temp_gch})
154 endif()
155
156 if(IS_CPP)
157 set(_pch_language CXX)
158 set(_cl_lang_flag "/TP")
159 else()
160 set(_pch_language C)
161 set(_cl_lang_flag "/TC")
162 endif()
163
164 if(MSVC_IDE)
165 set(_pch_path_name_flag "/Fp${_gch}")
166 endif()
167
168 # Build the precompiled header
169 # HEADER_FILE_ONLY FALSE: force compiling the header
170 set_source_files_properties(${_pch} PROPERTIES
171 HEADER_FILE_ONLY FALSE
172 LANGUAGE ${_pch_language}
173 COMPILE_FLAGS "${_cl_lang_flag} /Yc /Fp${_gch}"
174 OBJECT_OUTPUTS ${_gch})
175
176 # Prevent a race condition related to writing to the PDB files between the PCH and the excluded list of source files
177 get_target_property(_target_sources ${_target} SOURCES)
178 list(REMOVE_ITEM _target_sources ${_pch})
179 foreach(_target_src ${_target_sources})
180 set_property(SOURCE ${_target_src} APPEND PROPERTY OBJECT_DEPENDS ${_gch})
181 endforeach()
182
183 # Use the precompiled header with the specified source files, skipping the pch itself
184 list(REMOVE_ITEM ${_sources} ${_pch})
185 foreach(_src ${${_sources}})
186 set_property(SOURCE ${_src} APPEND_STRING PROPERTY COMPILE_FLAGS " /FI${_gch} /Yu${_gch} ${_pch_path_name_flag}")
187 endforeach()
188 endmacro()
189 else()
190 macro(add_pch _target _pch _sources)
191 endmacro()
192 endif()
193
194 function(set_entrypoint _module _entrypoint)
195 if(${_entrypoint} STREQUAL "0")
196 add_target_link_flags(${_module} "/NOENTRY")
197 elseif(ARCH STREQUAL "i386")
198 set(_entrysymbol ${_entrypoint})
199 if(${ARGC} GREATER 2)
200 set(_entrysymbol ${_entrysymbol}@${ARGV2})
201 endif()
202 add_target_link_flags(${_module} "/ENTRY:${_entrysymbol}")
203 else()
204 add_target_link_flags(${_module} "/ENTRY:${_entrypoint}")
205 endif()
206 endfunction()
207
208 function(set_subsystem MODULE SUBSYSTEM)
209 if(ARCH STREQUAL "amd64")
210 add_target_link_flags(${MODULE} "/SUBSYSTEM:${SUBSYSTEM},5.02")
211 else()
212 add_target_link_flags(${MODULE} "/SUBSYSTEM:${SUBSYSTEM},5.01")
213 endif()
214 endfunction()
215
216 function(set_image_base MODULE IMAGE_BASE)
217 add_target_link_flags(${MODULE} "/BASE:${IMAGE_BASE}")
218 endfunction()
219
220 function(set_module_type_toolchain MODULE TYPE)
221 if(CPP_USE_STL)
222 if((${TYPE} STREQUAL "kernelmodedriver") OR (${TYPE} STREQUAL "wdmdriver"))
223 message(FATAL_ERROR "Use of STL in kernelmodedriver or wdmdriver type module prohibited")
224 endif()
225 target_link_libraries(${MODULE} cpprt stlport oldnames)
226 elseif(CPP_USE_RT)
227 target_link_libraries(${MODULE} cpprt)
228 endif()
229 if((${TYPE} STREQUAL "win32dll") OR (${TYPE} STREQUAL "win32ocx") OR (${TYPE} STREQUAL "cpl"))
230 add_target_link_flags(${MODULE} "/DLL")
231 elseif(${TYPE} STREQUAL "kernelmodedriver")
232 add_target_link_flags(${MODULE} "/DRIVER")
233 elseif(${TYPE} STREQUAL "wdmdriver")
234 add_target_link_flags(${MODULE} "/DRIVER:WDM")
235 endif()
236
237 if(RUNTIME_CHECKS)
238 target_link_libraries(${MODULE} runtmchk)
239 endif()
240
241 endfunction()
242
243 # Define those for having real libraries
244 set(CMAKE_IMPLIB_CREATE_STATIC_LIBRARY "LINK /LIB /NOLOGO <LINK_FLAGS> /OUT:<TARGET> <OBJECTS>")
245
246 if(ARCH STREQUAL "arm")
247 set(CMAKE_STUB_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> -nologo -o <OBJECT> <SOURCE>")
248 else()
249 set(CMAKE_STUB_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <SOURCE>")
250 endif()
251
252 function(add_delay_importlibs _module)
253 get_target_property(_module_type ${_module} TYPE)
254 if(_module_type STREQUAL "STATIC_LIBRARY")
255 message(FATAL_ERROR "Cannot add delay imports to a static library")
256 endif()
257 foreach(_lib ${ARGN})
258 add_target_link_flags(${_module} "/DELAYLOAD:${_lib}.dll")
259 target_link_libraries(${_module} lib${_lib})
260 endforeach()
261 target_link_libraries(${_module} delayimp)
262 endfunction()
263
264 function(generate_import_lib _libname _dllname _spec_file)
265
266 set(_def_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_exp.def)
267 set(_asm_stubs_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_stubs.asm)
268
269 # Generate the asm stub file and the def file for import library
270 add_custom_command(
271 OUTPUT ${_asm_stubs_file} ${_def_file}
272 COMMAND native-spec2def --ms -a=${SPEC2DEF_ARCH} --implib -n=${_dllname} -d=${_def_file} -l=${_asm_stubs_file} ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
273 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
274
275 if(MSVC_IDE)
276 # Compile the generated asm stub file
277 if(ARCH STREQUAL "arm")
278 set(_asm_stub_command ${CMAKE_ASM_COMPILER} -nologo -o ${_asm_stubs_file}.obj ${_asm_stubs_file})
279 else()
280 set(_asm_stub_command ${CMAKE_ASM_COMPILER} /Cp /Fo${_asm_stubs_file}.obj /c /Ta ${_asm_stubs_file})
281 endif()
282 add_custom_command(
283 OUTPUT ${_asm_stubs_file}.obj
284 COMMAND ${_asm_stub_command}
285 DEPENDS ${_asm_stubs_file})
286 else()
287 # Be clear about the "language"
288 # Thanks MS for creating a stupid linker
289 set_source_files_properties(${_asm_stubs_file} PROPERTIES LANGUAGE "STUB_ASM")
290 endif()
291
292 # Add our library
293 if(MSVC_IDE)
294 add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${_asm_stubs_file}.obj)
295 set_source_files_properties(${_asm_stubs_file}.obj PROPERTIES EXTERNAL_OBJECT 1)
296 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "C")
297 else()
298 # NOTE: as stub file and def file are generated in one pass, depending on one is like depending on the other
299 add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${_asm_stubs_file})
300 # set correct "link rule"
301 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "IMPLIB")
302 endif()
303 set_target_properties(${_libname} PROPERTIES STATIC_LIBRARY_FLAGS "/DEF:${_def_file}")
304 endfunction()
305
306 if(ARCH STREQUAL "amd64")
307 # This is NOT a typo.
308 # See https://software.intel.com/en-us/forums/topic/404643
309 add_definitions(/D__x86_64)
310 set(SPEC2DEF_ARCH x86_64)
311 elseif(ARCH STREQUAL "arm")
312 add_definitions(/D__arm__)
313 set(SPEC2DEF_ARCH arm)
314 else()
315 set(SPEC2DEF_ARCH i386)
316 endif()
317 function(spec2def _dllname _spec_file)
318 # Do we also want to add importlib targets?
319 if(${ARGC} GREATER 2)
320 if(${ARGN} STREQUAL "ADD_IMPORTLIB")
321 set(__add_importlib TRUE)
322 else()
323 message(FATAL_ERROR "Wrong argument passed to spec2def, ${ARGN}")
324 endif()
325 endif()
326
327 # Get library basename
328 get_filename_component(_file ${_dllname} NAME_WE)
329
330 # Error out on anything else than spec
331 if(NOT ${_spec_file} MATCHES ".*\\.spec")
332 message(FATAL_ERROR "spec2def only takes spec files as input.")
333 endif()
334
335 # Generate exports def and C stubs file for the DLL
336 add_custom_command(
337 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
338 COMMAND native-spec2def --ms -a=${SPEC2DEF_ARCH} -n=${_dllname} -d=${CMAKE_CURRENT_BINARY_DIR}/${_file}.def -s=${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
339 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
340
341 if(__add_importlib)
342 generate_import_lib(lib${_file} ${_dllname} ${_spec_file})
343 endif()
344 endfunction()
345
346 macro(macro_mc FLAG FILE)
347 set(COMMAND_MC ${CMAKE_MC_COMPILER} ${FLAG} -b ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.mc -r ${REACTOS_BINARY_DIR}/include/reactos -h ${REACTOS_BINARY_DIR}/include/reactos)
348 endmacro()
349
350 # PSEH workaround
351 set(PSEH_LIB "pseh")
352
353 # Use a full path for the x86 version of ml when using x64 VS.
354 # It's not a problem when using the DDK/WDK because, in x64 mode,
355 # both the x86 and x64 versions of ml are available.
356 if((ARCH STREQUAL "amd64") AND (DEFINED ENV{VCINSTALLDIR}))
357 set(CMAKE_ASM16_COMPILER $ENV{VCINSTALLDIR}/bin/ml.exe)
358 elseif(ARCH STREQUAL "arm")
359 set(CMAKE_ASM16_COMPILER armasm.exe)
360 else()
361 set(CMAKE_ASM16_COMPILER ml.exe)
362 endif()
363
364 function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
365 set(_object_file ${_binary_file}.obj)
366 set(_temp_file ${_binary_file}.tmp)
367
368 add_custom_command(
369 OUTPUT ${_temp_file}
370 COMMAND ${CMAKE_C_COMPILER} /nologo /X /I${REACTOS_SOURCE_DIR}/include/asm /I${REACTOS_BINARY_DIR}/include/asm /I${REACTOS_SOURCE_DIR}/boot/freeldr /D__ASM__ /D_USE_ML /EP /c ${_asm_file} > ${_temp_file}
371 DEPENDS ${_asm_file})
372
373 if(ARCH STREQUAL "arm")
374 set(_asm16_command ${CMAKE_ASM16_COMPILER} -nologo -o ${_object_file} ${_temp_file})
375 else()
376 set(_asm16_command ${CMAKE_ASM16_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_temp_file})
377 endif()
378
379 add_custom_command(
380 OUTPUT ${_object_file}
381 COMMAND ${_asm16_command}
382 DEPENDS ${_temp_file})
383
384 add_custom_command(
385 OUTPUT ${_binary_file}
386 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
387 DEPENDS ${_object_file} native-obj2bin)
388
389 set_source_files_properties(${_object_file} ${_temp_file} ${_binary_file} PROPERTIES GENERATED TRUE)
390
391 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
392 endfunction()
393
394 function(allow_warnings __module)
395 endfunction()
396
397 macro(add_asm_files _target)
398 if(MSVC_IDE AND (CMAKE_VERSION MATCHES "ReactOS"))
399 get_defines(_directory_defines)
400 get_includes(_directory_includes)
401 get_directory_property(_defines COMPILE_DEFINITIONS)
402 foreach(_source_file ${ARGN})
403 get_filename_component(_source_file_base_name ${_source_file} NAME_WE)
404 get_filename_component(_source_file_full_path ${_source_file} ABSOLUTE)
405 set(_preprocessed_asm_file ${CMAKE_CURRENT_BINARY_DIR}/asm/${_source_file_base_name}_${_target}.tmp)
406 set(_object_file ${CMAKE_CURRENT_BINARY_DIR}/asm/${_source_file_base_name}_${_target}.obj)
407 get_source_file_property(_defines_semicolon_list ${_source_file_full_path} COMPILE_DEFINITIONS)
408 unset(_source_file_defines)
409 foreach(_define ${_defines_semicolon_list})
410 if(NOT ${_define} STREQUAL "NOTFOUND")
411 list(APPEND _source_file_defines -D${_define})
412 endif()
413 endforeach()
414 if(ARCH STREQUAL "arm")
415 set(_pp_asm_compile_command ${CMAKE_ASM_COMPILER} -nologo -o ${_object_file} ${_preprocessed_asm_file})
416 else()
417 set(_pp_asm_compile_command ${CMAKE_ASM_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_preprocessed_asm_file})
418 endif()
419 add_custom_command(
420 OUTPUT ${_preprocessed_asm_file} ${_object_file}
421 COMMAND cl /nologo /X /I${REACTOS_SOURCE_DIR}/include/asm /I${REACTOS_BINARY_DIR}/include/asm ${_directory_includes} ${_source_file_defines} ${_directory_defines} /D__ASM__ /D_USE_ML /EP /c ${_source_file_full_path} > ${_preprocessed_asm_file} && ${_pp_asm_compile_command}
422 DEPENDS ${_source_file_full_path})
423 set_source_files_properties(${_object_file} PROPERTIES EXTERNAL_OBJECT 1)
424 list(APPEND ${_target} ${_object_file})
425 endforeach()
426 else()
427 list(APPEND ${_target} ${ARGN})
428 endif()
429 endmacro()