6deb5c3e6cab76e4dc36a3d4f91d6137f0eeb781
[reactos.git] / reactos / 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 bug #7174
27 if (MSVC_VERSION GREATER 1699 AND ARCH STREQUAL "i386")
28 add_compile_flags("/arch:IA32")
29 endif ()
30
31 # C4700 is almost always likely to result in broken code, so mark it as an error
32 add_compile_flags("/we4700")
33
34 # Debugging
35 #if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
36 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
37 if(NOT (_PREFAST_ OR _VS_ANALYZE_))
38 add_compile_flags("/Zi")
39 endif()
40 add_compile_flags("/Ob0 /Od")
41 #elseif(${CMAKE_BUILD_TYPE} STREQUAL "Release")
42 elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
43 add_compile_flags("/Ob2 /D NDEBUG")
44 endif()
45
46 if(MSVC_IDE)
47 add_compile_flags("/MP")
48 if(NOT DEFINED USE_FOLDER_STRUCTURE)
49 set(USE_FOLDER_STRUCTURE FALSE)
50 endif()
51 endif()
52
53 if(${_MACHINE_ARCH_FLAG} MATCHES X86)
54 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE")
55 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE")
56 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE")
57 endif()
58
59 if(MSVC_IDE AND (CMAKE_VERSION MATCHES "ReactOS"))
60 # for VS builds we'll only have en-US in resource files
61 add_definitions(/DLANGUAGE_EN_US)
62 else()
63 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> /nologo <FLAGS> <DEFINES> ${I18N_DEFS} /fo<OBJECT> <SOURCE>")
64 set(CMAKE_ASM_COMPILE_OBJECT
65 "cl /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"
66 "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <OBJECT>.tmp")
67 endif()
68
69 if(_VS_ANALYZE_)
70 message("VS static analysis enabled!")
71 add_compile_flags("/analyze")
72 elseif(_PREFAST_)
73 message("PREFAST enabled!")
74 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}"
75 "prefast LIST")
76 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}"
77 "prefast LIST")
78 set(CMAKE_C_LINK_EXECUTABLE
79 "<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}")
80 set(CMAKE_CXX_LINK_EXECUTABLE
81 "<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}")
82 endif()
83
84 set(CMAKE_RC_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
85 set(CMAKE_ASM_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
86 set(CMAKE_ASM_CREATE_STATIC_LIBRARY ${CMAKE_C_CREATE_STATIC_LIBRARY})
87
88 macro(add_pch _target_name _FILE)
89 endmacro()
90
91 function(set_entrypoint _module _entrypoint)
92 if(${_entrypoint} STREQUAL "0")
93 add_target_link_flags(${_module} "/NOENTRY")
94 elseif(ARCH STREQUAL "i386")
95 set(_entrysymbol ${_entrypoint})
96 if(${ARGC} GREATER 2)
97 set(_entrysymbol ${_entrysymbol}@${ARGV2})
98 endif()
99 add_target_link_flags(${_module} "/ENTRY:${_entrysymbol}")
100 else()
101 add_target_link_flags(${_module} "/ENTRY:${_entrypoint}")
102 endif()
103 endfunction()
104
105 function(set_subsystem MODULE SUBSYSTEM)
106 if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 17)
107 add_target_link_flags(${MODULE} "/SUBSYSTEM:${SUBSYSTEM},5.01")
108 else()
109 add_target_link_flags(${MODULE} "/SUBSYSTEM:${SUBSYSTEM}")
110 endif()
111 endfunction()
112
113 function(set_image_base MODULE IMAGE_BASE)
114 add_target_link_flags(${MODULE} "/BASE:${IMAGE_BASE}")
115 endfunction()
116
117 function(set_module_type_toolchain MODULE TYPE)
118 if(CPP_USE_STL)
119 if((${TYPE} STREQUAL "kernelmodedriver") OR (${TYPE} STREQUAL "wdmdriver"))
120 message(FATAL_ERROR "Use of STL in kernelmodedriver or wdmdriver type module prohibited")
121 endif()
122 target_link_libraries(${MODULE} cpprt stlport oldnames)
123 elseif(CPP_USE_RT)
124 target_link_libraries(${MODULE} cpprt)
125 endif()
126 if((${TYPE} STREQUAL "win32dll") OR (${TYPE} STREQUAL "win32ocx") OR (${TYPE} STREQUAL "cpl"))
127 add_target_link_flags(${MODULE} "/DLL")
128 elseif(${TYPE} STREQUAL "kernelmodedriver")
129 add_target_link_flags(${MODULE} "/DRIVER")
130 elseif(${TYPE} STREQUAL "wdmdriver")
131 add_target_link_flags(${MODULE} "/DRIVER:WDM")
132 endif()
133 endfunction()
134
135 #define those for having real libraries
136 set(CMAKE_IMPLIB_CREATE_STATIC_LIBRARY "LINK /LIB /NOLOGO <LINK_FLAGS> /OUT:<TARGET> <OBJECTS>")
137 set(CMAKE_STUB_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <SOURCE>")
138 function(add_delay_importlibs _module)
139 get_target_property(_module_type ${_module} TYPE)
140 if(_module_type STREQUAL "STATIC_LIBRARY")
141 message(FATAL_ERROR "Cannot add delay imports to a static library")
142 endif()
143 foreach(_lib ${ARGN})
144 add_target_link_flags(${_module} "/DELAYLOAD:${_lib}.dll")
145 target_link_libraries(${_module} lib${_lib})
146 endforeach()
147 target_link_libraries(${_module} delayimp)
148 endfunction()
149
150 function(generate_import_lib _libname _dllname _spec_file)
151
152 set(_def_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_exp.def)
153 set(_asm_stubs_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_stubs.asm)
154
155 # Generate the asm stub file and the def file for import library
156 add_custom_command(
157 OUTPUT ${_asm_stubs_file} ${_def_file}
158 COMMAND native-spec2def --ms --kill-at -a=${SPEC2DEF_ARCH} --implib -n=${_dllname} -d=${_def_file} -l=${_asm_stubs_file} ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
159 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
160
161 if(MSVC_IDE)
162 # Compile the generated asm stub file
163 add_custom_command(
164 OUTPUT ${_asm_stubs_file}.obj
165 COMMAND ${CMAKE_ASM_COMPILER} /Cp /Fo${_asm_stubs_file}.obj /c /Ta ${_asm_stubs_file}
166 DEPENDS ${_asm_stubs_file})
167 else()
168 # be clear about the "language"
169 # Thanks MS for creating a stupid linker
170 set_source_files_properties(${_asm_stubs_file} PROPERTIES LANGUAGE "STUB_ASM")
171 endif()
172
173 # add our library
174 if(MSVC_IDE)
175 add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${_asm_stubs_file}.obj)
176 set_source_files_properties(${_asm_stubs_file}.obj PROPERTIES EXTERNAL_OBJECT 1)
177 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "C")
178 else()
179 # NOTE: as stub file and def file are generated in one pass, depending on one is like depending on the other
180 add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${_asm_stubs_file})
181 add_dependencies(${_libname} ${_def_file})
182 # set correct "link rule"
183 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "IMPLIB")
184 endif()
185 set_target_properties(${_libname} PROPERTIES STATIC_LIBRARY_FLAGS "/DEF:${_def_file}")
186 endfunction()
187
188 if(ARCH STREQUAL "amd64")
189 add_definitions(/D__x86_64)
190 set(SPEC2DEF_ARCH x86_64)
191 else()
192 set(SPEC2DEF_ARCH i386)
193 endif()
194 function(spec2def _dllname _spec_file)
195 # do we also want to add impotlib targets?
196 if(${ARGC} GREATER 2)
197 if(${ARGN} STREQUAL "ADD_IMPORTLIB")
198 set(__add_importlib TRUE)
199 else()
200 message(FATAL_ERROR "Wrong argument passed to spec2def, ${ARGN}")
201 endif()
202 endif()
203
204 # get library basename
205 get_filename_component(_file ${_dllname} NAME_WE)
206
207 # error out on anything else than spec
208 if(NOT ${_spec_file} MATCHES ".*\\.spec")
209 message(FATAL_ERROR "spec2def only takes spec files as input.")
210 endif()
211
212 #generate def for the DLL and C stubs file
213 add_custom_command(
214 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
215 COMMAND native-spec2def --ms --kill-at -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}
216 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
217
218 if(__add_importlib)
219 generate_import_lib(lib${_file} ${_dllname} ${_spec_file})
220 endif()
221 endfunction()
222
223 macro(macro_mc FLAG FILE)
224 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)
225 endmacro()
226
227 #pseh workaround
228 set(PSEH_LIB "pseh")
229
230 # Use a full path for the x86 version of ml when using x64 VS.
231 # It's not a problem when using the DDK/WDK because, in x64 mode,
232 # both the x86 and x64 versions of ml are available.
233 if((ARCH STREQUAL "amd64") AND (DEFINED ENV{VCINSTALLDIR}))
234 set(CMAKE_ASM16_COMPILER $ENV{VCINSTALLDIR}/bin/ml.exe)
235 else()
236 set(CMAKE_ASM16_COMPILER ml.exe)
237 endif()
238
239 function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
240 set(_object_file ${_binary_file}.obj)
241 set(_temp_file ${_binary_file}.tmp)
242
243 add_custom_command(
244 OUTPUT ${_temp_file}
245 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}
246 DEPENDS ${_asm_file})
247
248 add_custom_command(
249 OUTPUT ${_object_file}
250 COMMAND ${CMAKE_ASM16_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_temp_file}
251 DEPENDS ${_temp_file})
252
253 add_custom_command(
254 OUTPUT ${_binary_file}
255 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
256 DEPENDS ${_object_file} native-obj2bin)
257
258 set_source_files_properties(${_object_file} ${_temp_file} ${_binary_file} PROPERTIES GENERATED TRUE)
259
260 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
261 endfunction()
262
263 function(allow_warnings __module)
264 endfunction()
265
266 macro(add_asm_files _target)
267 if(MSVC_IDE AND (CMAKE_VERSION MATCHES "ReactOS"))
268 get_defines(_directory_defines)
269 get_includes(_directory_includes)
270 get_directory_property(_defines COMPILE_DEFINITIONS)
271 foreach(_source_file ${ARGN})
272 get_filename_component(_source_file_base_name ${_source_file} NAME_WE)
273 set(_preprocessed_asm_file ${CMAKE_CURRENT_BINARY_DIR}/asm/${_source_file_base_name}_${_target}.tmp)
274 set(_object_file ${CMAKE_CURRENT_BINARY_DIR}/asm/${_source_file_base_name}_${_target}.obj)
275 set(_source_file_full_path ${CMAKE_CURRENT_SOURCE_DIR}/${_source_file})
276 get_source_file_property(_defines_semicolon_list ${_source_file_full_path} COMPILE_DEFINITIONS)
277 unset(_source_file_defines)
278 foreach(_define ${_defines_semicolon_list})
279 if(NOT ${_define} STREQUAL "NOTFOUND")
280 list(APPEND _source_file_defines -D${_define})
281 endif()
282 endforeach()
283 add_custom_command(
284 OUTPUT ${_preprocessed_asm_file} ${_object_file}
285 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}
286 DEPENDS ${_source_file_full_path})
287 set_source_files_properties(${_object_file} PROPERTIES EXTERNAL_OBJECT 1)
288 list(APPEND ${_target} ${_object_file})
289 endforeach()
290 else()
291 list(APPEND ${_target} ${ARGN})
292 endif()
293 endmacro()