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