3ea234358151fca56b42d20ff5ab5824000fbf3a
[reactos.git] / reactos / cmake / gcc.cmake
1
2 # Compiler Core
3 add_compile_flags("-pipe -fms-extensions")
4
5 # Debugging (Note: DWARF-4 on 4.5.1 when we ship)
6 add_compile_flags("-gdwarf-2 -g2 -femit-struct-debug-detailed=none -feliminate-unused-debug-types")
7
8 # Tuning
9 if(ARCH MATCHES i386)
10 add_compile_flags("-march=${OARCH} -mtune=${TUNE}")
11 else()
12 add_compile_flags("-march=${OARCH}")
13 endif()
14
15 # Warnings
16 add_compile_flags("-Wall -Wno-char-subscripts -Wpointer-arith -Wno-multichar -Wno-error=uninitialized -Wno-unused-value -Winvalid-pch")
17
18 if(ARCH MATCHES amd64)
19 add_compile_flags("-Wno-format")
20 elseif(ARCH MATCHES arm)
21 add_compile_flags("-Wno-attributes")
22 endif()
23
24 # Optimizations
25 if(OPTIMIZE STREQUAL "1")
26 add_compile_flags("-Os")
27 elseif(OPTIMIZE STREQUAL "2")
28 add_compile_flags("-Os")
29 elseif(OPTIMIZE STREQUAL "3")
30 add_compile_flags("-O1")
31 elseif(OPTIMIZE STREQUAL "4")
32 add_compile_flags("-O2")
33 elseif(OPTIMIZE STREQUAL "5")
34 add_compile_flags("-O3")
35 endif()
36
37 add_compile_flags("-fno-strict-aliasing")
38
39 if(ARCH MATCHES i386)
40 add_compile_flags("-mpreferred-stack-boundary=2 -fno-set-stack-executable -fno-optimize-sibling-calls -fno-omit-frame-pointer")
41 if(OPTIMIZE STREQUAL "1")
42 add_compile_flags("-ftracer -momit-leaf-frame-pointer")
43 endif()
44 elseif(ARCH MATCHES amd64)
45 add_compile_flags("-mpreferred-stack-boundary=4")
46 if(OPTIMIZE STREQUAL "1")
47 add_compile_flags("-ftracer -momit-leaf-frame-pointer")
48 endif()
49 elseif(ARCH MATCHES arm)
50 if(OPTIMIZE STREQUAL "1")
51 add_compile_flags("-ftracer")
52 endif()
53 endif()
54
55 # Other
56 if(ARCH MATCHES amd64)
57 add_definitions(-U_X86_ -UWIN32)
58 elseif(ARCH MATCHES arm)
59 add_definitions(-U_UNICODE -UUNICODE)
60 add_definitions(-D__MSVCRT__) # DUBIOUS
61 endif()
62
63 # alternative arch name
64 if(ARCH MATCHES amd64)
65 set(ARCH2 x86_64)
66 else()
67 set(ARCH2 ${ARCH})
68 endif()
69
70 # Linking
71 if(ARCH MATCHES i386)
72 link_directories(${REACTOS_SOURCE_DIR}/importlibs)
73 endif()
74
75 link_directories(${REACTOS_BINARY_DIR}/lib/sdk/crt)
76
77 set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
78
79 set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
80
81 set(CMAKE_EXE_LINKER_FLAGS "-nodefaultlibs -nostdlib -Wl,--enable-auto-image-base -Wl,--disable-auto-import -Wl,--disable-stdcall-fixup")
82
83 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
84
85 set(CMAKE_C_CREATE_SHARED_LIBRARY "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
86 set(CMAKE_CXX_CREATE_SHARED_LIBRARY "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
87 set(CMAKE_RC_CREATE_SHARED_LIBRARY "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
88
89 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> ${CMAKE_C_FLAGS} <DEFINES> -D__ASM__ -c <SOURCE>")
90
91 #set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -i <SOURCE> <CMAKE_C_LINK_FLAGS> <DEFINES> -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>")
92
93 # Temporary, until windres issues are fixed
94 get_target_property(WRC native-wrc IMPORTED_LOCATION_NOCONFIG)
95 set(CMAKE_RC_COMPILE_OBJECT
96 "<CMAKE_C_COMPILER> -DRC_INVOKED -D__WIN32__=1 -D__FLAT__=1 <DEFINES> -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 -xc -E <SOURCE> -o <OBJECT>"
97 "${WRC} -i <OBJECT> -o <OBJECT>"
98 "<CMAKE_RC_COMPILER> -i <OBJECT> -J res -O coff -o <OBJECT>")
99
100 # Optional 3rd parameter: stdcall stack bytes
101 function(set_entrypoint MODULE ENTRYPOINT)
102 if(${ENTRYPOINT} STREQUAL "0")
103 add_target_link_flags(${MODULE} "-Wl,-entry,0")
104 elseif(ARCH MATCHES i386)
105 set(_entrysymbol _${ENTRYPOINT})
106 if (${ARGC} GREATER 2)
107 set(_entrysymbol ${_entrysymbol}@${ARGV2})
108 endif()
109 add_target_link_flags(${MODULE} "-Wl,-entry,${_entrysymbol}")
110 else()
111 add_target_link_flags(${MODULE} "-Wl,-entry,${ENTRYPOINT}")
112 endif()
113 endfunction()
114
115 function(set_subsystem MODULE SUBSYSTEM)
116 add_target_link_flags(${MODULE} "-Wl,--subsystem,${SUBSYSTEM}")
117 endfunction()
118
119 function(set_image_base MODULE IMAGE_BASE)
120 add_target_link_flags(${MODULE} "-Wl,--image-base,${IMAGE_BASE}")
121 endfunction()
122
123 function(set_module_type MODULE TYPE)
124
125 add_dependencies(${MODULE} psdk)
126 if(${IS_CPP})
127 target_link_libraries(${MODULE} stlport -lsupc++ -lgcc)
128 endif()
129
130 if(${TYPE} MATCHES nativecui)
131 set_subsystem(${MODULE} native)
132 set_entrypoint(${MODULE} NtProcessStartup 4)
133 elseif(${TYPE} MATCHES win32gui)
134 set_subsystem(${MODULE} windows)
135 if(IS_UNICODE)
136 set_entrypoint(${MODULE} wWinMainCRTStartup)
137 else()
138 set_entrypoint(${MODULE} WinMainCRTStartup)
139 endif(IS_UNICODE)
140 elseif(${TYPE} MATCHES win32cui)
141 set_subsystem(${MODULE} console)
142 if(IS_UNICODE)
143 set_entrypoint(${MODULE} wmainCRTStartup)
144 else()
145 set_entrypoint(${MODULE} mainCRTStartup)
146 endif(IS_UNICODE)
147 elseif(${TYPE} MATCHES win32dll)
148 set_entrypoint(${MODULE} DllMainCRTStartup 12)
149 if(DEFINED baseaddress_${MODULE})
150 set_image_base(${MODULE} ${baseaddress_${MODULE}})
151 else()
152 message(STATUS "${MODULE} has no base address")
153 endif()
154 elseif(${TYPE} MATCHES win32ocx)
155 set_entrypoint(${MODULE} DllMainCRTStartup 12)
156 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
157 elseif(${TYPE} MATCHES cpl)
158 set_entrypoint(${MODULE} DllMainCRTStartup 12)
159 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
160 elseif(${TYPE} MATCHES kernelmodedriver)
161 add_target_link_flags(${MODULE} "-Wl,--exclude-all-symbols -Wl,-file-alignment=0x1000 -Wl,-section-alignment=0x1000")
162 set_target_properties(${MODULE} PROPERTIES SUFFIX ".sys")
163 set_entrypoint(${MODULE} DriverEntry 8)
164 set_subsystem(${MODULE} native)
165 set_image_base(${MODULE} 0x00010000)
166 add_dependencies(${MODULE} bugcodes)
167 elseif(${TYPE} MATCHES nativedll)
168 set_subsystem(${MODULE} native)
169 set_entrypoint(${MODULE} DllMain 12)
170 else()
171 message(FATAL_ERROR "Unknown module type : ${TYPE}")
172 endif()
173 endfunction()
174
175 # Workaround lack of mingw RC support in cmake
176 function(set_rc_compiler)
177 get_directory_property(defines COMPILE_DEFINITIONS)
178 get_directory_property(includes INCLUDE_DIRECTORIES)
179
180 foreach(arg ${defines})
181 set(rc_result_defs "${rc_result_defs} -D${arg}")
182 endforeach()
183
184 foreach(arg ${includes})
185 set(rc_result_incs "-I${arg} ${rc_result_incs}")
186 endforeach()
187
188 #set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${rc_result_defs} ${rc_result_incs} -i <SOURCE> -O coff -o <OBJECT>" PARENT_SCOPE)
189 set(CMAKE_RC_COMPILE_OBJECT
190 "<CMAKE_C_COMPILER> -DRC_INVOKED -D__WIN32__=1 -D__FLAT__=1 ${rc_result_defs} -I${CMAKE_CURRENT_SOURCE_DIR} ${rc_result_incs} -xc -E <SOURCE> -o <OBJECT>"
191 "${WRC} -I${CMAKE_CURRENT_SOURCE_DIR} ${rc_result_incs} -i <OBJECT> -o <OBJECT>"
192 "<CMAKE_RC_COMPILER> -i <OBJECT> -J res -O coff -o <OBJECT>" PARENT_SCOPE)
193 endfunction()
194
195 function(add_delay_importlibs MODULE)
196 foreach(LIB ${ARGN})
197 target_link_libraries(${MODULE} ${CMAKE_BINARY_DIR}/importlibs/lib${LIB}_delayed.a)
198 add_dependencies(${MODULE} lib${LIB}_delayed)
199 endforeach()
200 target_link_libraries(${MODULE} delayimp)
201 endfunction()
202
203 if(NOT ARCH MATCHES i386)
204 set(DECO_OPTION "-@")
205 endif()
206
207 function(add_importlib_target _exports_file)
208
209 get_filename_component(_name ${_exports_file} NAME_WE)
210 get_filename_component(_extension ${_exports_file} EXT)
211 get_target_property(_suffix ${_name} SUFFIX)
212 if(${_suffix} STREQUAL "_suffix-NOTFOUND")
213 get_target_property(_type ${_name} TYPE)
214 if(${_type} MATCHES EXECUTABLE)
215 set(_suffix ".exe")
216 else()
217 set(_suffix ".dll")
218 endif()
219 endif()
220
221 if (${_extension} STREQUAL ".spec")
222
223 # Normal importlib creation
224 add_custom_command(
225 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
226 COMMAND native-spec2def -n=${_name}${_suffix} -a=${ARCH2} -d=${CMAKE_CURRENT_BINARY_DIR}/${_name}_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file}
227 COMMAND ${MINGW_PREFIX}dlltool --def ${CMAKE_CURRENT_BINARY_DIR}/${_name}_implib.def --kill-at --output-lib=${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
228 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} native-spec2def)
229
230 # Delayed importlib creation
231 add_custom_command(
232 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_delayed.a
233 COMMAND native-spec2def -n=${_name}${_suffix} -a=${ARCH2} -d=${CMAKE_CURRENT_BINARY_DIR}/${_name}_delayed_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file}
234 COMMAND ${MINGW_PREFIX}dlltool --def ${CMAKE_CURRENT_BINARY_DIR}/${_name}_delayed_implib.def --kill-at --output-delaylib ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_delayed.a
235 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} native-spec2def)
236
237 elseif(${_extension} STREQUAL ".def")
238 message("Use of def files for import libs is deprecated: ${_exports_file}")
239 add_custom_command(
240 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
241 COMMAND ${MINGW_PREFIX}dlltool --def ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} --kill-at --output-lib=${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
242 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file})
243 add_custom_command(
244 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_delayed.a
245 COMMAND ${MINGW_PREFIX}dlltool --def ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} --kill-at --output-delaylib ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_delayed.a
246 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file})
247 else()
248 message(FATAL_ERROR "Unsupported exports file extension: ${_extension}")
249 endif()
250
251 # Normal importlib target
252 add_custom_target(
253 lib${_name}
254 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a)
255 # Delayed importlib target
256 add_custom_target(
257 lib${_name}_delayed
258 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_delayed.a)
259
260 endfunction()
261
262 function(spec2def _dllname _spec_file)
263
264 if(${ARGC} GREATER 2)
265 set(_file ${ARGV2})
266 else()
267 get_filename_component(_file ${_spec_file} NAME_WE)
268 endif()
269
270 add_custom_command(
271 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
272 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}
273 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
274 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
275 PROPERTIES GENERATED TRUE EXTERNAL_OBJECT TRUE)
276 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c PROPERTIES GENERATED TRUE)
277 endfunction()
278
279 macro(macro_mc FILE)
280 set(COMMAND_MC ${MINGW_PREFIX}windmc -A -b ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.mc -r ${REACTOS_BINARY_DIR}/include/reactos -h ${REACTOS_BINARY_DIR}/include/reactos)
281 endmacro()
282
283 #pseh lib, needed with mingw
284 set(PSEH_LIB "pseh")
285
286 # Macros
287 if(PCH)
288 macro(_PCH_GET_COMPILE_FLAGS _target_name _out_compile_flags _header_filename)
289 # Add the precompiled header to the build
290 get_filename_component(_FILE ${_header_filename} NAME)
291 set(_gch_filename "${_FILE}.gch")
292 list(APPEND ${_out_compile_flags} -c ${_header_filename} -o ${_gch_filename})
293
294 # This gets us our includes
295 get_directory_property(DIRINC INCLUDE_DIRECTORIES)
296 foreach(item ${DIRINC})
297 list(APPEND ${_out_compile_flags} -I${item})
298 endforeach()
299
300 # This our definitions
301 get_directory_property(_compiler_flags DEFINITIONS)
302 list(APPEND ${_out_compile_flags} ${_compiler_flags})
303
304 # This gets any specific definitions that were added with set-target-property
305 get_target_property(_target_defs ${_target_name} COMPILE_DEFINITIONS)
306 if (_target_defs)
307 foreach(item ${_target_defs})
308 list(APPEND ${_out_compile_flags} -D${item})
309 endforeach()
310 endif()
311
312 if(IS_CPP)
313 list(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS})
314 else()
315 list(APPEND ${_out_compile_flags} ${CMAKE_C_FLAGS})
316 endif()
317
318 separate_arguments(${_out_compile_flags})
319 endmacro()
320
321 macro(add_pch _target_name _FILE)
322 set(_header_filename ${CMAKE_CURRENT_SOURCE_DIR}/${_FILE})
323 get_filename_component(_basename ${_FILE} NAME)
324 set(_gch_filename ${_basename}.gch)
325 _PCH_GET_COMPILE_FLAGS(${_target_name} _args ${_header_filename})
326
327 if(IS_CPP)
328 set(__lang CXX)
329 set(__compiler ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1})
330 else()
331 set(__lang C)
332 set(__compiler ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1})
333 endif()
334
335 add_custom_command(OUTPUT ${_gch_filename}
336 COMMAND ${__compiler} ${_args}
337 IMPLICIT_DEPENDS ${__lang} ${_header_filename}
338 DEPENDS ${_header_filename} ${ARGN})
339 get_target_property(_src_files ${_target_name} SOURCES)
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 COMPILE_FLAGS "-fpch-preprocess" 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 _object_file _base_address)
355 get_filename_component(OBJECT_PATH ${_object_file} PATH)
356 get_filename_component(OBJECT_NAME ${_object_file} NAME)
357 file(MAKE_DIRECTORY ${OBJECT_PATH})
358 get_directory_property(defines COMPILE_DEFINITIONS)
359 get_directory_property(includes INCLUDE_DIRECTORIES)
360
361 foreach(arg ${defines})
362 set(result_defs ${result_defs} -D${arg})
363 endforeach()
364
365 foreach(arg ${includes})
366 set(result_incs -I${arg} ${result_incs})
367 endforeach()
368
369 add_custom_command(
370 OUTPUT ${_object_file}
371 COMMAND nasm -o ${_object_file} ${result_incs} ${result_defs} -f bin ${_asm_file}
372 DEPENDS ${_asm_file})
373 set_source_files_properties(${_object_file} PROPERTIES GENERATED TRUE)
374 add_custom_target(${_target_name} ALL DEPENDS ${_object_file})
375 endfunction()
376
377 function(CreateBootSectorTarget2 _target_name _asm_file _binary_file _base_address)
378 set(_object_file ${_binary_file}.o)
379
380 add_custom_command(
381 OUTPUT ${_object_file}
382 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}
383 DEPENDS ${_asm_file})
384
385 add_custom_command(
386 OUTPUT ${_binary_file}
387 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
388 # COMMAND objcopy --output-target binary --image-base 0x${_base_address} ${_object_file} ${_binary_file}
389 DEPENDS ${_object_file} native-obj2bin)
390
391 set_source_files_properties(${_object_file} ${_binary_file} PROPERTIES GENERATED TRUE)
392
393 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
394
395 endfunction()