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