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