4b50e427a02486740da2b35804038da4cc74098e
[reactos.git] / reactos / cmake / gcc.cmake
1
2 # Compiler Core
3 add_compiler_flags(-pipe -fms-extensions)
4
5 # Debugging (Note: DWARF-4 on 4.5.1 when we ship)
6 add_compiler_flags(-gdwarf-2 -g2 -femit-struct-debug-detailed=none -feliminate-unused-debug-types)
7
8 # Tuning
9 if(ARCH MATCHES i386)
10 add_compiler_flags(-march=${OARCH} -mtune=${TUNE})
11 else()
12 add_compiler_flags(-march=${OARCH})
13 endif()
14
15 # Warnings
16 add_compiler_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_compiler_flags(-Wno-format)
20 elseif(ARCH MATCHES arm)
21 add_compiler_flags(-Wno-attributes)
22 endif()
23
24 # Optimizations
25 if(OPTIMIZE STREQUAL "1")
26 add_compiler_flags(-Os)
27 elseif(OPTIMIZE STREQUAL "2")
28 add_compiler_flags(-Os)
29 elseif(OPTIMIZE STREQUAL "3")
30 add_compiler_flags(-O1)
31 elseif(OPTIMIZE STREQUAL "4")
32 add_compiler_flags(-O2)
33 elseif(OPTIMIZE STREQUAL "5")
34 add_compiler_flags(-O3)
35 endif()
36
37 add_compiler_flags(-fno-strict-aliasing)
38
39 if(ARCH MATCHES i386)
40 add_compiler_flags(-mpreferred-stack-boundary=2 -fno-set-stack-executable -fno-optimize-sibling-calls -fno-omit-frame-pointer)
41 if(OPTIMIZE STREQUAL "1")
42 add_compiler_flags(-ftracer -momit-leaf-frame-pointer)
43 endif()
44 elseif(ARCH MATCHES amd64)
45 add_compiler_flags(-mpreferred-stack-boundary=4)
46 if(OPTIMIZE STREQUAL "1")
47 add_compiler_flags(-ftracer -momit-leaf-frame-pointer)
48 endif()
49 elseif(ARCH MATCHES arm)
50 if(OPTIMIZE STREQUAL "1")
51 add_compiler_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_linkerflag(${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_linkerflag(${MODULE} "-Wl,-entry,${_entrysymbol}")
110 else()
111 add_linkerflag(${MODULE} "-Wl,-entry,${ENTRYPOINT}")
112 endif()
113 endfunction()
114
115 function(set_subsystem MODULE SUBSYSTEM)
116 add_linkerflag(${MODULE} "-Wl,--subsystem,${SUBSYSTEM}")
117 endfunction()
118
119 function(set_image_base MODULE IMAGE_BASE)
120 add_linkerflag(${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 set_target_properties(${MODULE} PROPERTIES LINK_FLAGS "-Wl,--exclude-all-symbols -Wl,-file-alignment=0x1000 -Wl,-section-alignment=0x1000" SUFFIX ".sys")
162 set_entrypoint(${MODULE} DriverEntry 8)
163 set_subsystem(${MODULE} native)
164 set_image_base(${MODULE} 0x00010000)
165 add_dependencies(${MODULE} bugcodes)
166 elseif(${TYPE} MATCHES nativedll)
167 set_subsystem(${MODULE} native)
168 set_entrypoint(${MODULE} DllMain 12)
169 else()
170 message(FATAL_ERROR "Unknown module type : ${TYPE}")
171 endif()
172 endfunction()
173
174 # Workaround lack of mingw RC support in cmake
175 function(set_rc_compiler)
176 get_directory_property(defines COMPILE_DEFINITIONS)
177 get_directory_property(includes INCLUDE_DIRECTORIES)
178
179 foreach(arg ${defines})
180 set(rc_result_defs "${rc_result_defs} -D${arg}")
181 endforeach()
182
183 foreach(arg ${includes})
184 set(rc_result_incs "-I${arg} ${rc_result_incs}")
185 endforeach()
186
187 #set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${rc_result_defs} ${rc_result_incs} -i <SOURCE> -O coff -o <OBJECT>" PARENT_SCOPE)
188 set(CMAKE_RC_COMPILE_OBJECT
189 "<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>"
190 "${WRC} -I${CMAKE_CURRENT_SOURCE_DIR} -i <OBJECT> -o <OBJECT>"
191 "<CMAKE_RC_COMPILER> -i <OBJECT> -J res -O coff -o <OBJECT>" PARENT_SCOPE)
192 endfunction()
193
194 #idl files support
195 set(IDL_COMPILER native-widl)
196
197 if(ARCH MATCHES i386)
198 set(IDL_FLAGS -m32 --win32)
199 elseif(ARCH MATCHES amd64)
200 set(IDL_FLAGS -m64 --win64)
201 endif()
202
203 function(add_delay_importlibs MODULE)
204 foreach(LIB ${ARGN})
205 target_link_libraries(${MODULE} ${CMAKE_BINARY_DIR}/importlibs/lib${LIB}_delayed.a)
206 add_dependencies(${MODULE} lib${LIB}_delayed)
207 endforeach()
208 target_link_libraries(${MODULE} delayimp)
209 endfunction()
210
211 if(NOT ARCH MATCHES i386)
212 set(DECO_OPTION "-@")
213 endif()
214
215 function(add_importlib_target _exports_file)
216
217 get_filename_component(_name ${_exports_file} NAME_WE)
218 get_filename_component(_extension ${_exports_file} EXT)
219 get_target_property(_suffix ${_name} SUFFIX)
220 if(${_suffix} STREQUAL "_suffix-NOTFOUND")
221 get_target_property(_type ${_name} TYPE)
222 if(${_type} MATCHES EXECUTABLE)
223 set(_suffix ".exe")
224 else()
225 set(_suffix ".dll")
226 endif()
227 endif()
228
229 if (${_extension} STREQUAL ".spec")
230
231 # Normal importlib creation
232 add_custom_command(
233 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
234 COMMAND native-spec2def -n=${_name}${_suffix} -a=${ARCH2} -d=${CMAKE_CURRENT_BINARY_DIR}/${_name}_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file}
235 COMMAND ${MINGW_PREFIX}dlltool --def ${CMAKE_CURRENT_BINARY_DIR}/${_name}_implib.def --kill-at --output-lib=${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
236 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} native-spec2def)
237
238 # Delayed importlib creation
239 add_custom_command(
240 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_delayed.a
241 COMMAND native-spec2def -n=${_name}${_suffix} -a=${ARCH2} -d=${CMAKE_CURRENT_BINARY_DIR}/${_name}_delayed_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file}
242 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
243 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} native-spec2def)
244
245 elseif(${_extension} STREQUAL ".def")
246 message("Use of def files for import libs is deprecated: ${_exports_file}")
247 add_custom_command(
248 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
249 COMMAND ${MINGW_PREFIX}dlltool --def ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} --kill-at --output-lib=${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
250 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file})
251 add_custom_command(
252 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_delayed.a
253 COMMAND ${MINGW_PREFIX}dlltool --def ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} --kill-at --output-delaylib ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_delayed.a
254 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file})
255 else()
256 message(FATAL_ERROR "Unsupported exports file extension: ${_extension}")
257 endif()
258
259 # Normal importlib target
260 add_custom_target(
261 lib${_name}
262 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a)
263 # Delayed importlib target
264 add_custom_target(
265 lib${_name}_delayed
266 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_delayed.a)
267
268 endfunction()
269
270 function(spec2def _dllname _spec_file)
271
272 if(${ARGC} GREATER 2)
273 set(_file ${ARGV2})
274 else()
275 get_filename_component(_file ${_spec_file} NAME_WE)
276 endif()
277
278 add_custom_command(
279 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
280 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}
281 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
282 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
283 PROPERTIES GENERATED TRUE EXTERNAL_OBJECT TRUE)
284 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c PROPERTIES GENERATED TRUE)
285 endfunction()
286
287 macro(macro_mc FILE)
288 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)
289 endmacro()
290
291 #pseh lib, needed with mingw
292 set(PSEH_LIB "pseh")
293
294 # Macros
295 if(PCH)
296 macro(_PCH_GET_COMPILE_FLAGS _target_name _out_compile_flags _header_filename)
297 # Add the precompiled header to the build
298 get_filename_component(_FILE ${_header_filename} NAME)
299 set(_gch_filename "${_FILE}.gch")
300 list(APPEND ${_out_compile_flags} -c ${_header_filename} -o ${_gch_filename})
301
302 # This gets us our includes
303 get_directory_property(DIRINC INCLUDE_DIRECTORIES)
304 foreach(item ${DIRINC})
305 list(APPEND ${_out_compile_flags} -I${item})
306 endforeach()
307
308 # This our definitions
309 get_directory_property(_compiler_flags DEFINITIONS)
310 list(APPEND ${_out_compile_flags} ${_compiler_flags})
311
312 # This gets any specific definitions that were added with set-target-property
313 get_target_property(_target_defs ${_target_name} COMPILE_DEFINITIONS)
314 if (_target_defs)
315 foreach(item ${_target_defs})
316 list(APPEND ${_out_compile_flags} -D${item})
317 endforeach()
318 endif()
319
320 if(IS_CPP)
321 list(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS})
322 else()
323 list(APPEND ${_out_compile_flags} ${CMAKE_C_FLAGS})
324 endif()
325
326 separate_arguments(${_out_compile_flags})
327 endmacro()
328
329 macro(add_pch _target_name _FILE)
330 set(_header_filename ${CMAKE_CURRENT_SOURCE_DIR}/${_FILE})
331 get_filename_component(_basename ${_FILE} NAME)
332 set(_gch_filename ${_basename}.gch)
333 _PCH_GET_COMPILE_FLAGS(${_target_name} _args ${_header_filename})
334
335 if(IS_CPP)
336 set(__lang CXX)
337 set(__compiler ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1})
338 else()
339 set(__lang C)
340 set(__compiler ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1})
341 endif()
342
343 add_custom_command(OUTPUT ${_gch_filename}
344 COMMAND ${__compiler} ${_args}
345 IMPLICIT_DEPENDS ${__lang} ${_header_filename}
346 DEPENDS ${_header_filename} ${ARGN})
347 get_target_property(_src_files ${_target_name} SOURCES)
348 foreach(_item in ${_src_files})
349 get_source_file_property(__src_lang ${_item} LANGUAGE)
350 if(__src_lang STREQUAL __lang)
351 set_source_files_properties(${_item} PROPERTIES COMPILE_FLAGS "-fpch-preprocess" OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_gch_filename})
352 endif()
353 endforeach()
354 #set dependency checking : depends on precompiled header only whixh already depends on deeper header
355 set_target_properties(${_target_name} PROPERTIES IMPLICIT_DEPENDS_INCLUDE_TRANSFORM "\"${_basename}\"=;<${_basename}>=")
356 endmacro()
357 else()
358 macro(add_pch _target_name _FILE)
359 endmacro()
360 endif()
361
362 function(CreateBootSectorTarget _target_name _asm_file _object_file _base_address)
363 get_filename_component(OBJECT_PATH ${_object_file} PATH)
364 get_filename_component(OBJECT_NAME ${_object_file} NAME)
365 file(MAKE_DIRECTORY ${OBJECT_PATH})
366 get_directory_property(defines COMPILE_DEFINITIONS)
367 get_directory_property(includes INCLUDE_DIRECTORIES)
368
369 foreach(arg ${defines})
370 set(result_defs ${result_defs} -D${arg})
371 endforeach()
372
373 foreach(arg ${includes})
374 set(result_incs -I${arg} ${result_incs})
375 endforeach()
376
377 add_custom_command(
378 OUTPUT ${_object_file}
379 COMMAND nasm -o ${_object_file} ${result_incs} ${result_defs} -f bin ${_asm_file}
380 DEPENDS ${_asm_file})
381 set_source_files_properties(${_object_file} PROPERTIES GENERATED TRUE)
382 add_custom_target(${_target_name} ALL DEPENDS ${_object_file})
383 endfunction()
384
385 function(CreateBootSectorTarget2 _target_name _asm_file _binary_file _base_address)
386 set(_object_file ${_binary_file}.o)
387
388 add_custom_command(
389 OUTPUT ${_object_file}
390 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}
391 DEPENDS ${_asm_file})
392
393 add_custom_command(
394 OUTPUT ${_binary_file}
395 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
396 # COMMAND objcopy --output-target binary --image-base 0x${_base_address} ${_object_file} ${_binary_file}
397 DEPENDS ${_object_file} native-obj2bin)
398
399 set_source_files_properties(${_object_file} ${_binary_file} PROPERTIES GENERATED TRUE)
400
401 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
402
403 endfunction()