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