[SHELL32] Fix Control_RunDLLW (#5400)
[reactos.git] / reactos / 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(SEPARATE_DBG)
13 file(MAKE_DIRECTORY ${REACTOS_BINARY_DIR}/symbols)
14 endif()
15
16 # Compiler Core
17 add_compile_flags("-pipe -fms-extensions")
18
19 #bug
20 #file(TO_NATIVE_PATH ${REACTOS_SOURCE_DIR} REACTOS_SOURCE_DIR_NATIVE)
21 #workaround
22 set(REACTOS_SOURCE_DIR_NATIVE ${REACTOS_SOURCE_DIR})
23 if(CMAKE_HOST_SYSTEM_NAME MATCHES Windows)
24 string(REPLACE "/" "\\" REACTOS_SOURCE_DIR_NATIVE ${REACTOS_SOURCE_DIR})
25 endif()
26 add_compile_flags("-fdebug-prefix-map=${REACTOS_SOURCE_DIR_NATIVE}=ReactOS")
27
28 # Debugging
29 if(SEPARATE_DBG)
30 add_compile_flags("-gdwarf-2 -g2")
31 else()
32 add_compile_flags("-gstabs+")
33 endif()
34
35 # Do not allow warnings
36 add_compile_flags("-Werror")
37
38 # For some reason, cmake sets -fPIC, and we don't want it
39 if(DEFINED CMAKE_SHARED_LIBRARY_ASM_FLAGS)
40 string(REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_ASM_FLAGS ${CMAKE_SHARED_LIBRARY_ASM_FLAGS})
41 endif()
42
43 # Tuning
44 if(ARCH MATCHES i386)
45 add_compile_flags("-march=${OARCH} -mtune=${TUNE}")
46 else()
47 add_compile_flags("-march=${OARCH}")
48 endif()
49
50 # Warnings
51 add_compile_flags("-Wall -Wno-char-subscripts -Wpointer-arith -Wno-multichar -Wno-unused-value")
52
53 if(GCC_VERSION VERSION_LESS 4.6)
54 add_compile_flags("-Wno-error=uninitialized")
55 elseif((GCC_VERSION VERSION_GREATER 4.6 OR GCC_VERSION VERSION_EQUAL 4.6) AND GCC_VERSION VERSION_LESS 4.7)
56 add_compile_flags("-Wno-error=unused-but-set-variable -Wno-error=uninitialized")
57 elseif(GCC_VERSION VERSION_EQUAL 4.7 OR GCC_VERSION VERSION_GREATER 4.7)
58 add_compile_flags("-Wno-error=unused-but-set-variable -Wno-error=maybe-uninitialized -Wno-error=delete-non-virtual-dtor -Wno-error=narrowing")
59 endif()
60
61 if(ARCH MATCHES amd64)
62 add_compile_flags("-Wno-format")
63 elseif(ARCH MATCHES arm)
64 add_compile_flags("-Wno-attributes")
65 endif()
66
67 # Optimizations
68 if(OPTIMIZE STREQUAL "1")
69 add_compile_flags("-Os")
70 elseif(OPTIMIZE STREQUAL "2")
71 add_compile_flags("-Os")
72 elseif(OPTIMIZE STREQUAL "3")
73 add_compile_flags("-O1")
74 elseif(OPTIMIZE STREQUAL "4")
75 add_compile_flags("-O2")
76 elseif(OPTIMIZE STREQUAL "5")
77 add_compile_flags("-O3")
78 endif()
79
80 # Link-time code generation
81 if(LTCG)
82 add_compile_flags("-flto -Wno-error=clobbered")
83 endif()
84
85 add_compile_flags("-fno-strict-aliasing")
86
87 if(ARCH MATCHES i386)
88 add_compile_flags("-mpreferred-stack-boundary=2 -fno-set-stack-executable -fno-optimize-sibling-calls -fno-omit-frame-pointer")
89 if(OPTIMIZE STREQUAL "1")
90 add_compile_flags("-ftracer -momit-leaf-frame-pointer")
91 endif()
92 elseif(ARCH MATCHES amd64)
93 add_compile_flags("-mpreferred-stack-boundary=4")
94 if(OPTIMIZE STREQUAL "1")
95 add_compile_flags("-ftracer -momit-leaf-frame-pointer")
96 endif()
97 elseif(ARCH MATCHES arm)
98 if(OPTIMIZE STREQUAL "1")
99 add_compile_flags("-ftracer")
100 endif()
101 endif()
102
103 # Other
104 if(ARCH MATCHES amd64)
105 add_definitions(-U_X86_ -UWIN32)
106 elseif(ARCH MATCHES arm)
107 add_definitions(-U_UNICODE -UUNICODE)
108 add_definitions(-D__MSVCRT__) # DUBIOUS
109 endif()
110
111 add_definitions(-D_inline=__inline)
112
113 # alternative arch name
114 if(ARCH MATCHES amd64)
115 set(ARCH2 x86_64)
116 else()
117 set(ARCH2 ${ARCH})
118 endif()
119
120 if(SEPARATE_DBG)
121 # PDB style debug puts all dwarf debug info in a separate dbg file
122 set(OBJCOPY ${CMAKE_OBJCOPY})
123 set(CMAKE_C_LINK_EXECUTABLE
124 "<CMAKE_C_COMPILER> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
125 "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/<TARGET>.dbg"
126 "${OBJCOPY} --strip-debug <TARGET>")
127 set(CMAKE_CXX_LINK_EXECUTABLE
128 "<CMAKE_CXX_COMPILER> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
129 "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/<TARGET>.dbg"
130 "${OBJCOPY} --strip-debug <TARGET>")
131 set(CMAKE_C_CREATE_SHARED_LIBRARY
132 "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
133 "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/<TARGET>.dbg"
134 "${OBJCOPY} --strip-debug <TARGET>")
135 set(CMAKE_CXX_CREATE_SHARED_LIBRARY
136 "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
137 "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/<TARGET>.dbg"
138 "${OBJCOPY} --strip-debug <TARGET>")
139 set(CMAKE_RC_CREATE_SHARED_LIBRARY
140 "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
141 "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/<TARGET>.dbg"
142 "${OBJCOPY} --strip-debug <TARGET>")
143 else()
144 # Normal rsym build
145 get_target_property(RSYM native-rsym IMPORTED_LOCATION_NOCONFIG)
146 set(CMAKE_C_LINK_EXECUTABLE
147 "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
148 "${RSYM} <TARGET> <TARGET>")
149 set(CMAKE_CXX_LINK_EXECUTABLE
150 "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
151 "${RSYM} <TARGET> <TARGET>")
152 set(CMAKE_C_CREATE_SHARED_LIBRARY
153 "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
154 "${RSYM} <TARGET> <TARGET>")
155 set(CMAKE_CXX_CREATE_SHARED_LIBRARY
156 "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
157 "${RSYM} <TARGET> <TARGET>")
158 set(CMAKE_RC_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} <TARGET> <TARGET>")
161 endif()
162
163 set(CMAKE_EXE_LINKER_FLAGS "-nostdlib -Wl,--enable-auto-image-base,--disable-auto-import,--disable-stdcall-fixup")
164 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
165
166 SET(CMAKE_C_COMPILE_OBJECT "${CCACHE} <CMAKE_C_COMPILER> <DEFINES> <FLAGS> -o <OBJECT> -c <SOURCE>")
167 SET(CMAKE_CXX_COMPILE_OBJECT "${CCACHE} <CMAKE_CXX_COMPILER> <DEFINES> <FLAGS> -o <OBJECT> -c <SOURCE>")
168 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> <DEFINES> -D__ASM__ -c <SOURCE>")
169
170 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -O coff <FLAGS> -DRC_INVOKED -D__WIN32__=1 -D__FLAT__=1 ${I18N_DEFS} <DEFINES> <SOURCE> <OBJECT>")
171 set(CMAKE_DEPFILE_FLAGS_RC "--preprocessor \"<CMAKE_C_COMPILER> -E -xc-header -MMD -MF <DEPFILE> -MT <OBJECT>\"")
172
173 # Optional 3rd parameter: stdcall stack bytes
174 function(set_entrypoint MODULE ENTRYPOINT)
175 if(${ENTRYPOINT} STREQUAL "0")
176 add_target_link_flags(${MODULE} "-Wl,-entry,0")
177 elseif(ARCH MATCHES i386)
178 set(_entrysymbol _${ENTRYPOINT})
179 if(${ARGC} GREATER 2)
180 set(_entrysymbol ${_entrysymbol}@${ARGV2})
181 endif()
182 add_target_link_flags(${MODULE} "-Wl,-entry,${_entrysymbol}")
183 else()
184 add_target_link_flags(${MODULE} "-Wl,-entry,${ENTRYPOINT}")
185 endif()
186 endfunction()
187
188 function(set_subsystem MODULE SUBSYSTEM)
189 add_target_link_flags(${MODULE} "-Wl,--subsystem,${SUBSYSTEM}")
190 endfunction()
191
192 function(set_image_base MODULE IMAGE_BASE)
193 add_target_link_flags(${MODULE} "-Wl,--image-base,${IMAGE_BASE}")
194 endfunction()
195
196 function(set_module_type_toolchain MODULE TYPE)
197 if(IS_CPP)
198 target_link_libraries(${MODULE} -lstdc++ -lsupc++ -lgcc -lmingwex)
199 endif()
200
201 if(${TYPE} STREQUAL kernelmodedriver)
202 add_target_link_flags(${MODULE} "-Wl,--exclude-all-symbols,-file-alignment=0x1000,-section-alignment=0x1000")
203 endif()
204 endfunction()
205
206 function(set_rc_compiler)
207 endfunction()
208
209 function(add_delay_importlibs MODULE)
210 foreach(LIB ${ARGN})
211 target_link_libraries(${MODULE} lib${LIB}_delayed)
212 endforeach()
213 target_link_libraries(${MODULE} delayimp)
214 endfunction()
215
216 if(NOT ARCH MATCHES i386)
217 set(DECO_OPTION "-@")
218 endif()
219
220 # Cute little hack to produce import libs
221 set(CMAKE_IMPLIB_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def <OBJECTS> --kill-at --output-lib=<TARGET>")
222 set(CMAKE_IMPLIB_DELAYED_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def <OBJECTS> --kill-at --output-delaylib=<TARGET>")
223 function(spec2def _dllname _spec_file)
224 # do we also want to add impotlib targets?
225 if(${ARGC} GREATER 2)
226 if(${ARGN} STREQUAL "ADD_IMPORTLIB")
227 set(__add_importlib TRUE)
228 else()
229 message(FATAL_ERROR "Wrong argument passed to spec2def, ${ARGN}")
230 endif()
231 endif()
232
233 # get library basename
234 get_filename_component(_file ${_dllname} NAME_WE)
235
236 # error out on anything else than spec
237 if(NOT ${_spec_file} MATCHES ".*\\.spec")
238 message(FATAL_ERROR "spec2def only takes spec files as input.")
239 endif()
240
241 # generate exports def and stubs C file for the module
242 add_custom_command(
243 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
244 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}
245 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
246 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def PROPERTIES EXTERNAL_OBJECT TRUE)
247
248 if(__add_importlib)
249 # generate the def for the export lib
250 add_custom_command(
251 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}_implib.def
252 COMMAND native-spec2def -n=${_dllname} -a=${ARCH2} -d=${CMAKE_CURRENT_BINARY_DIR}/${_file}_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
253 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
254 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}_implib.def PROPERTIES EXTERNAL_OBJECT TRUE)
255
256 #create normal importlib
257 _add_library(lib${_file} STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_file}_implib.def)
258 set_target_properties(lib${_file} PROPERTIES LINKER_LANGUAGE "IMPLIB" PREFIX "")
259
260 #create delayed importlib
261 _add_library(lib${_file}_delayed STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_file}_implib.def)
262 set_target_properties(lib${_file}_delayed PROPERTIES LINKER_LANGUAGE "IMPLIB_DELAYED" PREFIX "")
263 endif()
264 endfunction()
265
266 macro(macro_mc FLAG FILE)
267 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)
268 endmacro()
269
270 #pseh lib, needed with mingw
271 set(PSEH_LIB "pseh")
272
273 # Macros
274 if(PCH)
275 macro(_PCH_GET_COMPILE_FLAGS _target_name _out_compile_flags _header_filename)
276 # Add the precompiled header to the build
277 get_filename_component(_FILE ${_header_filename} NAME)
278 set(_gch_filename "${_FILE}.gch")
279 list(APPEND ${_out_compile_flags} -c ${_header_filename} -o ${_gch_filename})
280
281 # This gets us our includes
282 get_directory_property(DIRINC INCLUDE_DIRECTORIES)
283 foreach(item ${DIRINC})
284 list(APPEND ${_out_compile_flags} -I${item})
285 endforeach()
286
287 # This our definitions
288 get_directory_property(_compiler_flags DEFINITIONS)
289 list(APPEND ${_out_compile_flags} ${_compiler_flags})
290
291 # This gets any specific definitions that were added with set-target-property
292 get_target_property(_target_defs ${_target_name} COMPILE_DEFINITIONS)
293 if(_target_defs)
294 foreach(item ${_target_defs})
295 list(APPEND ${_out_compile_flags} -D${item})
296 endforeach()
297 endif()
298
299 if(IS_CPP)
300 list(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS})
301 else()
302 list(APPEND ${_out_compile_flags} ${CMAKE_C_FLAGS})
303 endif()
304
305 separate_arguments(${_out_compile_flags})
306 endmacro()
307
308 macro(add_pch _target_name _FILE)
309 set(_header_filename ${CMAKE_CURRENT_SOURCE_DIR}/${_FILE})
310 get_filename_component(_basename ${_FILE} NAME)
311 set(_gch_filename ${_basename}.gch)
312 _PCH_GET_COMPILE_FLAGS(${_target_name} _args ${_header_filename})
313
314 if(IS_CPP)
315 set(__lang CXX)
316 set(__compiler ${CCACHE} ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1})
317 else()
318 set(__lang C)
319 set(__compiler ${CCACHE} ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1})
320 endif()
321
322 add_custom_command(OUTPUT ${_gch_filename}
323 COMMAND ${__compiler} ${_args}
324 IMPLICIT_DEPENDS ${__lang} ${_header_filename}
325 DEPENDS ${_header_filename} ${ARGN})
326 get_target_property(_src_files ${_target_name} SOURCES)
327 add_target_compile_flags(${_target_name} "-fpch-preprocess -Winvalid-pch -Wno-error=invalid-pch")
328 foreach(_item in ${_src_files})
329 get_source_file_property(__src_lang ${_item} LANGUAGE)
330 if(__src_lang STREQUAL __lang)
331 set_source_files_properties(${_item} PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_gch_filename})
332 endif()
333 endforeach()
334 #set dependency checking : depends on precompiled header only which already depends on deeper header
335 set_target_properties(${_target_name} PROPERTIES IMPLICIT_DEPENDS_INCLUDE_TRANSFORM "\"${_basename}\"=;<${_basename}>=")
336 endmacro()
337 else()
338 macro(add_pch _target_name _FILE)
339 endmacro()
340 endif()
341
342 function(CreateBootSectorTarget _target_name _asm_file _object_file _base_address)
343 get_filename_component(OBJECT_PATH ${_object_file} PATH)
344 get_filename_component(OBJECT_NAME ${_object_file} NAME)
345 file(MAKE_DIRECTORY ${OBJECT_PATH})
346 get_directory_property(defines COMPILE_DEFINITIONS)
347 get_directory_property(includes INCLUDE_DIRECTORIES)
348
349 foreach(arg ${defines})
350 set(result_defs ${result_defs} -D${arg})
351 endforeach()
352
353 foreach(arg ${includes})
354 set(result_incs -I${arg} ${result_incs})
355 endforeach()
356
357 add_custom_command(
358 OUTPUT ${_object_file}
359 COMMAND nasm -o ${_object_file} ${result_incs} ${result_defs} -f bin ${_asm_file}
360 DEPENDS ${_asm_file})
361 set_source_files_properties(${_object_file} PROPERTIES GENERATED TRUE)
362 add_custom_target(${_target_name} ALL DEPENDS ${_object_file})
363 endfunction()
364
365 function(CreateBootSectorTarget2 _target_name _asm_file _binary_file _base_address)
366 set(_object_file ${_binary_file}.o)
367
368 add_custom_command(
369 OUTPUT ${_object_file}
370 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}
371 DEPENDS ${_asm_file})
372
373 add_custom_command(
374 OUTPUT ${_binary_file}
375 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
376 # COMMAND objcopy --output-target binary --image-base 0x${_base_address} ${_object_file} ${_binary_file}
377 DEPENDS ${_object_file} native-obj2bin)
378
379 set_source_files_properties(${_object_file} ${_binary_file} PROPERTIES GENERATED TRUE)
380
381 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
382
383 endfunction()
384
385 function(allow_warnings __module)
386 add_target_compile_flags(${__module} "-Wno-error")
387 endfunction()