* Sync up to trunk HEAD (r62286).
[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 # - TODO: C4244: integer truncation
40 # - C4290: C++ exception specification ignored
41 #add_compile_flags("/wd4018 /wd4244 /wd4290")
42 add_compile_flags("/wd4290")
43
44 # The following warnings are treated as errors:
45 # - C4013: implicit function declaration
46 # - C4022: pointer type mismatch for parameter
47 # - TODO: C4028: formal parameter different from declaration
48 # - C4047: different level of indirection
49 # - TODO: C4090: different 'modifier' qualifiers (for C programs only;
50 # for C++ programs, the compiler error C2440 is issued)
51 # - C4098: void function returning a value
52 # - C4113: parameter lists differ
53 # - C4129: unrecognized escape sequence
54 # - TODO: C4133: incompatible types
55 # - C4229: modifiers on data are ignored
56 # - C4700: uninitialized variable usage
57 # - C4603: macro is not defined or definition is different after precompiled header use
58 add_compile_flags("/we4013 /we4022 /we4047 /we4098 /we4113 /we4129 /we4229 /we4700 /we4603")
59
60 # Enable warnings above the default level, but don't treat them as errors:
61 # - C4115: named type definition in parentheses
62 add_compile_flags("/w14115")
63
64 # Debugging
65 #if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
66 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
67 if(NOT (_PREFAST_ OR _VS_ANALYZE_))
68 add_compile_flags("/Zi")
69 endif()
70 add_compile_flags("/Ob0 /Od")
71 #elseif(${CMAKE_BUILD_TYPE} STREQUAL "Release")
72 elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
73 add_compile_flags("/Ob2 /D NDEBUG")
74 endif()
75
76 if(MSVC_IDE)
77 add_compile_flags("/MP")
78 if(NOT DEFINED USE_FOLDER_STRUCTURE)
79 set(USE_FOLDER_STRUCTURE FALSE)
80 endif()
81 endif()
82
83 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE")
84 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE")
85 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE")
86
87 if(CMAKE_DISABLE_NINJA_DEPSLOG)
88 set(cl_includes_flag "")
89 else()
90 set(cl_includes_flag "/showIncludes")
91 endif()
92
93 if(MSVC_IDE AND (CMAKE_VERSION MATCHES "ReactOS"))
94 # for VS builds we'll only have en-US in resource files
95 add_definitions(/DLANGUAGE_EN_US)
96 else()
97 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> /nologo <FLAGS> <DEFINES> ${I18N_DEFS} /fo<OBJECT> <SOURCE>")
98 set(CMAKE_ASM_COMPILE_OBJECT
99 "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"
100 "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <OBJECT>.tmp")
101 endif()
102
103 if(_VS_ANALYZE_)
104 message("VS static analysis enabled!")
105 add_compile_flags("/analyze")
106 elseif(_PREFAST_)
107 message("PREFAST enabled!")
108 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}"
109 "prefast LIST")
110 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}"
111 "prefast LIST")
112 set(CMAKE_C_LINK_EXECUTABLE
113 "<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}")
114 set(CMAKE_CXX_LINK_EXECUTABLE
115 "<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}")
116 endif()
117
118 set(CMAKE_RC_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
119 set(CMAKE_ASM_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
120 set(CMAKE_ASM_CREATE_STATIC_LIBRARY ${CMAKE_C_CREATE_STATIC_LIBRARY})
121
122 if(PCH)
123 macro(add_pch _target _pch _sources)
124 set(_gch ${CMAKE_CURRENT_BINARY_DIR}/${_target}.pch)
125
126 if(IS_CPP)
127 set(_pch_language CXX)
128 set(_cl_lang_flag "/TP")
129 else()
130 set(_pch_language C)
131 set(_cl_lang_flag "/TC")
132 endif()
133
134 if(MSVC_IDE)
135 set(_pch_path_name_flag "/Fp${_gch}")
136 endif()
137
138 # Build the precompiled header
139 # HEADER_FILE_ONLY FALSE: force compiling the header
140 set_source_files_properties(${_pch} PROPERTIES
141 HEADER_FILE_ONLY FALSE
142 LANGUAGE ${_pch_language}
143 COMPILE_FLAGS "${_cl_lang_flag} /Yc /Fp${_gch}"
144 OBJECT_OUTPUTS ${_gch})
145
146 # Prevent a race condition related to writing to the PDB files between the PCH and the excluded list of source files
147 get_target_property(_target_sources ${_target} SOURCES)
148 list(REMOVE_ITEM _target_sources ${_pch})
149 foreach(_target_src ${_target_sources})
150 set_property(SOURCE ${_target_src} APPEND PROPERTY OBJECT_DEPENDS ${_gch})
151 endforeach()
152
153 # Use the precompiled header with the specified source files, skipping the pch itself
154 list(REMOVE_ITEM ${_sources} ${_pch})
155 foreach(_src ${${_sources}})
156 set_property(SOURCE ${_src} APPEND_STRING PROPERTY COMPILE_FLAGS " /FI${_gch} /Yu${_gch} ${_pch_path_name_flag}")
157 endforeach()
158 endmacro()
159 else()
160 macro(add_pch _target _pch _sources)
161 endmacro()
162 endif()
163
164 function(set_entrypoint _module _entrypoint)
165 if(${_entrypoint} STREQUAL "0")
166 add_target_link_flags(${_module} "/NOENTRY")
167 elseif(ARCH STREQUAL "i386")
168 set(_entrysymbol ${_entrypoint})
169 if(${ARGC} GREATER 2)
170 set(_entrysymbol ${_entrysymbol}@${ARGV2})
171 endif()
172 add_target_link_flags(${_module} "/ENTRY:${_entrysymbol}")
173 else()
174 add_target_link_flags(${_module} "/ENTRY:${_entrypoint}")
175 endif()
176 endfunction()
177
178 function(set_subsystem MODULE SUBSYSTEM)
179 if(ARCH STREQUAL "amd64")
180 add_target_link_flags(${MODULE} "/SUBSYSTEM:${SUBSYSTEM},5.02")
181 else()
182 add_target_link_flags(${MODULE} "/SUBSYSTEM:${SUBSYSTEM},5.01")
183 endif()
184 endfunction()
185
186 function(set_image_base MODULE IMAGE_BASE)
187 add_target_link_flags(${MODULE} "/BASE:${IMAGE_BASE}")
188 endfunction()
189
190 function(set_module_type_toolchain MODULE TYPE)
191 if(CPP_USE_STL)
192 if((${TYPE} STREQUAL "kernelmodedriver") OR (${TYPE} STREQUAL "wdmdriver"))
193 message(FATAL_ERROR "Use of STL in kernelmodedriver or wdmdriver type module prohibited")
194 endif()
195 target_link_libraries(${MODULE} cpprt stlport oldnames)
196 elseif(CPP_USE_RT)
197 target_link_libraries(${MODULE} cpprt)
198 endif()
199 if((${TYPE} STREQUAL "win32dll") OR (${TYPE} STREQUAL "win32ocx") OR (${TYPE} STREQUAL "cpl"))
200 add_target_link_flags(${MODULE} "/DLL")
201 elseif(${TYPE} STREQUAL "kernelmodedriver")
202 add_target_link_flags(${MODULE} "/DRIVER")
203 elseif(${TYPE} STREQUAL "wdmdriver")
204 add_target_link_flags(${MODULE} "/DRIVER:WDM")
205 endif()
206 endfunction()
207
208 #define those for having real libraries
209 set(CMAKE_IMPLIB_CREATE_STATIC_LIBRARY "LINK /LIB /NOLOGO <LINK_FLAGS> /OUT:<TARGET> <OBJECTS>")
210 set(CMAKE_STUB_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <SOURCE>")
211 function(add_delay_importlibs _module)
212 get_target_property(_module_type ${_module} TYPE)
213 if(_module_type STREQUAL "STATIC_LIBRARY")
214 message(FATAL_ERROR "Cannot add delay imports to a static library")
215 endif()
216 foreach(_lib ${ARGN})
217 add_target_link_flags(${_module} "/DELAYLOAD:${_lib}.dll")
218 target_link_libraries(${_module} lib${_lib})
219 endforeach()
220 target_link_libraries(${_module} delayimp)
221 endfunction()
222
223 function(generate_import_lib _libname _dllname _spec_file)
224
225 set(_def_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_exp.def)
226 set(_asm_stubs_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_stubs.asm)
227
228 # Generate the asm stub file and the def file for import library
229 add_custom_command(
230 OUTPUT ${_asm_stubs_file} ${_def_file}
231 COMMAND native-spec2def --ms -a=${SPEC2DEF_ARCH} --implib -n=${_dllname} -d=${_def_file} -l=${_asm_stubs_file} ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
232 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
233
234 if(MSVC_IDE)
235 # Compile the generated asm stub file
236 add_custom_command(
237 OUTPUT ${_asm_stubs_file}.obj
238 COMMAND ${CMAKE_ASM_COMPILER} /Cp /Fo${_asm_stubs_file}.obj /c /Ta ${_asm_stubs_file}
239 DEPENDS ${_asm_stubs_file})
240 else()
241 # be clear about the "language"
242 # Thanks MS for creating a stupid linker
243 set_source_files_properties(${_asm_stubs_file} PROPERTIES LANGUAGE "STUB_ASM")
244 endif()
245
246 # add our library
247 if(MSVC_IDE)
248 add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${_asm_stubs_file}.obj)
249 set_source_files_properties(${_asm_stubs_file}.obj PROPERTIES EXTERNAL_OBJECT 1)
250 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "C")
251 else()
252 # NOTE: as stub file and def file are generated in one pass, depending on one is like depending on the other
253 add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${_asm_stubs_file})
254 add_dependencies(${_libname} ${_def_file})
255 # set correct "link rule"
256 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "IMPLIB")
257 endif()
258 set_target_properties(${_libname} PROPERTIES STATIC_LIBRARY_FLAGS "/DEF:${_def_file}")
259 endfunction()
260
261 if(ARCH STREQUAL "amd64")
262 add_definitions(/D__x86_64)
263 set(SPEC2DEF_ARCH x86_64)
264 else()
265 set(SPEC2DEF_ARCH i386)
266 endif()
267 function(spec2def _dllname _spec_file)
268 # do we also want to add importlib targets?
269 if(${ARGC} GREATER 2)
270 if(${ARGN} STREQUAL "ADD_IMPORTLIB")
271 set(__add_importlib TRUE)
272 else()
273 message(FATAL_ERROR "Wrong argument passed to spec2def, ${ARGN}")
274 endif()
275 endif()
276
277 # get library basename
278 get_filename_component(_file ${_dllname} NAME_WE)
279
280 # error out on anything else than spec
281 if(NOT ${_spec_file} MATCHES ".*\\.spec")
282 message(FATAL_ERROR "spec2def only takes spec files as input.")
283 endif()
284
285 #generate def for the DLL and C stubs file
286 add_custom_command(
287 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
288 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}
289 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
290
291 if(__add_importlib)
292 generate_import_lib(lib${_file} ${_dllname} ${_spec_file})
293 endif()
294 endfunction()
295
296 macro(macro_mc FLAG FILE)
297 set(COMMAND_MC ${CMAKE_MC_COMPILER} ${FLAG} -r ${REACTOS_BINARY_DIR}/include/reactos -h ${REACTOS_BINARY_DIR}/include/reactos ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.mc)
298 endmacro()
299
300 #pseh workaround
301 set(PSEH_LIB "pseh")
302
303 # Use a full path for the x86 version of ml when using x64 VS.
304 # It's not a problem when using the DDK/WDK because, in x64 mode,
305 # both the x86 and x64 versions of ml are available.
306 if((ARCH STREQUAL "amd64") AND (DEFINED ENV{VCINSTALLDIR}))
307 set(CMAKE_ASM16_COMPILER $ENV{VCINSTALLDIR}/bin/ml.exe)
308 else()
309 set(CMAKE_ASM16_COMPILER ml.exe)
310 endif()
311
312 function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
313 set(_object_file ${_binary_file}.obj)
314 set(_temp_file ${_binary_file}.tmp)
315
316 add_custom_command(
317 OUTPUT ${_temp_file}
318 COMMAND ${CMAKE_C_COMPILER} /nologo /X /I${REACTOS_SOURCE_DIR}/include/asm /I${REACTOS_BINARY_DIR}/include/asm /D__ASM__ /D_USE_ML /EP /c ${_asm_file} > ${_temp_file}
319 DEPENDS ${_asm_file})
320
321 add_custom_command(
322 OUTPUT ${_object_file}
323 COMMAND ${CMAKE_ASM16_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_temp_file}
324 DEPENDS ${_temp_file})
325
326 add_custom_command(
327 OUTPUT ${_binary_file}
328 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
329 DEPENDS ${_object_file} native-obj2bin)
330
331 set_source_files_properties(${_object_file} ${_temp_file} ${_binary_file} PROPERTIES GENERATED TRUE)
332
333 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
334 endfunction()
335
336 function(allow_warnings __module)
337 endfunction()
338
339 macro(add_asm_files _target)
340 if(MSVC_IDE AND (CMAKE_VERSION MATCHES "ReactOS"))
341 get_defines(_directory_defines)
342 get_includes(_directory_includes)
343 get_directory_property(_defines COMPILE_DEFINITIONS)
344 foreach(_source_file ${ARGN})
345 get_filename_component(_source_file_base_name ${_source_file} NAME_WE)
346 set(_preprocessed_asm_file ${CMAKE_CURRENT_BINARY_DIR}/asm/${_source_file_base_name}_${_target}.tmp)
347 set(_object_file ${CMAKE_CURRENT_BINARY_DIR}/asm/${_source_file_base_name}_${_target}.obj)
348 set(_source_file_full_path ${CMAKE_CURRENT_SOURCE_DIR}/${_source_file})
349 get_source_file_property(_defines_semicolon_list ${_source_file_full_path} COMPILE_DEFINITIONS)
350 unset(_source_file_defines)
351 foreach(_define ${_defines_semicolon_list})
352 if(NOT ${_define} STREQUAL "NOTFOUND")
353 list(APPEND _source_file_defines -D${_define})
354 endif()
355 endforeach()
356 add_custom_command(
357 OUTPUT ${_preprocessed_asm_file} ${_object_file}
358 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} && ${CMAKE_ASM_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_preprocessed_asm_file}
359 DEPENDS ${_source_file_full_path})
360 set_source_files_properties(${_object_file} PROPERTIES EXTERNAL_OBJECT 1)
361 list(APPEND ${_target} ${_object_file})
362 endforeach()
363 else()
364 list(APPEND ${_target} ${ARGN})
365 endif()
366 endmacro()