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