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