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