586565b0954840486e45a3b53b20b1e080f332a1
[reactos.git] / gcc.cmake
1
2 # Linking
3 if(ARCH MATCHES i386)
4 link_directories("${REACTOS_SOURCE_DIR}/importlibs")
5 endif()
6 link_directories(${REACTOS_BINARY_DIR}/lib/3rdparty/mingw)
7 set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
8 set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
9 set(CMAKE_EXE_LINKER_FLAGS "-nodefaultlibs -nostdlib -Wl,--enable-auto-image-base -Wl,--disable-auto-import")
10 # -Wl,-T,${REACTOS_SOURCE_DIR}/global.lds
11 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
12
13 # Compiler Core
14 add_definitions(-pipe -fms-extensions)
15
16 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>")
17 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>")
18 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>")
19
20 # Debugging (Note: DWARF-4 on 4.5.1 when we ship)
21 add_definitions(-gdwarf-2 -g2 -femit-struct-debug-detailed=none -feliminate-unused-debug-types)
22
23 # Tuning
24 if(ARCH MATCHES i386)
25 add_definitions(-march=${OARCH} -mtune=${TUNE})
26 else()
27 add_definitions(-march=${OARCH})
28 endif()
29
30 # Warnings
31
32 add_definitions(-Wall -Wno-char-subscripts -Wpointer-arith -Wno-multichar -Wno-error=uninitialized -Wno-unused-value -Winvalid-pch)
33
34 if(ARCH MATCHES amd64)
35 add_definitions(-Wno-format)
36 elseif(ARCH MATCHES arm)
37 add_definitions(-Wno-attributes)
38 endif()
39
40 # Optimizations
41
42 if(OPTIMIZE STREQUAL "1")
43 add_definitions(-Os)
44 elseif(OPTIMIZE STREQUAL "2")
45 add_definitions(-Os)
46 elseif(OPTIMIZE STREQUAL "3")
47 add_definitions(-O1)
48 elseif(OPTIMIZE STREQUAL "4")
49 add_definitions(-O2)
50 elseif(OPTIMIZE STREQUAL "5")
51 add_definitions(-O3)
52 endif()
53
54 add_definitions(-fno-strict-aliasing)
55
56 if(ARCH MATCHES i386)
57 add_definitions(-mpreferred-stack-boundary=2 -fno-set-stack-executable -fno-optimize-sibling-calls)
58 if(OPTIMIZE STREQUAL "1")
59 add_definitions(-ftracer -momit-leaf-frame-pointer)
60 endif()
61 elseif(ARCH MATCHES amd64)
62 add_definitions(-mpreferred-stack-boundary=4)
63 if(OPTIMIZE STREQUAL "1")
64 add_definitions(-ftracer -momit-leaf-frame-pointer)
65 endif()
66 elseif(ARCH MATCHES arm)
67 if(OPTIMIZE STREQUAL "1")
68 add_definitions(-ftracer)
69 endif()
70 endif()
71
72 # Other
73 if(ARCH MATCHES amd64)
74 add_definitions(-U_X86_ -UWIN32)
75 elseif(ARCH MATCHES arm)
76 add_definitions(-U_UNICODE -UUNICODE)
77 add_definitions(-D__MSVCRT__) # DUBIOUS
78 endif()
79
80 # alternative arch name
81 if(ARCH MATCHES amd64)
82 set(ARCH2 x86_64)
83 else()
84 set(ARCH2 ${ARCH})
85 endif()
86
87 # Macros
88 macro(_PCH_GET_COMPILE_FLAGS _target_name _out_compile_flags _header_filename)
89 # Add the precompiled header to the build
90 get_filename_component(FILE ${_header_filename} NAME)
91 set(_gch_filename "${_target_name}_${FILE}.gch")
92 list(APPEND ${_out_compile_flags} -c ${_header_filename} -o ${_gch_filename})
93
94 # This gets us our includes
95 get_directory_property(DIRINC INCLUDE_DIRECTORIES)
96 foreach(item ${DIRINC})
97 list(APPEND ${_out_compile_flags} -I${item})
98 endforeach()
99
100 # This is a particular bit of undocumented/hacky magic I'm quite proud of
101 get_directory_property(_compiler_flags DEFINITIONS)
102 string(REPLACE "\ " "\t" _compiler_flags ${_compiler_flags})
103 list(APPEND ${_out_compile_flags} ${_compiler_flags})
104
105 # This gets any specific definitions that were added with set-target-property
106 get_target_property(_target_defs ${_target_name} COMPILE_DEFINITIONS)
107 if (_target_defs)
108 foreach(item ${_target_defs})
109 list(APPEND ${_out_compile_flags} -D${item})
110 endforeach()
111 endif()
112 endmacro()
113
114 macro(add_pch _target_name _header_filename _src_list)
115 get_filename_component(FILE ${_header_filename} NAME)
116 set(_gch_filename "${_target_name}_${FILE}.gch")
117 list(APPEND ${_src_list} ${_gch_filename})
118 _PCH_GET_COMPILE_FLAGS(${_target_name} _args ${_header_filename})
119 file(REMOVE ${_gch_filename})
120 add_custom_command(
121 OUTPUT ${_gch_filename}
122 COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1} ${_args}
123 DEPENDS ${_header_filename})
124 endmacro()
125
126 macro(add_linkerflag MODULE _flag)
127 set(NEW_LINKER_FLAGS ${_flag})
128 get_target_property(LINKER_FLAGS ${MODULE} LINK_FLAGS)
129 if(LINKER_FLAGS)
130 set(NEW_LINKER_FLAGS "${LINKER_FLAGS} ${NEW_LINKER_FLAGS}")
131 endif()
132 set_target_properties(${MODULE} PROPERTIES LINK_FLAGS ${NEW_LINKER_FLAGS})
133 endmacro()
134
135 # Optional 3rd parameter: stdcall stack bytes
136 macro(set_entrypoint MODULE ENTRYPOINT)
137 if(${ENTRYPOINT} STREQUAL "0")
138 add_linkerflag(${MODULE} "-Wl,-entry,0")
139 elseif(ARCH MATCHES i386)
140 set(_entrysymbol _${ENTRYPOINT})
141 if (${ARGC} GREATER 2)
142 set(_entrysymbol ${_entrysymbol}@${ARGV2})
143 endif()
144 add_linkerflag(${MODULE} "-Wl,-entry,${_entrysymbol}")
145 else()
146 add_linkerflag(${MODULE} "-Wl,-entry,${ENTRYPOINT}")
147 endif()
148 endmacro()
149
150 macro(set_subsystem MODULE SUBSYSTEM)
151 add_linkerflag(${MODULE} "-Wl,--subsystem,${SUBSYSTEM}")
152 endmacro()
153
154 macro(set_image_base MODULE IMAGE_BASE)
155 add_linkerflag(${MODULE} "-Wl,--image-base,${IMAGE_BASE}")
156 endmacro()
157
158 macro(set_module_type MODULE TYPE)
159
160 add_dependencies(${MODULE} psdk buildno_header)
161 if(${IS_CPP})
162 target_link_libraries(${MODULE} stlport -lsupc++ -lgcc)
163 endif()
164
165 if(${TYPE} MATCHES nativecui)
166 set_subsystem(${MODULE} native)
167 set_entrypoint(${MODULE} NtProcessStartup 4)
168 elseif(${TYPE} MATCHES win32gui)
169 set_subsystem(${MODULE} windows)
170 set_entrypoint(${MODULE} WinMainCRTStartup)
171 if(NOT IS_UNICODE)
172 target_link_libraries(${MODULE} mingw_main)
173 else()
174 target_link_libraries(${MODULE} mingw_wmain)
175 endif(NOT IS_UNICODE)
176 target_link_libraries(${MODULE} mingw_common)
177 elseif(${TYPE} MATCHES win32cui)
178 set_subsystem(${MODULE} console)
179 set_entrypoint(${MODULE} mainCRTStartup)
180 if(NOT IS_UNICODE)
181 target_link_libraries(${MODULE} mingw_main)
182 else()
183 target_link_libraries(${MODULE} mingw_wmain)
184 endif(NOT IS_UNICODE)
185 target_link_libraries(${MODULE} mingw_common)
186 elseif(${TYPE} MATCHES win32dll)
187 set_entrypoint(${MODULE} DllMain 12)
188 target_link_libraries(${MODULE} mingw_dllmain mingw_common)
189 if(DEFINED baseaddress_${MODULE})
190 set_image_base(${MODULE} ${baseaddress_${MODULE}})
191 else()
192 message(STATUS "${MODULE} has no base address")
193 endif()
194 elseif(${TYPE} MATCHES win32ocx)
195 set_entrypoint(${MODULE} DllMain 12)
196 target_link_libraries(${MODULE} mingw_dllmain mingw_common)
197 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
198 elseif(${TYPE} MATCHES cpl)
199 set_entrypoint(${MODULE} DllMain 12)
200 target_link_libraries(${MODULE} mingw_dllmain mingw_common)
201 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
202 elseif(${TYPE} MATCHES kernelmodedriver)
203 set_target_properties(${MODULE} PROPERTIES LINK_FLAGS "-Wl,--exclude-all-symbols -Wl,-file-alignment=0x1000 -Wl,-section-alignment=0x1000" SUFFIX ".sys")
204 set_entrypoint(${MODULE} DriverEntry 8)
205 set_subsystem(${MODULE} native)
206 set_image_base(${MODULE} 0x00010000)
207 add_dependencies(${MODULE} bugcodes)
208 elseif(${TYPE} MATCHES nativedll)
209 set_subsystem(${MODULE} native)
210 else()
211 message(FATAL_ERROR "Unknown module type : ${TYPE}")
212 endif()
213 endmacro()
214
215 macro(set_unicode)
216 add_definitions(-DUNICODE -D_UNICODE)
217 set(IS_UNICODE 1)
218 endmacro()
219
220 # Workaround lack of mingw RC support in cmake
221 macro(set_rc_compiler)
222 get_directory_property(defines COMPILE_DEFINITIONS)
223 get_directory_property(includes INCLUDE_DIRECTORIES)
224
225 foreach(arg ${defines})
226 set(rc_result_defs "${rc_result_defs} -D${arg}")
227 endforeach()
228
229 foreach(arg ${includes})
230 set(rc_result_incs "-I${arg} ${rc_result_incs}")
231 endforeach()
232
233 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${rc_result_defs} ${rc_result_incs} -i <SOURCE> -O coff -o <OBJECT>")
234 endmacro()
235
236 #idl files support
237 set(IDL_COMPILER native-widl)
238
239 if(ARCH MATCHES i386)
240 set(IDL_FLAGS -m32 --win32)
241 elseif(ARCH MATCHES amd64)
242 set(IDL_FLAGS -m64 --win64)
243 endif()
244
245 set(IDL_HEADER_ARG -h -o) #.h
246 set(IDL_TYPELIB_ARG -t -o) #.tlb
247 set(IDL_SERVER_ARG -s -S) #.c for server library
248 set(IDL_CLIENT_ARG -c -C) #.c for stub client library
249 set(IDL_PROXY_ARG -p -P)
250 set(IDL_INTERFACE_ARG -u -o)
251 set(IDL_DLLDATA_ARG --dlldata-only -o)
252
253
254 macro(add_importlibs MODULE)
255 add_dependency_node(${MODULE})
256 foreach(LIB ${ARGN})
257 target_link_libraries(${MODULE} ${CMAKE_BINARY_DIR}/importlibs/lib${LIB}.a)
258 add_dependencies(${MODULE} lib${LIB})
259 add_dependency_edge(${MODULE} ${LIB})
260 endforeach()
261 endmacro()
262
263 if(NOT ARCH MATCHES i386)
264 set(DECO_OPTION "-@")
265 endif()
266
267 macro(add_importlib_target _exports_file)
268
269 get_filename_component(_name ${_exports_file} NAME_WE)
270 get_filename_component(_extension ${_exports_file} EXT)
271 get_target_property(_suffix ${_name} SUFFIX)
272 if(${_suffix} STREQUAL "_suffix-NOTFOUND")
273 get_target_property(_type ${_name} TYPE)
274 if(${_type} MATCHES EXECUTABLE)
275 set(_suffix ".exe")
276 else()
277 set(_suffix ".dll")
278 endif()
279 endif()
280
281 if (${_extension} STREQUAL ".spec")
282
283 add_custom_command(
284 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
285 COMMAND native-spec2def -n=${_name}${_suffix} -a=${ARCH2} -d=${CMAKE_CURRENT_BINARY_DIR}/${_name}_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file}
286 COMMAND ${MINGW_PREFIX}dlltool --def ${CMAKE_CURRENT_BINARY_DIR}/${_name}_implib.def --kill-at --output-lib=${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
287 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file})
288
289 elseif(${_extension} STREQUAL ".def")
290 message("Use of def files for import libs is deprecated: ${_exports_file}")
291 add_custom_command(
292 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
293 COMMAND ${MINGW_PREFIX}dlltool --def ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} --kill-at --output-lib=${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a
294 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file})
295 else()
296 message(FATAL_ERROR "Unsupported exports file extension: ${_extension}")
297 endif()
298
299 add_custom_target(
300 lib${_name}
301 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.a)
302
303 endmacro()
304
305 macro(spec2def _dllname _spec_file)
306 get_filename_component(_file ${_spec_file} NAME_WE)
307 add_custom_command(
308 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
309 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}
310 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})
311 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
312 PROPERTIES GENERATED TRUE EXTERNAL_OBJECT TRUE)
313 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c PROPERTIES GENERATED TRUE)
314 list(APPEND SOURCE ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c)
315 endmacro()
316
317 macro(macro_mc FILE)
318 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)
319 endmacro()
320
321 #pseh lib, needed with mingw
322 set(PSEH_LIB "pseh")