d68eba535c9f11a8c4bc2c6d8c068094d6a273a9
[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(SEPARATE_DBG)
13 file(MAKE_DIRECTORY ${REACTOS_BINARY_DIR}/symbols)
14 endif()
15
16 # Compiler Core
17 add_compile_flags("-pipe -fms-extensions")
18
19 #bug
20 #file(TO_NATIVE_PATH ${REACTOS_SOURCE_DIR} REACTOS_SOURCE_DIR_NATIVE)
21 #workaround
22 set(REACTOS_SOURCE_DIR_NATIVE ${REACTOS_SOURCE_DIR})
23 IF(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
24 STRING(REPLACE "/" "\\" REACTOS_SOURCE_DIR_NATIVE ${REACTOS_SOURCE_DIR})
25 endif()
26 add_compile_flags("-fdebug-prefix-map=${REACTOS_SOURCE_DIR_NATIVE}=ReactOS")
27
28 # Debugging
29 if(SEPARATE_DBG)
30 add_compile_flags("-gdwarf-2 -g2")
31 else()
32 add_compile_flags("-gstabs+")
33 endif()
34
35
36 # Do not allow warnings
37 add_compile_flags("-Werror")
38
39 # For some reason, cmake sets -fPIC, and we don't want it
40 if(DEFINED CMAKE_SHARED_LIBRARY_ASM_FLAGS)
41 string(REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_ASM_FLAGS ${CMAKE_SHARED_LIBRARY_ASM_FLAGS})
42 endif()
43
44 # Tuning
45 if(ARCH MATCHES i386)
46 add_compile_flags("-march=${OARCH} -mtune=${TUNE}")
47 else()
48 add_compile_flags("-march=${OARCH}")
49 endif()
50
51 # Warnings
52 add_compile_flags("-Wall -Wno-char-subscripts -Wpointer-arith -Wno-multichar -Wno-unused-value")
53
54 if(GCC_VERSION VERSION_LESS 4.6)
55 add_compile_flags("-Wno-error=uninitialized")
56 elseif((GCC_VERSION VERSION_GREATER 4.6 OR GCC_VERSION VERSION_EQUAL 4.6) AND GCC_VERSION VERSION_LESS 4.7)
57 add_compile_flags("-Wno-error=unused-but-set-variable -Wno-error=uninitialized")
58 elseif(GCC_VERSION VERSION_EQUAL 4.7 OR GCC_VERSION VERSION_GREATER 4.7)
59 add_compile_flags("-Wno-error=unused-but-set-variable -Wno-error=maybe-uninitialized -Wno-error=delete-non-virtual-dtor -Wno-error=narrowing")
60 endif()
61
62 if(ARCH MATCHES amd64)
63 add_compile_flags("-Wno-format")
64 elseif(ARCH MATCHES arm)
65 add_compile_flags("-Wno-attributes")
66 endif()
67
68 # Optimizations
69 if(OPTIMIZE STREQUAL "1")
70 add_compile_flags("-Os")
71 elseif(OPTIMIZE STREQUAL "2")
72 add_compile_flags("-Os")
73 elseif(OPTIMIZE STREQUAL "3")
74 add_compile_flags("-O1")
75 elseif(OPTIMIZE STREQUAL "4")
76 add_compile_flags("-O2")
77 elseif(OPTIMIZE STREQUAL "5")
78 add_compile_flags("-O3")
79 endif()
80
81 add_compile_flags("-fno-strict-aliasing")
82
83 if(ARCH MATCHES i386)
84 add_compile_flags("-mpreferred-stack-boundary=2 -fno-set-stack-executable -fno-optimize-sibling-calls -fno-omit-frame-pointer")
85 if(OPTIMIZE STREQUAL "1")
86 add_compile_flags("-ftracer -momit-leaf-frame-pointer")
87 endif()
88 elseif(ARCH MATCHES amd64)
89 add_compile_flags("-mpreferred-stack-boundary=4")
90 if(OPTIMIZE STREQUAL "1")
91 add_compile_flags("-ftracer -momit-leaf-frame-pointer")
92 endif()
93 elseif(ARCH MATCHES arm)
94 if(OPTIMIZE STREQUAL "1")
95 add_compile_flags("-ftracer")
96 endif()
97 endif()
98
99 # Other
100 if(ARCH MATCHES amd64)
101 add_definitions(-U_X86_ -UWIN32)
102 elseif(ARCH MATCHES arm)
103 add_definitions(-U_UNICODE -UUNICODE)
104 add_definitions(-D__MSVCRT__) # DUBIOUS
105 endif()
106
107 add_definitions(-D_inline=__inline)
108
109 # alternative arch name
110 if(ARCH MATCHES amd64)
111 set(ARCH2 x86_64)
112 else()
113 set(ARCH2 ${ARCH})
114 endif()
115
116 if(SEPARATE_DBG)
117 # PDB style debug puts all dwarf debug info in a separate dbg file
118 set(OBJCOPY ${CMAKE_OBJCOPY})
119 set(CMAKE_C_LINK_EXECUTABLE
120 "<CMAKE_C_COMPILER> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
121 "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/<TARGET>.dbg"
122 "${OBJCOPY} --strip-debug <TARGET>")
123 set(CMAKE_CXX_LINK_EXECUTABLE
124 "<CMAKE_CXX_COMPILER> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
125 "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/<TARGET>.dbg"
126 "${OBJCOPY} --strip-debug <TARGET>")
127 set(CMAKE_C_CREATE_SHARED_LIBRARY
128 "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
129 "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/<TARGET>.dbg"
130 "${OBJCOPY} --strip-debug <TARGET>")
131 set(CMAKE_CXX_CREATE_SHARED_LIBRARY
132 "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
133 "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/<TARGET>.dbg"
134 "${OBJCOPY} --strip-debug <TARGET>")
135 set(CMAKE_RC_CREATE_SHARED_LIBRARY
136 "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
137 "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/<TARGET>.dbg"
138 "${OBJCOPY} --strip-debug <TARGET>")
139 else()
140 # Normal rsym build
141 get_target_property(RSYM native-rsym IMPORTED_LOCATION_NOCONFIG)
142 set(CMAKE_C_LINK_EXECUTABLE
143 "<CMAKE_C_COMPILER> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
144 "${RSYM} <TARGET> <TARGET>")
145 set(CMAKE_CXX_LINK_EXECUTABLE
146 "<CMAKE_CXX_COMPILER> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
147 "${RSYM} <TARGET> <TARGET>")
148 set(CMAKE_C_CREATE_SHARED_LIBRARY
149 "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
150 "${RSYM} <TARGET> <TARGET>")
151 set(CMAKE_CXX_CREATE_SHARED_LIBRARY
152 "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
153 "${RSYM} <TARGET> <TARGET>")
154 set(CMAKE_RC_CREATE_SHARED_LIBRARY
155 "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
156 "${RSYM} <TARGET> <TARGET>")
157 endif()
158
159 set(CMAKE_EXE_LINKER_FLAGS "-nostdlib -Wl,--enable-auto-image-base,--disable-auto-import,--disable-stdcall-fixup")
160 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
161
162 SET(CMAKE_C_COMPILE_OBJECT "${CCACHE} <CMAKE_C_COMPILER> <DEFINES> <FLAGS> -o <OBJECT> -c <SOURCE>")
163 SET(CMAKE_CXX_COMPILE_OBJECT "${CCACHE} <CMAKE_CXX_COMPILER> <DEFINES> <FLAGS> -o <OBJECT> -c <SOURCE>")
164 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>")
165
166 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -i <SOURCE> <CMAKE_C_LINK_FLAGS> <DEFINES> ${I18N_DEFS} -DRC_INVOKED -D__WIN32__=1 -D__FLAT__=1 -I${REACTOS_SOURCE_DIR}/include/psdk -I${REACTOS_BINARY_DIR}/include/psdk -I${REACTOS_SOURCE_DIR}/include/ -I${REACTOS_SOURCE_DIR}/include/reactos -I${REACTOS_BINARY_DIR}/include/reactos -I${REACTOS_SOURCE_DIR}/include/reactos/wine -I${REACTOS_SOURCE_DIR}/include/crt -I${REACTOS_SOURCE_DIR}/include/crt/mingw32 -O coff -o <OBJECT>")
167
168 # Optional 3rd parameter: stdcall stack bytes
169 function(set_entrypoint MODULE ENTRYPOINT)
170 if(${ENTRYPOINT} STREQUAL "0")
171 add_target_link_flags(${MODULE} "-Wl,-entry,0")
172 elseif(ARCH MATCHES i386)
173 set(_entrysymbol _${ENTRYPOINT})
174 if(${ARGC} GREATER 2)
175 set(_entrysymbol ${_entrysymbol}@${ARGV2})
176 endif()
177 add_target_link_flags(${MODULE} "-Wl,-entry,${_entrysymbol}")
178 else()
179 add_target_link_flags(${MODULE} "-Wl,-entry,${ENTRYPOINT}")
180 endif()
181 endfunction()
182
183 function(set_subsystem MODULE SUBSYSTEM)
184 add_target_link_flags(${MODULE} "-Wl,--subsystem,${SUBSYSTEM}")
185 endfunction()
186
187 function(set_image_base MODULE IMAGE_BASE)
188 add_target_link_flags(${MODULE} "-Wl,--image-base,${IMAGE_BASE}")
189 endfunction()
190
191 function(set_module_type_toolchain MODULE TYPE)
192 if(IS_CPP)
193 target_link_libraries(${MODULE} -lstdc++ -lsupc++ -lgcc -lmingwex)
194 endif()
195
196 if(${TYPE} STREQUAL kernelmodedriver)
197 add_target_link_flags(${MODULE} "-Wl,--exclude-all-symbols,-file-alignment=0x1000,-section-alignment=0x1000")
198 endif()
199 endfunction()
200
201 function(set_rc_compiler)
202 get_directory_property(defines COMPILE_DEFINITIONS)
203 get_directory_property(includes INCLUDE_DIRECTORIES)
204
205 foreach(arg ${defines})
206 set(rc_result_defs "${rc_result_defs} -D${arg}")
207 endforeach()
208
209 foreach(arg ${includes})
210 set(rc_result_incs "-I${arg} ${rc_result_incs}")
211 endforeach()
212
213 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${rc_result_defs} ${I18N_DEFS} -DRC_INVOKED -D__WIN32__=1 -D__FLAT__=1 -I${CMAKE_CURRENT_SOURCE_DIR} ${rc_result_incs} -i <SOURCE> -O coff -o <OBJECT>" PARENT_SCOPE)
214 endfunction()
215
216 function(add_delay_importlibs MODULE)
217 foreach(LIB ${ARGN})
218 target_link_libraries(${MODULE} lib${LIB}_delayed)
219 endforeach()
220 target_link_libraries(${MODULE} delayimp)
221 endfunction()
222
223 if(NOT ARCH MATCHES i386)
224 set(DECO_OPTION "-@")
225 endif()
226
227 # Cute little hack to produce import libs
228 set(CMAKE_IMPLIB_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def <OBJECTS> --kill-at --output-lib=<TARGET>")
229 set(CMAKE_IMPLIB_DELAYED_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def <OBJECTS> --kill-at --output-delaylib=<TARGET>")
230 function(add_importlib_target _exports_file _implib_name)
231 get_filename_component(_name ${_exports_file} NAME_WE)
232 get_filename_component(_extension ${_exports_file} EXT)
233
234 if(${_extension} STREQUAL ".spec")
235
236 # generate .def
237 add_custom_command(
238 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_name}_implib.def
239 COMMAND native-spec2def -n=${_implib_name} -a=${ARCH2} -d=${CMAKE_CURRENT_BINARY_DIR}/${_name}_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file}
240 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} native-spec2def)
241 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_name}_implib.def PROPERTIES EXTERNAL_OBJECT TRUE)
242
243 #create normal importlib
244 add_library(lib${_name} STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_name}_implib.def)
245 set_target_properties(lib${_name} PROPERTIES LINKER_LANGUAGE "IMPLIB" PREFIX "")
246
247 #create delayed importlib
248 add_library(lib${_name}_delayed STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_name}_implib.def)
249 set_target_properties(lib${_name}_delayed PROPERTIES LINKER_LANGUAGE "IMPLIB_DELAYED" PREFIX "")
250 else()
251 message(FATAL_ERROR "Unsupported exports file extension: ${_extension}")
252 endif()
253 endfunction()
254
255 function(spec2def _dllname _spec_file)
256
257 if(${ARGC} GREATER 2)
258 set(_file ${ARGV2})
259 else()
260 get_filename_component(_file ${_spec_file} NAME_WE)
261 endif()
262
263 add_custom_command(
264 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
265 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}
266 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
267 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
268 PROPERTIES GENERATED TRUE EXTERNAL_OBJECT TRUE)
269 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c PROPERTIES GENERATED TRUE)
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 _object_file _base_address)
349 get_filename_component(OBJECT_PATH ${_object_file} PATH)
350 get_filename_component(OBJECT_NAME ${_object_file} NAME)
351 file(MAKE_DIRECTORY ${OBJECT_PATH})
352 get_directory_property(defines COMPILE_DEFINITIONS)
353 get_directory_property(includes INCLUDE_DIRECTORIES)
354
355 foreach(arg ${defines})
356 set(result_defs ${result_defs} -D${arg})
357 endforeach()
358
359 foreach(arg ${includes})
360 set(result_incs -I${arg} ${result_incs})
361 endforeach()
362
363 add_custom_command(
364 OUTPUT ${_object_file}
365 COMMAND nasm -o ${_object_file} ${result_incs} ${result_defs} -f bin ${_asm_file}
366 DEPENDS ${_asm_file})
367 set_source_files_properties(${_object_file} PROPERTIES GENERATED TRUE)
368 add_custom_target(${_target_name} ALL DEPENDS ${_object_file})
369 endfunction()
370
371 function(CreateBootSectorTarget2 _target_name _asm_file _binary_file _base_address)
372 set(_object_file ${_binary_file}.o)
373
374 add_custom_command(
375 OUTPUT ${_object_file}
376 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}
377 DEPENDS ${_asm_file})
378
379 add_custom_command(
380 OUTPUT ${_binary_file}
381 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
382 # COMMAND objcopy --output-target binary --image-base 0x${_base_address} ${_object_file} ${_binary_file}
383 DEPENDS ${_object_file} native-obj2bin)
384
385 set_source_files_properties(${_object_file} ${_binary_file} PROPERTIES GENERATED TRUE)
386
387 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
388
389 endfunction()
390
391 function(allow_warnings __module)
392 add_target_compile_flags(${__module} "-Wno-error")
393 endfunction()