a8707d66e5939d3ee31daa7dd4b47cb11d5f81c0
[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} ${rc_result_incs} -i <OBJECT> -o <OBJECT>"
191 "<CMAKE_RC_COMPILER> -i <OBJECT> -J res -O coff -o <OBJECT>" PARENT_SCOPE)
192 endfunction()
193
194 function(add_delay_importlibs MODULE)
195 foreach(LIB ${ARGN})
196 target_link_libraries(${MODULE} ${CMAKE_BINARY_DIR}/importlibs/lib${LIB}_delayed.a)
197 add_dependencies(${MODULE} lib${LIB}_delayed)
198 endforeach()
199 target_link_libraries(${MODULE} delayimp)
200 endfunction()
201
202 if(NOT ARCH MATCHES i386)
203 set(DECO_OPTION "-@")
204 endif()
205
206 function(add_importlib_target _exports_file)
207
208 get_filename_component(_name ${_exports_file} NAME_WE)
209 get_filename_component(_extension ${_exports_file} EXT)
210 get_target_property(_suffix ${_name} SUFFIX)
211 if(${_suffix} STREQUAL "_suffix-NOTFOUND")
212 get_target_property(_type ${_name} TYPE)
213 if(${_type} MATCHES EXECUTABLE)
214 set(_suffix ".exe")
215 else()
216 set(_suffix ".dll")
217 endif()
218 endif()
219
220 if (${_extension} STREQUAL ".spec")
221
222 # Normal importlib creation
223 add_custom_command(
224 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
225 COMMAND native-spec2def -n=${_name}${_suffix} -a=${ARCH2} -d=${CMAKE_CURRENT_BINARY_DIR}/${_name}_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file}
226 COMMAND ${MINGW_PREFIX}dlltool --def ${CMAKE_CURRENT_BINARY_DIR}/${_name}_implib.def --kill-at --output-lib=${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
227 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} native-spec2def)
228
229 # Delayed importlib creation
230 add_custom_command(
231 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_delayed.a
232 COMMAND native-spec2def -n=${_name}${_suffix} -a=${ARCH2} -d=${CMAKE_CURRENT_BINARY_DIR}/${_name}_delayed_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file}
233 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
234 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} native-spec2def)
235
236 elseif(${_extension} STREQUAL ".def")
237 message("Use of def files for import libs is deprecated: ${_exports_file}")
238 add_custom_command(
239 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
240 COMMAND ${MINGW_PREFIX}dlltool --def ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} --kill-at --output-lib=${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
241 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file})
242 add_custom_command(
243 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_delayed.a
244 COMMAND ${MINGW_PREFIX}dlltool --def ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} --kill-at --output-delaylib ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_delayed.a
245 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file})
246 else()
247 message(FATAL_ERROR "Unsupported exports file extension: ${_extension}")
248 endif()
249
250 # Normal importlib target
251 add_custom_target(
252 lib${_name}
253 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a)
254 # Delayed importlib target
255 add_custom_target(
256 lib${_name}_delayed
257 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_delayed.a)
258
259 endfunction()
260
261 function(spec2def _dllname _spec_file)
262
263 if(${ARGC} GREATER 2)
264 set(_file ${ARGV2})
265 else()
266 get_filename_component(_file ${_spec_file} NAME_WE)
267 endif()
268
269 add_custom_command(
270 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
271 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}
272 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
273 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
274 PROPERTIES GENERATED TRUE EXTERNAL_OBJECT TRUE)
275 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c PROPERTIES GENERATED TRUE)
276 endfunction()
277
278 macro(macro_mc FILE)
279 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)
280 endmacro()
281
282 #pseh lib, needed with mingw
283 set(PSEH_LIB "pseh")
284
285 # Macros
286 if(PCH)
287 macro(_PCH_GET_COMPILE_FLAGS _target_name _out_compile_flags _header_filename)
288 # Add the precompiled header to the build
289 get_filename_component(_FILE ${_header_filename} NAME)
290 set(_gch_filename "${_FILE}.gch")
291 list(APPEND ${_out_compile_flags} -c ${_header_filename} -o ${_gch_filename})
292
293 # This gets us our includes
294 get_directory_property(DIRINC INCLUDE_DIRECTORIES)
295 foreach(item ${DIRINC})
296 list(APPEND ${_out_compile_flags} -I${item})
297 endforeach()
298
299 # This our definitions
300 get_directory_property(_compiler_flags DEFINITIONS)
301 list(APPEND ${_out_compile_flags} ${_compiler_flags})
302
303 # This gets any specific definitions that were added with set-target-property
304 get_target_property(_target_defs ${_target_name} COMPILE_DEFINITIONS)
305 if (_target_defs)
306 foreach(item ${_target_defs})
307 list(APPEND ${_out_compile_flags} -D${item})
308 endforeach()
309 endif()
310
311 if(IS_CPP)
312 list(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS})
313 else()
314 list(APPEND ${_out_compile_flags} ${CMAKE_C_FLAGS})
315 endif()
316
317 separate_arguments(${_out_compile_flags})
318 endmacro()
319
320 macro(add_pch _target_name _FILE)
321 set(_header_filename ${CMAKE_CURRENT_SOURCE_DIR}/${_FILE})
322 get_filename_component(_basename ${_FILE} NAME)
323 set(_gch_filename ${_basename}.gch)
324 _PCH_GET_COMPILE_FLAGS(${_target_name} _args ${_header_filename})
325
326 if(IS_CPP)
327 set(__lang CXX)
328 set(__compiler ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1})
329 else()
330 set(__lang C)
331 set(__compiler ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1})
332 endif()
333
334 add_custom_command(OUTPUT ${_gch_filename}
335 COMMAND ${__compiler} ${_args}
336 IMPLICIT_DEPENDS ${__lang} ${_header_filename}
337 DEPENDS ${_header_filename} ${ARGN})
338 get_target_property(_src_files ${_target_name} SOURCES)
339 foreach(_item in ${_src_files})
340 get_source_file_property(__src_lang ${_item} LANGUAGE)
341 if(__src_lang STREQUAL __lang)
342 set_source_files_properties(${_item} PROPERTIES COMPILE_FLAGS "-fpch-preprocess" OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_gch_filename})
343 endif()
344 endforeach()
345 #set dependency checking : depends on precompiled header only whixh already depends on deeper header
346 set_target_properties(${_target_name} PROPERTIES IMPLICIT_DEPENDS_INCLUDE_TRANSFORM "\"${_basename}\"=;<${_basename}>=")
347 endmacro()
348 else()
349 macro(add_pch _target_name _FILE)
350 endmacro()
351 endif()
352
353 function(CreateBootSectorTarget _target_name _asm_file _object_file _base_address)
354 get_filename_component(OBJECT_PATH ${_object_file} PATH)
355 get_filename_component(OBJECT_NAME ${_object_file} NAME)
356 file(MAKE_DIRECTORY ${OBJECT_PATH})
357 get_directory_property(defines COMPILE_DEFINITIONS)
358 get_directory_property(includes INCLUDE_DIRECTORIES)
359
360 foreach(arg ${defines})
361 set(result_defs ${result_defs} -D${arg})
362 endforeach()
363
364 foreach(arg ${includes})
365 set(result_incs -I${arg} ${result_incs})
366 endforeach()
367
368 add_custom_command(
369 OUTPUT ${_object_file}
370 COMMAND nasm -o ${_object_file} ${result_incs} ${result_defs} -f bin ${_asm_file}
371 DEPENDS ${_asm_file})
372 set_source_files_properties(${_object_file} PROPERTIES GENERATED TRUE)
373 add_custom_target(${_target_name} ALL DEPENDS ${_object_file})
374 endfunction()
375
376 function(CreateBootSectorTarget2 _target_name _asm_file _binary_file _base_address)
377 set(_object_file ${_binary_file}.o)
378
379 add_custom_command(
380 OUTPUT ${_object_file}
381 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}
382 DEPENDS ${_asm_file})
383
384 add_custom_command(
385 OUTPUT ${_binary_file}
386 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
387 # COMMAND objcopy --output-target binary --image-base 0x${_base_address} ${_object_file} ${_binary_file}
388 DEPENDS ${_object_file} native-obj2bin)
389
390 set_source_files_properties(${_object_file} ${_binary_file} PROPERTIES GENERATED TRUE)
391
392 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
393
394 endfunction()