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