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