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