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