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