xref: /reactos/sdk/cmake/gcc.cmake (revision 1ac9e484)
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_flags(${MODULE} "-fstack-protector-all")
39endif()
40
41# Compiler Core
42add_compile_flags("-pipe -fms-extensions -fno-strict-aliasing")
43
44# Prevent GCC from searching any of the default directories
45add_compile_flags("-nostdinc")
46
47if(GCC_VERSION VERSION_GREATER 4.7)
48    add_compile_flags("-mstackrealign")
49endif()
50if(NOT GCC_VERSION VERSION_LESS 4.8)
51    add_compile_flags("-fno-aggressive-loop-optimizations")
52endif()
53
54if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
55    add_compile_flags_language("-std=gnu89 -Wno-microsoft" "C")
56    set(CMAKE_LINK_DEF_FILE_FLAG "")
57    set(CMAKE_STATIC_LIBRARY_SUFFIX ".a")
58    set(CMAKE_LINK_LIBRARY_SUFFIX "")
59    set(CMAKE_CREATE_WIN32_EXE "")
60    set(CMAKE_C_COMPILE_OPTIONS_PIC "")
61    set(CMAKE_CXX_COMPILE_OPTIONS_PIC "")
62    set(CMAKE_C_COMPILE_OPTIONS_PIE "")
63    set(CMAKE_CXX_COMPILE_OPTIONS_PIE "")
64    set(CMAKE_ASM_FLAGS_DEBUG "")
65    set(CMAKE_C_FLAGS_DEBUG "")
66    set(CMAKE_CXX_FLAGS_DEBUG "")
67endif()
68
69if(DBG)
70    if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
71        add_compile_flags_language("-Wold-style-declaration" "C")
72    endif()
73    add_compile_flags_language("-Wdeclaration-after-statement" "C")
74endif()
75
76add_compile_flags_language("-fno-rtti -fno-exceptions" "CXX")
77
78#bug
79#file(TO_NATIVE_PATH ${REACTOS_SOURCE_DIR} REACTOS_SOURCE_DIR_NATIVE)
80#workaround
81set(REACTOS_SOURCE_DIR_NATIVE ${REACTOS_SOURCE_DIR})
82 if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
83string(REPLACE "/" "\\" REACTOS_SOURCE_DIR_NATIVE ${REACTOS_SOURCE_DIR})
84endif()
85
86if((NOT CMAKE_C_COMPILER_ID STREQUAL "Clang") AND (NOT SEPARATE_DBG))
87    add_compile_flags("-fdebug-prefix-map=\"${REACTOS_SOURCE_DIR_NATIVE}\"=ReactOS")
88endif()
89
90# Debugging
91if(NOT CMAKE_BUILD_TYPE STREQUAL "Release")
92    if(SEPARATE_DBG)
93        add_compile_flags("-gdwarf-2 -ggdb")
94    else()
95        add_compile_flags("-gdwarf-2 -gstrict-dwarf")
96        if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
97            add_compile_flags("-femit-struct-debug-detailed=none -feliminate-unused-debug-symbols")
98        endif()
99    endif()
100endif()
101
102# Tuning
103if(ARCH STREQUAL "i386")
104    add_compile_flags("-march=${OARCH} -mtune=${TUNE}")
105else()
106    add_compile_flags("-march=${OARCH}")
107endif()
108
109# Warnings, errors
110if((NOT CMAKE_BUILD_TYPE STREQUAL "Release") AND (NOT CMAKE_C_COMPILER_ID STREQUAL "Clang"))
111    add_compile_flags("-Werror")
112endif()
113
114add_compile_flags("-Wall -Wpointer-arith")
115add_compile_flags("-Wno-char-subscripts -Wno-multichar -Wno-unused-value")
116add_compile_flags("-Wno-unused-const-variable")
117
118if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
119    add_compile_flags("-Wno-maybe-uninitialized")
120endif()
121
122if(ARCH STREQUAL "amd64")
123    add_compile_flags("-Wno-format")
124elseif(ARCH STREQUAL "arm")
125    add_compile_flags("-Wno-attributes")
126endif()
127
128# Optimizations
129# FIXME: Revisit this to see if we even need these levels
130if(CMAKE_BUILD_TYPE STREQUAL "Release")
131    add_compile_flags("-O2 -DNDEBUG")
132else()
133    if(OPTIMIZE STREQUAL "1")
134        add_compile_flags("-Os -ftracer")
135    elseif(OPTIMIZE STREQUAL "2")
136        add_compile_flags("-Os")
137    elseif(OPTIMIZE STREQUAL "3")
138        add_compile_flags("-Og")
139    elseif(OPTIMIZE STREQUAL "4")
140        add_compile_flags("-O1")
141    elseif(OPTIMIZE STREQUAL "5")
142        add_compile_flags("-O2")
143    elseif(OPTIMIZE STREQUAL "6")
144        add_compile_flags("-O3")
145    elseif(OPTIMIZE STREQUAL "7")
146        add_compile_flags("-Ofast")
147    endif()
148endif()
149
150# Link-time code generation
151if(LTCG)
152    add_compile_flags("-flto -fno-fat-lto-objects")
153endif()
154
155if(ARCH STREQUAL "i386")
156    add_compile_flags("-fno-optimize-sibling-calls -fno-omit-frame-pointer")
157    if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
158        add_compile_flags("-mpreferred-stack-boundary=3 -fno-set-stack-executable")
159    endif()
160    # FIXME: this doesn't work. CMAKE_BUILD_TYPE is always "Debug"
161    if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
162        add_compile_flags("-momit-leaf-frame-pointer")
163    endif()
164elseif(ARCH STREQUAL "amd64")
165    add_compile_flags("-mpreferred-stack-boundary=4")
166endif()
167
168# Other
169if(ARCH STREQUAL "amd64")
170    add_definitions(-U_X86_ -UWIN32)
171elseif(ARCH STREQUAL "arm")
172    add_definitions(-U_UNICODE -UUNICODE)
173    add_definitions(-D__MSVCRT__) # DUBIOUS
174endif()
175
176add_definitions(-D_inline=__inline)
177
178# Alternative arch name
179if(ARCH STREQUAL "amd64")
180    set(ARCH2 x86_64)
181else()
182    set(ARCH2 ${ARCH})
183endif()
184
185if(SEPARATE_DBG)
186    # PDB style debug puts all dwarf debug info in a separate dbg file
187    message(STATUS "Building separate debug symbols")
188    file(MAKE_DIRECTORY ${REACTOS_BINARY_DIR}/symbols)
189    if(CMAKE_GENERATOR STREQUAL "Ninja")
190        set(CMAKE_DEBUG_SYMBOL_SUFFIX "")
191        set(SYMBOL_FILE <TARGET_PDB>)
192    else()
193        set(SYMBOL_FILE <TARGET>)
194    endif()
195    set(OBJCOPY ${CMAKE_OBJCOPY})
196    set(CMAKE_C_LINK_EXECUTABLE
197        "<CMAKE_C_COMPILER> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
198        "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
199        "${OBJCOPY} --strip-debug <TARGET>")
200    set(CMAKE_CXX_LINK_EXECUTABLE
201        "<CMAKE_CXX_COMPILER> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
202        "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
203        "${OBJCOPY} --strip-debug <TARGET>")
204    set(CMAKE_C_CREATE_SHARED_LIBRARY
205        "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
206        "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
207        "${OBJCOPY} --strip-debug <TARGET>")
208    set(CMAKE_CXX_CREATE_SHARED_LIBRARY
209        "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
210        "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
211        "${OBJCOPY} --strip-debug <TARGET>")
212    set(CMAKE_RC_CREATE_SHARED_LIBRARY
213        "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
214        "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
215        "${OBJCOPY} --strip-debug <TARGET>")
216elseif(NO_ROSSYM)
217    # Dwarf-based build
218    message(STATUS "Generating a dwarf-based build (no rsym)")
219    set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
220    set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
221    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>")
222    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>")
223    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>")
224else()
225    # Normal rsym build
226    if(NEW_STYLE_BUILD)
227        string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
228        get_target_property(RSYM native-rsym IMPORTED_LOCATION_${_build_type})
229    else()
230        get_target_property(RSYM native-rsym IMPORTED_LOCATION_NOCONFIG)
231    endif()
232
233    set(CMAKE_C_LINK_EXECUTABLE
234        "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
235        "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
236    set(CMAKE_CXX_LINK_EXECUTABLE
237        "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
238        "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
239    set(CMAKE_C_CREATE_SHARED_LIBRARY
240        "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
241        "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
242    set(CMAKE_CXX_CREATE_SHARED_LIBRARY
243        "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
244        "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
245    set(CMAKE_RC_CREATE_SHARED_LIBRARY
246        "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
247endif()
248
249set(CMAKE_C_CREATE_SHARED_MODULE ${CMAKE_C_CREATE_SHARED_LIBRARY})
250set(CMAKE_CXX_CREATE_SHARED_MODULE ${CMAKE_CXX_CREATE_SHARED_LIBRARY})
251set(CMAKE_RC_CREATE_SHARED_MODULE ${CMAKE_RC_CREATE_SHARED_LIBRARY})
252
253set(CMAKE_EXE_LINKER_FLAGS "-nostdlib -Wl,--enable-auto-image-base,--disable-auto-import,--disable-stdcall-fixup")
254set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
255set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
256
257if((CMAKE_C_COMPILER_ID STREQUAL "GNU") AND
258   (NOT CMAKE_BUILD_TYPE STREQUAL "Release") AND
259   (NOT CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0))
260    # FIXME: Set this once Clang toolchain works with it
261    set(_compress_debug_sections_flag "-Wa,--compress-debug-sections")
262endif()
263
264if(CMAKE_VERSION VERSION_LESS 3.4.0)
265    set(CMAKE_C_COMPILE_OBJECT "${CCACHE} <CMAKE_C_COMPILER> <DEFINES> ${_compress_debug_sections_flag} <FLAGS> -o <OBJECT> -c <SOURCE>")
266    # FIXME: Once the GCC toolchain bugs are fixed, add _compress_debug_sections_flag to CXX too
267    set(CMAKE_CXX_COMPILE_OBJECT "${CCACHE} <CMAKE_CXX_COMPILER> <DEFINES> <FLAGS> -o <OBJECT> -c <SOURCE>")
268    set(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 <FLAGS> <DEFINES> -D__ASM__ -c <SOURCE>")
269
270    set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -O coff <FLAGS> -DRC_INVOKED -D__WIN32__=1 -D__FLAT__=1 ${I18N_DEFS} <DEFINES> <SOURCE> <OBJECT>")
271else()
272    set(CMAKE_C_COMPILE_OBJECT "${CCACHE} <CMAKE_C_COMPILER> <DEFINES> ${_compress_debug_sections_flag} <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
273    # FIXME: Once the GCC toolchain bugs are fixed, add _compress_debug_sections_flag to CXX too
274    set(CMAKE_CXX_COMPILE_OBJECT "${CCACHE} <CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
275    set(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>")
276
277    set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -O coff <INCLUDES> <FLAGS> -DRC_INVOKED -D__WIN32__=1 -D__FLAT__=1 ${I18N_DEFS} <DEFINES> <SOURCE> <OBJECT>")
278endif()
279set(CMAKE_DEPFILE_FLAGS_RC "--preprocessor \"${MINGW_TOOLCHAIN_PREFIX}gcc${MINGW_TOOLCHAIN_SUFFIX} -E -xc-header -MMD -MF <DEPFILE> -MT <OBJECT>\" ")
280
281# Optional 3rd parameter: stdcall stack bytes
282function(set_entrypoint MODULE ENTRYPOINT)
283    if(${ENTRYPOINT} STREQUAL "0")
284        add_target_link_flags(${MODULE} "-Wl,-entry,0")
285    elseif(ARCH STREQUAL "i386")
286        set(_entrysymbol _${ENTRYPOINT})
287        if(${ARGC} GREATER 2)
288            set(_entrysymbol ${_entrysymbol}@${ARGV2})
289        endif()
290        add_target_link_flags(${MODULE} "-Wl,-entry,${_entrysymbol}")
291    else()
292        add_target_link_flags(${MODULE} "-Wl,-entry,${ENTRYPOINT}")
293    endif()
294endfunction()
295
296function(set_subsystem MODULE SUBSYSTEM)
297    add_target_link_flags(${MODULE} "-Wl,--subsystem,${SUBSYSTEM}:5.01")
298endfunction()
299
300function(set_image_base MODULE IMAGE_BASE)
301    add_target_link_flags(${MODULE} "-Wl,--image-base,${IMAGE_BASE}")
302endfunction()
303
304function(set_module_type_toolchain MODULE TYPE)
305    if(CPP_USE_STL)
306        if((${TYPE} STREQUAL "kernelmodedriver") OR (${TYPE} STREQUAL "wdmdriver"))
307            message(FATAL_ERROR "Use of STL in kernelmodedriver or wdmdriver type module prohibited")
308        endif()
309        target_link_libraries(${MODULE} -lstdc++ -lsupc++ -lgcc -lmingwex)
310    elseif(CPP_USE_RT)
311        target_link_libraries(${MODULE} -lsupc++ -lgcc)
312    elseif(IS_CPP)
313        target_link_libraries(${MODULE} -lgcc)
314    endif()
315
316    if((${TYPE} STREQUAL "kernelmodedriver") OR (${TYPE} STREQUAL "wdmdriver"))
317        add_target_link_flags(${MODULE} "-Wl,--exclude-all-symbols,-file-alignment=0x1000,-section-alignment=0x1000")
318        if(${TYPE} STREQUAL "wdmdriver")
319            add_target_link_flags(${MODULE} "-Wl,--wdmdriver")
320        endif()
321    endif()
322
323    if(STACK_PROTECTOR)
324        target_link_libraries(${MODULE} gcc_ssp)
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(generate_import_lib _libname _dllname _spec_file)
345    # Generate the def for the import lib
346    add_custom_command(
347        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def
348        COMMAND native-spec2def -n=${_dllname} -a=${ARCH2} ${ARGN} --implib -d=${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
349        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
350    set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def PROPERTIES EXTERNAL_OBJECT TRUE)
351
352    # Create normal importlib
353    _add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def)
354    set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "IMPLIB" PREFIX "")
355
356    # Create delayed importlib
357    _add_library(${_libname}_delayed STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def)
358    set_target_properties(${_libname}_delayed PROPERTIES LINKER_LANGUAGE "IMPLIB_DELAYED" PREFIX "")
359endfunction()
360
361# Cute little hack to produce import libs
362set(CMAKE_IMPLIB_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def <OBJECTS> --kill-at --output-lib=<TARGET>")
363set(CMAKE_IMPLIB_DELAYED_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def <OBJECTS> --kill-at --output-delaylib=<TARGET>")
364function(spec2def _dllname _spec_file)
365
366    cmake_parse_arguments(__spec2def "ADD_IMPORTLIB;NO_PRIVATE_WARNINGS;WITH_RELAY" "VERSION" "" ${ARGN})
367
368    # Get library basename
369    get_filename_component(_file ${_dllname} NAME_WE)
370
371    # Error out on anything else than spec
372    if(NOT ${_spec_file} MATCHES ".*\\.spec")
373        message(FATAL_ERROR "spec2def only takes spec files as input.")
374    endif()
375
376    if (__spec2def_WITH_RELAY)
377        set(__with_relay_arg "--with-tracing")
378    endif()
379
380    if(__spec2def_VERSION)
381        set(__version_arg "--version=0x${__spec2def_VERSION}")
382    endif()
383
384    # Generate exports def and C stubs file for the DLL
385    add_custom_command(
386        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
387        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}
388        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
389
390    if(__spec2def_ADD_IMPORTLIB)
391        set(_extraflags)
392        if(__spec2def_NO_PRIVATE_WARNINGS)
393            set(_extraflags --no-private-warnings)
394        endif()
395
396        generate_import_lib(lib${_file} ${_dllname} ${_spec_file} ${_extraflags})
397    endif()
398endfunction()
399
400macro(macro_mc FLAG FILE)
401    set(COMMAND_MC ${CMAKE_MC_COMPILER} -u ${FLAG} -b -h ${CMAKE_CURRENT_BINARY_DIR}/ -r ${CMAKE_CURRENT_BINARY_DIR}/ ${FILE})
402endmacro()
403
404# PSEH lib, needed with mingw
405set(PSEH_LIB "pseh")
406
407# Macros
408if(PCH AND (NOT ENABLE_CCACHE) AND (NOT CMAKE_HOST_APPLE))
409    add_compile_flags("-Winvalid-pch -Werror=invalid-pch")
410    macro(add_pch _target _pch _sources)
411        # When including x.h GCC looks for x.h.gch first
412        set(_pch_final_name "${_target}_pch.h")
413        set(_gch ${CMAKE_CURRENT_BINARY_DIR}/${_pch_final_name}.gch)
414
415        if(IS_CPP)
416            set(_pch_language CXX)
417        else()
418            set(_pch_language C)
419        endif()
420
421        # Build the precompiled header
422        # HEADER_FILE_ONLY FALSE: force compiling the header
423        # EXTERNAL_SOURCE TRUE: don't use the .gch file when linking
424        set_source_files_properties(${_pch} PROPERTIES
425            HEADER_FILE_ONLY FALSE
426            LANGUAGE ${_pch_language}
427            EXTERNAL_SOURCE TRUE
428            OBJECT_LOCATION ${_gch})
429
430        # Include the gch in the specified source files, skipping the pch file itself
431        list(REMOVE_ITEM ${_sources} ${_pch})
432        foreach(_src ${${_sources}})
433            set_property(SOURCE ${_src} APPEND_STRING PROPERTY COMPILE_FLAGS " ${_ccache_flag} -include ${CMAKE_CURRENT_BINARY_DIR}/${_pch_final_name}")
434            set_property(SOURCE ${_src} APPEND PROPERTY OBJECT_DEPENDS ${_gch})
435        endforeach()
436    endmacro()
437else()
438    macro(add_pch _target _pch _sources)
439    endmacro()
440endif()
441
442function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
443    set(_object_file ${_binary_file}.o)
444
445    add_custom_command(
446        OUTPUT ${_object_file}
447        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 -I${REACTOS_SOURCE_DIR}/boot/freeldr -D__ASM__ -c ${_asm_file}
448        DEPENDS ${_asm_file})
449
450    add_custom_command(
451        OUTPUT ${_binary_file}
452        COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
453        # COMMAND objcopy --output-target binary --image-base 0x${_base_address} ${_object_file} ${_binary_file}
454        DEPENDS ${_object_file} native-obj2bin)
455
456    set_source_files_properties(${_object_file} ${_binary_file} PROPERTIES GENERATED TRUE)
457
458    add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
459endfunction()
460
461function(allow_warnings __module)
462    # We don't allow warnings in trunk, this needs to be reworked. See CORE-6959.
463    #add_target_compile_flags(${__module} "-Wno-error")
464endfunction()
465
466macro(add_asm_files _target)
467    list(APPEND ${_target} ${ARGN})
468endmacro()
469
470function(add_linker_script _target _linker_script_file)
471    get_filename_component(_file_full_path ${_linker_script_file} ABSOLUTE)
472    add_target_link_flags(${_target} "-Wl,-T,${_file_full_path}")
473    add_target_property(${_target} LINK_DEPENDS ${_file_full_path})
474endfunction()
475