xref: /reactos/sdk/cmake/gcc.cmake (revision dad056e0)
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# A long double is 64 bits
46add_compile_options(-mlong-double-64)
47
48# Prevent GCC from searching any of the default directories.
49# The case for C++ is handled through the reactos_c++ INTERFACE library
50add_compile_options("$<$<NOT:$<COMPILE_LANGUAGE:CXX>>:-nostdinc>")
51
52if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
53    add_compile_options(-fno-aggressive-loop-optimizations)
54    if (DBG)
55        add_compile_options("$<$<COMPILE_LANGUAGE:C>:-Wold-style-declaration>")
56    endif()
57elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
58    add_compile_options("$<$<COMPILE_LANGUAGE:C>:-Wno-microsoft>")
59    add_compile_options(-Wno-pragma-pack)
60    add_compile_options(-fno-associative-math)
61
62    if(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 12.0)
63        # disable "libcall optimization"
64        # see https://mudongliang.github.io/2020/12/02/undefined-reference-to-stpcpy.html
65        add_compile_options(-fno-builtin-stpcpy)
66    endif()
67
68    set(CMAKE_LINK_DEF_FILE_FLAG "")
69    set(CMAKE_STATIC_LIBRARY_SUFFIX ".a")
70    set(CMAKE_LINK_LIBRARY_SUFFIX "")
71    set(CMAKE_CREATE_WIN32_EXE "")
72    set(CMAKE_C_COMPILE_OPTIONS_PIC "")
73    set(CMAKE_CXX_COMPILE_OPTIONS_PIC "")
74    set(CMAKE_C_COMPILE_OPTIONS_PIE "")
75    set(CMAKE_CXX_COMPILE_OPTIONS_PIE "")
76    set(CMAKE_ASM_FLAGS_DEBUG "")
77    set(CMAKE_C_FLAGS_DEBUG "")
78    set(CMAKE_CXX_FLAGS_DEBUG "")
79endif()
80
81# Debugging
82if(NOT CMAKE_BUILD_TYPE STREQUAL "Release")
83    if(SEPARATE_DBG)
84        add_compile_options(-gdwarf-2 -ggdb)
85    else()
86        add_compile_options(-gdwarf-2 -gstrict-dwarf)
87        if(NOT CMAKE_C_COMPILER_ID STREQUAL Clang)
88            add_compile_options(-femit-struct-debug-detailed=none -feliminate-unused-debug-symbols)
89        endif()
90    endif()
91endif()
92
93# Tuning
94add_compile_options(-march=${OARCH} -mtune=${TUNE})
95
96# Warnings, errors
97if((NOT CMAKE_BUILD_TYPE STREQUAL "Release") AND (NOT CMAKE_C_COMPILER_ID STREQUAL Clang))
98    add_compile_options(-Werror)
99endif()
100
101add_compile_options(-Wall -Wpointer-arith)
102add_compile_options(-Wno-char-subscripts -Wno-multichar -Wno-unused-value)
103add_compile_options(-Wno-unused-const-variable)
104add_compile_options(-Wno-unused-local-typedefs)
105add_compile_options(-Wno-deprecated)
106add_compile_options(-Wno-unused-result) # FIXME To be removed when CORE-17637 is resolved
107
108if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
109    add_compile_options(-Wno-maybe-uninitialized)
110endif()
111
112if(ARCH STREQUAL "amd64")
113    add_compile_options(-Wno-format)
114elseif(ARCH STREQUAL "arm")
115    add_compile_options(-Wno-attributes)
116endif()
117
118# Optimizations
119# FIXME: Revisit this to see if we even need these levels
120if(CMAKE_BUILD_TYPE STREQUAL "Release")
121    add_compile_options(-O2 -DNDEBUG)
122else()
123    if(OPTIMIZE STREQUAL "1")
124        add_compile_options(-Os)
125        if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
126            add_compile_options(-ftracer)
127        endif()
128    elseif(OPTIMIZE STREQUAL "2")
129        add_compile_options(-Os)
130    elseif(OPTIMIZE STREQUAL "3")
131        add_compile_options(-Og)
132    elseif(OPTIMIZE STREQUAL "4")
133        add_compile_options(-O1)
134    elseif(OPTIMIZE STREQUAL "5")
135        add_compile_options(-O2)
136    elseif(OPTIMIZE STREQUAL "6")
137        add_compile_options(-O3)
138    elseif(OPTIMIZE STREQUAL "7")
139        add_compile_options(-Ofast)
140    endif()
141endif()
142
143# Link-time code generation
144if(LTCG)
145    add_compile_options(-flto -fno-fat-lto-objects)
146endif()
147
148if(ARCH STREQUAL "i386")
149    add_compile_options(-fno-optimize-sibling-calls -fno-omit-frame-pointer -mstackrealign)
150    if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
151        add_compile_options(-mpreferred-stack-boundary=3 -fno-set-stack-executable)
152    endif()
153    # FIXME: this doesn't work. CMAKE_BUILD_TYPE is always "Debug"
154    if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
155        add_compile_options(-momit-leaf-frame-pointer)
156    endif()
157elseif(ARCH STREQUAL "amd64")
158    if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
159        add_compile_options(-mpreferred-stack-boundary=4)
160    endif()
161    add_compile_options(-Wno-error)
162endif()
163
164# Other
165if(ARCH STREQUAL "amd64")
166    add_compile_options(-mcx16) # Generate CMPXCHG16
167    add_definitions(-U_X86_ -UWIN32)
168elseif(ARCH STREQUAL "arm")
169    add_definitions(-U_UNICODE -UUNICODE)
170    add_definitions(-D__MSVCRT__) # DUBIOUS
171endif()
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 __version_arg)
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 ${__version_arg} -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    else()
422        set(__version_arg "--version=${DLL_EXPORT_VERSION}")
423    endif()
424
425    # Generate exports def and C stubs file for the DLL
426    add_custom_command(
427        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
428        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}
429        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
430
431    # Do not use precompiled headers for the stub file
432    set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
433
434    if(__spec2def_ADD_IMPORTLIB)
435        set(_extraflags)
436        if(__spec2def_NO_PRIVATE_WARNINGS)
437            set(_extraflags --no-private-warnings)
438        endif()
439
440        generate_import_lib(lib${_file} ${_dllname} ${_spec_file} ${_extraflags} "${__version_arg}")
441    endif()
442endfunction()
443
444macro(macro_mc FLAG FILE)
445    set(COMMAND_MC ${CMAKE_MC_COMPILER} -u ${FLAG} -b -h ${CMAKE_CURRENT_BINARY_DIR}/ -r ${CMAKE_CURRENT_BINARY_DIR}/ ${FILE})
446endmacro()
447
448# PSEH lib, needed with mingw
449set(PSEH_LIB "pseh")
450
451function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
452    set(_object_file ${_binary_file}.o)
453
454    get_defines(_defines)
455    get_includes(_includes)
456
457    add_custom_command(
458        OUTPUT ${_object_file}
459        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}
460        DEPENDS ${_asm_file})
461
462    add_custom_command(
463        OUTPUT ${_binary_file}
464        COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
465        # COMMAND objcopy --output-target binary --image-base 0x${_base_address} ${_object_file} ${_binary_file}
466        DEPENDS ${_object_file} native-obj2bin)
467
468    set_source_files_properties(${_object_file} ${_binary_file} PROPERTIES GENERATED TRUE)
469
470    add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
471endfunction()
472
473function(allow_warnings __module)
474    # We don't allow warnings in trunk, this needs to be reworked. See CORE-6959.
475    #target_compile_options(${__module} PRIVATE "-Wno-error")
476endfunction()
477
478function(convert_asm_file _source_file _target_file)
479    get_filename_component(_source_file_base_name ${_source_file} NAME_WE)
480    get_filename_component(_source_file_full_path ${_source_file} ABSOLUTE)
481    set(_preprocessed_asm_file ${CMAKE_CURRENT_BINARY_DIR}/${_target_file})
482    add_custom_command(
483        OUTPUT ${_preprocessed_asm_file}
484        COMMAND native-asmpp ${_source_file_full_path} > ${_preprocessed_asm_file}
485        DEPENDS native-asmpp ${_source_file_full_path})
486
487endfunction()
488
489function(convert_asm_files)
490    foreach(_source_file ${ARGN})
491        convert_asm_file(${_source_file} ${_source_file}.s)
492    endforeach()
493endfunction()
494
495macro(add_asm_files _target)
496    foreach(_source_file ${ARGN})
497        get_filename_component(_extension ${_source_file} EXT)
498        get_filename_component(_source_file_base_name ${_source_file} NAME_WE)
499        if (${_extension} STREQUAL ".asm")
500            convert_asm_file(${_source_file} ${_source_file}.s)
501            list(APPEND ${_target} ${CMAKE_CURRENT_BINARY_DIR}/${_source_file}.s)
502        elseif (${_extension} STREQUAL ".inc")
503            convert_asm_file(${_source_file} ${_source_file}.h)
504            list(APPEND ${_target} ${CMAKE_CURRENT_BINARY_DIR}/${_source_file}.h)
505        else()
506            list(APPEND ${_target} ${_source_file})
507        endif()
508    endforeach()
509endmacro()
510
511function(add_linker_script _target _linker_script_file)
512    get_filename_component(_file_full_path ${_linker_script_file} ABSOLUTE)
513    target_link_options(${_target} PRIVATE "-Wl,-T,${_file_full_path}")
514    set_property(TARGET ${_target} APPEND PROPERTY LINK_DEPENDS ${_file_full_path})
515endfunction()
516
517# Manage our C++ options
518# we disable standard includes if we don't use the STL
519add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<NOT:$<IN_LIST:cppstl,$<TARGET_PROPERTY:LINK_LIBRARIES>>>>:-nostdinc>")
520# we disable RTTI, unless said so
521add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:$<IF:$<BOOL:$<TARGET_PROPERTY:WITH_CXX_RTTI>>,-frtti,-fno-rtti>>")
522# We disable exceptions, unless said so
523add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:$<IF:$<BOOL:$<TARGET_PROPERTY:WITH_CXX_EXCEPTIONS>>,-fexceptions,-fno-exceptions>>")
524
525# G++ shipped with ROSBE uses sjlj exceptions on i386. Tell Clang it is so
526if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND ARCH STREQUAL "i386")
527    add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<BOOL:$<TARGET_PROPERTY:WITH_CXX_EXCEPTIONS>>>:-fsjlj-exceptions>")
528endif()
529
530# Find default G++ libraries
531if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
532    set(GXX_EXECUTABLE ${CMAKE_CXX_COMPILER_TARGET}-g++)
533else()
534    set(GXX_EXECUTABLE ${CMAKE_CXX_COMPILER})
535endif()
536
537add_library(libgcc STATIC IMPORTED)
538execute_process(COMMAND ${GXX_EXECUTABLE} -print-file-name=libgcc.a OUTPUT_VARIABLE LIBGCC_LOCATION)
539string(STRIP ${LIBGCC_LOCATION} LIBGCC_LOCATION)
540set_target_properties(libgcc PROPERTIES IMPORTED_LOCATION ${LIBGCC_LOCATION})
541# libgcc needs kernel32 imports, a CRT and msvcrtex
542target_link_libraries(libgcc INTERFACE libkernel32 libmsvcrt msvcrtex)
543
544add_library(libsupc++ STATIC IMPORTED GLOBAL)
545execute_process(COMMAND ${GXX_EXECUTABLE} -print-file-name=libsupc++.a OUTPUT_VARIABLE LIBSUPCXX_LOCATION)
546string(STRIP ${LIBSUPCXX_LOCATION} LIBSUPCXX_LOCATION)
547set_target_properties(libsupc++ PROPERTIES IMPORTED_LOCATION ${LIBSUPCXX_LOCATION})
548# libsupc++ requires libgcc
549target_link_libraries(libsupc++ INTERFACE libgcc)
550
551add_library(libmingwex STATIC IMPORTED)
552execute_process(COMMAND ${GXX_EXECUTABLE} -print-file-name=libmingwex.a OUTPUT_VARIABLE LIBMINGWEX_LOCATION)
553string(STRIP ${LIBMINGWEX_LOCATION} LIBMINGWEX_LOCATION)
554set_target_properties(libmingwex PROPERTIES IMPORTED_LOCATION ${LIBMINGWEX_LOCATION})
555# libmingwex requires a CRT and imports from kernel32
556target_link_libraries(libmingwex INTERFACE libmsvcrt libkernel32)
557
558add_library(libstdc++ STATIC IMPORTED GLOBAL)
559execute_process(COMMAND ${GXX_EXECUTABLE} -print-file-name=libstdc++.a OUTPUT_VARIABLE LIBSTDCCXX_LOCATION)
560string(STRIP ${LIBSTDCCXX_LOCATION} LIBSTDCCXX_LOCATION)
561set_target_properties(libstdc++ PROPERTIES IMPORTED_LOCATION ${LIBSTDCCXX_LOCATION})
562# libstdc++ requires libsupc++ and mingwex provided by GCC
563target_link_libraries(libstdc++ INTERFACE libsupc++ libmingwex)
564# this is for our SAL annotations
565target_compile_definitions(libstdc++ INTERFACE "$<$<COMPILE_LANGUAGE:CXX>:PAL_STDCPP_COMPAT>")
566
567# Create our alias libraries
568add_library(cppstl ALIAS libstdc++)
569add_library(cpprt ALIAS libsupc++)
570
571