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