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