|CMAKE] Use ExternalProject for host-tools build
[reactos.git] / sdk / cmake / gcc.cmake
1
2 # Show a note about ccache build
3 if(ENABLE_CCACHE)
4 message("-- Enabling ccache build - done")
5 set(CMAKE_C_USE_RESPONSE_FILE_FOR_INCLUDES OFF)
6 set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES OFF)
7 endif()
8
9 # PDB style debug info
10 if(NOT DEFINED SEPARATE_DBG)
11 set(SEPARATE_DBG FALSE)
12 endif()
13
14 # Dwarf based builds (no rsym)
15 if(CMAKE_BUILD_TYPE STREQUAL "Release")
16 set(NO_ROSSYM TRUE)
17 elseif(NOT DEFINED NO_ROSSYM)
18 set(NO_ROSSYM FALSE)
19 endif()
20
21 if(NOT DEFINED USE_PSEH3)
22 set(USE_PSEH3 1)
23 endif()
24
25 if(USE_PSEH3)
26 add_definitions(-D_USE_PSEH3=1)
27 endif()
28
29 if(NOT DEFINED USE_DUMMY_PSEH)
30 set(USE_DUMMY_PSEH 0)
31 endif()
32
33 if(USE_DUMMY_PSEH)
34 add_definitions(-D_USE_DUMMY_PSEH=1)
35 endif()
36
37 if(STACK_PROTECTOR)
38 add_compile_options(-fstack-protector-all)
39 endif()
40
41 # Compiler Core
42 add_compile_options(-pipe -fms-extensions -fno-strict-aliasing)
43
44 # Prevent GCC from searching any of the default directories.
45 # The case for C++ is handled through the reactos_c++ INTERFACE library
46 add_compile_options("$<$<NOT:$<COMPILE_LANGUAGE:CXX>>:-nostdinc>")
47
48 add_compile_options(-mstackrealign -fno-aggressive-loop-optimizations)
49
50 if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
51 add_compile_options("$<$<COMPILE_LANGUAGE:C>:-std=gnu99;-Wno-microsoft>")
52 set(CMAKE_LINK_DEF_FILE_FLAG "")
53 set(CMAKE_STATIC_LIBRARY_SUFFIX ".a")
54 set(CMAKE_LINK_LIBRARY_SUFFIX "")
55 set(CMAKE_CREATE_WIN32_EXE "")
56 set(CMAKE_C_COMPILE_OPTIONS_PIC "")
57 set(CMAKE_CXX_COMPILE_OPTIONS_PIC "")
58 set(CMAKE_C_COMPILE_OPTIONS_PIE "")
59 set(CMAKE_CXX_COMPILE_OPTIONS_PIE "")
60 set(CMAKE_ASM_FLAGS_DEBUG "")
61 set(CMAKE_C_FLAGS_DEBUG "")
62 set(CMAKE_CXX_FLAGS_DEBUG "")
63 endif()
64
65 if(DBG)
66 if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
67 add_compile_options("$<$<COMPILE_LANGUAGE:C>:-Wold-style-declaration>")
68 endif()
69 endif()
70
71 # Debugging
72 if(NOT CMAKE_BUILD_TYPE STREQUAL "Release")
73 if(SEPARATE_DBG)
74 add_compile_options(-gdwarf-2 -ggdb)
75 else()
76 add_compile_options(-gdwarf-2 -gstrict-dwarf)
77 if(NOT CMAKE_C_COMPILER_ID STREQUAL Clang)
78 add_compile_options(-femit-struct-debug-detailed=none -feliminate-unused-debug-symbols)
79 endif()
80 endif()
81 endif()
82
83 # Tuning
84 if(ARCH STREQUAL "i386")
85 add_compile_options(-march=${OARCH} -mtune=${TUNE})
86 else()
87 add_compile_options(-march=${OARCH})
88 endif()
89
90 # Warnings, errors
91 if((NOT CMAKE_BUILD_TYPE STREQUAL "Release") AND (NOT CMAKE_C_COMPILER_ID STREQUAL Clang))
92 add_compile_options(-Werror)
93 endif()
94
95 add_compile_options(-Wall -Wpointer-arith)
96 add_compile_options(-Wno-char-subscripts -Wno-multichar -Wno-unused-value)
97 add_compile_options(-Wno-unused-const-variable)
98 add_compile_options(-Wno-unused-local-typedefs)
99 add_compile_options(-Wno-deprecated)
100
101 if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
102 add_compile_options(-Wno-maybe-uninitialized)
103 endif()
104
105 if(ARCH STREQUAL "amd64")
106 add_compile_options(-Wno-format)
107 elseif(ARCH STREQUAL "arm")
108 add_compile_options(-Wno-attributes)
109 endif()
110
111 # Optimizations
112 # FIXME: Revisit this to see if we even need these levels
113 if(CMAKE_BUILD_TYPE STREQUAL "Release")
114 add_compile_options(-O2 -DNDEBUG)
115 else()
116 if(OPTIMIZE STREQUAL "1")
117 add_compile_options(-Os -ftracer)
118 elseif(OPTIMIZE STREQUAL "2")
119 add_compile_options(-Os)
120 elseif(OPTIMIZE STREQUAL "3")
121 add_compile_options(-Og)
122 elseif(OPTIMIZE STREQUAL "4")
123 add_compile_options(-O1)
124 elseif(OPTIMIZE STREQUAL "5")
125 add_compile_options(-O2)
126 elseif(OPTIMIZE STREQUAL "6")
127 add_compile_options(-O3)
128 elseif(OPTIMIZE STREQUAL "7")
129 add_compile_options(-Ofast)
130 endif()
131 endif()
132
133 # Link-time code generation
134 if(LTCG)
135 add_compile_options(-flto -fno-fat-lto-objects)
136 endif()
137
138 if(ARCH STREQUAL "i386")
139 add_compile_options(-fno-optimize-sibling-calls -fno-omit-frame-pointer)
140 if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
141 add_compile_options(-mpreferred-stack-boundary=3 -fno-set-stack-executable)
142 endif()
143 # FIXME: this doesn't work. CMAKE_BUILD_TYPE is always "Debug"
144 if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
145 add_compile_options(-momit-leaf-frame-pointer)
146 endif()
147 elseif(ARCH STREQUAL "amd64")
148 add_compile_options(-mpreferred-stack-boundary=4)
149 endif()
150
151 # Other
152 if(ARCH STREQUAL "amd64")
153 add_definitions(-U_X86_ -UWIN32)
154 elseif(ARCH STREQUAL "arm")
155 add_definitions(-U_UNICODE -UUNICODE)
156 add_definitions(-D__MSVCRT__) # DUBIOUS
157 endif()
158
159 add_definitions(-D_inline=__inline)
160
161 # Fix build with GLIBCXX + our c++ headers
162 add_definitions(-D_GLIBCXX_HAVE_BROKEN_VSWPRINTF)
163
164 # Alternative arch name
165 if(ARCH STREQUAL "amd64")
166 set(ARCH2 x86_64)
167 else()
168 set(ARCH2 ${ARCH})
169 endif()
170
171 if(SEPARATE_DBG)
172 # PDB style debug puts all dwarf debug info in a separate dbg file
173 message(STATUS "Building separate debug symbols")
174 file(MAKE_DIRECTORY ${REACTOS_BINARY_DIR}/symbols)
175 if(CMAKE_GENERATOR STREQUAL "Ninja")
176 # Those variables seems to be set but empty in newer CMake versions
177 # and Ninja generator relies on them to generate PDB name, so unset them.
178 unset(MSVC_C_ARCHITECTURE_ID)
179 unset(MSVC_CXX_ARCHITECTURE_ID)
180 set(CMAKE_DEBUG_SYMBOL_SUFFIX "")
181 set(SYMBOL_FILE <TARGET_PDB>)
182 else()
183 set(SYMBOL_FILE <TARGET>)
184 endif()
185 set(OBJCOPY ${CMAKE_OBJCOPY})
186 set(CMAKE_C_LINK_EXECUTABLE
187 "<CMAKE_C_COMPILER> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
188 "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
189 "${OBJCOPY} --strip-debug <TARGET>")
190 set(CMAKE_CXX_LINK_EXECUTABLE
191 "<CMAKE_CXX_COMPILER> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
192 "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
193 "${OBJCOPY} --strip-debug <TARGET>")
194 set(CMAKE_C_CREATE_SHARED_LIBRARY
195 "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
196 "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
197 "${OBJCOPY} --strip-debug <TARGET>")
198 set(CMAKE_CXX_CREATE_SHARED_LIBRARY
199 "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
200 "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
201 "${OBJCOPY} --strip-debug <TARGET>")
202 set(CMAKE_RC_CREATE_SHARED_LIBRARY
203 "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
204 "${OBJCOPY} --only-keep-debug <TARGET> ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}"
205 "${OBJCOPY} --strip-debug <TARGET>")
206 elseif(NO_ROSSYM)
207 # Dwarf-based build
208 message(STATUS "Generating a dwarf-based build (no rsym)")
209 set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
210 set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
211 set(CMAKE_C_CREATE_SHARED_LIBRARY "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
212 set(CMAKE_CXX_CREATE_SHARED_LIBRARY "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
213 set(CMAKE_RC_CREATE_SHARED_LIBRARY "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
214 else()
215 # Normal rsym build
216 get_target_property(RSYM native-rsym IMPORTED_LOCATION)
217
218 set(CMAKE_C_LINK_EXECUTABLE
219 "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
220 "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
221 set(CMAKE_CXX_LINK_EXECUTABLE
222 "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>"
223 "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
224 set(CMAKE_C_CREATE_SHARED_LIBRARY
225 "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
226 "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
227 set(CMAKE_CXX_CREATE_SHARED_LIBRARY
228 "<CMAKE_CXX_COMPILER> ${CMAKE_CXX_FLAGS} <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>"
229 "${RSYM} -s ${REACTOS_SOURCE_DIR} <TARGET> <TARGET>")
230 set(CMAKE_RC_CREATE_SHARED_LIBRARY
231 "<CMAKE_C_COMPILER> ${CMAKE_C_FLAGS} <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
232 endif()
233
234 set(CMAKE_C_CREATE_SHARED_MODULE ${CMAKE_C_CREATE_SHARED_LIBRARY})
235 set(CMAKE_CXX_CREATE_SHARED_MODULE ${CMAKE_CXX_CREATE_SHARED_LIBRARY})
236 set(CMAKE_RC_CREATE_SHARED_MODULE ${CMAKE_RC_CREATE_SHARED_LIBRARY})
237
238 set(CMAKE_EXE_LINKER_FLAGS "-nostdlib -Wl,--enable-auto-image-base,--disable-auto-import,--disable-stdcall-fixup,--gc-sections")
239 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
240 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
241
242 if((CMAKE_C_COMPILER_ID STREQUAL "GNU") AND
243 (NOT CMAKE_BUILD_TYPE STREQUAL "Release") AND
244 (NOT CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0))
245 # FIXME: Set this once Clang toolchain works with it
246 set(_compress_debug_sections_flag "-Wa,--compress-debug-sections")
247 endif()
248
249 set(CMAKE_C_COMPILE_OBJECT "${CCACHE} <CMAKE_C_COMPILER> <DEFINES> ${_compress_debug_sections_flag} <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
250 # FIXME: Once the GCC toolchain bugs are fixed, add _compress_debug_sections_flag to CXX too
251 set(CMAKE_CXX_COMPILE_OBJECT "${CCACHE} <CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
252 set(CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> ${_compress_debug_sections_flag} -x assembler-with-cpp -o <OBJECT> -I${REACTOS_SOURCE_DIR}/sdk/include/asm -I${REACTOS_BINARY_DIR}/sdk/include/asm <INCLUDES> <FLAGS> <DEFINES> -D__ASM__ -c <SOURCE>")
253
254 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -O coff <INCLUDES> <FLAGS> -DRC_INVOKED -D__WIN32__=1 -D__FLAT__=1 ${I18N_DEFS} <DEFINES> <SOURCE> <OBJECT>")
255 set(CMAKE_DEPFILE_FLAGS_RC "--preprocessor \"${MINGW_TOOLCHAIN_PREFIX}gcc${MINGW_TOOLCHAIN_SUFFIX} -E -xc-header -MMD -MF <DEPFILE> -MT <OBJECT>\" ")
256
257 # Optional 3rd parameter: stdcall stack bytes
258 function(set_entrypoint MODULE ENTRYPOINT)
259 if(${ENTRYPOINT} STREQUAL "0")
260 add_target_link_flags(${MODULE} "-Wl,-entry,0")
261 elseif(ARCH STREQUAL "i386")
262 set(_entrysymbol _${ENTRYPOINT})
263 if(${ARGC} GREATER 2)
264 set(_entrysymbol ${_entrysymbol}@${ARGV2})
265 endif()
266 add_target_link_flags(${MODULE} "-Wl,-entry,${_entrysymbol}")
267 else()
268 add_target_link_flags(${MODULE} "-Wl,-entry,${ENTRYPOINT}")
269 endif()
270 endfunction()
271
272 function(set_subsystem MODULE SUBSYSTEM)
273 add_target_link_flags(${MODULE} "-Wl,--subsystem,${SUBSYSTEM}:5.01")
274 endfunction()
275
276 function(set_image_base MODULE IMAGE_BASE)
277 add_target_link_flags(${MODULE} "-Wl,--image-base,${IMAGE_BASE}")
278 endfunction()
279
280 function(set_module_type_toolchain MODULE TYPE)
281 if((${TYPE} STREQUAL "kernelmodedriver") OR (${TYPE} STREQUAL "wdmdriver"))
282 add_target_link_flags(${MODULE} "-Wl,--exclude-all-symbols,-file-alignment=0x1000,-section-alignment=0x1000")
283 if(${TYPE} STREQUAL "wdmdriver")
284 add_target_link_flags(${MODULE} "-Wl,--wdmdriver")
285 endif()
286 #Disabled due to LD bug: ROSBE-154
287 #add_linker_script(${MODULE} ${REACTOS_SOURCE_DIR}/sdk/cmake/init-section.lds)
288 endif()
289
290 if(STACK_PROTECTOR)
291 target_link_libraries(${MODULE} gcc_ssp)
292 endif()
293 endfunction()
294
295 function(add_delay_importlibs _module)
296 get_target_property(_module_type ${_module} TYPE)
297 if(_module_type STREQUAL "STATIC_LIBRARY")
298 message(FATAL_ERROR "Cannot add delay imports to a static library")
299 endif()
300 foreach(_lib ${ARGN})
301 get_filename_component(_basename "${_lib}" NAME_WE)
302 target_link_libraries(${_module} lib${_basename}_delayed)
303 endforeach()
304 target_link_libraries(${_module} delayimp)
305 endfunction()
306
307 if(NOT ARCH STREQUAL "i386")
308 set(DECO_OPTION "-@")
309 endif()
310
311 function(fixup_load_config _target)
312 add_custom_command(TARGET ${_target} POST_BUILD
313 COMMAND native-pefixup "$<TARGET_FILE:${_target}>"
314 COMMENT "Patching in LOAD_CONFIG"
315 DEPENDS native-pefixup)
316 endfunction()
317
318 function(generate_import_lib _libname _dllname _spec_file)
319 # Generate the def for the import lib
320 add_custom_command(
321 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def
322 COMMAND native-spec2def -n=${_dllname} -a=${ARCH2} ${ARGN} --implib -d=${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
323 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
324 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def PROPERTIES EXTERNAL_OBJECT TRUE)
325
326 # Create normal importlib
327 _add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def)
328 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "IMPLIB" PREFIX "")
329
330 # Create delayed importlib
331 _add_library(${_libname}_delayed STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def)
332 set_target_properties(${_libname}_delayed PROPERTIES LINKER_LANGUAGE "IMPLIB_DELAYED" PREFIX "")
333 endfunction()
334
335 # Cute little hack to produce import libs
336 set(CMAKE_IMPLIB_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def <OBJECTS> --kill-at --output-lib=<TARGET>")
337 set(CMAKE_IMPLIB_DELAYED_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def <OBJECTS> --kill-at --output-delaylib=<TARGET>")
338 function(spec2def _dllname _spec_file)
339
340 cmake_parse_arguments(__spec2def "ADD_IMPORTLIB;NO_PRIVATE_WARNINGS;WITH_RELAY" "VERSION" "" ${ARGN})
341
342 # Get library basename
343 get_filename_component(_file ${_dllname} NAME_WE)
344
345 # Error out on anything else than spec
346 if(NOT ${_spec_file} MATCHES ".*\\.spec")
347 message(FATAL_ERROR "spec2def only takes spec files as input.")
348 endif()
349
350 if(__spec2def_WITH_RELAY)
351 set(__with_relay_arg "--with-tracing")
352 endif()
353
354 if(__spec2def_VERSION)
355 set(__version_arg "--version=0x${__spec2def_VERSION}")
356 endif()
357
358 # Generate exports def and C stubs file for the DLL
359 add_custom_command(
360 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
361 COMMAND native-spec2def -n=${_dllname} -a=${ARCH2} -d=${CMAKE_CURRENT_BINARY_DIR}/${_file}.def -s=${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c ${__with_relay_arg} ${__version_arg} ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
362 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
363
364 if(__spec2def_ADD_IMPORTLIB)
365 set(_extraflags)
366 if(__spec2def_NO_PRIVATE_WARNINGS)
367 set(_extraflags --no-private-warnings)
368 endif()
369
370 generate_import_lib(lib${_file} ${_dllname} ${_spec_file} ${_extraflags})
371 endif()
372 endfunction()
373
374 macro(macro_mc FLAG FILE)
375 set(COMMAND_MC ${CMAKE_MC_COMPILER} -u ${FLAG} -b -h ${CMAKE_CURRENT_BINARY_DIR}/ -r ${CMAKE_CURRENT_BINARY_DIR}/ ${FILE})
376 endmacro()
377
378 # PSEH lib, needed with mingw
379 set(PSEH_LIB "pseh")
380
381 function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
382 set(_object_file ${_binary_file}.o)
383
384 get_defines(_defines)
385 get_includes(_includes)
386
387 add_custom_command(
388 OUTPUT ${_object_file}
389 COMMAND ${CMAKE_ASM_COMPILER} -x assembler-with-cpp -o ${_object_file} -I${REACTOS_SOURCE_DIR}/sdk/include/asm -I${REACTOS_BINARY_DIR}/sdk/include/asm ${_includes} ${_defines} -D__ASM__ -c ${_asm_file}
390 DEPENDS ${_asm_file})
391
392 add_custom_command(
393 OUTPUT ${_binary_file}
394 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
395 # COMMAND objcopy --output-target binary --image-base 0x${_base_address} ${_object_file} ${_binary_file}
396 DEPENDS ${_object_file} native-obj2bin)
397
398 set_source_files_properties(${_object_file} ${_binary_file} PROPERTIES GENERATED TRUE)
399
400 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
401 endfunction()
402
403 function(allow_warnings __module)
404 # We don't allow warnings in trunk, this needs to be reworked. See CORE-6959.
405 #target_compile_options(${__module} PRIVATE "-Wno-error")
406 endfunction()
407
408 macro(add_asm_files _target)
409 list(APPEND ${_target} ${ARGN})
410 endmacro()
411
412 function(add_linker_script _target _linker_script_file)
413 get_filename_component(_file_full_path ${_linker_script_file} ABSOLUTE)
414 add_target_link_flags(${_target} "-Wl,-T,${_file_full_path}")
415 add_target_property(${_target} LINK_DEPENDS ${_file_full_path})
416 endfunction()
417
418 # Manage our C++ options
419 # we disable standard includes if we don't use the STL
420 add_compile_options("$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<NOT:$<IN_LIST:cppstl,$<TARGET_PROPERTY:LINK_LIBRARIES>>>>:-nostdinc>")
421 # we disable RTTI, unless said so
422 add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:$<IF:$<BOOL:$<TARGET_PROPERTY:WITH_CXX_RTTI>>,-frtti,-fno-rtti>>")
423 # We disable exceptions, unless said so
424 add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:$<IF:$<BOOL:$<TARGET_PROPERTY:WITH_CXX_EXCEPTIONS>>,-fexceptions,-fno-exceptions>>")
425
426 # Find default G++ libraries
427 add_library(libgcc STATIC IMPORTED)
428 execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libgcc.a OUTPUT_VARIABLE LIBGCC_LOCATION)
429 string(STRIP ${LIBGCC_LOCATION} LIBGCC_LOCATION)
430 set_target_properties(libgcc PROPERTIES IMPORTED_LOCATION ${LIBGCC_LOCATION})
431 # libgcc needs kernel32 imports, a CRT and msvcrtex
432 target_link_libraries(libgcc INTERFACE libkernel32 libmsvcrt msvcrtex)
433
434 add_library(libsupc++ STATIC IMPORTED GLOBAL)
435 execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libsupc++.a OUTPUT_VARIABLE LIBSUPCXX_LOCATION)
436 string(STRIP ${LIBSUPCXX_LOCATION} LIBSUPCXX_LOCATION)
437 set_target_properties(libsupc++ PROPERTIES IMPORTED_LOCATION ${LIBSUPCXX_LOCATION})
438 # libsupc++ requires libgcc
439 target_link_libraries(libsupc++ INTERFACE libgcc)
440
441 add_library(libmingwex STATIC IMPORTED)
442 execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libmingwex.a OUTPUT_VARIABLE LIBMINGWEX_LOCATION)
443 string(STRIP ${LIBMINGWEX_LOCATION} LIBMINGWEX_LOCATION)
444 set_target_properties(libmingwex PROPERTIES IMPORTED_LOCATION ${LIBMINGWEX_LOCATION})
445 # libmingwex requires a CRT and imports from kernel32
446 target_link_libraries(libmingwex INTERFACE libmsvcrt libkernel32)
447
448 add_library(libstdc++ STATIC IMPORTED GLOBAL)
449 execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libstdc++.a OUTPUT_VARIABLE LIBSTDCCXX_LOCATION)
450 string(STRIP ${LIBSTDCCXX_LOCATION} LIBSTDCCXX_LOCATION)
451 set_target_properties(libstdc++ PROPERTIES IMPORTED_LOCATION ${LIBSTDCCXX_LOCATION})
452 # libstdc++ requires libsupc++ and mingwex provided by GCC
453 target_link_libraries(libstdc++ INTERFACE libsupc++ libmingwex)
454 # this is for our SAL annotations
455 target_compile_definitions(libstdc++ INTERFACE "$<$<COMPILE_LANGUAGE:CXX>:PAL_STDCPP_COMPAT>")
456
457 # Create our alias libraries
458 add_library(cppstl ALIAS libstdc++)
459 add_library(cpprt ALIAS libsupc++)
460