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