xref: /reactos/sdk/cmake/msvc.cmake (revision a1fc312a)
1
2if(CMAKE_BUILD_TYPE STREQUAL "Release")
3    add_compile_options(/Ox /Ob2 /Ot /Oy /GT)
4elseif(OPTIMIZE STREQUAL "1")
5    add_compile_options(/O1)
6elseif(OPTIMIZE STREQUAL "2")
7    add_compile_options(/O2)
8elseif(OPTIMIZE STREQUAL "3")
9    add_compile_options(/Ot /Ox /GS-)
10elseif(OPTIMIZE STREQUAL "4")
11    add_compile_options(/Ob0 /Od)
12elseif(OPTIMIZE STREQUAL "5")
13    add_compile_options(/Gy /Ob2 /Os /Ox /GS-)
14endif()
15
16# Always use string pooling: this helps reducing the binaries size since a lot
17# of redundancy come from the usage of __FILE__ / __RELFILE__ in the debugging
18# helper macros. Note also that GCC builds use string pooling by default.
19add_compile_options(/GF)
20
21# Enable function level linking and comdat folding
22add_compile_options(/Gy)
23add_link_options(/OPT:REF /OPT:ICF)
24
25if(ARCH STREQUAL "i386")
26    add_definitions(/DWIN32 /D_WINDOWS)
27endif()
28
29add_definitions(/Dinline=__inline /D__STDC__=1)
30
31# Ignore any "standard" include paths, and do not use any default CRT library.
32if(NOT USE_CLANG_CL)
33    add_compile_options(/X /Zl)
34endif()
35
36# Disable buffer security checks by default.
37add_compile_options(/GS-)
38
39if(USE_CLANG_CL)
40    set(CMAKE_CL_SHOWINCLUDES_PREFIX "Note: including file: ")
41endif()
42
43# HACK: for VS 11+ we need to explicitly disable SSE, which is off by
44# default for older compilers. See CORE-6507
45if(ARCH STREQUAL "i386")
46    # Clang's IA32 means i386, which doesn't have cmpxchg8b
47    if(USE_CLANG_CL)
48        add_compile_options(-march=${OARCH})
49    else()
50        add_compile_options(/arch:IA32)
51    endif()
52endif()
53
54# CLang default to -fno-common from version 11 onward. We are not rady for this now
55if (USE_CLANG_CL)
56    add_compile_options(-fcommon)
57endif()
58
59# VS 12+ requires /FS when used in parallel compilations
60if(NOT MSVC_IDE)
61    add_compile_options(/FS)
62endif()
63
64# VS14+ tries to use thread-safe initialization
65add_compile_options(/Zc:threadSafeInit-)
66
67# HACK: Disable use of __CxxFrameHandler4 on VS 16.3+ (x64 only)
68# See https://developercommunity.visualstudio.com/content/problem/746534/visual-c-163-runtime-uses-an-unsupported-api-for-u.html
69if(ARCH STREQUAL "amd64" AND MSVC_VERSION GREATER 1922)
70    add_compile_options(/d2FH4-)
71    add_link_options(/d2:-FH4-)
72endif()
73
74# Generate Warnings Level 3
75add_compile_options(/W3)
76
77# Disable overly sensitive warnings as well as those that generally aren't
78# useful to us.
79# - C4244: implicit integer truncation
80# - C4290: C++ exception specification ignored
81# - C4800: forcing value to bool 'true' or 'false' (performance warning)
82# - C4200: nonstandard extension used : zero-sized array in struct/union
83# - C4214: nonstandard extension used : bit field types other than int
84add_compile_options(/wd4244 /wd4290 /wd4800 /wd4200 /wd4214)
85
86# FIXME: Temporarily disable C4018 until we fix more of the others. CORE-10113
87add_compile_options(/wd4018)
88
89# The following warnings are treated as errors:
90# - C4013: implicit function declaration
91# - C4020: too many actual parameters
92# - C4022: pointer type mismatch for parameter
93# - C4028: formal parameter different from declaration
94# - C4047: different level of indirection
95# - TODO: C4090: different 'modifier' qualifiers (for C programs only;
96#          for C++ programs, the compiler error C2440 is issued)
97# - C4098: void function returning a value
98# - C4101: unreferenced local variable
99# - C4113: parameter lists differ
100# - C4129: unrecognized escape sequence
101# - C4133: incompatible types - from '<x> *' to '<y> *'
102# - C4163: 'identifier': not available as an intrinsic function
103# - C4229: modifiers on data are ignored
104# - C4311: pointer truncation from '<pointer>' to '<integer>'
105# - C4312: conversion from '<integer>' to '<pointer>' of greater size
106# - C4313: 'fprintf': '%x' in format string conflicts with argument n of type 'HANDLE'
107# - C4477: '_snprintf' : format string '%ld' requires an argument of type 'long', but variadic argument 1 has type 'DWORD_PTR'
108# - C4603: macro is not defined or definition is different after precompiled header use
109# - C4700: uninitialized variable usage
110# - C4715: 'function': not all control paths return a value
111# - C4716: function must return a value
112add_compile_options(/we4013 /we4020 /we4022 /we4028 /we4047 /we4098 /we4101 /we4113 /we4129 /we4133 /we4163 /we4229 /we4311 /we4312 /we4313 /we4477 /we4603 /we4700 /we4715 /we4716)
113
114# - C4189: local variable initialized but not referenced
115# Not in Release mode, msbuild generator doesn't like CMAKE_BUILD_TYPE
116if(MSVC_IDE OR CMAKE_BUILD_TYPE STREQUAL "Debug")
117    add_compile_options(/we4189)
118endif()
119
120# Enable warnings above the default level, but don't treat them as errors:
121# - C4115: named type definition in parentheses
122add_compile_options(/w14115)
123
124if(USE_CLANG_CL)
125    add_compile_options("$<$<COMPILE_LANGUAGE:C,CXX>:-nostdinc;-Wno-multichar;-Wno-char-subscripts;-Wno-microsoft-enum-forward-reference;-Wno-pragma-pack;-Wno-microsoft-anon-tag;-Wno-parentheses-equality;-Wno-unknown-pragmas>")
126endif()
127
128# Debugging
129if(NOT (_PREFAST_ OR _VS_ANALYZE_))
130    add_compile_options($<$<CONFIG:Debug>:/Zi>)
131endif()
132add_compile_definitions($<$<CONFIG:Release>:NDEBUG>)
133
134# Hotpatchable images
135if(ARCH STREQUAL "i386")
136    if(NOT USE_CLANG_CL)
137        add_compile_options(/hotpatch)
138    endif()
139    set(_hotpatch_link_flag "/FUNCTIONPADMIN:5")
140elseif(ARCH STREQUAL "amd64")
141    set(_hotpatch_link_flag "/FUNCTIONPADMIN:6")
142endif()
143
144if(MSVC_IDE AND (NOT DEFINED USE_FOLDER_STRUCTURE))
145    set(USE_FOLDER_STRUCTURE TRUE)
146endif()
147
148if(RUNTIME_CHECKS)
149    add_definitions(-D__RUNTIME_CHECKS__)
150    add_compile_options(/RTC1)
151endif()
152
153add_link_options(/MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE ${_hotpatch_link_flag} /IGNORE:4039)
154
155set(CMAKE_MSVC_RUNTIME_LIBRARY "")
156
157# HACK: Remove the /implib argument, implibs are generated separately
158string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}")
159string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE}")
160string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_C_CREATE_SHARED_LIBRARY "${CMAKE_C_CREATE_SHARED_LIBRARY}")
161string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_CXX_CREATE_SHARED_LIBRARY "${CMAKE_CXX_CREATE_SHARED_LIBRARY}")
162string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_C_CREATE_SHARED_MODULE "${CMAKE_C_CREATE_SHARED_MODULE}")
163string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_CXX_CREATE_SHARED_MODULE "${CMAKE_CXX_CREATE_SHARED_MODULE}")
164
165# HACK2: CMake lacks the ability to completely remove the 'implib' argument for solution files...
166# To work around this, we just let it create a dummy file
167if(MSVC_IDE)
168    set(CMAKE_IMPORT_LIBRARY_SUFFIX ".dummy")
169endif()
170
171
172if(CMAKE_DISABLE_NINJA_DEPSLOG)
173    set(cl_includes_flag "")
174else()
175    set(cl_includes_flag "/showIncludes")
176endif()
177
178if(MSVC_IDE)
179    # For VS builds we'll only have en-US in resource files
180    add_definitions(/DLANGUAGE_EN_US)
181else()
182    set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> /nologo <INCLUDES> <FLAGS> <DEFINES> ${I18N_DEFS} /fo <OBJECT> <SOURCE>")
183endif()
184
185# We don't put <INCLUDES> <DEFINES> <FLAGS> because this is handled in add_asm_files macro
186if (NOT MSVC_IDE)
187    if(ARCH STREQUAL "arm")
188        set(CMAKE_ASM_MASM_COMPILE_OBJECT "<CMAKE_ASM_MASM_COMPILER> -nologo -o <OBJECT> <SOURCE>")
189    else()
190        set(CMAKE_ASM_MASM_COMPILE_OBJECT "<CMAKE_ASM_MASM_COMPILER> /nologo /Cp /Fo <OBJECT> /c /Ta <SOURCE>")
191    endif()
192endif()
193
194if(_VS_ANALYZE_)
195    message("VS static analysis enabled!")
196    add_compile_options(/analyze)
197elseif(_PREFAST_)
198    message("PREFAST enabled!")
199    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}"
200    "prefast LIST")
201    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}"
202    "prefast LIST")
203    set(CMAKE_C_LINK_EXECUTABLE
204    "<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}")
205    set(CMAKE_CXX_LINK_EXECUTABLE
206    "<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}")
207endif()
208
209set(CMAKE_RC_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
210set(CMAKE_ASM_MASM_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
211set(CMAKE_RC_CREATE_SHARED_MODULE ${CMAKE_C_CREATE_SHARED_MODULE})
212set(CMAKE_ASM_MASM_CREATE_SHARED_MODULE ${CMAKE_C_CREATE_SHARED_MODULE})
213set(CMAKE_ASM_CREATE_STATIC_LIBRARY ${CMAKE_C_CREATE_STATIC_LIBRARY})
214
215function(set_entrypoint _module _entrypoint)
216    if(${_entrypoint} STREQUAL "0")
217        add_target_link_flags(${_module} "/NOENTRY")
218    elseif(ARCH STREQUAL "i386")
219        set(_entrysymbol ${_entrypoint})
220        if(${ARGC} GREATER 2)
221            set(_entrysymbol ${_entrysymbol}@${ARGV2})
222        endif()
223        add_target_link_flags(${_module} "/ENTRY:${_entrysymbol}")
224    else()
225        add_target_link_flags(${_module} "/ENTRY:${_entrypoint}")
226    endif()
227endfunction()
228
229function(set_subsystem MODULE SUBSYSTEM)
230    string(TOUPPER ${SUBSYSTEM} _subsystem)
231    if(ARCH STREQUAL "amd64")
232        add_target_link_flags(${MODULE} "/SUBSYSTEM:${_subsystem},5.02")
233    elseif(ARCH STREQUAL "arm")
234        add_target_link_flags(${MODULE} "/SUBSYSTEM:${_subsystem},6.02")
235    else()
236        add_target_link_flags(${MODULE} "/SUBSYSTEM:${_subsystem},5.01")
237    endif()
238endfunction()
239
240function(set_image_base MODULE IMAGE_BASE)
241    add_target_link_flags(${MODULE} "/BASE:${IMAGE_BASE}")
242endfunction()
243
244function(set_module_type_toolchain MODULE TYPE)
245    # Set the PE image version numbers from the NT OS version ReactOS is based on
246    target_link_options(${MODULE} PRIVATE "/VERSION:5.01")
247
248    if((TYPE STREQUAL win32dll) OR (TYPE STREQUAL win32ocx) OR (TYPE STREQUAL cpl))
249        target_link_options(${MODULE} PRIVATE /DLL)
250    elseif(TYPE IN_LIST KERNEL_MODULE_TYPES)
251        # Mark INIT section as Executable Read Write Discardable
252        target_link_options(${MODULE} PRIVATE /SECTION:INIT,ERWD)
253
254        if(TYPE STREQUAL kernelmodedriver)
255            target_link_options(${MODULE} PRIVATE /DRIVER)
256        elseif(TYPE STREQUAL wdmdriver)
257            target_link_options(${MODULE} PRIVATE /DRIVER:WDM)
258        elseif (TYPE STREQUAL kernel)
259            # Mark .rsrc section as non-disposable non-pageable, as bugcheck code needs to access it
260            target_link_options(${MODULE} PRIVATE /SECTION:.rsrc,!DP)
261        endif()
262    endif()
263
264    if(RUNTIME_CHECKS)
265        target_link_libraries(${MODULE} runtmchk)
266    endif()
267
268endfunction()
269
270function(add_delay_importlibs _module)
271    get_target_property(_module_type ${_module} TYPE)
272    if(_module_type STREQUAL "STATIC_LIBRARY")
273        message(FATAL_ERROR "Cannot add delay imports to a static library")
274    endif()
275    foreach(_lib ${ARGN})
276        get_filename_component(_basename "${_lib}" NAME_WE)
277        get_filename_component(_ext "${_lib}" EXT)
278        if(NOT _ext)
279            set(_ext ".dll")
280        endif()
281        add_target_link_flags(${_module} "/DELAYLOAD:${_basename}${_ext}")
282        target_link_libraries(${_module} "lib${_basename}")
283    endforeach()
284    target_link_libraries(${_module} delayimp)
285endfunction()
286
287function(fixup_load_config _target)
288    # msvc knows how to generate a load_config so no hacks here
289endfunction()
290
291function(generate_import_lib _libname _dllname _spec_file)
292
293    set(_def_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def)
294    set(_asm_stubs_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_stubs.asm)
295
296    # Generate the def and asm stub files
297    add_custom_command(
298        OUTPUT ${_asm_stubs_file} ${_def_file}
299        COMMAND native-spec2def --ms -a=${SPEC2DEF_ARCH} --implib -n=${_dllname} -d=${_def_file} -l=${_asm_stubs_file} ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
300        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
301
302    # Compile the generated asm stub file
303    if(ARCH STREQUAL "arm")
304        set(_asm_stub_command ${CMAKE_ASM_MASM_COMPILER} -nologo -o ${_asm_stubs_file}.obj ${_asm_stubs_file})
305    else()
306        set(_asm_stub_command ${CMAKE_ASM_MASM_COMPILER} /nologo /Cp /Fo${_asm_stubs_file}.obj /c /Ta ${_asm_stubs_file})
307    endif()
308    add_custom_command(
309        OUTPUT ${_asm_stubs_file}.obj
310        COMMAND ${_asm_stub_command}
311        DEPENDS ${_asm_stubs_file})
312
313    # generate the intermediate import lib
314    set(_libfile_tmp ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_tmp.lib)
315    set(_static_lib_options )
316
317    set(_implib_command ${CMAKE_LINKER} /LIB /NOLOGO /MACHINE:${WINARCH}
318        $<TARGET_PROPERTY:${_libname},STATIC_LIBRARY_FLAGS> $<TARGET_PROPERTY:${_libname},STATIC_LIBRARY_OPTIONS>
319        /DEF:${_def_file} /OUT:${_libfile_tmp} ${_asm_stubs_file}.obj)
320
321    add_custom_command(
322        OUTPUT ${_libfile_tmp}
323        COMMAND ${_implib_command}
324        DEPENDS ${_asm_stubs_file}.obj ${_def_file})
325
326    # By giving the import lib as an object input, LIB extracts the relevant object files and make a new library.
327    # This allows us to treat the implib as a regular static library
328    set_source_files_properties(${_libfile_tmp} PROPERTIES EXTERNAL_OBJECT TRUE)
329    add_library(${_libname} STATIC ${_libfile_tmp})
330
331    set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "C")
332endfunction()
333
334if(ARCH STREQUAL "amd64")
335    # This is NOT a typo.
336    # See https://software.intel.com/en-us/forums/topic/404643
337    add_definitions(/D__x86_64)
338    set(SPEC2DEF_ARCH x86_64)
339elseif(ARCH STREQUAL "arm")
340    add_definitions(/D__arm__)
341    set(SPEC2DEF_ARCH arm)
342else()
343    set(SPEC2DEF_ARCH i386)
344endif()
345function(spec2def _dllname _spec_file)
346
347    cmake_parse_arguments(__spec2def "ADD_IMPORTLIB;NO_PRIVATE_WARNINGS;WITH_RELAY" "VERSION" "" ${ARGN})
348
349    # Get library basename
350    get_filename_component(_file ${_dllname} NAME_WE)
351
352    # Error out on anything else than spec
353    if(NOT ${_spec_file} MATCHES ".*\\.spec")
354        message(FATAL_ERROR "spec2def only takes spec files as input.")
355    endif()
356
357    if(__spec2def_WITH_RELAY)
358        set(__with_relay_arg "--with-tracing")
359    endif()
360
361    if(__spec2def_VERSION)
362        set(__version_arg "--version=0x${__spec2def_VERSION}")
363    endif()
364
365    # Generate exports def and C stubs file for the DLL
366    add_custom_command(
367        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
368        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}
369        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
370
371    if(__spec2def_ADD_IMPORTLIB)
372        generate_import_lib(lib${_file} ${_dllname} ${_spec_file})
373        if(__spec2def_NO_PRIVATE_WARNINGS)
374            set_property(TARGET lib${_file} APPEND PROPERTY STATIC_LIBRARY_OPTIONS /ignore:4104)
375        endif()
376    endif()
377endfunction()
378
379macro(macro_mc FLAG FILE)
380    set(COMMAND_MC ${CMAKE_MC_COMPILER} -u ${FLAG} -b -h ${CMAKE_CURRENT_BINARY_DIR}/ -r ${CMAKE_CURRENT_BINARY_DIR}/ ${FILE})
381endmacro()
382
383# PSEH workaround
384set(PSEH_LIB "pseh")
385
386# Use a full path for the x86 version of ml when using x64 VS.
387# It's not a problem when using the DDK/WDK because, in x64 mode,
388# both the x86 and x64 versions of ml are available.
389if((ARCH STREQUAL "amd64") AND (DEFINED ENV{VCToolsInstallDir}))
390    set(CMAKE_ASM16_COMPILER $ENV{VCToolsInstallDir}/bin/HostX86/x86/ml.exe)
391elseif((ARCH STREQUAL "amd64") AND (DEFINED ENV{VCINSTALLDIR}))
392    set(CMAKE_ASM16_COMPILER $ENV{VCINSTALLDIR}/bin/ml.exe)
393elseif(ARCH STREQUAL "arm")
394    set(CMAKE_ASM16_COMPILER armasm.exe)
395else()
396    set(CMAKE_ASM16_COMPILER ml.exe)
397endif()
398
399function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
400    set(_object_file ${_binary_file}.obj)
401    set(_temp_file ${_binary_file}.tmp)
402
403    get_defines(_defines)
404    get_includes(_includes)
405
406    if(USE_CLANG_CL)
407        set(_no_std_includes_flag "-nostdinc")
408    else()
409        set(_no_std_includes_flag "/X")
410    endif()
411
412    add_custom_command(
413        OUTPUT ${_temp_file}
414        COMMAND ${CMAKE_C_COMPILER} /nologo ${_no_std_includes_flag} /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm ${_includes} ${_defines} /D__ASM__ /D_USE_ML /EP /c ${_asm_file} > ${_temp_file}
415        DEPENDS ${_asm_file})
416
417    if(ARCH STREQUAL "arm")
418        set(_asm16_command ${CMAKE_ASM16_COMPILER} -nologo -o ${_object_file} ${_temp_file})
419    else()
420        set(_asm16_command ${CMAKE_ASM16_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_temp_file})
421    endif()
422
423    add_custom_command(
424        OUTPUT ${_object_file}
425        COMMAND ${_asm16_command}
426        DEPENDS ${_temp_file})
427
428    add_custom_command(
429        OUTPUT ${_binary_file}
430        COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
431        DEPENDS ${_object_file} native-obj2bin)
432
433    set_source_files_properties(${_object_file} ${_temp_file} ${_binary_file} PROPERTIES GENERATED TRUE)
434
435    add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
436endfunction()
437
438function(allow_warnings __module)
439endfunction()
440
441macro(add_asm_files _target)
442    get_defines(_directory_defines)
443    get_includes(_directory_includes)
444    get_directory_property(_defines COMPILE_DEFINITIONS)
445    foreach(_source_file ${ARGN})
446        get_filename_component(_source_file_base_name ${_source_file} NAME_WE)
447        get_filename_component(_source_file_full_path ${_source_file} ABSOLUTE)
448        set(_preprocessed_asm_file ${CMAKE_CURRENT_BINARY_DIR}/asm/${_source_file_base_name}_${_target}.asm)
449        get_source_file_property(_defines_semicolon_list ${_source_file_full_path} COMPILE_DEFINITIONS)
450        unset(_source_file_defines)
451        foreach(_define ${_defines_semicolon_list})
452            if(NOT ${_define} STREQUAL "NOTFOUND")
453                list(APPEND _source_file_defines -D${_define})
454            endif()
455        endforeach()
456        add_custom_command(
457            OUTPUT ${_preprocessed_asm_file}
458            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}
459            DEPENDS ${_source_file_full_path})
460        list(APPEND ${_target} ${_preprocessed_asm_file})
461    endforeach()
462endmacro()
463
464function(add_linker_script _target _linker_script_file)
465    get_filename_component(_file_full_path ${_linker_script_file} ABSOLUTE)
466    get_filename_component(_file_name ${_linker_script_file} NAME)
467    set(_generated_file_path_prefix "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_target}.dir/${_file_name}")
468
469    # Generate the ASM module containing sections specifications and layout.
470    set(_generated_file "${_generated_file_path_prefix}.S")
471    add_custom_command(
472        OUTPUT ${_generated_file}
473        COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${_file_full_path}" "${_generated_file}"
474        DEPENDS ${_file_full_path})
475    set_source_files_properties(${_generated_file} PROPERTIES LANGUAGE "ASM_MASM" GENERATED TRUE)
476    add_asm_files(${_target}_linker_file ${_generated_file})
477
478    # Generate the C module containing extra sections specifications and layout,
479    # as well as comment-type linker #pragma directives.
480    set(_generated_file "${_generated_file_path_prefix}.c")
481    add_custom_command(
482        OUTPUT ${_generated_file}
483        COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${_file_full_path}" "${_generated_file}"
484        DEPENDS ${_file_full_path})
485    set_source_files_properties(${_generated_file} PROPERTIES LANGUAGE "C" GENERATED TRUE)
486    list(APPEND ${_target}_linker_file ${_generated_file})
487
488    # Add both files to the sources of the target.
489    target_sources(${_target} PRIVATE "${${_target}_linker_file}")
490
491    # Create the additional linker response file.
492    set(_generated_file "${_generated_file_path_prefix}.rsp")
493    if(USE_CLANG_CL)
494        set(_no_std_includes_flag "-nostdinc")
495    else()
496        set(_no_std_includes_flag "/X")
497    endif()
498    if(MSVC_IDE)
499        # MSBuild, via the VS IDE, uses response files when calling CL or LINK.
500        # We cannot specify a custom response file on the linker command-line,
501        # since specifying response files from within response files is forbidden.
502        # We therefore have to pre-process, at configuration time, the linker
503        # script so as to retrieve the custom linker options to be appended
504        # to the linker command-line.
505        execute_process(
506            COMMAND ${CMAKE_C_COMPILER} /nologo ${_no_std_includes_flag} /D__LINKER__ /EP /c "${_file_full_path}"
507            # OUTPUT_FILE "${_generated_file}"
508            OUTPUT_VARIABLE linker_options
509            ERROR_QUIET
510            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
511            RESULT_VARIABLE linker_rsp_result
512            OUTPUT_STRIP_TRAILING_WHITESPACE)
513        if(NOT linker_rsp_result EQUAL 0)
514            message(FATAL_ERROR "Generating pre-processed linker options for target '${_target}' failed with error ${linker_rsp_result}.")
515        endif()
516        # file(STRINGS ${_generated_file} linker_options NEWLINE_CONSUME)
517        string(REGEX REPLACE "[\r\n]+" " " linker_options "${linker_options}")
518        add_target_link_flags(${_target} ${linker_options})
519    else()
520        # Generate at compile-time a linker response file and append it
521        # to the linker command-line.
522        add_custom_command(
523            # OUTPUT ${_generated_file}
524            TARGET ${_target} PRE_LINK # PRE_BUILD
525            COMMAND ${CMAKE_C_COMPILER} /nologo ${_no_std_includes_flag} /D__LINKER__ /EP /c "${_file_full_path}" > "${_generated_file}"
526            DEPENDS ${_file_full_path}
527            VERBATIM)
528        set_source_files_properties(${_generated_file} PROPERTIES GENERATED TRUE)
529        # add_custom_target("${_target}_${_file_name}" ALL DEPENDS ${_generated_file})
530        # add_dependencies(${_target} "${_target}_${_file_name}")
531        add_target_link_flags(${_target} "@${_generated_file}")
532        add_target_property(${_target} LINK_DEPENDS ${_file_full_path})
533    endif()
534endfunction()
535
536# handle C++ options
537# disable RTTI unless said so
538add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:$<IF:$<BOOL:$<TARGET_PROPERTY:WITH_CXX_RTTI>>,/GR,/GR->>")
539# disable exceptions unless said so
540add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:$<IF:$<BOOL:$<TARGET_PROPERTY:WITH_CXX_EXCEPTIONS>>,/EHsc,/EHs-c->>")
541
542# Create our interface libraries wrapping the needed library for this compiler
543add_library(cppstl INTERFACE)
544target_link_libraries(cppstl INTERFACE cpprt stlport oldnames)
545# We set this properties through our INTERFACE library
546set_target_properties(cppstl PROPERTIES INTERFACE_WITH_CXX_STL TRUE)
547# add_library(cpprt INTERFACE)
548# Our runtime library is already called cpprt
549