xref: /reactos/sdk/cmake/gcc.cmake (revision a1faa1cc)
1
2# Show a note about ccache build
3if(ENABLE_CCACHE)
4    message("-- Enabling ccache build - done")
5    set(CMAKE_C_USE_RESPONSE_FILE_FOR_INCLUDES OFF)
6    set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES OFF)
7endif()
8
9# PDB style debug info
10if(NOT DEFINED SEPARATE_DBG)
11    set(SEPARATE_DBG FALSE)
12endif()
13
14# Dwarf based builds (no rsym)
15if(CMAKE_BUILD_TYPE STREQUAL "Release")
16    set(NO_ROSSYM TRUE)
17elseif(NOT DEFINED NO_ROSSYM)
18    set(NO_ROSSYM FALSE)
19endif()
20
21if(NOT DEFINED USE_PSEH3)
22    set(USE_PSEH3 1)
23endif()
24
25if(USE_PSEH3)
26    add_definitions(-D_USE_PSEH3=1)
27endif()
28
29if(NOT DEFINED USE_DUMMY_PSEH)
30    set(USE_DUMMY_PSEH 0)
31endif()
32
33if(USE_DUMMY_PSEH)
34    add_definitions(-D_USE_DUMMY_PSEH=1)
35endif()
36
37if(STACK_PROTECTOR)
38    add_compile_options(-fstack-protector-strong)
39endif()
40
41# Compiler Core
42add_compile_options(-pipe -fms-extensions -fno-strict-aliasing)
43
44# Prevent GCC from searching any of the default directories.
45# The case for C++ is handled through the reactos_c++ INTERFACE library
46add_compile_options("$<$<NOT:$<COMPILE_LANGUAGE:CXX>>:-nostdinc>")
47
48if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
49    add_compile_options(-fno-aggressive-loop-optimizations)
50    if (DBG)
51        add_compile_options("$<$<COMPILE_LANGUAGE:C>:-Wold-style-declaration>")
52    endif()
53elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
54    add_compile_options("$<$<COMPILE_LANGUAGE:C>:-Wno-microsoft>")
55    add_compile_options(-Wno-pragma-pack)
56    add_compile_options(-fno-associative-math)
57    add_compile_options(-fcommon)
58
59    if(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 12.0)
60        # disable "libcall optimization"
61        # see https://mudongliang.github.io/2020/12/02/undefined-reference-to-stpcpy.html
62        add_compile_options(-fno-builtin-stpcpy)
63    endif()
64
65    set(CMAKE_LINK_DEF_FILE_FLAG "")
66    set(CMAKE_STATIC_LIBRARY_SUFFIX ".a")
67    set(CMAKE_LINK_LIBRARY_SUFFIX "")
68    set(CMAKE_CREATE_WIN32_EXE "")
69    set(CMAKE_C_COMPILE_OPTIONS_PIC "")
70    set(CMAKE_CXX_COMPILE_OPTIONS_PIC "")
71    set(CMAKE_C_COMPILE_OPTIONS_PIE "")
72    set(CMAKE_CXX_COMPILE_OPTIONS_PIE "")
73    set(CMAKE_ASM_FLAGS_DEBUG "")
74    set(CMAKE_C_FLAGS_DEBUG "")
75    set(CMAKE_CXX_FLAGS_DEBUG "")
76endif()
77
78# Debugging
79if(NOT CMAKE_BUILD_TYPE STREQUAL "Release")
80    if(SEPARATE_DBG)
81        add_compile_options(-gdwarf-2 -ggdb)
82    else()
83        add_compile_options(-gdwarf-2 -gstrict-dwarf)
84        if(NOT CMAKE_C_COMPILER_ID STREQUAL Clang)
85            add_compile_options(-femit-struct-debug-detailed=none -feliminate-unused-debug-symbols)
86        endif()
87    endif()
88endif()
89
90# Tuning
91add_compile_options(-march=${OARCH} -mtune=${TUNE})
92
93# Warnings, errors
94if((NOT CMAKE_BUILD_TYPE STREQUAL "Release") AND (NOT CMAKE_C_COMPILER_ID STREQUAL Clang))
95    add_compile_options(-Werror)
96endif()
97
98add_compile_options(-Wall -Wpointer-arith)
99add_compile_options(-Wno-char-subscripts -Wno-multichar -Wno-unused-value)
100add_compile_options(-Wno-unused-const-variable)
101add_compile_options(-Wno-unused-local-typedefs)
102add_compile_options(-Wno-deprecated)
103add_compile_options(-Wno-unused-result) # FIXME To be removed when CORE-17637 is resolved
104
105if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
106    add_compile_options(-Wno-maybe-uninitialized)
107endif()
108
109if(ARCH STREQUAL "amd64")
110    add_compile_options(-Wno-format)
111elseif(ARCH STREQUAL "arm")
112    add_compile_options(-Wno-attributes)
113endif()
114
115# Optimizations
116# FIXME: Revisit this to see if we even need these levels
117if(CMAKE_BUILD_TYPE STREQUAL "Release")
118    add_compile_options(-O2 -DNDEBUG)
119else()
120    if(OPTIMIZE STREQUAL "1")
121        add_compile_options(-Os)
122        if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
123            add_compile_options(-ftracer)
124        endif()
125    elseif(OPTIMIZE STREQUAL "2")
126        add_compile_options(-Os)
127    elseif(OPTIMIZE STREQUAL "3")
128        add_compile_options(-Og)
129    elseif(OPTIMIZE STREQUAL "4")
130        add_compile_options(-O1)
131    elseif(OPTIMIZE STREQUAL "5")
132        add_compile_options(-O2)
133    elseif(OPTIMIZE STREQUAL "6")
134        add_compile_options(-O3)
135    elseif(OPTIMIZE STREQUAL "7")
136        add_compile_options(-Ofast)
137    endif()
138endif()
139
140# Link-time code generation
141if(LTCG)
142    add_compile_options(-flto -fno-fat-lto-objects)
143endif()
144
145if(ARCH STREQUAL "i386")
146    add_compile_options(-fno-optimize-sibling-calls -fno-omit-frame-pointer -mstackrealign)
147    if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
148        add_compile_options(-mpreferred-stack-boundary=3 -fno-set-stack-executable)
149    endif()
150    # FIXME: this doesn't work. CMAKE_BUILD_TYPE is always "Debug"
151    if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
152        add_compile_options(-momit-leaf-frame-pointer)
153    endif()
154elseif(ARCH STREQUAL "amd64")
155    if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
156        add_compile_options(-mpreferred-stack-boundary=4)
157    endif()
158    add_compile_options(-Wno-error)
159endif()
160
161# Other
162if(ARCH STREQUAL "amd64")
163    add_definitions(-U_X86_ -UWIN32)
164elseif(ARCH STREQUAL "arm")
165    add_definitions(-U_UNICODE -UUNICODE)
166    add_definitions(-D__MSVCRT__) # DUBIOUS
167endif()
168
169add_definitions(-D_inline=__inline)
170
171# Fix build with GLIBCXX + our c++ headers
172add_definitions(-D_GLIBCXX_HAVE_BROKEN_VSWPRINTF)
173
174# Alternative arch name
175if(ARCH STREQUAL "amd64")
176    set(ARCH2 x86_64)
177else()
178    set(ARCH2 ${ARCH})
179endif()
180
181if(SEPARATE_DBG)
182    # PDB style debug puts all dwarf debug info in a separate dbg file
183    message(STATUS "Building separate debug symbols")
184    file(MAKE_DIRECTORY ${REACTOS_BINARY_DIR}/symbols)
185    if(CMAKE_GENERATOR STREQUAL "Ninja")
186        # Those variables seems to be set but empty in newer CMake versions
187        # and Ninja generator relies on them to generate PDB name, so unset them.
188        unset(MSVC_C_ARCHITECTURE_ID)
189        unset(MSVC_CXX_ARCHITECTURE_ID)
190        set(CMAKE_DEBUG_SYMBOL_SUFFIX "")
191        set(SYMBOL_FILE <TARGET_PDB>)
192    else()
193        set(SYMBOL_FILE <TARGET>)
194    endif()
195
196    if (NOT NO_ROSSYM)
197        get_target_property(RSYM native-rsym IMPORTED_LOCATION)
198        set(strip_debug "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
199    else()
200        set(strip_debug "${CMAKE_STRIP} --strip-debug <TARGET>")
201    endif()
202
203    set(CMAKE_C_LINK_EXECUTABLE
204        "<CMAKE_C_COMPILER> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
205        "${CMAKE_STRIP} --only-keep-debug <TARGET> -o ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
206        ${strip_debug})
207    set(CMAKE_CXX_LINK_EXECUTABLE
208        "<CMAKE_CXX_COMPILER> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
209        "${CMAKE_STRIP} --only-keep-debug <TARGET> -o ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
210        ${strip_debug})
211    set(CMAKE_C_CREATE_SHARED_LIBRARY
212        "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
213        "${CMAKE_STRIP} --only-keep-debug <TARGET> -o ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
214        ${strip_debug})
215    set(CMAKE_CXX_CREATE_SHARED_LIBRARY
216        "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
217        "${CMAKE_STRIP} --only-keep-debug <TARGET> -o ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
218        ${strip_debug})
219    set(CMAKE_RC_CREATE_SHARED_LIBRARY
220        "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
221        "${CMAKE_STRIP} --only-keep-debug <TARGET> -o ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
222        ${strip_debug})
223elseif(NO_ROSSYM)
224    # Dwarf-based build
225    message(STATUS "Generating a dwarf-based build (no rsym)")
226    set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
227    set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
228    set(CMAKE_C_CREATE_SHARED_LIBRARY "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
229    set(CMAKE_CXX_CREATE_SHARED_LIBRARY "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
230    set(CMAKE_RC_CREATE_SHARED_LIBRARY "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
231else()
232    # Normal rsym build
233    get_target_property(RSYM native-rsym IMPORTED_LOCATION)
234
235    set(CMAKE_C_LINK_EXECUTABLE
236        "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
237        "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
238    set(CMAKE_CXX_LINK_EXECUTABLE
239        "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
240        "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
241    set(CMAKE_C_CREATE_SHARED_LIBRARY
242        "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
243        "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
244    set(CMAKE_CXX_CREATE_SHARED_LIBRARY
245        "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
246        "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
247    set(CMAKE_RC_CREATE_SHARED_LIBRARY
248        "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
249endif()
250
251set(CMAKE_C_CREATE_SHARED_MODULE ${CMAKE_C_CREATE_SHARED_LIBRARY})
252set(CMAKE_CXX_CREATE_SHARED_MODULE ${CMAKE_CXX_CREATE_SHARED_LIBRARY})
253set(CMAKE_RC_CREATE_SHARED_MODULE ${CMAKE_RC_CREATE_SHARED_LIBRARY})
254
255set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup,--gc-sections")
256set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
257set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
258
259set(CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> ${_compress_debug_sections_flag} <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
260# FIXME: Once the GCC toolchain bugs are fixed, add _compress_debug_sections_flag to CXX too
261set(CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
262set(CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> ${_compress_debug_sections_flag} -x assembler-with-cpp -o <OBJECT> -I${REACTOS_SOURCE_DIR}/sdk/include/asm -I${REACTOS_BINARY_DIR}/sdk/include/asm <INCLUDES> <FLAGS> <DEFINES> -D__ASM__ -c <SOURCE>")
263
264set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -O coff <INCLUDES> <FLAGS> -DRC_INVOKED -D__WIN32__=1 -D__FLAT__=1 ${I18N_DEFS} <DEFINES> <SOURCE> <OBJECT>")
265
266if (CMAKE_C_COMPILER_ID STREQUAL "Clang")
267    set(RC_PREPROCESSOR_TARGET "--preprocessor-arg=--target=${CMAKE_C_COMPILER_TARGET}")
268else()
269    set(RC_PREPROCESSOR_TARGET "")
270endif()
271
272# We have to pass args to windres. one... by... one...
273set(CMAKE_DEPFILE_FLAGS_RC "--preprocessor=\"${CMAKE_C_COMPILER}\" ${RC_PREPROCESSOR_TARGET} --preprocessor-arg=-E --preprocessor-arg=-nostdinc --preprocessor-arg=-xc-header --preprocessor-arg=-MMD --preprocessor-arg=-MF --preprocessor-arg=<DEPFILE> --preprocessor-arg=-MT --preprocessor-arg=<OBJECT>")
274
275# Optional 3rd parameter: stdcall stack bytes
276function(set_entrypoint MODULE ENTRYPOINT)
277    if(${ENTRYPOINT} STREQUAL "0")
278        target_link_options(${MODULE} PRIVATE "-Wl,-entry,0")
279    elseif(ARCH STREQUAL "i386")
280        set(_entrysymbol _${ENTRYPOINT})
281        if(${ARGC} GREATER 2)
282            set(_entrysymbol ${_entrysymbol}@${ARGV2})
283        endif()
284        target_link_options(${MODULE} PRIVATE "-Wl,-entry,${_entrysymbol}")
285    else()
286        target_link_options(${MODULE} PRIVATE "-Wl,-entry,${ENTRYPOINT}")
287    endif()
288endfunction()
289
290function(set_subsystem MODULE SUBSYSTEM)
291    target_link_options(${MODULE} PRIVATE "-Wl,--subsystem,${SUBSYSTEM}:5.01")
292endfunction()
293
294function(set_image_base MODULE IMAGE_BASE)
295    target_link_options(${MODULE} PRIVATE "-Wl,--image-base,${IMAGE_BASE}")
296endfunction()
297
298function(set_module_type_toolchain MODULE TYPE)
299    # Set the PE image version numbers from the NT OS version ReactOS is based on
300    target_link_options(${MODULE} PRIVATE
301        -Wl,--major-image-version,5 -Wl,--minor-image-version,01 -Wl,--major-os-version,5 -Wl,--minor-os-version,01)
302
303    if(TYPE IN_LIST KERNEL_MODULE_TYPES)
304        target_link_options(${MODULE} PRIVATE -Wl,--exclude-all-symbols,-file-alignment=0x1000,-section-alignment=0x1000)
305
306        if(${TYPE} STREQUAL "wdmdriver")
307            target_link_options(${MODULE} PRIVATE "-Wl,--wdmdriver")
308        endif()
309
310        # Place INIT &.rsrc section at the tail of the module, before .reloc
311        add_linker_script(${MODULE} ${REACTOS_SOURCE_DIR}/sdk/cmake/init-section.lds)
312
313        # Fixup section characteristics
314        #  - Remove flags that LD overzealously puts (alignment flag, Initialized flags for code sections)
315        #  - INIT section is made discardable
316        #  - .rsrc is made read-only and discardable
317        #  - PAGE & .edata sections are made pageable.
318        add_custom_command(TARGET ${MODULE} POST_BUILD
319            COMMAND native-pefixup --${TYPE} $<TARGET_FILE:${MODULE}>)
320
321        # Believe it or not, cmake doesn't do that
322        set_property(TARGET ${MODULE} APPEND PROPERTY LINK_DEPENDS $<TARGET_PROPERTY:native-pefixup,IMPORTED_LOCATION>)
323    endif()
324endfunction()
325
326function(add_delay_importlibs _module)
327    get_target_property(_module_type ${_module} TYPE)
328    if(_module_type STREQUAL "STATIC_LIBRARY")
329        message(FATAL_ERROR "Cannot add delay imports to a static library")
330    endif()
331    foreach(_lib ${ARGN})
332        get_filename_component(_basename "${_lib}" NAME_WE)
333        target_link_libraries(${_module} lib${_basename}_delayed)
334    endforeach()
335    target_link_libraries(${_module} delayimp)
336endfunction()
337
338if(NOT ARCH STREQUAL "i386")
339    set(DECO_OPTION "-@")
340endif()
341
342function(fixup_load_config _target)
343    add_custom_command(TARGET ${_target} POST_BUILD
344        COMMAND native-pefixup --loadconfig "$<TARGET_FILE:${_target}>"
345        COMMENT "Patching in LOAD_CONFIG"
346        DEPENDS native-pefixup)
347endfunction()
348
349function(generate_import_lib _libname _dllname _spec_file)
350    # Generate the def for the import lib
351    add_custom_command(
352        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def
353        COMMAND native-spec2def -n=${_dllname} -a=${ARCH2} ${ARGN} --implib -d=${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
354        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
355
356    # With this, we let DLLTOOL create an import library
357    set(LIBRARY_PRIVATE_DIR ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_libname}.dir)
358    add_custom_command(
359        OUTPUT ${LIBRARY_PRIVATE_DIR}/${_libname}.a
360        # ar just puts stuff into the archive, without looking twice. Just delete the lib, we're going to rebuild it anyway
361        COMMAND ${CMAKE_COMMAND} -E rm -f $<TARGET_FILE:${_libname}>
362        COMMAND ${CMAKE_DLLTOOL} --def ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def --kill-at --output-lib=${_libname}.a -t ${_libname}
363        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def
364        WORKING_DIRECTORY ${LIBRARY_PRIVATE_DIR})
365
366    # We create a static library with the importlib thus created as object. AR will extract the obj files and archive it again as a thin lib
367    set_source_files_properties(
368        ${LIBRARY_PRIVATE_DIR}/${_libname}.a
369        PROPERTIES
370        EXTERNAL_OBJECT TRUE)
371    _add_library(${_libname} STATIC EXCLUDE_FROM_ALL
372        ${LIBRARY_PRIVATE_DIR}/${_libname}.a)
373    set_target_properties(${_libname}
374        PROPERTIES
375        LINKER_LANGUAGE "C"
376        PREFIX "")
377
378    # Do the same with delay-import libs
379    set(LIBRARY_PRIVATE_DIR ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_libname}_delayed.dir)
380    add_custom_command(
381        OUTPUT ${LIBRARY_PRIVATE_DIR}/${_libname}_delayed.a
382        # ar just puts stuff into the archive, without looking twice. Just delete the lib, we're going to rebuild it anyway
383        COMMAND ${CMAKE_COMMAND} -E rm -f $<TARGET_FILE:${_libname}_delayed>
384        COMMAND ${CMAKE_DLLTOOL} --def ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def --kill-at --output-delaylib=${_libname}_delayed.a -t ${_libname}_delayed
385        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def
386        WORKING_DIRECTORY ${LIBRARY_PRIVATE_DIR})
387
388    # We create a static library with the importlib thus created. AR will extract the obj files and archive it again as a thin lib
389    set_source_files_properties(
390        ${LIBRARY_PRIVATE_DIR}/${_libname}_delayed.a
391        PROPERTIES
392        EXTERNAL_OBJECT TRUE)
393    _add_library(${_libname}_delayed STATIC EXCLUDE_FROM_ALL
394        ${LIBRARY_PRIVATE_DIR}/${_libname}_delayed.a)
395    set_target_properties(${_libname}_delayed
396        PROPERTIES
397        LINKER_LANGUAGE "C"
398        PREFIX "")
399endfunction()
400
401function(spec2def _dllname _spec_file)
402
403    cmake_parse_arguments(__spec2def "ADD_IMPORTLIB;NO_PRIVATE_WARNINGS;WITH_RELAY" "VERSION" "" ${ARGN})
404
405    # Get library basename
406    get_filename_component(_file ${_dllname} NAME_WE)
407
408    # Error out on anything else than spec
409    if(NOT ${_spec_file} MATCHES ".*\\.spec")
410        message(FATAL_ERROR "spec2def only takes spec files as input.")
411    endif()
412
413    if(__spec2def_WITH_RELAY)
414        set(__with_relay_arg "--with-tracing")
415    endif()
416
417    if(__spec2def_VERSION)
418        set(__version_arg "--version=0x${__spec2def_VERSION}")
419    endif()
420
421    # Generate exports def and C stubs file for the DLL
422    add_custom_command(
423        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
424        COMMAND native-spec2def -n=${_dllname} -a=${ARCH2} -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}
425        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
426
427    if(__spec2def_ADD_IMPORTLIB)
428        set(_extraflags)
429        if(__spec2def_NO_PRIVATE_WARNINGS)
430            set(_extraflags --no-private-warnings)
431        endif()
432
433        generate_import_lib(lib${_file} ${_dllname} ${_spec_file} ${_extraflags})
434    endif()
435endfunction()
436
437macro(macro_mc FLAG FILE)
438    set(COMMAND_MC ${CMAKE_MC_COMPILER} -u ${FLAG} -b -h ${CMAKE_CURRENT_BINARY_DIR}/ -r ${CMAKE_CURRENT_BINARY_DIR}/ ${FILE})
439endmacro()
440
441# PSEH lib, needed with mingw
442set(PSEH_LIB "pseh")
443
444function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
445    set(_object_file ${_binary_file}.o)
446
447    get_defines(_defines)
448    get_includes(_includes)
449
450    add_custom_command(
451        OUTPUT ${_object_file}
452        COMMAND ${CMAKE_ASM_COMPILER} -x assembler-with-cpp -o ${_object_file} -I${REACTOS_SOURCE_DIR}/sdk/include/asm -I${REACTOS_BINARY_DIR}/sdk/include/asm ${_includes} ${_defines} -D__ASM__ -c ${_asm_file}
453        DEPENDS ${_asm_file})
454
455    add_custom_command(
456        OUTPUT ${_binary_file}
457        COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
458        # COMMAND objcopy --output-target binary --image-base 0x${_base_address} ${_object_file} ${_binary_file}
459        DEPENDS ${_object_file} native-obj2bin)
460
461    set_source_files_properties(${_object_file} ${_binary_file} PROPERTIES GENERATED TRUE)
462
463    add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
464endfunction()
465
466function(allow_warnings __module)
467    # We don't allow warnings in trunk, this needs to be reworked. See CORE-6959.
468    #target_compile_options(${__module} PRIVATE "-Wno-error")
469endfunction()
470
471macro(add_asm_files _target)
472    list(APPEND ${_target} ${ARGN})
473endmacro()
474
475function(add_linker_script _target _linker_script_file)
476    get_filename_component(_file_full_path ${_linker_script_file} ABSOLUTE)
477    target_link_options(${_target} PRIVATE "-Wl,-T,${_file_full_path}")
478    set_property(TARGET ${_target} APPEND PROPERTY LINK_DEPENDS ${_file_full_path})
479endfunction()
480
481# Manage our C++ options
482# we disable standard includes if we don't use the STL
483add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<NOT:$<IN_LIST:cppstl,$<TARGET_PROPERTY:LINK_LIBRARIES>>>>:-nostdinc>")
484# we disable RTTI, unless said so
485add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:$<IF:$<BOOL:$<TARGET_PROPERTY:WITH_CXX_RTTI>>,-frtti,-fno-rtti>>")
486# We disable exceptions, unless said so
487add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:$<IF:$<BOOL:$<TARGET_PROPERTY:WITH_CXX_EXCEPTIONS>>,-fexceptions,-fno-exceptions>>")
488
489# G++ shipped with ROSBE uses sjlj exceptions on i386. Tell Clang it is so
490if (CLANG AND (ARCH STREQUAL "i386"))
491    add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:$<TARGET_PROPERTY:WITH_CXX_EXCEPTIONS>>>:-fsjlj-exceptions>")
492endif()
493
494# Find default G++ libraries
495if (CLANG)
496    set(GXX_EXECUTABLE ${CMAKE_CXX_COMPILER_TARGET}-g++)
497else()
498    set(GXX_EXECUTABLE ${CMAKE_CXX_COMPILER})
499endif()
500
501add_library(libgcc STATIC IMPORTED)
502execute_process(COMMAND ${GXX_EXECUTABLE} -print-file-name=libgcc.a OUTPUT_VARIABLE LIBGCC_LOCATION)
503string(STRIP ${LIBGCC_LOCATION} LIBGCC_LOCATION)
504set_target_properties(libgcc PROPERTIES IMPORTED_LOCATION ${LIBGCC_LOCATION})
505# libgcc needs kernel32 imports, a CRT and msvcrtex
506target_link_libraries(libgcc INTERFACE libkernel32 libmsvcrt msvcrtex)
507
508add_library(libsupc++ STATIC IMPORTED GLOBAL)
509execute_process(COMMAND ${GXX_EXECUTABLE} -print-file-name=libsupc++.a OUTPUT_VARIABLE LIBSUPCXX_LOCATION)
510string(STRIP ${LIBSUPCXX_LOCATION} LIBSUPCXX_LOCATION)
511set_target_properties(libsupc++ PROPERTIES IMPORTED_LOCATION ${LIBSUPCXX_LOCATION})
512# libsupc++ requires libgcc
513target_link_libraries(libsupc++ INTERFACE libgcc)
514
515add_library(libmingwex STATIC IMPORTED)
516execute_process(COMMAND ${GXX_EXECUTABLE} -print-file-name=libmingwex.a OUTPUT_VARIABLE LIBMINGWEX_LOCATION)
517string(STRIP ${LIBMINGWEX_LOCATION} LIBMINGWEX_LOCATION)
518set_target_properties(libmingwex PROPERTIES IMPORTED_LOCATION ${LIBMINGWEX_LOCATION})
519# libmingwex requires a CRT and imports from kernel32
520target_link_libraries(libmingwex INTERFACE libmsvcrt libkernel32)
521
522add_library(libstdc++ STATIC IMPORTED GLOBAL)
523execute_process(COMMAND ${GXX_EXECUTABLE} -print-file-name=libstdc++.a OUTPUT_VARIABLE LIBSTDCCXX_LOCATION)
524string(STRIP ${LIBSTDCCXX_LOCATION} LIBSTDCCXX_LOCATION)
525set_target_properties(libstdc++ PROPERTIES IMPORTED_LOCATION ${LIBSTDCCXX_LOCATION})
526# libstdc++ requires libsupc++ and mingwex provided by GCC
527target_link_libraries(libstdc++ INTERFACE libsupc++ libmingwex)
528# this is for our SAL annotations
529target_compile_definitions(libstdc++ INTERFACE "$<$<COMPILE_LANGUAGE:CXX>:PAL_STDCPP_COMPAT>")
530
531# Create our alias libraries
532add_library(cppstl ALIAS libstdc++)
533add_library(cpprt ALIAS libsupc++)
534
535