# Show a note about ccache build if(ENABLE_CCACHE) message("-- Enabling ccache build - done") set(CMAKE_C_USE_RESPONSE_FILE_FOR_INCLUDES OFF) set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES OFF) endif() # PDB style debug info if(NOT DEFINED SEPARATE_DBG) set(SEPARATE_DBG FALSE) endif() # Dwarf based builds (no rsym) if(CMAKE_BUILD_TYPE STREQUAL "Release") set(NO_ROSSYM TRUE) elseif(NOT DEFINED NO_ROSSYM) set(NO_ROSSYM FALSE) endif() if(NOT DEFINED USE_PSEH3) set(USE_PSEH3 1) endif() if(USE_PSEH3) add_definitions(-D_USE_PSEH3=1) endif() if(NOT DEFINED USE_DUMMY_PSEH) set(USE_DUMMY_PSEH 0) endif() if(USE_DUMMY_PSEH) add_definitions(-D_USE_DUMMY_PSEH=1) endif() if(STACK_PROTECTOR) add_compile_options(-fstack-protector-all) endif() # Compiler Core add_compile_options(-pipe -fms-extensions -fno-strict-aliasing) # Prevent GCC from searching any of the default directories. # The case for C++ is handled through the reactos_c++ INTERFACE library add_compile_options("$<$>:-nostdinc>") add_compile_options(-mstackrealign -fno-aggressive-loop-optimizations) if(CMAKE_C_COMPILER_ID STREQUAL "Clang") add_compile_options("$<$:-std=gnu99;-Wno-microsoft>") set(CMAKE_LINK_DEF_FILE_FLAG "") set(CMAKE_STATIC_LIBRARY_SUFFIX ".a") set(CMAKE_LINK_LIBRARY_SUFFIX "") set(CMAKE_CREATE_WIN32_EXE "") set(CMAKE_C_COMPILE_OPTIONS_PIC "") set(CMAKE_CXX_COMPILE_OPTIONS_PIC "") set(CMAKE_C_COMPILE_OPTIONS_PIE "") set(CMAKE_CXX_COMPILE_OPTIONS_PIE "") set(CMAKE_ASM_FLAGS_DEBUG "") set(CMAKE_C_FLAGS_DEBUG "") set(CMAKE_CXX_FLAGS_DEBUG "") endif() if(DBG) if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang") add_compile_options("$<$:-Wold-style-declaration>") endif() endif() # Debugging if(NOT CMAKE_BUILD_TYPE STREQUAL "Release") if(SEPARATE_DBG) add_compile_options(-gdwarf-2 -ggdb) else() add_compile_options(-gdwarf-2 -gstrict-dwarf) if(NOT CMAKE_C_COMPILER_ID STREQUAL Clang) add_compile_options(-femit-struct-debug-detailed=none -feliminate-unused-debug-symbols) endif() endif() endif() # Tuning if(ARCH STREQUAL "i386") add_compile_options(-march=${OARCH} -mtune=${TUNE}) else() add_compile_options(-march=${OARCH}) endif() # Warnings, errors if((NOT CMAKE_BUILD_TYPE STREQUAL "Release") AND (NOT CMAKE_C_COMPILER_ID STREQUAL Clang)) add_compile_options(-Werror) endif() add_compile_options(-Wall -Wpointer-arith) add_compile_options(-Wno-char-subscripts -Wno-multichar -Wno-unused-value) add_compile_options(-Wno-unused-const-variable) add_compile_options(-Wno-unused-local-typedefs) add_compile_options(-Wno-deprecated) if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang") add_compile_options(-Wno-maybe-uninitialized) endif() if(ARCH STREQUAL "amd64") add_compile_options(-Wno-format) elseif(ARCH STREQUAL "arm") add_compile_options(-Wno-attributes) endif() # Optimizations # FIXME: Revisit this to see if we even need these levels if(CMAKE_BUILD_TYPE STREQUAL "Release") add_compile_options(-O2 -DNDEBUG) else() if(OPTIMIZE STREQUAL "1") add_compile_options(-Os -ftracer) elseif(OPTIMIZE STREQUAL "2") add_compile_options(-Os) elseif(OPTIMIZE STREQUAL "3") add_compile_options(-Og) elseif(OPTIMIZE STREQUAL "4") add_compile_options(-O1) elseif(OPTIMIZE STREQUAL "5") add_compile_options(-O2) elseif(OPTIMIZE STREQUAL "6") add_compile_options(-O3) elseif(OPTIMIZE STREQUAL "7") add_compile_options(-Ofast) endif() endif() # Link-time code generation if(LTCG) add_compile_options(-flto -fno-fat-lto-objects) endif() if(ARCH STREQUAL "i386") add_compile_options(-fno-optimize-sibling-calls -fno-omit-frame-pointer) if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang") add_compile_options(-mpreferred-stack-boundary=3 -fno-set-stack-executable) endif() # FIXME: this doesn't work. CMAKE_BUILD_TYPE is always "Debug" if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") add_compile_options(-momit-leaf-frame-pointer) endif() elseif(ARCH STREQUAL "amd64") add_compile_options(-mpreferred-stack-boundary=4) endif() # Other if(ARCH STREQUAL "amd64") add_definitions(-U_X86_ -UWIN32) elseif(ARCH STREQUAL "arm") add_definitions(-U_UNICODE -UUNICODE) add_definitions(-D__MSVCRT__) # DUBIOUS endif() add_definitions(-D_inline=__inline) # Fix build with GLIBCXX + our c++ headers add_definitions(-D_GLIBCXX_HAVE_BROKEN_VSWPRINTF) # Alternative arch name if(ARCH STREQUAL "amd64") set(ARCH2 x86_64) else() set(ARCH2 ${ARCH}) endif() if(SEPARATE_DBG) # PDB style debug puts all dwarf debug info in a separate dbg file message(STATUS "Building separate debug symbols") file(MAKE_DIRECTORY ${REACTOS_BINARY_DIR}/symbols) if(CMAKE_GENERATOR STREQUAL "Ninja") # Those variables seems to be set but empty in newer CMake versions # and Ninja generator relies on them to generate PDB name, so unset them. unset(MSVC_C_ARCHITECTURE_ID) unset(MSVC_CXX_ARCHITECTURE_ID) set(CMAKE_DEBUG_SYMBOL_SUFFIX "") set(SYMBOL_FILE ) else() set(SYMBOL_FILE ) endif() set(OBJCOPY ${CMAKE_OBJCOPY}) set(CMAKE_C_LINK_EXECUTABLE " -o " "${OBJCOPY} --only-keep-debug ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}" "${OBJCOPY} --strip-debug ") set(CMAKE_CXX_LINK_EXECUTABLE " -o " "${OBJCOPY} --only-keep-debug ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}" "${OBJCOPY} --strip-debug ") set(CMAKE_C_CREATE_SHARED_LIBRARY " -o " "${OBJCOPY} --only-keep-debug ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}" "${OBJCOPY} --strip-debug ") set(CMAKE_CXX_CREATE_SHARED_LIBRARY " -o " "${OBJCOPY} --only-keep-debug ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}" "${OBJCOPY} --strip-debug ") set(CMAKE_RC_CREATE_SHARED_LIBRARY " -o " "${OBJCOPY} --only-keep-debug ${REACTOS_BINARY_DIR}/symbols/${SYMBOL_FILE}" "${OBJCOPY} --strip-debug ") elseif(NO_ROSSYM) # Dwarf-based build message(STATUS "Generating a dwarf-based build (no rsym)") set(CMAKE_C_LINK_EXECUTABLE " ${CMAKE_C_FLAGS} -o ") set(CMAKE_CXX_LINK_EXECUTABLE " ${CMAKE_CXX_FLAGS} -o ") set(CMAKE_C_CREATE_SHARED_LIBRARY " ${CMAKE_C_FLAGS} -o ") set(CMAKE_CXX_CREATE_SHARED_LIBRARY " ${CMAKE_CXX_FLAGS} -o ") set(CMAKE_RC_CREATE_SHARED_LIBRARY " ${CMAKE_C_FLAGS} -o ") else() # Normal rsym build get_target_property(RSYM native-rsym IMPORTED_LOCATION) set(CMAKE_C_LINK_EXECUTABLE " ${CMAKE_C_FLAGS} -o " "${RSYM} -s ${REACTOS_SOURCE_DIR} ") set(CMAKE_CXX_LINK_EXECUTABLE " ${CMAKE_CXX_FLAGS} -o " "${RSYM} -s ${REACTOS_SOURCE_DIR} ") set(CMAKE_C_CREATE_SHARED_LIBRARY " ${CMAKE_C_FLAGS} -o " "${RSYM} -s ${REACTOS_SOURCE_DIR} ") set(CMAKE_CXX_CREATE_SHARED_LIBRARY " ${CMAKE_CXX_FLAGS} -o " "${RSYM} -s ${REACTOS_SOURCE_DIR} ") set(CMAKE_RC_CREATE_SHARED_LIBRARY " ${CMAKE_C_FLAGS} -o ") endif() set(CMAKE_C_CREATE_SHARED_MODULE ${CMAKE_C_CREATE_SHARED_LIBRARY}) set(CMAKE_CXX_CREATE_SHARED_MODULE ${CMAKE_CXX_CREATE_SHARED_LIBRARY}) set(CMAKE_RC_CREATE_SHARED_MODULE ${CMAKE_RC_CREATE_SHARED_LIBRARY}) set(CMAKE_EXE_LINKER_FLAGS "-nostdlib -Wl,--enable-auto-image-base,--disable-auto-import,--disable-stdcall-fixup,--gc-sections") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup") set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup") if((CMAKE_C_COMPILER_ID STREQUAL "GNU") AND (NOT CMAKE_BUILD_TYPE STREQUAL "Release") AND (NOT CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)) # FIXME: Set this once Clang toolchain works with it set(_compress_debug_sections_flag "-Wa,--compress-debug-sections") endif() set(CMAKE_C_COMPILE_OBJECT "${CCACHE} ${_compress_debug_sections_flag} -o -c ") # FIXME: Once the GCC toolchain bugs are fixed, add _compress_debug_sections_flag to CXX too set(CMAKE_CXX_COMPILE_OBJECT "${CCACHE} -o -c ") set(CMAKE_ASM_COMPILE_OBJECT " ${_compress_debug_sections_flag} -x assembler-with-cpp -o -I${REACTOS_SOURCE_DIR}/sdk/include/asm -I${REACTOS_BINARY_DIR}/sdk/include/asm -D__ASM__ -c ") set(CMAKE_RC_COMPILE_OBJECT " -O coff -DRC_INVOKED -D__WIN32__=1 -D__FLAT__=1 ${I18N_DEFS} ") set(CMAKE_DEPFILE_FLAGS_RC "--preprocessor \"${MINGW_TOOLCHAIN_PREFIX}gcc${MINGW_TOOLCHAIN_SUFFIX} -E -xc-header -MMD -MF -MT \" ") # Optional 3rd parameter: stdcall stack bytes function(set_entrypoint MODULE ENTRYPOINT) if(${ENTRYPOINT} STREQUAL "0") add_target_link_flags(${MODULE} "-Wl,-entry,0") elseif(ARCH STREQUAL "i386") set(_entrysymbol _${ENTRYPOINT}) if(${ARGC} GREATER 2) set(_entrysymbol ${_entrysymbol}@${ARGV2}) endif() add_target_link_flags(${MODULE} "-Wl,-entry,${_entrysymbol}") else() add_target_link_flags(${MODULE} "-Wl,-entry,${ENTRYPOINT}") endif() endfunction() function(set_subsystem MODULE SUBSYSTEM) add_target_link_flags(${MODULE} "-Wl,--subsystem,${SUBSYSTEM}:5.01") endfunction() function(set_image_base MODULE IMAGE_BASE) add_target_link_flags(${MODULE} "-Wl,--image-base,${IMAGE_BASE}") endfunction() function(set_module_type_toolchain MODULE TYPE) if((${TYPE} STREQUAL "kernelmodedriver") OR (${TYPE} STREQUAL "wdmdriver")) add_target_link_flags(${MODULE} "-Wl,--exclude-all-symbols,-file-alignment=0x1000,-section-alignment=0x1000") if(${TYPE} STREQUAL "wdmdriver") add_target_link_flags(${MODULE} "-Wl,--wdmdriver") endif() #Disabled due to LD bug: ROSBE-154 #add_linker_script(${MODULE} ${REACTOS_SOURCE_DIR}/sdk/cmake/init-section.lds) endif() if(STACK_PROTECTOR) target_link_libraries(${MODULE} gcc_ssp) endif() endfunction() function(add_delay_importlibs _module) get_target_property(_module_type ${_module} TYPE) if(_module_type STREQUAL "STATIC_LIBRARY") message(FATAL_ERROR "Cannot add delay imports to a static library") endif() foreach(_lib ${ARGN}) get_filename_component(_basename "${_lib}" NAME_WE) target_link_libraries(${_module} lib${_basename}_delayed) endforeach() target_link_libraries(${_module} delayimp) endfunction() if(NOT ARCH STREQUAL "i386") set(DECO_OPTION "-@") endif() function(fixup_load_config _target) add_custom_command(TARGET ${_target} POST_BUILD COMMAND native-pefixup "$" COMMENT "Patching in LOAD_CONFIG" DEPENDS native-pefixup) endfunction() function(generate_import_lib _libname _dllname _spec_file) # Generate the def for the import lib add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def COMMAND native-spec2def -n=${_dllname} -a=${ARCH2} ${ARGN} --implib -d=${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def) set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def PROPERTIES EXTERNAL_OBJECT TRUE) # Create normal importlib _add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def) set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "IMPLIB" PREFIX "") # Create delayed importlib _add_library(${_libname}_delayed STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def) set_target_properties(${_libname}_delayed PROPERTIES LINKER_LANGUAGE "IMPLIB_DELAYED" PREFIX "") endfunction() # Cute little hack to produce import libs set(CMAKE_IMPLIB_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def --kill-at --output-lib=") set(CMAKE_IMPLIB_DELAYED_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def --kill-at --output-delaylib=") function(spec2def _dllname _spec_file) cmake_parse_arguments(__spec2def "ADD_IMPORTLIB;NO_PRIVATE_WARNINGS;WITH_RELAY" "VERSION" "" ${ARGN}) # Get library basename get_filename_component(_file ${_dllname} NAME_WE) # Error out on anything else than spec if(NOT ${_spec_file} MATCHES ".*\\.spec") message(FATAL_ERROR "spec2def only takes spec files as input.") endif() if(__spec2def_WITH_RELAY) set(__with_relay_arg "--with-tracing") endif() if(__spec2def_VERSION) set(__version_arg "--version=0x${__spec2def_VERSION}") endif() # Generate exports def and C stubs file for the DLL add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c 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} DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def) if(__spec2def_ADD_IMPORTLIB) set(_extraflags) if(__spec2def_NO_PRIVATE_WARNINGS) set(_extraflags --no-private-warnings) endif() generate_import_lib(lib${_file} ${_dllname} ${_spec_file} ${_extraflags}) endif() endfunction() macro(macro_mc FLAG FILE) set(COMMAND_MC ${CMAKE_MC_COMPILER} -u ${FLAG} -b -h ${CMAKE_CURRENT_BINARY_DIR}/ -r ${CMAKE_CURRENT_BINARY_DIR}/ ${FILE}) endmacro() # PSEH lib, needed with mingw set(PSEH_LIB "pseh") function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address) set(_object_file ${_binary_file}.o) get_defines(_defines) get_includes(_includes) add_custom_command( OUTPUT ${_object_file} 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} DEPENDS ${_asm_file}) add_custom_command( OUTPUT ${_binary_file} COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address} # COMMAND objcopy --output-target binary --image-base 0x${_base_address} ${_object_file} ${_binary_file} DEPENDS ${_object_file} native-obj2bin) set_source_files_properties(${_object_file} ${_binary_file} PROPERTIES GENERATED TRUE) add_custom_target(${_target_name} ALL DEPENDS ${_binary_file}) endfunction() function(allow_warnings __module) # We don't allow warnings in trunk, this needs to be reworked. See CORE-6959. #target_compile_options(${__module} PRIVATE "-Wno-error") endfunction() macro(add_asm_files _target) list(APPEND ${_target} ${ARGN}) endmacro() function(add_linker_script _target _linker_script_file) get_filename_component(_file_full_path ${_linker_script_file} ABSOLUTE) add_target_link_flags(${_target} "-Wl,-T,${_file_full_path}") add_target_property(${_target} LINK_DEPENDS ${_file_full_path}) endfunction() # Manage our C++ options # we disable standard includes if we don't use the STL add_compile_options("$<$,$>>>:-nostdinc>") # we disable RTTI, unless said so add_compile_options("$<$:$>,-frtti,-fno-rtti>>") # We disable exceptions, unless said so add_compile_options("$<$:$>,-fexceptions,-fno-exceptions>>") # Find default G++ libraries add_library(libgcc STATIC IMPORTED) execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libgcc.a OUTPUT_VARIABLE LIBGCC_LOCATION) string(STRIP ${LIBGCC_LOCATION} LIBGCC_LOCATION) set_target_properties(libgcc PROPERTIES IMPORTED_LOCATION ${LIBGCC_LOCATION}) # libgcc needs kernel32 imports, a CRT and msvcrtex target_link_libraries(libgcc INTERFACE libkernel32 libmsvcrt msvcrtex) add_library(libsupc++ STATIC IMPORTED GLOBAL) execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libsupc++.a OUTPUT_VARIABLE LIBSUPCXX_LOCATION) string(STRIP ${LIBSUPCXX_LOCATION} LIBSUPCXX_LOCATION) set_target_properties(libsupc++ PROPERTIES IMPORTED_LOCATION ${LIBSUPCXX_LOCATION}) # libsupc++ requires libgcc target_link_libraries(libsupc++ INTERFACE libgcc) add_library(libmingwex STATIC IMPORTED) execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libmingwex.a OUTPUT_VARIABLE LIBMINGWEX_LOCATION) string(STRIP ${LIBMINGWEX_LOCATION} LIBMINGWEX_LOCATION) set_target_properties(libmingwex PROPERTIES IMPORTED_LOCATION ${LIBMINGWEX_LOCATION}) # libmingwex requires a CRT and imports from kernel32 target_link_libraries(libmingwex INTERFACE libmsvcrt libkernel32) add_library(libstdc++ STATIC IMPORTED GLOBAL) execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libstdc++.a OUTPUT_VARIABLE LIBSTDCCXX_LOCATION) string(STRIP ${LIBSTDCCXX_LOCATION} LIBSTDCCXX_LOCATION) set_target_properties(libstdc++ PROPERTIES IMPORTED_LOCATION ${LIBSTDCCXX_LOCATION}) # libstdc++ requires libsupc++ and mingwex provided by GCC target_link_libraries(libstdc++ INTERFACE libsupc++ libmingwex) # this is for our SAL annotations target_compile_definitions(libstdc++ INTERFACE "$<$:PAL_STDCPP_COMPAT>") # Create our alias libraries add_library(cppstl ALIAS libstdc++) add_library(cpprt ALIAS libsupc++)