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