747b23b199cbb51caad1ba1c553dc4312c1780ef
[reactos.git] / sdk / cmake / msvc.cmake
1
2 #if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
3 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
4 # no optimization
5 add_compile_flags("/Ob0 /Od")
6 elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
7 add_compile_flags("/Ox /Ob2 /Ot /Oy /GT /GF")
8 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /OPT:REF /OPT:ICF")
9 elseif(OPTIMIZE STREQUAL "1")
10 add_definitions(/O1)
11 elseif(OPTIMIZE STREQUAL "2")
12 add_definitions(/O2)
13 elseif(OPTIMIZE STREQUAL "3")
14 add_definitions(/Ot /Ox /GS-)
15 elseif(OPTIMIZE STREQUAL "4")
16 add_definitions(/Os /Ox /GS-)
17 elseif(OPTIMIZE STREQUAL "5")
18 add_definitions(/GF /Gy /Ob2 /Os /Ox /GS-)
19 endif()
20
21 # Enable function level linking and comdat folding
22 add_compile_flags("/Gy")
23 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /OPT:REF /OPT:ICF")
24 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /OPT:REF /OPT:ICF")
25
26 if(ARCH STREQUAL "i386")
27 add_definitions(/DWIN32 /D_WINDOWS)
28 endif()
29
30 add_definitions(/Dinline=__inline /D__STDC__=1)
31
32 if(NOT USE_CLANG_CL)
33 add_compile_flags("/X /Zl")
34 endif()
35
36 add_compile_flags("/GR- /EHs-c- /GS- /W3")
37
38 if(USE_CLANG_CL)
39 set(CMAKE_CL_SHOWINCLUDES_PREFIX "Note: including file: ")
40 endif()
41
42 # HACK: for VS 11+ we need to explicitly disable SSE, which is off by
43 # default for older compilers. See CORE-6507
44 if(MSVC_VERSION GREATER 1699 AND ARCH STREQUAL "i386")
45 add_compile_flags("/arch:IA32")
46 endif ()
47
48 # VS 12+ requires /FS when used in parallel compilations
49 if(MSVC_VERSION GREATER 1799 AND NOT MSVC_IDE)
50 add_compile_flags("/FS")
51 endif ()
52
53 # VS14+ tries to use thread-safe initialization
54 if(MSVC_VERSION GREATER 1899)
55 add_compile_flags("/Zc:threadSafeInit-")
56 endif ()
57
58 # Disable overly sensitive warnings as well as those that generally aren't
59 # useful to us.
60 # - C4244: implicit integer truncation
61 # - C4290: C++ exception specification ignored
62 # - C4800: forcing value to bool 'true' or 'false' (performance warning)
63 # - C4200: nonstandard extension used : zero-sized array in struct/union
64 # - C4214: nonstandard extension used : bit field types other than int
65 add_compile_flags("/wd4244 /wd4290 /wd4800 /wd4200 /wd4214")
66
67 # FIXME: Temporarily disable C4018 until we fix more of the others. CORE-10113
68 add_compile_flags("/wd4018")
69
70 # The following warnings are treated as errors:
71 # - C4013: implicit function declaration
72 # - C4020: too many actual parameters
73 # - C4022: pointer type mismatch for parameter
74 # - C4028: formal parameter different from declaration
75 # - C4047: different level of indirection
76 # - TODO: C4090: different 'modifier' qualifiers (for C programs only;
77 # for C++ programs, the compiler error C2440 is issued)
78 # - C4098: void function returning a value
79 # - C4113: parameter lists differ
80 # - C4129: unrecognized escape sequence
81 # - TODO: C4133: incompatible types
82 # - C4163: 'identifier': not available as an intrinsic function
83 # - C4229: modifiers on data are ignored
84 # - C4700: uninitialized variable usage
85 # - C4603: macro is not defined or definition is different after precompiled header use
86 # - C4716: function must return a value
87 add_compile_flags("/we4013 /we4020 /we4022 /we4047 /we4098 /we4113 /we4129 /we4163 /we4229 /we4700 /we4603 /we4716")
88 # TODO: Check and fix other architectures.
89 if(ARCH STREQUAL "i386")
90 add_compile_flags("/we4028")
91 endif()
92
93 # - C4189: local variable initialized but not referenced
94 # Not in Release mode and not with MSVC 2010
95 if((NOT CMAKE_BUILD_TYPE STREQUAL "Release") AND (NOT MSVC_VERSION LESS 1700))
96 add_compile_flags("/we4189")
97 endif()
98
99 # Enable warnings above the default level, but don't treat them as errors:
100 # - C4115: named type definition in parentheses
101 add_compile_flags("/w14115")
102
103 if(USE_CLANG_CL)
104 add_compile_flags_language("-nostdinc -Wno-multichar -Wno-char-subscripts -Wno-microsoft-enum-forward-reference -Wno-pragma-pack -Wno-microsoft-anon-tag -Wno-parentheses-equality -Wno-unknown-pragmas" "C")
105 add_compile_flags_language("-nostdinc -Wno-multichar -Wno-char-subscripts -Wno-microsoft-enum-forward-reference -Wno-pragma-pack -Wno-microsoft-anon-tag -Wno-parentheses-equality -Wno-unknown-pragmas" "CXX")
106 endif()
107
108 # Debugging
109 #if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
110 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
111 if(NOT (_PREFAST_ OR _VS_ANALYZE_))
112 add_compile_flags("/Zi")
113 endif()
114 #elseif(${CMAKE_BUILD_TYPE} STREQUAL "Release")
115 elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
116 add_definitions("/D NDEBUG")
117 endif()
118
119 # Hotpatchable images
120 if(ARCH STREQUAL "i386")
121 if(NOT USE_CLANG_CL)
122 add_compile_flags("/hotpatch")
123 endif()
124 set(_hotpatch_link_flag "/FUNCTIONPADMIN:5")
125 elseif(ARCH STREQUAL "amd64")
126 set(_hotpatch_link_flag "/FUNCTIONPADMIN:6")
127 endif()
128
129 if(MSVC_IDE AND (NOT DEFINED USE_FOLDER_STRUCTURE))
130 set(USE_FOLDER_STRUCTURE TRUE)
131 endif()
132
133 if(NOT DEFINED RUNTIME_CHECKS)
134 set(RUNTIME_CHECKS FALSE)
135 endif()
136
137 if(RUNTIME_CHECKS)
138 add_definitions(-D__RUNTIME_CHECKS__)
139 add_compile_flags("/RTC1")
140 endif()
141
142 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE ${_hotpatch_link_flag} /IGNORE:4039")
143 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE ${_hotpatch_link_flag} /IGNORE:4104 /IGNORE:4039")
144 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE ${_hotpatch_link_flag} /IGNORE:4039")
145
146 # HACK: Remove the /implib argument, implibs are generated separately
147 string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}")
148 string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE}")
149 string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_C_CREATE_SHARED_LIBRARY "${CMAKE_C_CREATE_SHARED_LIBRARY}")
150 string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_CXX_CREATE_SHARED_LIBRARY "${CMAKE_CXX_CREATE_SHARED_LIBRARY}")
151
152 if(CMAKE_DISABLE_NINJA_DEPSLOG)
153 set(cl_includes_flag "")
154 else()
155 set(cl_includes_flag "/showIncludes")
156 endif()
157
158 if(MSVC_IDE AND (CMAKE_VERSION MATCHES "ReactOS"))
159 # For VS builds we'll only have en-US in resource files
160 add_definitions(/DLANGUAGE_EN_US)
161 else()
162 if(CMAKE_VERSION VERSION_LESS 3.4.0)
163 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> /nologo <FLAGS> <DEFINES> ${I18N_DEFS} /fo<OBJECT> <SOURCE>")
164 if(ARCH STREQUAL "arm")
165 set(CMAKE_ASM_COMPILE_OBJECT
166 "cl ${cl_includes_flag} /nologo /X /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm <FLAGS> <DEFINES> /D__ASM__ /D_USE_ML /EP /c <SOURCE> > <OBJECT>.tmp"
167 "<CMAKE_ASM_COMPILER> -nologo -o <OBJECT> <OBJECT>.tmp")
168 else()
169 set(CMAKE_ASM_COMPILE_OBJECT
170 "cl ${cl_includes_flag} /nologo /X /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm <FLAGS> <DEFINES> /D__ASM__ /D_USE_ML /EP /c <SOURCE> > <OBJECT>.tmp"
171 "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <OBJECT>.tmp")
172 endif()
173 else()
174 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> /nologo <INCLUDES> <FLAGS> <DEFINES> ${I18N_DEFS} /fo<OBJECT> <SOURCE>")
175 if(ARCH STREQUAL "arm")
176 set(CMAKE_ASM_COMPILE_OBJECT
177 "cl ${cl_includes_flag} /nologo /X /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm <INCLUDES> <FLAGS> <DEFINES> /D__ASM__ /D_USE_ML /EP /c <SOURCE> > <OBJECT>.tmp"
178 "<CMAKE_ASM_COMPILER> -nologo -o <OBJECT> <OBJECT>.tmp")
179 else()
180 set(CMAKE_ASM_COMPILE_OBJECT
181 "cl ${cl_includes_flag} /nologo /X /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm <INCLUDES> <FLAGS> <DEFINES> /D__ASM__ /D_USE_ML /EP /c <SOURCE> > <OBJECT>.tmp"
182 "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <OBJECT>.tmp")
183 endif()
184 endif()
185 endif()
186
187 if(_VS_ANALYZE_)
188 message("VS static analysis enabled!")
189 add_compile_flags("/analyze")
190 elseif(_PREFAST_)
191 message("PREFAST enabled!")
192 if(CMAKE_VERSION VERSION_LESS 3.4.0)
193 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}"
194 "prefast LIST")
195 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}"
196 "prefast LIST")
197 set(CMAKE_C_LINK_EXECUTABLE
198 "<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}")
199 set(CMAKE_CXX_LINK_EXECUTABLE
200 "<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}")
201 else()
202 set(CMAKE_C_COMPILE_OBJECT "prefast <CMAKE_C_COMPILER> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO} <INCLUDES> <FLAGS> <DEFINES> /Fo<OBJECT> -c <SOURCE>${CMAKE_END_TEMP_FILE}"
203 "prefast LIST")
204 set(CMAKE_CXX_COMPILE_OBJECT "prefast <CMAKE_CXX_COMPILER> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO} <INCLUDES> <FLAGS> <DEFINES> /TP /Fo<OBJECT> -c <SOURCE>${CMAKE_END_TEMP_FILE}"
205 "prefast LIST")
206 set(CMAKE_C_LINK_EXECUTABLE
207 "<CMAKE_C_COMPILER> ${CMAKE_CL_NOLOGO} <OBJECTS> ${CMAKE_START_TEMP_FILE} <INCLUDES> <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}")
208 set(CMAKE_CXX_LINK_EXECUTABLE
209 "<CMAKE_CXX_COMPILER> ${CMAKE_CL_NOLOGO} <OBJECTS> ${CMAKE_START_TEMP_FILE} <INCLUDES> <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}")
210 endif()
211 endif()
212
213 set(CMAKE_RC_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
214 set(CMAKE_ASM_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
215 set(CMAKE_ASM_CREATE_STATIC_LIBRARY ${CMAKE_C_CREATE_STATIC_LIBRARY})
216
217 if(PCH)
218 macro(add_pch _target _pch _sources)
219
220 # Workaround for the MSVC toolchain (MSBUILD) /MP bug
221 set(_temp_gch ${CMAKE_CURRENT_BINARY_DIR}/${_target}.pch)
222 if(MSVC_IDE)
223 file(TO_NATIVE_PATH ${_temp_gch} _gch)
224 else()
225 set(_gch ${_temp_gch})
226 endif()
227
228 if(IS_CPP)
229 set(_pch_language CXX)
230 if(NOT USE_CLANG_CL)
231 set(_cl_lang_flag "/TP")
232 endif()
233 else()
234 set(_pch_language C)
235 set(_cl_lang_flag "/TC")
236 endif()
237
238 if(MSVC_IDE)
239 set(_pch_path_name_flag "/Fp${_gch}")
240 endif()
241
242 if(USE_CLANG_CL)
243 set(_pch_compile_flags "${_cl_lang_flag} /Yc${_pch} /FI${_pch} /Fp${_gch}")
244 else()
245 set(_pch_compile_flags "${_cl_lang_flag} /Yc /Fp${_gch}")
246 endif()
247
248 # Build the precompiled header
249 # HEADER_FILE_ONLY FALSE: force compiling the header
250 set_source_files_properties(${_pch} PROPERTIES
251 HEADER_FILE_ONLY FALSE
252 LANGUAGE ${_pch_language}
253 COMPILE_FLAGS ${_pch_compile_flags}
254 OBJECT_OUTPUTS ${_gch})
255
256 # Prevent a race condition related to writing to the PDB files between the PCH and the excluded list of source files
257 get_target_property(_target_sources ${_target} SOURCES)
258 list(REMOVE_ITEM _target_sources ${_pch})
259 foreach(_target_src ${_target_sources})
260 set_property(SOURCE ${_target_src} APPEND PROPERTY OBJECT_DEPENDS ${_gch})
261 endforeach()
262
263 # Use the precompiled header with the specified source files, skipping the pch itself
264 list(REMOVE_ITEM ${_sources} ${_pch})
265 foreach(_src ${${_sources}})
266 set_property(SOURCE ${_src} APPEND_STRING PROPERTY COMPILE_FLAGS " /FI${_gch} /Yu${_gch} ${_pch_path_name_flag}")
267 endforeach()
268 endmacro()
269 else()
270 macro(add_pch _target _pch _sources)
271 endmacro()
272 endif()
273
274 function(set_entrypoint _module _entrypoint)
275 if(${_entrypoint} STREQUAL "0")
276 add_target_link_flags(${_module} "/NOENTRY")
277 elseif(ARCH STREQUAL "i386")
278 set(_entrysymbol ${_entrypoint})
279 if(${ARGC} GREATER 2)
280 set(_entrysymbol ${_entrysymbol}@${ARGV2})
281 endif()
282 add_target_link_flags(${_module} "/ENTRY:${_entrysymbol}")
283 else()
284 add_target_link_flags(${_module} "/ENTRY:${_entrypoint}")
285 endif()
286 endfunction()
287
288 function(set_subsystem MODULE SUBSYSTEM)
289 string(TOUPPER ${SUBSYSTEM} _subsystem)
290 if(ARCH STREQUAL "amd64")
291 add_target_link_flags(${MODULE} "/SUBSYSTEM:${_subsystem},5.02")
292 elseif(ARCH STREQUAL "arm")
293 add_target_link_flags(${MODULE} "/SUBSYSTEM:${_subsystem},6.02")
294 else()
295 add_target_link_flags(${MODULE} "/SUBSYSTEM:${_subsystem},5.01")
296 endif()
297 endfunction()
298
299 function(set_image_base MODULE IMAGE_BASE)
300 add_target_link_flags(${MODULE} "/BASE:${IMAGE_BASE}")
301 endfunction()
302
303 function(set_module_type_toolchain MODULE TYPE)
304 if(CPP_USE_STL)
305 if((${TYPE} STREQUAL "kernelmodedriver") OR (${TYPE} STREQUAL "wdmdriver"))
306 message(FATAL_ERROR "Use of STL in kernelmodedriver or wdmdriver type module prohibited")
307 endif()
308 target_link_libraries(${MODULE} cpprt stlport oldnames)
309 elseif(CPP_USE_RT)
310 target_link_libraries(${MODULE} cpprt)
311 endif()
312 if((${TYPE} STREQUAL "win32dll") OR (${TYPE} STREQUAL "win32ocx") OR (${TYPE} STREQUAL "cpl"))
313 add_target_link_flags(${MODULE} "/DLL")
314 elseif(${TYPE} STREQUAL "kernelmodedriver")
315 # Disable linker warning 4078 (multiple sections found with different attributes) for INIT section use
316 add_target_link_flags(${MODULE} "/DRIVER /IGNORE:4078")
317 elseif(${TYPE} STREQUAL "wdmdriver")
318 add_target_link_flags(${MODULE} "/DRIVER:WDM /IGNORE:4078")
319 endif()
320
321 if(RUNTIME_CHECKS)
322 target_link_libraries(${MODULE} runtmchk)
323 endif()
324
325 endfunction()
326
327 # Define those for having real libraries
328 set(CMAKE_IMPLIB_CREATE_STATIC_LIBRARY "LINK /LIB /NOLOGO <LINK_FLAGS> /OUT:<TARGET> <OBJECTS>")
329
330 if(ARCH STREQUAL "arm")
331 set(CMAKE_STUB_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> -nologo -o <OBJECT> <SOURCE>")
332 else()
333 set(CMAKE_STUB_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <SOURCE>")
334 endif()
335
336 function(add_delay_importlibs _module)
337 get_target_property(_module_type ${_module} TYPE)
338 if(_module_type STREQUAL "STATIC_LIBRARY")
339 message(FATAL_ERROR "Cannot add delay imports to a static library")
340 endif()
341 foreach(_lib ${ARGN})
342 get_filename_component(_basename "${_lib}" NAME_WE)
343 get_filename_component(_ext "${_lib}" EXT)
344 if(NOT _ext)
345 set(_ext ".dll")
346 endif()
347 add_target_link_flags(${_module} "/DELAYLOAD:${_basename}${_ext}")
348 target_link_libraries(${_module} "lib${_basename}")
349 endforeach()
350 target_link_libraries(${_module} delayimp)
351 endfunction()
352
353 function(generate_import_lib _libname _dllname _spec_file)
354
355 set(_def_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_exp.def)
356 set(_asm_stubs_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_stubs.asm)
357
358 # Generate the asm stub file and the def file for import library
359 add_custom_command(
360 OUTPUT ${_asm_stubs_file} ${_def_file}
361 COMMAND native-spec2def --ms -a=${SPEC2DEF_ARCH} --implib -n=${_dllname} -d=${_def_file} -l=${_asm_stubs_file} ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
362 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
363
364 if(MSVC_IDE)
365 # Compile the generated asm stub file
366 if(ARCH STREQUAL "arm")
367 set(_asm_stub_command ${CMAKE_ASM_COMPILER} -nologo -o ${_asm_stubs_file}.obj ${_asm_stubs_file})
368 else()
369 set(_asm_stub_command ${CMAKE_ASM_COMPILER} /Cp /Fo${_asm_stubs_file}.obj /c /Ta ${_asm_stubs_file})
370 endif()
371 add_custom_command(
372 OUTPUT ${_asm_stubs_file}.obj
373 COMMAND ${_asm_stub_command}
374 DEPENDS ${_asm_stubs_file})
375 else()
376 # Be clear about the "language"
377 # Thanks MS for creating a stupid linker
378 set_source_files_properties(${_asm_stubs_file} PROPERTIES LANGUAGE "STUB_ASM")
379 endif()
380
381 # Add our library
382 if(MSVC_IDE)
383 add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${_asm_stubs_file}.obj)
384 set_source_files_properties(${_asm_stubs_file}.obj PROPERTIES EXTERNAL_OBJECT 1)
385 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "C")
386 else()
387 # NOTE: as stub file and def file are generated in one pass, depending on one is like depending on the other
388 add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${_asm_stubs_file})
389 # set correct "link rule"
390 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "IMPLIB")
391 endif()
392 set_target_properties(${_libname} PROPERTIES STATIC_LIBRARY_FLAGS "/DEF:${_def_file}")
393 endfunction()
394
395 if(ARCH STREQUAL "amd64")
396 # This is NOT a typo.
397 # See https://software.intel.com/en-us/forums/topic/404643
398 add_definitions(/D__x86_64)
399 set(SPEC2DEF_ARCH x86_64)
400 elseif(ARCH STREQUAL "arm")
401 add_definitions(/D__arm__)
402 set(SPEC2DEF_ARCH arm)
403 else()
404 set(SPEC2DEF_ARCH i386)
405 endif()
406 function(spec2def _dllname _spec_file)
407
408 cmake_parse_arguments(__spec2def "ADD_IMPORTLIB;NO_PRIVATE_WARNINGS;WITH_RELAY" "VERSION" "" ${ARGN})
409
410 # Get library basename
411 get_filename_component(_file ${_dllname} NAME_WE)
412
413 # Error out on anything else than spec
414 if(NOT ${_spec_file} MATCHES ".*\\.spec")
415 message(FATAL_ERROR "spec2def only takes spec files as input.")
416 endif()
417
418 if (__spec2def_WITH_RELAY)
419 set(__with_relay_arg "--with-tracing")
420 endif()
421
422 if(__spec2def_VERSION)
423 set(__version_arg "--version=0x${__spec2def_VERSION}")
424 endif()
425
426 # Generate exports def and C stubs file for the DLL
427 add_custom_command(
428 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
429 COMMAND native-spec2def --ms -a=${SPEC2DEF_ARCH} -n=${_dllname} -d=${CMAKE_CURRENT_BINARY_DIR}/${_file}.def -s=${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c ${__with_relay_arg} ${__version_arg} ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
430 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
431
432 if(__spec2def_ADD_IMPORTLIB)
433 generate_import_lib(lib${_file} ${_dllname} ${_spec_file})
434 if(__spec2def_NO_PRIVATE_WARNINGS)
435 add_target_property(lib${_file} STATIC_LIBRARY_FLAGS "/ignore:4104")
436 endif()
437 endif()
438 endfunction()
439
440 macro(macro_mc FLAG FILE)
441 set(COMMAND_MC ${CMAKE_MC_COMPILER} -u ${FLAG} -b -h ${CMAKE_CURRENT_BINARY_DIR}/ -r ${CMAKE_CURRENT_BINARY_DIR}/ ${FILE})
442 endmacro()
443
444 # PSEH workaround
445 set(PSEH_LIB "pseh")
446
447 # Use a full path for the x86 version of ml when using x64 VS.
448 # It's not a problem when using the DDK/WDK because, in x64 mode,
449 # both the x86 and x64 versions of ml are available.
450 if((ARCH STREQUAL "amd64") AND (DEFINED ENV{VCToolsInstallDir}))
451 set(CMAKE_ASM16_COMPILER $ENV{VCToolsInstallDir}/bin/HostX86/x86/ml.exe)
452 elseif((ARCH STREQUAL "amd64") AND (DEFINED ENV{VCINSTALLDIR}))
453 set(CMAKE_ASM16_COMPILER $ENV{VCINSTALLDIR}/bin/ml.exe)
454 elseif(ARCH STREQUAL "arm")
455 set(CMAKE_ASM16_COMPILER armasm.exe)
456 else()
457 set(CMAKE_ASM16_COMPILER ml.exe)
458 endif()
459
460 function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
461 set(_object_file ${_binary_file}.obj)
462 set(_temp_file ${_binary_file}.tmp)
463
464 if(USE_CLANG_CL)
465 set(_no_std_includes_flag "-nostdinc")
466 else()
467 set(_no_std_includes_flag "/X")
468 endif()
469
470 add_custom_command(
471 OUTPUT ${_temp_file}
472 COMMAND ${CMAKE_C_COMPILER} /nologo ${_no_std_includes_flag} /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm /I${REACTOS_SOURCE_DIR}/boot/freeldr /D__ASM__ /D_USE_ML /EP /c ${_asm_file} > ${_temp_file}
473 DEPENDS ${_asm_file})
474
475 if(ARCH STREQUAL "arm")
476 set(_asm16_command ${CMAKE_ASM16_COMPILER} -nologo -o ${_object_file} ${_temp_file})
477 else()
478 set(_asm16_command ${CMAKE_ASM16_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_temp_file})
479 endif()
480
481 add_custom_command(
482 OUTPUT ${_object_file}
483 COMMAND ${_asm16_command}
484 DEPENDS ${_temp_file})
485
486 add_custom_command(
487 OUTPUT ${_binary_file}
488 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
489 DEPENDS ${_object_file} native-obj2bin)
490
491 set_source_files_properties(${_object_file} ${_temp_file} ${_binary_file} PROPERTIES GENERATED TRUE)
492
493 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
494 endfunction()
495
496 function(allow_warnings __module)
497 endfunction()
498
499 macro(add_asm_files _target)
500 if(MSVC_IDE AND (CMAKE_VERSION MATCHES "ReactOS"))
501 get_defines(_directory_defines)
502 get_includes(_directory_includes)
503 get_directory_property(_defines COMPILE_DEFINITIONS)
504 foreach(_source_file ${ARGN})
505 get_filename_component(_source_file_base_name ${_source_file} NAME_WE)
506 get_filename_component(_source_file_full_path ${_source_file} ABSOLUTE)
507 set(_preprocessed_asm_file ${CMAKE_CURRENT_BINARY_DIR}/asm/${_source_file_base_name}_${_target}.tmp)
508 set(_object_file ${CMAKE_CURRENT_BINARY_DIR}/asm/${_source_file_base_name}_${_target}.obj)
509 get_source_file_property(_defines_semicolon_list ${_source_file_full_path} COMPILE_DEFINITIONS)
510 unset(_source_file_defines)
511 foreach(_define ${_defines_semicolon_list})
512 if(NOT ${_define} STREQUAL "NOTFOUND")
513 list(APPEND _source_file_defines -D${_define})
514 endif()
515 endforeach()
516 if(ARCH STREQUAL "arm")
517 set(_pp_asm_compile_command ${CMAKE_ASM_COMPILER} -nologo -o ${_object_file} ${_preprocessed_asm_file})
518 else()
519 set(_pp_asm_compile_command ${CMAKE_ASM_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_preprocessed_asm_file})
520 endif()
521 add_custom_command(
522 OUTPUT ${_preprocessed_asm_file} ${_object_file}
523 COMMAND cl /nologo /X /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/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}
524 DEPENDS ${_source_file_full_path})
525 set_source_files_properties(${_object_file} PROPERTIES EXTERNAL_OBJECT 1)
526 list(APPEND ${_target} ${_object_file})
527 endforeach()
528 else()
529 list(APPEND ${_target} ${ARGN})
530 endif()
531 endmacro()