xref: /reactos/sdk/cmake/msvc.cmake (revision 9be382ec)
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(/Gy /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(/Dinline=__inline /D__STDC__=1)
34
35# Ignore any "standard" include paths, and do not use any default CRT library.
36if(NOT USE_CLANG_CL)
37    add_compile_options(/X /Zl)
38endif()
39
40# Disable buffer security checks by default.
41add_compile_options(/GS-)
42
43if(USE_CLANG_CL)
44    set(CMAKE_CL_SHOWINCLUDES_PREFIX "Note: including file: ")
45endif()
46
47# HACK: for VS 11+ we need to explicitly disable SSE, which is off by
48# default for older compilers. See CORE-6507
49if(ARCH STREQUAL "i386")
50    # Clang's IA32 means i386, which doesn't have cmpxchg8b
51    if(USE_CLANG_CL)
52        add_compile_options(-march=${OARCH})
53    else()
54        add_compile_options(/arch:IA32)
55    endif()
56endif()
57
58# CLang default to -fno-common from version 11 onward. We are not rady for this now
59if (USE_CLANG_CL)
60    add_compile_options(-fcommon)
61endif()
62
63# VS 12+ requires /FS when used in parallel compilations
64if(NOT MSVC_IDE)
65    add_compile_options(/FS)
66endif()
67
68# VS14+ tries to use thread-safe initialization
69add_compile_options(/Zc:threadSafeInit-)
70
71# HACK: Disable use of __CxxFrameHandler4 on VS 16.3+ (x64 only)
72# See https://developercommunity.visualstudio.com/content/problem/746534/visual-c-163-runtime-uses-an-unsupported-api-for-u.html
73if(ARCH STREQUAL "amd64" AND MSVC_VERSION GREATER 1922)
74    if (NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
75        add_compile_options(/d2FH4-)
76    endif()
77    add_link_options(/d2:-FH4-)
78endif()
79
80# Generate Warnings Level 3
81add_compile_options(/W3)
82
83# Disable overly sensitive warnings as well as those that generally aren't
84# useful to us.
85# - C4244: implicit integer truncation
86# - C4290: C++ exception specification ignored
87# - C4800: forcing value to bool 'true' or 'false' (performance warning)
88# - C4200: nonstandard extension used : zero-sized array in struct/union
89# - C4214: nonstandard extension used : bit field types other than int
90add_compile_options(/wd4244 /wd4290 /wd4800 /wd4200 /wd4214)
91
92# FIXME: Temporarily disable C4018 until we fix more of the others. CORE-10113
93add_compile_options(/wd4018)
94
95# Allow all warnings on msbuild/VS IDE
96if (MSVC_IDE)
97    set(ALLOW_WARNINGS TRUE)
98endif()
99
100# On x86 Debug builds, if it's not Clang-CL or msbuild, treat all warnings as errors
101if ((ARCH STREQUAL "i386") AND (CMAKE_BUILD_TYPE STREQUAL "Debug") AND (NOT USE_CLANG_CL) AND (NOT MSVC_IDE))
102    set(TREAT_ALL_WARNINGS_AS_ERRORS=TRUE)
103endif()
104
105# Define ALLOW_WARNINGS=TRUE on the cmake/configure command line to bypass errors
106if (ALLOW_WARNINGS)
107    # Nothing
108elseif (TREAT_ALL_WARNINGS_AS_ERRORS)
109    add_compile_options(/WX)
110else()
111# The following warnings are treated as errors:
112# - C4013: implicit function declaration
113# - C4020: too many actual parameters
114# - C4022: pointer type mismatch for parameter
115# - C4028: formal parameter different from declaration
116# - C4047: different level of indirection
117# - TODO: C4090: different 'modifier' qualifiers (for C programs only;
118#          for C++ programs, the compiler error C2440 is issued)
119# - C4098: void function returning a value
120# - C4113: parameter lists differ
121# - C4129: unrecognized escape sequence
122# - C4133: incompatible types - from '<x> *' to '<y> *'
123# - C4163: 'identifier': not available as an intrinsic function
124# - C4229: modifiers on data are ignored
125# - C4311: pointer truncation from '<pointer>' to '<integer>'
126# - C4312: conversion from '<integer>' to '<pointer>' of greater size
127# - C4313: 'fprintf': '%x' in format string conflicts with argument n of type 'HANDLE'
128# - C4477: '_snprintf' : format string '%ld' requires an argument of type 'long', but variadic argument 1 has type 'DWORD_PTR'
129# - C4603: macro is not defined or definition is different after precompiled header use
130# - C4700: uninitialized variable usage
131# - C4715: 'function': not all control paths return a value
132# - C4716: function must return a value
133add_compile_options(/we4013 /we4020 /we4022 /we4028 /we4047 /we4098 /we4113 /we4129 /we4133 /we4163 /we4229 /we4311 /we4312 /we4313 /we4477 /we4603 /we4700 /we4715 /we4716)
134
135# - C4101: unreferenced local variable
136# - C4189: local variable initialized but not referenced
137# Not in Release mode, msbuild generator doesn't like CMAKE_BUILD_TYPE
138if(MSVC_IDE OR CMAKE_BUILD_TYPE STREQUAL "Debug")
139    add_compile_options(/we4101 /we4189)
140endif()
141
142endif()
143
144# Enable warnings above the default level, but don't treat them as errors:
145# - C4115: named type definition in parentheses
146add_compile_options(/w14115)
147
148if(USE_CLANG_CL)
149    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>")
150endif()
151
152# Debugging
153if(NOT (_PREFAST_ OR _VS_ANALYZE_))
154    add_compile_options($<$<CONFIG:Debug>:/Zi>)
155endif()
156add_compile_definitions($<$<CONFIG:Release>:NDEBUG>)
157
158# Hotpatchable images
159if(ARCH STREQUAL "i386")
160    if(NOT USE_CLANG_CL)
161        add_compile_options(/hotpatch)
162    endif()
163    set(_hotpatch_link_flag "/FUNCTIONPADMIN:5")
164elseif(ARCH STREQUAL "amd64")
165    set(_hotpatch_link_flag "/FUNCTIONPADMIN:6")
166endif()
167
168if(MSVC_IDE AND (NOT DEFINED USE_FOLDER_STRUCTURE))
169    set(USE_FOLDER_STRUCTURE TRUE)
170endif()
171
172if(RUNTIME_CHECKS)
173    add_definitions(-D__RUNTIME_CHECKS__)
174    add_compile_options(/RTC1)
175endif()
176
177add_link_options(/MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE ${_hotpatch_link_flag} /IGNORE:4039)
178
179set(CMAKE_MSVC_RUNTIME_LIBRARY "")
180
181# HACK: Remove the /implib argument, implibs are generated separately
182string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}")
183string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE}")
184string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_C_CREATE_SHARED_LIBRARY "${CMAKE_C_CREATE_SHARED_LIBRARY}")
185string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_CXX_CREATE_SHARED_LIBRARY "${CMAKE_CXX_CREATE_SHARED_LIBRARY}")
186string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_C_CREATE_SHARED_MODULE "${CMAKE_C_CREATE_SHARED_MODULE}")
187string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_CXX_CREATE_SHARED_MODULE "${CMAKE_CXX_CREATE_SHARED_MODULE}")
188
189# HACK2: CMake lacks the ability to completely remove the 'implib' argument for solution files...
190# To work around this, we just let it create a dummy file
191if(MSVC_IDE)
192    set(CMAKE_IMPORT_LIBRARY_SUFFIX ".dummy")
193endif()
194
195
196if(CMAKE_DISABLE_NINJA_DEPSLOG)
197    set(cl_includes_flag "")
198else()
199    set(cl_includes_flag "/showIncludes")
200endif()
201
202if(MSVC_IDE)
203    # For VS builds we'll only have en-US in resource files
204    add_definitions(/DLANGUAGE_EN_US)
205else()
206    set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> /nologo <INCLUDES> <FLAGS> <DEFINES> ${I18N_DEFS} /fo <OBJECT> <SOURCE>")
207endif()
208
209# We don't put <INCLUDES> <DEFINES> <FLAGS> because this is handled in add_asm_files macro
210if (NOT MSVC_IDE)
211    if(ARCH STREQUAL "arm")
212        set(CMAKE_ASM_MASM_COMPILE_OBJECT "<CMAKE_ASM_MASM_COMPILER> -nologo -o <OBJECT> <SOURCE>")
213    else()
214        set(CMAKE_ASM_MASM_COMPILE_OBJECT "<CMAKE_ASM_MASM_COMPILER> /nologo /Cp /Fo <OBJECT> /c /Ta <SOURCE>")
215    endif()
216endif()
217
218if(_VS_ANALYZE_)
219    message("-- VS static analysis enabled!")
220    add_compile_options(/analyze:WX-)
221elseif(_PREFAST_)
222    message("PREFAST enabled!")
223    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}"
224    "prefast LIST")
225    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}"
226    "prefast LIST")
227    set(CMAKE_C_LINK_EXECUTABLE
228    "<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}")
229    set(CMAKE_CXX_LINK_EXECUTABLE
230    "<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}")
231endif()
232
233set(CMAKE_RC_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
234set(CMAKE_ASM_MASM_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
235set(CMAKE_RC_CREATE_SHARED_MODULE ${CMAKE_C_CREATE_SHARED_MODULE})
236set(CMAKE_ASM_MASM_CREATE_SHARED_MODULE ${CMAKE_C_CREATE_SHARED_MODULE})
237set(CMAKE_ASM_CREATE_STATIC_LIBRARY ${CMAKE_C_CREATE_STATIC_LIBRARY})
238
239function(set_entrypoint _module _entrypoint)
240    if(${_entrypoint} STREQUAL "0")
241        add_target_link_flags(${_module} "/NOENTRY")
242    elseif(ARCH STREQUAL "i386")
243        set(_entrysymbol ${_entrypoint})
244        if(${ARGC} GREATER 2)
245            set(_entrysymbol ${_entrysymbol}@${ARGV2})
246        endif()
247        add_target_link_flags(${_module} "/ENTRY:${_entrysymbol}")
248    else()
249        add_target_link_flags(${_module} "/ENTRY:${_entrypoint}")
250    endif()
251endfunction()
252
253function(set_subsystem MODULE SUBSYSTEM)
254    string(TOUPPER ${SUBSYSTEM} _subsystem)
255    if(ARCH STREQUAL "amd64")
256        add_target_link_flags(${MODULE} "/SUBSYSTEM:${_subsystem},5.02")
257    elseif(ARCH STREQUAL "arm")
258        add_target_link_flags(${MODULE} "/SUBSYSTEM:${_subsystem},6.02")
259    else()
260        add_target_link_flags(${MODULE} "/SUBSYSTEM:${_subsystem},5.01")
261    endif()
262endfunction()
263
264function(set_image_base MODULE IMAGE_BASE)
265    add_target_link_flags(${MODULE} "/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        add_target_link_flags(${_module} "/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")
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    add_definitions(/D__arm__)
365    set(SPEC2DEF_ARCH arm)
366else()
367    set(SPEC2DEF_ARCH i386)
368endif()
369function(spec2def _dllname _spec_file)
370
371    cmake_parse_arguments(__spec2def "ADD_IMPORTLIB;NO_PRIVATE_WARNINGS;WITH_RELAY" "VERSION" "" ${ARGN})
372
373    # Get library basename
374    get_filename_component(_file ${_dllname} NAME_WE)
375
376    # Error out on anything else than spec
377    if(NOT ${_spec_file} MATCHES ".*\\.spec")
378        message(FATAL_ERROR "spec2def only takes spec files as input.")
379    endif()
380
381    if(__spec2def_WITH_RELAY)
382        set(__with_relay_arg "--with-tracing")
383    endif()
384
385    if(__spec2def_VERSION)
386        set(__version_arg "--version=0x${__spec2def_VERSION}")
387    endif()
388
389    # Generate exports def and C stubs file for the DLL
390    add_custom_command(
391        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
392        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}
393        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
394
395    if(__spec2def_ADD_IMPORTLIB)
396        generate_import_lib(lib${_file} ${_dllname} ${_spec_file})
397        if(__spec2def_NO_PRIVATE_WARNINGS)
398            set_property(TARGET lib${_file} APPEND PROPERTY STATIC_LIBRARY_OPTIONS /ignore:4104)
399        endif()
400    endif()
401endfunction()
402
403macro(macro_mc FLAG FILE)
404    set(COMMAND_MC ${CMAKE_MC_COMPILER} -u ${FLAG} -b -h ${CMAKE_CURRENT_BINARY_DIR}/ -r ${CMAKE_CURRENT_BINARY_DIR}/ ${FILE})
405endmacro()
406
407# PSEH workaround
408set(PSEH_LIB "pseh")
409
410# Use a full path for the x86 version of ml when using x64 VS.
411# It's not a problem when using the DDK/WDK because, in x64 mode,
412# both the x86 and x64 versions of ml are available.
413if((ARCH STREQUAL "amd64") AND (DEFINED ENV{VCToolsInstallDir}))
414    set(CMAKE_ASM16_COMPILER $ENV{VCToolsInstallDir}/bin/HostX86/x86/ml.exe)
415elseif((ARCH STREQUAL "amd64") AND (DEFINED ENV{VCINSTALLDIR}))
416    set(CMAKE_ASM16_COMPILER $ENV{VCINSTALLDIR}/bin/ml.exe)
417elseif(ARCH STREQUAL "arm")
418    set(CMAKE_ASM16_COMPILER armasm.exe)
419else()
420    set(CMAKE_ASM16_COMPILER ml.exe)
421endif()
422
423function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
424    set(_object_file ${_binary_file}.obj)
425    set(_temp_file ${_binary_file}.tmp)
426
427    get_defines(_defines)
428    get_includes(_includes)
429
430    if(USE_CLANG_CL)
431        set(_no_std_includes_flag "-nostdinc")
432    else()
433        set(_no_std_includes_flag "/X")
434    endif()
435
436    add_custom_command(
437        OUTPUT ${_temp_file}
438        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}
439        DEPENDS ${_asm_file})
440
441    if(ARCH STREQUAL "arm")
442        set(_asm16_command ${CMAKE_ASM16_COMPILER} -nologo -o ${_object_file} ${_temp_file})
443    else()
444        set(_asm16_command ${CMAKE_ASM16_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_temp_file})
445    endif()
446
447    add_custom_command(
448        OUTPUT ${_object_file}
449        COMMAND ${_asm16_command}
450        DEPENDS ${_temp_file})
451
452    add_custom_command(
453        OUTPUT ${_binary_file}
454        COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
455        DEPENDS ${_object_file} native-obj2bin)
456
457    set_source_files_properties(${_object_file} ${_temp_file} ${_binary_file} PROPERTIES GENERATED TRUE)
458
459    add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
460endfunction()
461
462function(allow_warnings __module)
463endfunction()
464
465macro(add_asm_files _target)
466    get_defines(_directory_defines)
467    get_includes(_directory_includes)
468    get_directory_property(_defines COMPILE_DEFINITIONS)
469    foreach(_source_file ${ARGN})
470        get_filename_component(_source_file_base_name ${_source_file} NAME_WE)
471        get_filename_component(_source_file_full_path ${_source_file} ABSOLUTE)
472        set(_preprocessed_asm_file ${CMAKE_CURRENT_BINARY_DIR}/asm/${_source_file_base_name}_${_target}.asm)
473        get_source_file_property(_defines_semicolon_list ${_source_file_full_path} COMPILE_DEFINITIONS)
474        unset(_source_file_defines)
475        foreach(_define ${_defines_semicolon_list})
476            if(NOT ${_define} STREQUAL "NOTFOUND")
477                list(APPEND _source_file_defines -D${_define})
478            endif()
479        endforeach()
480        add_custom_command(
481            OUTPUT ${_preprocessed_asm_file}
482            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}
483            DEPENDS ${_source_file_full_path})
484        list(APPEND ${_target} ${_preprocessed_asm_file})
485    endforeach()
486endmacro()
487
488function(add_linker_script _target _linker_script_file)
489    get_filename_component(_file_full_path ${_linker_script_file} ABSOLUTE)
490    get_filename_component(_file_name ${_linker_script_file} NAME)
491    set(_generated_file_path_prefix "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_target}.dir/${_file_name}")
492
493    # Generate the ASM module containing sections specifications and layout.
494    set(_generated_file "${_generated_file_path_prefix}.S")
495    add_custom_command(
496        OUTPUT ${_generated_file}
497        COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${_file_full_path}" "${_generated_file}"
498        DEPENDS ${_file_full_path})
499    set_source_files_properties(${_generated_file} PROPERTIES LANGUAGE "ASM_MASM" GENERATED TRUE)
500    add_asm_files(${_target}_linker_file ${_generated_file})
501
502    # Generate the C module containing extra sections specifications and layout,
503    # as well as comment-type linker #pragma directives.
504    set(_generated_file "${_generated_file_path_prefix}.c")
505    add_custom_command(
506        OUTPUT ${_generated_file}
507        COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${_file_full_path}" "${_generated_file}"
508        DEPENDS ${_file_full_path})
509    set_source_files_properties(${_generated_file} PROPERTIES LANGUAGE "C" GENERATED TRUE)
510    list(APPEND ${_target}_linker_file ${_generated_file})
511
512    # Add both files to the sources of the target.
513    target_sources(${_target} PRIVATE "${${_target}_linker_file}")
514
515    # Create the additional linker response file.
516    set(_generated_file "${_generated_file_path_prefix}.rsp")
517    if(USE_CLANG_CL)
518        set(_no_std_includes_flag "-nostdinc")
519    else()
520        set(_no_std_includes_flag "/X")
521    endif()
522    if(MSVC_IDE)
523        # MSBuild, via the VS IDE, uses response files when calling CL or LINK.
524        # We cannot specify a custom response file on the linker command-line,
525        # since specifying response files from within response files is forbidden.
526        # We therefore have to pre-process, at configuration time, the linker
527        # script so as to retrieve the custom linker options to be appended
528        # to the linker command-line.
529        execute_process(
530            COMMAND ${CMAKE_C_COMPILER} /nologo ${_no_std_includes_flag} /D__LINKER__ /EP /c "${_file_full_path}"
531            # OUTPUT_FILE "${_generated_file}"
532            OUTPUT_VARIABLE linker_options
533            ERROR_QUIET
534            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
535            RESULT_VARIABLE linker_rsp_result
536            OUTPUT_STRIP_TRAILING_WHITESPACE)
537        if(NOT linker_rsp_result EQUAL 0)
538            message(FATAL_ERROR "Generating pre-processed linker options for target '${_target}' failed with error ${linker_rsp_result}.")
539        endif()
540        # file(STRINGS ${_generated_file} linker_options NEWLINE_CONSUME)
541        string(REGEX REPLACE "[\r\n]+" " " linker_options "${linker_options}")
542        add_target_link_flags(${_target} ${linker_options})
543    else()
544        # Generate at compile-time a linker response file and append it
545        # to the linker command-line.
546        add_custom_command(
547            # OUTPUT ${_generated_file}
548            TARGET ${_target} PRE_LINK # PRE_BUILD
549            COMMAND ${CMAKE_C_COMPILER} /nologo ${_no_std_includes_flag} /D__LINKER__ /EP /c "${_file_full_path}" > "${_generated_file}"
550            DEPENDS ${_file_full_path}
551            VERBATIM)
552        set_source_files_properties(${_generated_file} PROPERTIES GENERATED TRUE)
553        # add_custom_target("${_target}_${_file_name}" ALL DEPENDS ${_generated_file})
554        # add_dependencies(${_target} "${_target}_${_file_name}")
555        add_target_link_flags(${_target} "@${_generated_file}")
556        add_target_property(${_target} LINK_DEPENDS ${_file_full_path})
557    endif()
558endfunction()
559
560# handle C++ options
561# disable RTTI unless said so
562add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:$<IF:$<BOOL:$<TARGET_PROPERTY:WITH_CXX_RTTI>>,/GR,/GR->>")
563# disable exceptions unless said so
564add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:$<IF:$<BOOL:$<TARGET_PROPERTY:WITH_CXX_EXCEPTIONS>>,/EHsc,/EHs-c->>")
565
566# Create our interface libraries wrapping the needed library for this compiler
567add_library(cppstl INTERFACE)
568target_link_libraries(cppstl INTERFACE cpprt stlport oldnames)
569# We set this properties through our INTERFACE library
570set_target_properties(cppstl PROPERTIES INTERFACE_WITH_CXX_STL TRUE)
571# add_library(cpprt INTERFACE)
572# Our runtime library is already called cpprt
573