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