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