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