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