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