Sync with trunk revision r58045 to bring the corrections on configure.cmd and on...
[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- /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(${_MACHINE_ARCH_FLAG} MATCHES X86)
47 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO /NODEFAULTLIB /RELEASE")
48 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SAFESEH:NO /NODEFAULTLIB /RELEASE")
49 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /SAFESEH:NO /NODEFAULTLIB /RELEASE")
50 endif()
51
52 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> <FLAGS> <DEFINES> ${I18N_DEFS} /fo<OBJECT> <SOURCE>")
53
54 if(MSVC_IDE)
55 # Asm source files are not supported in VS generators yet. As a result, <DEFINES> isn't recognized.
56 # We may temporarily use just the global defines, but this is not a solution as some modules (minihal for example) apply additional definitions to source files, so we get an incorrect build of such targets.
57 get_directory_property(definitions DEFINITIONS)
58 set(CMAKE_ASM_COMPILE_OBJECT
59 "cl /nologo /X /I${REACTOS_SOURCE_DIR}/include/asm /I${REACTOS_BINARY_DIR}/include/asm <FLAGS> ${definitions} /D__ASM__ /D_USE_ML /EP /c <SOURCE> > <OBJECT>.tmp"
60 "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <OBJECT>.tmp")
61 else()
62 # NMake Makefiles
63 set(CMAKE_ASM_COMPILE_OBJECT
64 "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"
65 "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <OBJECT>.tmp")
66 endif()
67
68 if(_VS_ANALYZE_)
69 message("VS static analysis enabled!")
70 add_compile_flags("/analyze")
71 elseif(_PREFAST_)
72 message("PREFAST enabled!")
73 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}"
74 "prefast LIST")
75 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}"
76 "prefast LIST")
77 set(CMAKE_C_LINK_EXECUTABLE
78 "<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}")
79 set(CMAKE_CXX_LINK_EXECUTABLE
80 "<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}")
81 endif()
82
83 set(CMAKE_RC_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
84 set(CMAKE_ASM_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
85 set(CMAKE_ASM_CREATE_STATIC_LIBRARY ${CMAKE_C_CREATE_STATIC_LIBRARY})
86
87 macro(add_pch _target_name _FILE)
88 endmacro()
89
90 function(set_entrypoint _module _entrypoint)
91 if(${_entrypoint} STREQUAL "0")
92 add_target_link_flags(${_module} "/NOENTRY")
93 elseif(ARCH STREQUAL "i386")
94 set(_entrysymbol ${_entrypoint})
95 if(${ARGC} GREATER 2)
96 set(_entrysymbol ${_entrysymbol}@${ARGV2})
97 endif()
98 add_target_link_flags(${_module} "/ENTRY:${_entrysymbol}")
99 else()
100 add_target_link_flags(${_module} "/ENTRY:${_entrypoint}")
101 endif()
102 endfunction()
103
104 function(set_subsystem MODULE SUBSYSTEM)
105 add_target_link_flags(${MODULE} "/subsystem:${SUBSYSTEM}")
106 endfunction()
107
108 function(set_image_base MODULE IMAGE_BASE)
109 add_target_link_flags(${MODULE} "/BASE:${IMAGE_BASE}")
110 endfunction()
111
112 function(set_module_type_toolchain MODULE TYPE)
113 if((${TYPE} STREQUAL "win32dll") OR (${TYPE} STREQUAL "win32ocx") OR (${TYPE} STREQUAL "cpl"))
114 add_target_link_flags(${MODULE} "/DLL")
115 elseif(${TYPE} STREQUAL "kernelmodedriver")
116 add_target_link_flags(${MODULE} "/DRIVER")
117 endif()
118 endfunction()
119
120 #define those for having real libraries
121 set(CMAKE_IMPLIB_CREATE_STATIC_LIBRARY "LINK /LIB /NOLOGO <LINK_FLAGS> /OUT:<TARGET> <OBJECTS>")
122 set(CMAKE_STUB_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> /Cp /Fo<OBJECT> /c /Ta <SOURCE>")
123 macro(add_delay_importlibs MODULE)
124 foreach(LIB ${ARGN})
125 add_target_link_flags(${MODULE} "/DELAYLOAD:${LIB}.dll")
126 target_link_libraries(${MODULE} lib${LIB})
127 endforeach()
128 target_link_libraries(${MODULE} delayimp)
129 endmacro()
130
131 function(generate_import_lib _libname _dllname _spec_file)
132
133 set(_def_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_exp.def)
134 set(_asm_stubs_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_stubs.asm)
135
136 # Generate the asm stub file and the def file for import library
137 add_custom_command(
138 OUTPUT ${_asm_stubs_file} ${_def_file}
139 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}
140 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
141
142 if(MSVC_IDE)
143 # Compile the generated asm stub file
144 add_custom_command(
145 OUTPUT ${_asm_stubs_file}.obj
146 COMMAND ${CMAKE_ASM_COMPILER} /Cp /Fo${_asm_stubs_file}.obj /c /Ta ${_asm_stubs_file}
147 DEPENDS ${_asm_stubs_file})
148 else()
149 # be clear about the "language"
150 # Thanks MS for creating a stupid linker
151 set_source_files_properties(${_asm_stubs_file} PROPERTIES LANGUAGE "STUB_ASM")
152 endif()
153
154 # add our library
155 if(MSVC_IDE)
156 add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${_asm_stubs_file}.obj)
157 set_source_files_properties(${_asm_stubs_file}.obj PROPERTIES EXTERNAL_OBJECT 1)
158 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "C")
159 else()
160 # NOTE: as stub file and def file are generated in one pass, depending on one is like depending on the other
161 add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${_asm_stubs_file})
162 add_dependencies(${_libname} ${_def_file})
163 # set correct "link rule"
164 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "IMPLIB")
165 endif()
166 set_target_properties(${_libname} PROPERTIES STATIC_LIBRARY_FLAGS "/DEF:${_def_file}")
167 endfunction()
168
169 if(ARCH STREQUAL "amd64")
170 add_definitions(/D__x86_64)
171 set(SPEC2DEF_ARCH x86_64)
172 else()
173 set(SPEC2DEF_ARCH i386)
174 endif()
175 function(spec2def _dllname _spec_file)
176 # do we also want to add impotlib targets?
177 if(${ARGC} GREATER 2)
178 if(${ARGN} STREQUAL "ADD_IMPORTLIB")
179 set(__add_importlib TRUE)
180 else()
181 message(FATAL_ERROR "Wrong argument passed to spec2def, ${ARGN}")
182 endif()
183 endif()
184
185 # get library basename
186 get_filename_component(_file ${_dllname} NAME_WE)
187
188 # error out on anything else than spec
189 if(NOT ${_spec_file} MATCHES ".*\\.spec")
190 message(FATAL_ERROR "spec2def only takes spec files as input.")
191 endif()
192
193 #generate def for the DLL and C stubs file
194 add_custom_command(
195 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
196 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}
197 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
198
199 if(__add_importlib)
200 generate_import_lib(lib${_file} ${_dllname} ${_spec_file})
201 endif()
202 endfunction()
203
204 macro(macro_mc FLAG FILE)
205 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)
206 endmacro()
207
208 #pseh workaround
209 set(PSEH_LIB "pseh")
210
211 # Use a full path for the x86 version of ml when using x64 VS.
212 # It's not a problem when using the DDK/WDK because, in x64 mode,
213 # both the x86 and x64 versions of ml are available.
214 if((ARCH STREQUAL "amd64") AND (DEFINED ENV{VCINSTALLDIR}))
215 set(CMAKE_ASM16_COMPILER $ENV{VCINSTALLDIR}/bin/ml.exe)
216 else()
217 set(CMAKE_ASM16_COMPILER ml.exe)
218 endif()
219
220 function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
221 set(_object_file ${_binary_file}.obj)
222 set(_temp_file ${_binary_file}.tmp)
223
224 add_custom_command(
225 OUTPUT ${_temp_file}
226 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}
227 DEPENDS ${_asm_file})
228
229 add_custom_command(
230 OUTPUT ${_object_file}
231 COMMAND ${CMAKE_ASM16_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_temp_file}
232 DEPENDS ${_temp_file})
233
234 add_custom_command(
235 OUTPUT ${_binary_file}
236 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
237 DEPENDS ${_object_file} native-obj2bin)
238
239 set_source_files_properties(${_object_file} ${_temp_file} ${_binary_file} PROPERTIES GENERATED TRUE)
240
241 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
242 endfunction()
243
244 function(allow_warnings __module)
245 endfunction()