[CMAKE]
[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 # 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("-gstabs+")
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=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 -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 "${RSYM} <TARGET> <TARGET>")
159 endif()
160
161 set(CMAKE_EXE_LINKER_FLAGS "-nostdlib -Wl,--enable-auto-image-base,--disable-auto-import,--disable-stdcall-fixup")
162 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
163
164 SET(CMAKE_C_COMPILE_OBJECT "${CCACHE} <CMAKE_C_COMPILER> <DEFINES> <FLAGS> -o <OBJECT> -c <SOURCE>")
165 SET(CMAKE_CXX_COMPILE_OBJECT "${CCACHE} <CMAKE_CXX_COMPILER> <DEFINES> <FLAGS> -o <OBJECT> -c <SOURCE>")
166 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>")
167
168 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -O coff <FLAGS> -DRC_INVOKED -D__WIN32__=1 -D__FLAT__=1 ${I18N_DEFS} <DEFINES> <SOURCE> <OBJECT>")
169 set(CMAKE_DEPFILE_FLAGS_RC "--preprocessor \"${MINGW_TOOLCHAIN_PREFIX}gcc${MINGW_TOOLCHAIN_SUFFIX} -E -xc-header -MMD -MF <DEPFILE> -MT <OBJECT>\" ")
170
171 # Optional 3rd parameter: stdcall stack bytes
172 function(set_entrypoint MODULE ENTRYPOINT)
173 if(${ENTRYPOINT} STREQUAL "0")
174 add_target_link_flags(${MODULE} "-Wl,-entry,0")
175 elseif(ARCH STREQUAL "i386")
176 set(_entrysymbol _${ENTRYPOINT})
177 if(${ARGC} GREATER 2)
178 set(_entrysymbol ${_entrysymbol}@${ARGV2})
179 endif()
180 add_target_link_flags(${MODULE} "-Wl,-entry,${_entrysymbol}")
181 else()
182 add_target_link_flags(${MODULE} "-Wl,-entry,${ENTRYPOINT}")
183 endif()
184 endfunction()
185
186 function(set_subsystem MODULE SUBSYSTEM)
187 add_target_link_flags(${MODULE} "-Wl,--subsystem,${SUBSYSTEM}")
188 endfunction()
189
190 function(set_image_base MODULE IMAGE_BASE)
191 add_target_link_flags(${MODULE} "-Wl,--image-base,${IMAGE_BASE}")
192 endfunction()
193
194 function(set_module_type_toolchain MODULE TYPE)
195 if(CPP_USE_STL)
196 if((${TYPE} STREQUAL "kernelmodedriver") OR (${TYPE} STREQUAL "wdmdriver"))
197 message(FATAL_ERROR "Use of STL in kernelmodedriver or wdmdriver type module prohibited")
198 endif()
199 target_link_libraries(${MODULE} -lstdc++ -lsupc++ -lgcc -lmingwex)
200 elseif(CPP_USE_RT)
201 target_link_libraries(${MODULE} -lsupc++ -lgcc)
202 elseif(IS_CPP)
203 target_link_libraries(${MODULE} -lgcc)
204 endif()
205
206 if((${TYPE} STREQUAL "kernelmodedriver") OR (${TYPE} STREQUAL "wdmdriver"))
207 add_target_link_flags(${MODULE} "-Wl,--exclude-all-symbols,-file-alignment=0x1000,-section-alignment=0x1000")
208 if(${TYPE} STREQUAL "wdmdriver")
209 add_target_link_flags(${MODULE} "-Wl,--wdmdriver")
210 endif()
211 endif()
212 endfunction()
213
214 function(add_delay_importlibs _module)
215 get_target_property(_module_type ${_module} TYPE)
216 if(_module_type STREQUAL "STATIC_LIBRARY")
217 message(FATAL_ERROR "Cannot add delay imports to a static library")
218 endif()
219 foreach(_lib ${ARGN})
220 target_link_libraries(${_module} lib${_lib}_delayed)
221 endforeach()
222 target_link_libraries(${_module} delayimp)
223 endfunction()
224
225 if(NOT ARCH STREQUAL "i386")
226 set(DECO_OPTION "-@")
227 endif()
228
229 function(generate_import_lib _libname _dllname _spec_file)
230 # generate the def for the import lib
231 add_custom_command(
232 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def
233 COMMAND native-spec2def -n=${_dllname} -a=${ARCH2} -d=${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
234 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
235 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def PROPERTIES EXTERNAL_OBJECT TRUE)
236
237 #create normal importlib
238 _add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def)
239 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "IMPLIB" PREFIX "")
240
241 #create delayed importlib
242 _add_library(${_libname}_delayed STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def)
243 set_target_properties(${_libname}_delayed PROPERTIES LINKER_LANGUAGE "IMPLIB_DELAYED" PREFIX "")
244 endfunction()
245
246 # Cute little hack to produce import libs
247 set(CMAKE_IMPLIB_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def <OBJECTS> --kill-at --output-lib=<TARGET>")
248 set(CMAKE_IMPLIB_DELAYED_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def <OBJECTS> --kill-at --output-delaylib=<TARGET>")
249 function(spec2def _dllname _spec_file)
250 # do we also want to add importlib targets?
251 if(${ARGC} GREATER 2)
252 if(${ARGN} STREQUAL "ADD_IMPORTLIB")
253 set(__add_importlib TRUE)
254 else()
255 message(FATAL_ERROR "Wrong argument passed to spec2def, ${ARGN}")
256 endif()
257 endif()
258
259 # get library basename
260 get_filename_component(_file ${_dllname} NAME_WE)
261
262 # error out on anything else than spec
263 if(NOT ${_spec_file} MATCHES ".*\\.spec")
264 message(FATAL_ERROR "spec2def only takes spec files as input.")
265 endif()
266
267 # generate exports def and stubs C file for the module
268 add_custom_command(
269 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
270 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}
271 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
272
273 if(__add_importlib)
274 generate_import_lib(lib${_file} ${_dllname} ${_spec_file})
275 endif()
276 endfunction()
277
278 macro(macro_mc FLAG FILE)
279 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)
280 endmacro()
281
282 #pseh lib, needed with mingw
283 set(PSEH_LIB "pseh")
284
285 # Macros
286 if(PCH)
287 macro(_PCH_GET_COMPILE_FLAGS _target_name _out_compile_flags _header_filename)
288 # Add the precompiled header to the build
289 get_filename_component(_FILE ${_header_filename} NAME)
290 set(_gch_filename "${_FILE}.gch")
291 list(APPEND ${_out_compile_flags} -c ${_header_filename} -o ${_gch_filename})
292
293 # This gets us our includes
294 get_directory_property(DIRINC INCLUDE_DIRECTORIES)
295 foreach(item ${DIRINC})
296 list(APPEND ${_out_compile_flags} -I${item})
297 endforeach()
298
299 # This our definitions
300 get_directory_property(_compiler_flags DEFINITIONS)
301 list(APPEND ${_out_compile_flags} ${_compiler_flags})
302
303 # This gets any specific definitions that were added with set-target-property
304 get_target_property(_target_defs ${_target_name} COMPILE_DEFINITIONS)
305 if(_target_defs)
306 foreach(item ${_target_defs})
307 list(APPEND ${_out_compile_flags} -D${item})
308 endforeach()
309 endif()
310
311 if(IS_CPP)
312 list(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS})
313 else()
314 list(APPEND ${_out_compile_flags} ${CMAKE_C_FLAGS})
315 endif()
316
317 separate_arguments(${_out_compile_flags})
318 endmacro()
319
320 macro(add_pch _target_name _FILE)
321 set(_header_filename ${CMAKE_CURRENT_SOURCE_DIR}/${_FILE})
322 get_filename_component(_basename ${_FILE} NAME)
323 set(_gch_filename ${_basename}.gch)
324 _PCH_GET_COMPILE_FLAGS(${_target_name} _args ${_header_filename})
325
326 if(IS_CPP)
327 set(__lang CXX)
328 set(__compiler ${CCACHE} ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1})
329 else()
330 set(__lang C)
331 set(__compiler ${CCACHE} ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1})
332 endif()
333
334 add_custom_command(OUTPUT ${_gch_filename}
335 COMMAND ${__compiler} ${_args}
336 IMPLICIT_DEPENDS ${__lang} ${_header_filename}
337 DEPENDS ${_header_filename} ${ARGN})
338 get_target_property(_src_files ${_target_name} SOURCES)
339 add_target_compile_flags(${_target_name} "-fpch-preprocess -Winvalid-pch -Wno-error=invalid-pch")
340 foreach(_item in ${_src_files})
341 get_source_file_property(__src_lang ${_item} LANGUAGE)
342 if(__src_lang STREQUAL __lang)
343 set_source_files_properties(${_item} PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_gch_filename})
344 endif()
345 endforeach()
346 #set dependency checking : depends on precompiled header only which already depends on deeper header
347 set_target_properties(${_target_name} PROPERTIES IMPLICIT_DEPENDS_INCLUDE_TRANSFORM "\"${_basename}\"=;<${_basename}>=")
348 endmacro()
349 else()
350 macro(add_pch _target_name _FILE)
351 endmacro()
352 endif()
353
354 function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
355 set(_object_file ${_binary_file}.o)
356
357 add_custom_command(
358 OUTPUT ${_object_file}
359 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}
360 DEPENDS ${_asm_file})
361
362 add_custom_command(
363 OUTPUT ${_binary_file}
364 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
365 # COMMAND objcopy --output-target binary --image-base 0x${_base_address} ${_object_file} ${_binary_file}
366 DEPENDS ${_object_file} native-obj2bin)
367
368 set_source_files_properties(${_object_file} ${_binary_file} PROPERTIES GENERATED TRUE)
369
370 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
371
372 endfunction()
373
374 function(allow_warnings __module)
375 add_target_compile_flags(${__module} "-Wno-error")
376 endfunction()
377
378 macro(add_asm_files _target)
379 list(APPEND ${_target} ${ARGN})
380 endmacro()
381