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