xref: /reactos/sdk/cmake/gcc.cmake (revision 1734f297)
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
42# note: -fno-common is default since GCC 10
43add_compile_options(-pipe -fms-extensions -fno-strict-aliasing -fno-common)
44
45# Prevent GCC from searching any of the default directories.
46# The case for C++ is handled through the reactos_c++ INTERFACE library
47add_compile_options("$<$<NOT:$<COMPILE_LANGUAGE:CXX>>:-nostdinc>")
48
49if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
50    add_compile_options(-fno-aggressive-loop-optimizations)
51    if (DBG)
52        add_compile_options("$<$<COMPILE_LANGUAGE:C>:-Wold-style-declaration>")
53    endif()
54elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
55    add_compile_options("$<$<COMPILE_LANGUAGE:C>:-Wno-microsoft>")
56    add_compile_options(-Wno-pragma-pack)
57    add_compile_options(-fno-associative-math)
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_compile_options(-mcx16) # Generate CMPXCHG16
164    add_definitions(-U_X86_ -UWIN32)
165elseif(ARCH STREQUAL "arm")
166    add_definitions(-U_UNICODE -UUNICODE)
167    add_definitions(-D__MSVCRT__) # DUBIOUS
168endif()
169
170# Fix build with GLIBCXX + our c++ headers
171add_definitions(-D_GLIBCXX_HAVE_BROKEN_VSWPRINTF)
172
173# Alternative arch name
174if(ARCH STREQUAL "amd64")
175    set(ARCH2 x86_64)
176else()
177    set(ARCH2 ${ARCH})
178endif()
179
180if(SEPARATE_DBG)
181    # PDB style debug puts all dwarf debug info in a separate dbg file
182    message(STATUS "Building separate debug symbols")
183    file(MAKE_DIRECTORY ${REACTOS_BINARY_DIR}/symbols)
184    if(CMAKE_GENERATOR STREQUAL "Ninja")
185        # Those variables seems to be set but empty in newer CMake versions
186        # and Ninja generator relies on them to generate PDB name, so unset them.
187        unset(MSVC_C_ARCHITECTURE_ID)
188        unset(MSVC_CXX_ARCHITECTURE_ID)
189        set(CMAKE_DEBUG_SYMBOL_SUFFIX "")
190        set(SYMBOL_FILE <TARGET_PDB>)
191    else()
192        set(SYMBOL_FILE <TARGET>)
193    endif()
194
195    if (NOT NO_ROSSYM)
196        get_target_property(RSYM native-rsym IMPORTED_LOCATION)
197        set(strip_debug "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
198    else()
199        set(strip_debug "${CMAKE_STRIP} --strip-debug <TARGET>")
200    endif()
201
202    set(CMAKE_C_LINK_EXECUTABLE
203        "<CMAKE_C_COMPILER> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
204        "${CMAKE_STRIP} --only-keep-debug <TARGET> -o ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
205        ${strip_debug})
206    set(CMAKE_CXX_LINK_EXECUTABLE
207        "<CMAKE_CXX_COMPILER> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
208        "${CMAKE_STRIP} --only-keep-debug <TARGET> -o ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
209        ${strip_debug})
210    set(CMAKE_C_CREATE_SHARED_LIBRARY
211        "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
212        "${CMAKE_STRIP} --only-keep-debug <TARGET> -o ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
213        ${strip_debug})
214    set(CMAKE_CXX_CREATE_SHARED_LIBRARY
215        "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
216        "${CMAKE_STRIP} --only-keep-debug <TARGET> -o ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
217        ${strip_debug})
218    set(CMAKE_RC_CREATE_SHARED_LIBRARY
219        "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
220        "${CMAKE_STRIP} --only-keep-debug <TARGET> -o ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
221        ${strip_debug})
222elseif(NO_ROSSYM)
223    # Dwarf-based build
224    message(STATUS "Generating a dwarf-based build (no rsym)")
225    set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
226    set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
227    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>")
228    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>")
229    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>")
230else()
231    # Normal rsym build
232    get_target_property(RSYM native-rsym IMPORTED_LOCATION)
233
234    set(CMAKE_C_LINK_EXECUTABLE
235        "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
236        "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
237    set(CMAKE_CXX_LINK_EXECUTABLE
238        "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
239        "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
240    set(CMAKE_C_CREATE_SHARED_LIBRARY
241        "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
242        "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
243    set(CMAKE_CXX_CREATE_SHARED_LIBRARY
244        "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
245        "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
246    set(CMAKE_RC_CREATE_SHARED_LIBRARY
247        "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
248endif()
249
250set(CMAKE_C_CREATE_SHARED_MODULE ${CMAKE_C_CREATE_SHARED_LIBRARY})
251set(CMAKE_CXX_CREATE_SHARED_MODULE ${CMAKE_CXX_CREATE_SHARED_LIBRARY})
252set(CMAKE_RC_CREATE_SHARED_MODULE ${CMAKE_RC_CREATE_SHARED_LIBRARY})
253
254set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup,--gc-sections")
255set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
256set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
257
258set(CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> ${_compress_debug_sections_flag} <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
259# FIXME: Once the GCC toolchain bugs are fixed, add _compress_debug_sections_flag to CXX too
260set(CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
261set(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>")
262
263set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -O coff <INCLUDES> <FLAGS> -DRC_INVOKED -D__WIN32__=1 -D__FLAT__=1 ${I18N_DEFS} <DEFINES> <SOURCE> <OBJECT>")
264
265if (CMAKE_C_COMPILER_ID STREQUAL "Clang")
266    set(RC_PREPROCESSOR_TARGET "--preprocessor-arg=--target=${CMAKE_C_COMPILER_TARGET}")
267else()
268    set(RC_PREPROCESSOR_TARGET "")
269endif()
270
271# We have to pass args to windres. one... by... one...
272set(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>")
273
274# Optional 3rd parameter: stdcall stack bytes
275function(set_entrypoint MODULE ENTRYPOINT)
276    if(${ENTRYPOINT} STREQUAL "0")
277        target_link_options(${MODULE} PRIVATE "-Wl,-entry,0")
278    elseif(ARCH STREQUAL "i386")
279        set(_entrysymbol _${ENTRYPOINT})
280        if(${ARGC} GREATER 2)
281            set(_entrysymbol ${_entrysymbol}@${ARGV2})
282        endif()
283        target_link_options(${MODULE} PRIVATE "-Wl,-entry,${_entrysymbol}")
284    else()
285        target_link_options(${MODULE} PRIVATE "-Wl,-entry,${ENTRYPOINT}")
286    endif()
287endfunction()
288
289function(set_subsystem MODULE SUBSYSTEM)
290    target_link_options(${MODULE} PRIVATE "-Wl,--subsystem,${SUBSYSTEM}:5.01")
291endfunction()
292
293function(set_image_base MODULE IMAGE_BASE)
294    target_link_options(${MODULE} PRIVATE "-Wl,--image-base,${IMAGE_BASE}")
295endfunction()
296
297function(set_module_type_toolchain MODULE TYPE)
298    # Set the PE image version numbers from the NT OS version ReactOS is based on
299    target_link_options(${MODULE} PRIVATE
300        -Wl,--major-image-version,5 -Wl,--minor-image-version,01 -Wl,--major-os-version,5 -Wl,--minor-os-version,01)
301
302    if(TYPE IN_LIST KERNEL_MODULE_TYPES)
303        target_link_options(${MODULE} PRIVATE -Wl,--exclude-all-symbols,-file-alignment=0x1000,-section-alignment=0x1000)
304
305        if(${TYPE} STREQUAL "wdmdriver")
306            target_link_options(${MODULE} PRIVATE "-Wl,--wdmdriver")
307        endif()
308
309        # Place INIT &.rsrc section at the tail of the module, before .reloc
310        add_linker_script(${MODULE} ${REACTOS_SOURCE_DIR}/sdk/cmake/init-section.lds)
311
312        # Fixup section characteristics
313        #  - Remove flags that LD overzealously puts (alignment flag, Initialized flags for code sections)
314        #  - INIT section is made discardable
315        #  - .rsrc is made read-only and discardable
316        #  - PAGE & .edata sections are made pageable.
317        add_custom_command(TARGET ${MODULE} POST_BUILD
318            COMMAND native-pefixup --${TYPE} $<TARGET_FILE:${MODULE}>)
319
320        # Believe it or not, cmake doesn't do that
321        set_property(TARGET ${MODULE} APPEND PROPERTY LINK_DEPENDS $<TARGET_PROPERTY:native-pefixup,IMPORTED_LOCATION>)
322    endif()
323endfunction()
324
325function(add_delay_importlibs _module)
326    get_target_property(_module_type ${_module} TYPE)
327    if(_module_type STREQUAL "STATIC_LIBRARY")
328        message(FATAL_ERROR "Cannot add delay imports to a static library")
329    endif()
330    foreach(_lib ${ARGN})
331        get_filename_component(_basename "${_lib}" NAME_WE)
332        target_link_libraries(${_module} lib${_basename}_delayed)
333    endforeach()
334    target_link_libraries(${_module} delayimp)
335endfunction()
336
337if(NOT ARCH STREQUAL "i386")
338    set(DECO_OPTION "-@")
339endif()
340
341function(fixup_load_config _target)
342    add_custom_command(TARGET ${_target} POST_BUILD
343        COMMAND native-pefixup --loadconfig "$<TARGET_FILE:${_target}>"
344        COMMENT "Patching in LOAD_CONFIG"
345        DEPENDS native-pefixup)
346endfunction()
347
348function(generate_import_lib _libname _dllname _spec_file)
349    # Generate the def for the import lib
350    add_custom_command(
351        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def
352        COMMAND native-spec2def -n=${_dllname} -a=${ARCH2} ${ARGN} --implib -d=${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
353        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
354
355    # With this, we let DLLTOOL create an import library
356    set(LIBRARY_PRIVATE_DIR ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_libname}.dir)
357    add_custom_command(
358        OUTPUT ${LIBRARY_PRIVATE_DIR}/${_libname}.a
359        # ar just puts stuff into the archive, without looking twice. Just delete the lib, we're going to rebuild it anyway
360        COMMAND ${CMAKE_COMMAND} -E rm -f $<TARGET_FILE:${_libname}>
361        COMMAND ${CMAKE_DLLTOOL} --def ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def --kill-at --output-lib=${_libname}.a -t ${_libname}
362        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def
363        WORKING_DIRECTORY ${LIBRARY_PRIVATE_DIR})
364
365    # 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
366    set_source_files_properties(
367        ${LIBRARY_PRIVATE_DIR}/${_libname}.a
368        PROPERTIES
369        EXTERNAL_OBJECT TRUE)
370    _add_library(${_libname} STATIC EXCLUDE_FROM_ALL
371        ${LIBRARY_PRIVATE_DIR}/${_libname}.a)
372    set_target_properties(${_libname}
373        PROPERTIES
374        LINKER_LANGUAGE "C"
375        PREFIX "")
376
377    # Do the same with delay-import libs
378    set(LIBRARY_PRIVATE_DIR ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_libname}_delayed.dir)
379    add_custom_command(
380        OUTPUT ${LIBRARY_PRIVATE_DIR}/${_libname}_delayed.a
381        # ar just puts stuff into the archive, without looking twice. Just delete the lib, we're going to rebuild it anyway
382        COMMAND ${CMAKE_COMMAND} -E rm -f $<TARGET_FILE:${_libname}_delayed>
383        COMMAND ${CMAKE_DLLTOOL} --def ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def --kill-at --output-delaylib=${_libname}_delayed.a -t ${_libname}_delayed
384        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def
385        WORKING_DIRECTORY ${LIBRARY_PRIVATE_DIR})
386
387    # We create a static library with the importlib thus created. AR will extract the obj files and archive it again as a thin lib
388    set_source_files_properties(
389        ${LIBRARY_PRIVATE_DIR}/${_libname}_delayed.a
390        PROPERTIES
391        EXTERNAL_OBJECT TRUE)
392    _add_library(${_libname}_delayed STATIC EXCLUDE_FROM_ALL
393        ${LIBRARY_PRIVATE_DIR}/${_libname}_delayed.a)
394    set_target_properties(${_libname}_delayed
395        PROPERTIES
396        LINKER_LANGUAGE "C"
397        PREFIX "")
398endfunction()
399
400function(spec2def _dllname _spec_file)
401
402    cmake_parse_arguments(__spec2def "ADD_IMPORTLIB;NO_PRIVATE_WARNINGS;WITH_RELAY" "VERSION" "" ${ARGN})
403
404    # Get library basename
405    get_filename_component(_file ${_dllname} NAME_WE)
406
407    # Error out on anything else than spec
408    if(NOT ${_spec_file} MATCHES ".*\\.spec")
409        message(FATAL_ERROR "spec2def only takes spec files as input.")
410    endif()
411
412    if(__spec2def_WITH_RELAY)
413        set(__with_relay_arg "--with-tracing")
414    endif()
415
416    if(__spec2def_VERSION)
417        set(__version_arg "--version=0x${__spec2def_VERSION}")
418    endif()
419
420    # Generate exports def and C stubs file for the DLL
421    add_custom_command(
422        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
423        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}
424        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
425
426    if(__spec2def_ADD_IMPORTLIB)
427        set(_extraflags)
428        if(__spec2def_NO_PRIVATE_WARNINGS)
429            set(_extraflags --no-private-warnings)
430        endif()
431
432        generate_import_lib(lib${_file} ${_dllname} ${_spec_file} ${_extraflags})
433    endif()
434endfunction()
435
436macro(macro_mc FLAG FILE)
437    set(COMMAND_MC ${CMAKE_MC_COMPILER} -u ${FLAG} -b -h ${CMAKE_CURRENT_BINARY_DIR}/ -r ${CMAKE_CURRENT_BINARY_DIR}/ ${FILE})
438endmacro()
439
440# PSEH lib, needed with mingw
441set(PSEH_LIB "pseh")
442
443function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
444    set(_object_file ${_binary_file}.o)
445
446    get_defines(_defines)
447    get_includes(_includes)
448
449    add_custom_command(
450        OUTPUT ${_object_file}
451        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}
452        DEPENDS ${_asm_file})
453
454    add_custom_command(
455        OUTPUT ${_binary_file}
456        COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
457        # COMMAND objcopy --output-target binary --image-base 0x${_base_address} ${_object_file} ${_binary_file}
458        DEPENDS ${_object_file} native-obj2bin)
459
460    set_source_files_properties(${_object_file} ${_binary_file} PROPERTIES GENERATED TRUE)
461
462    add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
463endfunction()
464
465function(allow_warnings __module)
466    # We don't allow warnings in trunk, this needs to be reworked. See CORE-6959.
467    #target_compile_options(${__module} PRIVATE "-Wno-error")
468endfunction()
469
470function(convert_asm_file _source_file _target_file)
471    get_filename_component(_source_file_base_name ${_source_file} NAME_WE)
472    get_filename_component(_source_file_full_path ${_source_file} ABSOLUTE)
473    set(_preprocessed_asm_file ${CMAKE_CURRENT_BINARY_DIR}/${_target_file})
474    add_custom_command(
475        OUTPUT ${_preprocessed_asm_file}
476        COMMAND native-asmpp ${_source_file_full_path} > ${_preprocessed_asm_file}
477        DEPENDS native-asmpp ${_source_file_full_path})
478
479endfunction()
480
481function(convert_asm_files)
482    foreach(_source_file ${ARGN})
483        convert_asm_file(${_source_file} ${_source_file}.s)
484    endforeach()
485endfunction()
486
487macro(add_asm_files _target)
488    foreach(_source_file ${ARGN})
489        get_filename_component(_extension ${_source_file} EXT)
490        get_filename_component(_source_file_base_name ${_source_file} NAME_WE)
491        if (${_extension} STREQUAL ".asm")
492            convert_asm_file(${_source_file} ${_source_file}.s)
493            list(APPEND ${_target} ${CMAKE_CURRENT_BINARY_DIR}/${_source_file}.s)
494        elseif (${_extension} STREQUAL ".inc")
495            convert_asm_file(${_source_file} ${_source_file}.h)
496            list(APPEND ${_target} ${CMAKE_CURRENT_BINARY_DIR}/${_source_file}.h)
497        else()
498            list(APPEND ${_target} ${_source_file})
499        endif()
500    endforeach()
501endmacro()
502
503function(add_linker_script _target _linker_script_file)
504    get_filename_component(_file_full_path ${_linker_script_file} ABSOLUTE)
505    target_link_options(${_target} PRIVATE "-Wl,-T,${_file_full_path}")
506    set_property(TARGET ${_target} APPEND PROPERTY LINK_DEPENDS ${_file_full_path})
507endfunction()
508
509# Manage our C++ options
510# we disable standard includes if we don't use the STL
511add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<NOT:$<IN_LIST:cppstl,$<TARGET_PROPERTY:LINK_LIBRARIES>>>>:-nostdinc>")
512# we disable RTTI, unless said so
513add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:$<IF:$<BOOL:$<TARGET_PROPERTY:WITH_CXX_RTTI>>,-frtti,-fno-rtti>>")
514# We disable exceptions, unless said so
515add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:$<IF:$<BOOL:$<TARGET_PROPERTY:WITH_CXX_EXCEPTIONS>>,-fexceptions,-fno-exceptions>>")
516
517# G++ shipped with ROSBE uses sjlj exceptions on i386. Tell Clang it is so
518if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND ARCH STREQUAL "i386")
519    add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:$<TARGET_PROPERTY:WITH_CXX_EXCEPTIONS>>>:-fsjlj-exceptions>")
520endif()
521
522# Find default G++ libraries
523if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
524    set(GXX_EXECUTABLE ${CMAKE_CXX_COMPILER_TARGET}-g++)
525else()
526    set(GXX_EXECUTABLE ${CMAKE_CXX_COMPILER})
527endif()
528
529add_library(libgcc STATIC IMPORTED)
530execute_process(COMMAND ${GXX_EXECUTABLE} -print-file-name=libgcc.a OUTPUT_VARIABLE LIBGCC_LOCATION)
531string(STRIP ${LIBGCC_LOCATION} LIBGCC_LOCATION)
532set_target_properties(libgcc PROPERTIES IMPORTED_LOCATION ${LIBGCC_LOCATION})
533# libgcc needs kernel32 imports, a CRT and msvcrtex
534target_link_libraries(libgcc INTERFACE libkernel32 libmsvcrt msvcrtex)
535
536add_library(libsupc++ STATIC IMPORTED GLOBAL)
537execute_process(COMMAND ${GXX_EXECUTABLE} -print-file-name=libsupc++.a OUTPUT_VARIABLE LIBSUPCXX_LOCATION)
538string(STRIP ${LIBSUPCXX_LOCATION} LIBSUPCXX_LOCATION)
539set_target_properties(libsupc++ PROPERTIES IMPORTED_LOCATION ${LIBSUPCXX_LOCATION})
540# libsupc++ requires libgcc
541target_link_libraries(libsupc++ INTERFACE libgcc)
542
543add_library(libmingwex STATIC IMPORTED)
544execute_process(COMMAND ${GXX_EXECUTABLE} -print-file-name=libmingwex.a OUTPUT_VARIABLE LIBMINGWEX_LOCATION)
545string(STRIP ${LIBMINGWEX_LOCATION} LIBMINGWEX_LOCATION)
546set_target_properties(libmingwex PROPERTIES IMPORTED_LOCATION ${LIBMINGWEX_LOCATION})
547# libmingwex requires a CRT and imports from kernel32
548target_link_libraries(libmingwex INTERFACE libmsvcrt libkernel32)
549
550add_library(libstdc++ STATIC IMPORTED GLOBAL)
551execute_process(COMMAND ${GXX_EXECUTABLE} -print-file-name=libstdc++.a OUTPUT_VARIABLE LIBSTDCCXX_LOCATION)
552string(STRIP ${LIBSTDCCXX_LOCATION} LIBSTDCCXX_LOCATION)
553set_target_properties(libstdc++ PROPERTIES IMPORTED_LOCATION ${LIBSTDCCXX_LOCATION})
554# libstdc++ requires libsupc++ and mingwex provided by GCC
555target_link_libraries(libstdc++ INTERFACE libsupc++ libmingwex)
556# this is for our SAL annotations
557target_compile_definitions(libstdc++ INTERFACE "$<$<COMPILE_LANGUAGE:CXX>:PAL_STDCPP_COMPAT>")
558
559# Create our alias libraries
560add_library(cppstl ALIAS libstdc++)
561add_library(cpprt ALIAS libsupc++)
562
563