[NTVDM]
[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 if(NOT DEFINED USE_PSEH3)
13 set(USE_PSEH3 0)
14 endif()
15
16 if(USE_PSEH3)
17 add_definitions(-D_USE_PSEH3=1)
18 endif()
19
20 # Compiler Core
21 add_compile_flags("-pipe -fms-extensions -fno-strict-aliasing")
22 if(GCC_VERSION VERSION_GREATER 4.7)
23 add_compile_flags("-mstackrealign")
24 endif()
25
26 add_compile_flags_language("-fno-rtti -fno-exceptions" "CXX")
27
28 #bug
29 #file(TO_NATIVE_PATH ${REACTOS_SOURCE_DIR} REACTOS_SOURCE_DIR_NATIVE)
30 #workaround
31 set(REACTOS_SOURCE_DIR_NATIVE ${REACTOS_SOURCE_DIR})
32 if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
33 string(REPLACE "/" "\\" REACTOS_SOURCE_DIR_NATIVE ${REACTOS_SOURCE_DIR})
34 endif()
35 add_compile_flags("-fdebug-prefix-map=\"${REACTOS_SOURCE_DIR_NATIVE}\"=ReactOS")
36
37 # Debugging
38 if(SEPARATE_DBG)
39 add_compile_flags("-gdwarf-2 -g2")
40 else()
41 add_compile_flags("-gdwarf-2 -gstrict-dwarf -femit-struct-debug-detailed=none -feliminate-unused-debug-symbols")
42 endif()
43
44 # For some reason, cmake sets -fPIC, and we don't want it
45 if(DEFINED CMAKE_SHARED_LIBRARY_ASM_FLAGS)
46 string(REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_ASM_FLAGS ${CMAKE_SHARED_LIBRARY_ASM_FLAGS})
47 endif()
48
49 # Tuning
50 if(ARCH STREQUAL "i386")
51 add_compile_flags("-march=${OARCH} -mtune=${TUNE}")
52 else()
53 add_compile_flags("-march=${OARCH}")
54 endif()
55
56 # Warnings
57 add_compile_flags("-Werror -Wall -Wno-char-subscripts -Wpointer-arith -Wno-multichar -Wno-unused-value")
58
59 if(GCC_VERSION VERSION_LESS 4.7)
60 add_compile_flags("-Wno-error=uninitialized")
61 elseif(GCC_VERSION VERSION_EQUAL 4.7 OR GCC_VERSION VERSION_GREATER 4.7)
62 add_compile_flags("-Wno-error=unused-but-set-variable -Wno-maybe-uninitialized -Wno-error=narrowing")
63 endif()
64
65 if(ARCH STREQUAL "amd64")
66 add_compile_flags("-Wno-format")
67 elseif(ARCH STREQUAL "arm")
68 add_compile_flags("-Wno-attributes")
69 endif()
70
71 # Optimizations
72 if(OPTIMIZE STREQUAL "1")
73 add_compile_flags("-Os -ftracer")
74 elseif(OPTIMIZE STREQUAL "2")
75 add_compile_flags("-Os")
76 elseif(OPTIMIZE STREQUAL "3")
77 add_compile_flags("-O1 -fno-inline-functions-called-once -fno-tree-sra")
78 elseif(OPTIMIZE STREQUAL "4")
79 add_compile_flags("-O1")
80 elseif(OPTIMIZE STREQUAL "5")
81 add_compile_flags("-O2")
82 elseif(OPTIMIZE STREQUAL "6")
83 add_compile_flags("-O3")
84 elseif(OPTIMIZE STREQUAL "7")
85 add_compile_flags("-Ofast")
86 endif()
87
88 # Link-time code generation
89 if(LTCG)
90 add_compile_flags("-flto -Wno-error=clobbered")
91 endif()
92
93 if(ARCH STREQUAL "i386")
94 add_compile_flags("-mpreferred-stack-boundary=3 -fno-set-stack-executable -fno-optimize-sibling-calls -fno-omit-frame-pointer")
95 if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
96 add_compile_flags("-momit-leaf-frame-pointer")
97 endif()
98 elseif(ARCH STREQUAL "amd64")
99 add_compile_flags("-mpreferred-stack-boundary=4")
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} -s ${REACTOS_SOURCE_DIR} <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} -s ${REACTOS_SOURCE_DIR} <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} -s ${REACTOS_SOURCE_DIR} <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} -s ${REACTOS_SOURCE_DIR} <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 endif()
167
168 set(CMAKE_EXE_LINKER_FLAGS "-nostdlib -Wl,--enable-auto-image-base,--disable-auto-import,--disable-stdcall-fixup")
169 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
170
171 SET(CMAKE_C_COMPILE_OBJECT "${CCACHE} <CMAKE_C_COMPILER> <DEFINES> -Wa,--compress-debug-sections <FLAGS> -o <OBJECT> -c <SOURCE>")
172 SET(CMAKE_CXX_COMPILE_OBJECT "${CCACHE} <CMAKE_CXX_COMPILER> <DEFINES> <FLAGS> -o <OBJECT> -c <SOURCE>")
173 set(CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> -Wa,--compress-debug-sections -x assembler-with-cpp -o <OBJECT> -I${REACTOS_SOURCE_DIR}/include/asm -I${REACTOS_BINARY_DIR}/include/asm <FLAGS> <DEFINES> -D__ASM__ -c <SOURCE>")
174
175 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -O coff <FLAGS> -DRC_INVOKED -D__WIN32__=1 -D__FLAT__=1 ${I18N_DEFS} <DEFINES> <SOURCE> <OBJECT>")
176 set(CMAKE_DEPFILE_FLAGS_RC "--preprocessor \"${MINGW_TOOLCHAIN_PREFIX}gcc${MINGW_TOOLCHAIN_SUFFIX} -E -xc-header -MMD -MF <DEPFILE> -MT <OBJECT>\" ")
177
178 # Optional 3rd parameter: stdcall stack bytes
179 function(set_entrypoint MODULE ENTRYPOINT)
180 if(${ENTRYPOINT} STREQUAL "0")
181 add_target_link_flags(${MODULE} "-Wl,-entry,0")
182 elseif(ARCH STREQUAL "i386")
183 set(_entrysymbol _${ENTRYPOINT})
184 if(${ARGC} GREATER 2)
185 set(_entrysymbol ${_entrysymbol}@${ARGV2})
186 endif()
187 add_target_link_flags(${MODULE} "-Wl,-entry,${_entrysymbol}")
188 else()
189 add_target_link_flags(${MODULE} "-Wl,-entry,${ENTRYPOINT}")
190 endif()
191 endfunction()
192
193 function(set_subsystem MODULE SUBSYSTEM)
194 add_target_link_flags(${MODULE} "-Wl,--subsystem,${SUBSYSTEM}")
195 endfunction()
196
197 function(set_image_base MODULE IMAGE_BASE)
198 add_target_link_flags(${MODULE} "-Wl,--image-base,${IMAGE_BASE}")
199 endfunction()
200
201 function(set_module_type_toolchain MODULE TYPE)
202 if(CPP_USE_STL)
203 if((${TYPE} STREQUAL "kernelmodedriver") OR (${TYPE} STREQUAL "wdmdriver"))
204 message(FATAL_ERROR "Use of STL in kernelmodedriver or wdmdriver type module prohibited")
205 endif()
206 target_link_libraries(${MODULE} -lstdc++ -lsupc++ -lgcc -lmingwex)
207 elseif(CPP_USE_RT)
208 target_link_libraries(${MODULE} -lsupc++ -lgcc)
209 elseif(IS_CPP)
210 target_link_libraries(${MODULE} -lgcc)
211 endif()
212
213 if((${TYPE} STREQUAL "kernelmodedriver") OR (${TYPE} STREQUAL "wdmdriver"))
214 add_target_link_flags(${MODULE} "-Wl,--exclude-all-symbols,-file-alignment=0x1000,-section-alignment=0x1000")
215 if(${TYPE} STREQUAL "wdmdriver")
216 add_target_link_flags(${MODULE} "-Wl,--wdmdriver")
217 endif()
218 endif()
219 endfunction()
220
221 function(add_delay_importlibs _module)
222 get_target_property(_module_type ${_module} TYPE)
223 if(_module_type STREQUAL "STATIC_LIBRARY")
224 message(FATAL_ERROR "Cannot add delay imports to a static library")
225 endif()
226 foreach(_lib ${ARGN})
227 target_link_libraries(${_module} lib${_lib}_delayed)
228 endforeach()
229 target_link_libraries(${_module} delayimp)
230 endfunction()
231
232 if(NOT ARCH STREQUAL "i386")
233 set(DECO_OPTION "-@")
234 endif()
235
236 function(generate_import_lib _libname _dllname _spec_file)
237 # generate the def for the import lib
238 add_custom_command(
239 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def
240 COMMAND native-spec2def -n=${_dllname} -a=${ARCH2} -d=${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
241 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
242 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def PROPERTIES EXTERNAL_OBJECT TRUE)
243
244 #create normal importlib
245 _add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def)
246 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "IMPLIB" PREFIX "")
247
248 #create delayed importlib
249 _add_library(${_libname}_delayed STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def)
250 set_target_properties(${_libname}_delayed PROPERTIES LINKER_LANGUAGE "IMPLIB_DELAYED" PREFIX "")
251 endfunction()
252
253 # Cute little hack to produce import libs
254 set(CMAKE_IMPLIB_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def <OBJECTS> --kill-at --output-lib=<TARGET>")
255 set(CMAKE_IMPLIB_DELAYED_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def <OBJECTS> --kill-at --output-delaylib=<TARGET>")
256 function(spec2def _dllname _spec_file)
257 # do we also want to add importlib targets?
258 if(${ARGC} GREATER 2)
259 if(${ARGN} STREQUAL "ADD_IMPORTLIB")
260 set(__add_importlib TRUE)
261 else()
262 message(FATAL_ERROR "Wrong argument passed to spec2def, ${ARGN}")
263 endif()
264 endif()
265
266 # get library basename
267 get_filename_component(_file ${_dllname} NAME_WE)
268
269 # error out on anything else than spec
270 if(NOT ${_spec_file} MATCHES ".*\\.spec")
271 message(FATAL_ERROR "spec2def only takes spec files as input.")
272 endif()
273
274 # generate exports def and stubs C file for the module
275 add_custom_command(
276 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
277 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}
278 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
279
280 if(__add_importlib)
281 generate_import_lib(lib${_file} ${_dllname} ${_spec_file})
282 endif()
283 endfunction()
284
285 macro(macro_mc FLAG FILE)
286 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)
287 endmacro()
288
289 #pseh lib, needed with mingw
290 set(PSEH_LIB "pseh")
291
292 # Macros
293 if(PCH)
294 macro(_PCH_GET_COMPILE_FLAGS _target_name _out_compile_flags _header_filename)
295 # Add the precompiled header to the build
296 get_filename_component(_FILE ${_header_filename} NAME)
297 set(_gch_filename "${_FILE}.gch")
298 list(APPEND ${_out_compile_flags} -c ${_header_filename} -o ${_gch_filename})
299
300 # This gets us our includes
301 get_directory_property(DIRINC INCLUDE_DIRECTORIES)
302 foreach(item ${DIRINC})
303 list(APPEND ${_out_compile_flags} -I${item})
304 endforeach()
305
306 # This our definitions
307 get_directory_property(_compiler_flags DEFINITIONS)
308 list(APPEND ${_out_compile_flags} ${_compiler_flags})
309
310 # This gets any specific definitions that were added with set-target-property
311 get_target_property(_target_defs ${_target_name} COMPILE_DEFINITIONS)
312 if(_target_defs)
313 foreach(item ${_target_defs})
314 list(APPEND ${_out_compile_flags} -D${item})
315 endforeach()
316 endif()
317
318 if(IS_CPP)
319 list(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS})
320 else()
321 list(APPEND ${_out_compile_flags} ${CMAKE_C_FLAGS})
322 endif()
323
324 separate_arguments(${_out_compile_flags})
325 endmacro()
326
327 macro(add_pch _target_name _FILE)
328 set(_header_filename ${CMAKE_CURRENT_SOURCE_DIR}/${_FILE})
329 get_filename_component(_basename ${_FILE} NAME)
330 set(_gch_filename ${_basename}.gch)
331 _PCH_GET_COMPILE_FLAGS(${_target_name} _args ${_header_filename})
332
333 if(IS_CPP)
334 set(__lang CXX)
335 set(__compiler ${CCACHE} ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1})
336 else()
337 set(__lang C)
338 set(__compiler ${CCACHE} ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1})
339 endif()
340
341 add_custom_command(OUTPUT ${_gch_filename}
342 COMMAND ${__compiler} ${_args}
343 IMPLICIT_DEPENDS ${__lang} ${_header_filename}
344 DEPENDS ${_header_filename} ${ARGN})
345 get_target_property(_src_files ${_target_name} SOURCES)
346 add_target_compile_flags(${_target_name} "-fpch-preprocess -Winvalid-pch -Wno-error=invalid-pch")
347 foreach(_item in ${_src_files})
348 get_source_file_property(__src_lang ${_item} LANGUAGE)
349 if(__src_lang STREQUAL __lang)
350 set_source_files_properties(${_item} PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_gch_filename})
351 endif()
352 endforeach()
353 #set dependency checking : depends on precompiled header only which already depends on deeper header
354 set_target_properties(${_target_name} PROPERTIES IMPLICIT_DEPENDS_INCLUDE_TRANSFORM "\"${_basename}\"=;<${_basename}>=")
355 endmacro()
356 else()
357 macro(add_pch _target_name _FILE)
358 endmacro()
359 endif()
360
361 function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
362 set(_object_file ${_binary_file}.o)
363
364 add_custom_command(
365 OUTPUT ${_object_file}
366 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}
367 DEPENDS ${_asm_file})
368
369 add_custom_command(
370 OUTPUT ${_binary_file}
371 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
372 # COMMAND objcopy --output-target binary --image-base 0x${_base_address} ${_object_file} ${_binary_file}
373 DEPENDS ${_object_file} native-obj2bin)
374
375 set_source_files_properties(${_object_file} ${_binary_file} PROPERTIES GENERATED TRUE)
376
377 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
378
379 endfunction()
380
381 function(allow_warnings __module)
382 add_target_compile_flags(${__module} "-Wno-error")
383 endfunction()
384
385 macro(add_asm_files _target)
386 list(APPEND ${_target} ${ARGN})
387 endmacro()
388