cmake: Don't overwrite cmake default flags

Also makes sure we don't push our warning settings onto 3rd party libraries
This commit is contained in:
TellowKrinkle 2021-04-17 17:29:55 -05:00 committed by tellowkrinkle
parent 7bb3f41fdc
commit c072c0e82b
5 changed files with 57 additions and 128 deletions

View File

@ -192,7 +192,7 @@ for ARG in "$@"; do
--dev|--devel ) flags="$flags -DCMAKE_BUILD_TYPE=Devel" ; build="$root/build_dev";; --dev|--devel ) flags="$flags -DCMAKE_BUILD_TYPE=Devel" ; build="$root/build_dev";;
--dbg|--debug ) flags="$flags -DCMAKE_BUILD_TYPE=Debug" ; build="$root/build_dbg";; --dbg|--debug ) flags="$flags -DCMAKE_BUILD_TYPE=Debug" ; build="$root/build_dbg";;
--rel|--release ) flags="$flags -DCMAKE_BUILD_TYPE=Release" ; build="$root/build_rel";; --rel|--release ) flags="$flags -DCMAKE_BUILD_TYPE=Release" ; build="$root/build_rel";;
--prof ) flags="$flags -DCMAKE_BUILD_TYPE=Prof" ; build="$root/build_prof";; --prof ) flags="$flags -DCMAKE_BUILD_TYPE=RelWithDebInfo"; build="$root/build_prof";;
--strip ) flags="$flags -DCMAKE_BUILD_STRIP=TRUE" ;; --strip ) flags="$flags -DCMAKE_BUILD_STRIP=TRUE" ;;
--sdl12 ) flags="$flags -DSDL2_API=FALSE" ;; --sdl12 ) flags="$flags -DSDL2_API=FALSE" ;;
--use-system-yaml ) flags="$flags -DUSE_SYSTEM_YAML=TRUE" ;; --use-system-yaml ) flags="$flags -DUSE_SYSTEM_YAML=TRUE" ;;

View File

@ -9,6 +9,9 @@
# control link flags : -DUSER_CMAKE_LD_FLAGS="ldflags" # control link flags : -DUSER_CMAKE_LD_FLAGS="ldflags"
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Extra preprocessor definitions that will be added to all pcsx2 builds
set(PCSX2_DEFS "")
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Misc option # Misc option
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
@ -18,7 +21,7 @@ option(USE_SYSTEM_YAML "Uses a system version of yaml, if found")
if(DISABLE_BUILD_DATE OR openSUSE) if(DISABLE_BUILD_DATE OR openSUSE)
message(STATUS "Disabling the inclusion of the binary compile date.") message(STATUS "Disabling the inclusion of the binary compile date.")
add_definitions(-DDISABLE_BUILD_DATE) list(APPEND PCSX2_DEFS DISABLE_BUILD_DATE)
endif() endif()
option(USE_VTUNE "Plug VTUNE to profile GS JIT.") option(USE_VTUNE "Plug VTUNE to profile GS JIT.")
@ -42,11 +45,10 @@ option(GTK2_API "Use GTK2 api (legacy)")
if(PACKAGE_MODE) if(PACKAGE_MODE)
# Compile all source codes with those defines # Compile all source codes with those defines
add_definitions( list(APPEND PCSX2_DEFS
-DPLUGIN_DIR_COMPILATION=${CMAKE_INSTALL_FULL_LIBDIR}/PCSX2 PLUGIN_DIR_COMPILATION=${CMAKE_INSTALL_FULL_LIBDIR}/PCSX2
-DGAMEINDEX_DIR_COMPILATION=${CMAKE_INSTALL_FULL_DATADIR}/PCSX2 GAMEINDEX_DIR_COMPILATION=${CMAKE_INSTALL_FULL_DATADIR}/PCSX2
-DDOC_DIR_COMPILATION=${CMAKE_INSTALL_FULL_DOCDIR} DOC_DIR_COMPILATION=${CMAKE_INSTALL_FULL_DOCDIR})
)
endif() endif()
if(APPLE) if(APPLE)
@ -77,10 +79,23 @@ endif()
# Note without the CMAKE_BUILD_TYPE options the value is still defined to "" # Note without the CMAKE_BUILD_TYPE options the value is still defined to ""
# Ensure that the value set by the User is correct to avoid some bad behavior later # Ensure that the value set by the User is correct to avoid some bad behavior later
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
if(NOT CMAKE_BUILD_TYPE MATCHES "Debug|Devel|Release|Prof") if(NOT CMAKE_BUILD_TYPE MATCHES "Debug|Devel|MinSizeRel|RelWithDebInfo|Release")
set(CMAKE_BUILD_TYPE Devel) set(CMAKE_BUILD_TYPE Devel)
message(STATUS "BuildType set to ${CMAKE_BUILD_TYPE} by default") message(STATUS "BuildType set to ${CMAKE_BUILD_TYPE} by default")
endif() endif()
# Add Devel build type
set(CMAKE_C_FLAGS_DEVEL "${CMAKE_C_FLAGS_RELWITHDEBINFO}"
CACHE STRING "Flags used by the C compiler during development builds" FORCE)
set(CMAKE_CXX_FLAGS_DEVEL "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}"
CACHE STRING "Flags used by the C++ compiler during development builds" FORCE)
set(CMAKE_LINKER_FLAGS_DEVEL "${CMAKE_LINKER_FLAGS_RELWITHDEBINFO}"
CACHE STRING "Flags used for linking binaries during development builds" FORCE)
set(CMAKE_SHARED_LINKER_FLAGS_DEVEL "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}"
CACHE STRING "Flags used for linking shared libraries during development builds" FORCE)
if(CMAKE_CONFIGURATION_TYPES)
list(INSERT CMAKE_CONFIGURATION_TYPES 0 Devel)
endif()
mark_as_advanced(CMAKE_C_FLAGS_PROF CMAKE_CXX_FLAGS_PROF CMAKE_LINKER_FLAGS_PROF CMAKE_SHARED_LINKER_FLAGS_PROF)
# AVX2 doesn't play well with gdb # AVX2 doesn't play well with gdb
if(CMAKE_BUILD_TYPE MATCHES "Debug") if(CMAKE_BUILD_TYPE MATCHES "Debug")
SET(DISABLE_ADVANCE_SIMD ON) SET(DISABLE_ADVANCE_SIMD ON)
@ -144,7 +159,7 @@ if(${PCSX2_TARGET_ARCHITECTURES} MATCHES "i386")
endif() endif()
endif() endif()
add_definitions(-D_ARCH_32=1 -D_M_X86=1 -D_M_X86_32=1) list(APPEND PCSX2_DEFS _ARCH_32=1 _M_X86=1 _M_X86_32=1)
set(_ARCH_32 1) set(_ARCH_32 1)
set(_M_X86 1) set(_M_X86 1)
set(_M_X86_32 1) set(_M_X86_32 1)
@ -164,7 +179,7 @@ elseif(${PCSX2_TARGET_ARCHITECTURES} MATCHES "x86_64")
set(ARCH_FLAG "-march=native") set(ARCH_FLAG "-march=native")
endif() endif()
endif() endif()
add_definitions(-D_ARCH_64=1 -D_M_X86=1 -D_M_X86_64=1 -D__M_X86_64=1) list(APPEND PCSX2_DEFS _ARCH_64=1 _M_X86=1 _M_X86_64=1 __M_X86_64=1)
set(_ARCH_64 1) set(_ARCH_64 1)
set(_M_X86 1) set(_M_X86 1)
set(_M_X86_64 1) set(_M_X86_64 1)
@ -174,6 +189,8 @@ else()
message(FATAL_ERROR "Unsupported architecture: ${PCSX2_TARGET_ARCHITECTURES}") message(FATAL_ERROR "Unsupported architecture: ${PCSX2_TARGET_ARCHITECTURES}")
endif() endif()
string(REPLACE " " ";" ARCH_FLAG_LIST "${ARCH_FLAG}")
add_compile_options("${ARCH_FLAG_LIST}")
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Control GCC flags # Control GCC flags
@ -190,16 +207,6 @@ endif()
# Use in release mode # Use in release mode
# ${CMAKE_CXX_FLAGS_RELEASE} = "-O3 -DNDEBUG" # ${CMAKE_CXX_FLAGS_RELEASE} = "-O3 -DNDEBUG"
#-------------------------------------------------------------------------------
# Do not use default cmake flags
#-------------------------------------------------------------------------------
set(CMAKE_C_FLAGS_DEBUG "")
set(CMAKE_CXX_FLAGS_DEBUG "")
set(CMAKE_C_FLAGS_DEVEL "")
set(CMAKE_CXX_FLAGS_DEVEL "")
set(CMAKE_C_FLAGS_RELEASE "")
set(CMAKE_CXX_FLAGS_RELEASE "")
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Remove bad default option # Remove bad default option
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
@ -221,18 +228,10 @@ option(USE_PGO_OPTIMIZE "Enable PGO optimization (use profile)")
# Note1: Builtin strcmp/memcmp was proved to be slower on Mesa than stdlib version. # Note1: Builtin strcmp/memcmp was proved to be slower on Mesa than stdlib version.
# Note2: float operation SSE is impacted by the PCSX2 SSE configuration. In particular, flush to zero denormal. # Note2: float operation SSE is impacted by the PCSX2 SSE configuration. In particular, flush to zero denormal.
set(COMMON_FLAG "-pipe -fvisibility=hidden -pthread -fno-builtin-strcmp -fno-builtin-memcmp -mfpmath=sse -fno-operator-names") add_compile_options(-pipe -fvisibility=hidden -pthread -fno-builtin-strcmp -fno-builtin-memcmp -mfpmath=sse -fno-operator-names)
if(USE_VTUNE) if(USE_VTUNE)
set(COMMON_FLAG "${COMMON_FLAG} -DENABLE_VTUNE") list(APPEND PCSX2_DEFS ENABLE_VTUNE)
endif()
# Remove FORTIFY_SOURCE when compiling as debug, because it spams a lot of warnings on clang due to no optimization.
# Should probably be checked on gcc as well, as the USE_CLANG might not be needed.
if (USE_CLANG AND CMAKE_BUILD_TYPE MATCHES "Debug")
set(HARDENING_FLAG "-Wformat -Wformat-security")
else()
set(HARDENING_FLAG "-D_FORTIFY_SOURCE=2 -Wformat -Wformat-security")
endif() endif()
# -Wno-attributes: "always_inline function might not be inlinable" <= real spam (thousand of warnings!!!) # -Wno-attributes: "always_inline function might not be inlinable" <= real spam (thousand of warnings!!!)
@ -246,149 +245,74 @@ endif()
# -Wno-stringop-truncation: Who comes up with these compiler warnings, anyways? # -Wno-stringop-truncation: Who comes up with these compiler warnings, anyways?
# -Wno-stringop-overflow: Probably the same people as this one... # -Wno-stringop-overflow: Probably the same people as this one...
set(DEFAULT_WARNINGS "-Wall -Wextra -Wno-attributes -Wno-unused-function -Wno-unused-parameter -Wno-missing-field-initializers -Wno-deprecated-declarations -Wno-format -Wno-format-security -Wno-overloaded-virtual") set(DEFAULT_WARNINGS -Wall -Wextra -Wno-attributes -Wno-unused-function -Wno-unused-parameter -Wno-missing-field-initializers -Wno-deprecated-declarations -Wno-format -Wno-format-security -Wno-overloaded-virtual)
if (NOT USE_ICC) if (NOT USE_ICC)
set(DEFAULT_WARNINGS "${DEFAULT_WARNINGS} -Wno-unused-value ") list(APPEND DEFAULT_WARNINGS -Wno-unused-value)
endif() endif()
if (USE_CLANG) if (USE_CLANG)
set(DEFAULT_WARNINGS "${DEFAULT_WARNINGS} -Wno-overloaded-virtual ") list(APPEND DEFAULT_WARNINGS -Wno-overloaded-virtual)
endif() endif()
if (USE_GCC) if (USE_GCC)
set(DEFAULT_WARNINGS "${DEFAULT_WARNINGS} -Wno-stringop-truncation -Wno-stringop-overflow ") list(APPEND DEFAULT_WARNINGS -Wno-stringop-truncation -Wno-stringop-overflow)
endif() endif()
# -Wstrict-aliasing=n: to fix one day aliasing issue. n=1/2/3 # -Wstrict-aliasing=n: to fix one day aliasing issue. n=1/2/3
if (USE_ICC) if (USE_ICC)
set(AGGRESSIVE_WARNING "-Wstrict-aliasing ") set(AGGRESSIVE_WARNING -Wstrict-aliasing)
else() else()
set(AGGRESSIVE_WARNING "-Wstrict-aliasing -Wstrict-overflow=1 ") set(AGGRESSIVE_WARNING -Wstrict-aliasing -Wstrict-overflow=1)
endif() endif()
if (USE_CLANG) if (USE_CLANG)
# -Wno-deprecated-register: glib issue... # -Wno-deprecated-register: glib issue...
set(DEFAULT_WARNINGS "${DEFAULT_WARNINGS} -Wno-deprecated-register -Wno-c++14-extensions") list(APPEND DEFAULT_WARNINGS -Wno-deprecated-register -Wno-c++14-extensions)
set(DBG "-g -fno-omit-frame-pointer")
elseif (USE_ICC)
set(DBG "-g -fno-omit-frame-pointer")
elseif (USE_GCC)
set(DBG "-ggdb3 -fno-omit-frame-pointer")
endif() endif()
if (USE_PGO_GENERATE OR USE_PGO_OPTIMIZE) if (USE_PGO_GENERATE OR USE_PGO_OPTIMIZE)
set(PGO_FLAGS "-fprofile-dir=${CMAKE_SOURCE_DIR}/profile") add_compile_options("-fprofile-dir=${CMAKE_SOURCE_DIR}/profile")
endif() endif()
if (USE_PGO_GENERATE) if (USE_PGO_GENERATE)
set(PGO_FLAGS "${PGO_FLAGS} -fprofile-generate") add_compile_options(-fprofile-generate)
endif() endif()
if(USE_PGO_OPTIMIZE) if(USE_PGO_OPTIMIZE)
set(PGO_FLAGS "${PGO_FLAGS} -fprofile-use") add_compile_options(-fprofile-use)
endif() endif()
if(CMAKE_BUILD_TYPE MATCHES "Debug") list(APPEND PCSX2_DEFS
set(DEBUG_FLAG "${DBG} -DPCSX2_DEVBUILD -DPCSX2_DEBUG -D_DEBUG") "$<$<CONFIG:Debug>:PCSX2_DEVBUILD;PCSX2_DEBUG;_DEBUG>"
elseif(CMAKE_BUILD_TYPE MATCHES "Devel") "$<$<CONFIG:Devel>:PCSX2_DEVBUILD;_DEVEL>")
set(DEBUG_FLAG "${DBG} -DNDEBUG -DPCSX2_DEVBUILD -D_DEVEL")
elseif(CMAKE_BUILD_TYPE MATCHES "Release")
set(DEBUG_FLAG "-DNDEBUG")
elseif(CMAKE_BUILD_TYPE MATCHES "Prof")
# Keep frame pointer and debug information for profiler tool
set(DEBUG_FLAG "-g -fno-omit-frame-pointer -DNDEBUG")
endif()
if (USE_ASAN) if (USE_ASAN)
set(ASAN_FLAG "-fsanitize=address ${DBG} -DASAN_WORKAROUND") add_compile_options(-fsanitize=address)
else() list(APPEND PCSX2_DEFS ASAN_WORKAROUND)
set(ASAN_FLAG "")
endif() endif()
if(NOT DEFINED OPTIMIZATION_FLAG) if(USE_CLANG AND TIMETRACE)
if (CMAKE_BUILD_TYPE STREQUAL Debug) add_compile_options(-ftime-trace)
if (USE_GCC)
set(OPTIMIZATION_FLAG -Og)
else()
set(OPTIMIZATION_FLAG -O0)
endif()
else()
set(OPTIMIZATION_FLAG -O2)
endif()
endif() endif()
if (NOT DEFINED PGO) set(PCSX2_WARNINGS ${DEFAULT_WARNINGS} ${AGGRESSIVE_WARNING})
set(PGO "none")
set(GCOV_LIBRARIES "")
else()
set(GCOV_LIBRARIES "-lgcov")
endif()
if(USE_CLANG)
if(TIMETRACE)
set(COMMON_FLAG "${COMMON_FLAG} -ftime-trace ")
endif()
endif()
# Note: -DGTK_DISABLE_DEPRECATED can be used to test a build without gtk deprecated feature. It could be useful to port to a newer API
# Disabling the hardening flags for the moment, as they spam quite a bit. ${HARDENING_FLAG}
set(DEFAULT_GCC_FLAG "${ARCH_FLAG} ${COMMON_FLAG} ${DEFAULT_WARNINGS} ${AGGRESSIVE_WARNING} ${DEBUG_FLAG} ${ASAN_FLAG} ${OPTIMIZATION_FLAG} ${LTO_FLAGS} ${PGO_FLAGS}")
# c++ only flags
set(DEFAULT_CPP_FLAG "${DEFAULT_GCC_FLAG} -Wno-invalid-offsetof")
#-------------------------------------------------------------------------------
# Allow user to set some default flags
# Note: string STRIP must be used to remove trailing and leading spaces.
# See policy CMP0004
#-------------------------------------------------------------------------------
# TODO: once we completely clean all flags management, this mess could be cleaned ;)
### linker flags
if(DEFINED USER_CMAKE_LD_FLAGS)
message(STATUS "Pcsx2 is very sensible with gcc flags, so use USER_CMAKE_LD_FLAGS at your own risk !!!")
string(STRIP "${USER_CMAKE_LD_FLAGS}" USER_CMAKE_LD_FLAGS)
else()
set(USER_CMAKE_LD_FLAGS "")
endif()
# ask the linker to strip the binary
if(CMAKE_BUILD_STRIP) if(CMAKE_BUILD_STRIP)
string(STRIP "${USER_CMAKE_LD_FLAGS} -s" USER_CMAKE_LD_FLAGS) add_link_options(-s)
endif() endif()
### c flags
# Note CMAKE_C_FLAGS is also send to the linker.
# By default allow build on amd64 machine
if(DEFINED USER_CMAKE_C_FLAGS)
message(STATUS "Pcsx2 is very sensible with gcc flags, so use USER_CMAKE_C_FLAGS at your own risk !!!")
string(STRIP "${USER_CMAKE_C_FLAGS}" CMAKE_C_FLAGS)
endif()
# Use some default machine flags
string(STRIP "${CMAKE_C_FLAGS} ${DEFAULT_GCC_FLAG}" CMAKE_C_FLAGS)
### C++ flags
# Note CMAKE_CXX_FLAGS is also send to the linker.
# By default allow build on amd64 machine
if(DEFINED USER_CMAKE_CXX_FLAGS)
message(STATUS "Pcsx2 is very sensible with gcc flags, so use USER_CMAKE_CXX_FLAGS at your own risk !!!")
string(STRIP "${USER_CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS)
endif()
# Use some default machine flags
string(STRIP "${CMAKE_CXX_FLAGS} ${DEFAULT_CPP_FLAG}" CMAKE_CXX_FLAGS)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# MacOS-specific things # MacOS-specific things
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.9) set(CMAKE_OSX_DEPLOYMENT_TARGET 10.9)
if (APPLE AND ${CMAKE_OSX_DEPLOYMENT_TARGET} VERSION_LESS 10.14) if (APPLE AND ${CMAKE_OSX_DEPLOYMENT_TARGET} VERSION_LESS 10.14 AND NOT ${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 10)
# Older versions of the macOS stdlib don't have operator new(size_t, align_val_t) # Older versions of the macOS stdlib don't have operator new(size_t, align_val_t)
# Disable use of them with this flag # Disable use of them with this flag
# Not great, but also no worse that what we were getting before we turned on C++17 # Not great, but also no worse that what we were getting before we turned on C++17
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-aligned-allocation") add_compile_options(-fno-aligned-allocation)
endif() endif()
# CMake defaults the suffix for modules to .so on macOS but wx tells us that the # CMake defaults the suffix for modules to .so on macOS but wx tells us that the
@ -410,6 +334,5 @@ if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
list(APPEND CMAKE_PREFIX_PATH "/usr") list(APPEND CMAKE_PREFIX_PATH "/usr")
endif() endif()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-dead_strip,-dead_strip_dylibs") add_link_options(-Wl,-dead_strip,-dead_strip_dylibs)
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-dead_strip,-dead_strip_dylibs")
endif() endif()

View File

@ -106,6 +106,8 @@ foreach(library Utilities Utilities_NO_TLS)
target_link_libraries(${library} PRIVATE ${LIBC_LIBRARIES} PUBLIC wxWidgets::all) target_link_libraries(${library} PRIVATE ${LIBC_LIBRARIES} PUBLIC wxWidgets::all)
target_compile_features(${library} PUBLIC cxx_std_17) target_compile_features(${library} PUBLIC cxx_std_17)
target_include_directories(${library} PUBLIC ../../../3rdparty/include ../../include PRIVATE ../../include/Utilities .) target_include_directories(${library} PUBLIC ../../../3rdparty/include ../../include PRIVATE ../../include/Utilities .)
target_compile_definitions(${library} PUBLIC "${PCSX2_DEFS}")
target_compile_options(${library} PRIVATE "${PCSX2_WARNINGS}")
if(COMMAND target_precompile_headers) if(COMMAND target_precompile_headers)
target_precompile_headers(${library} PRIVATE PrecompiledHeader.h) target_precompile_headers(${library} PRIVATE PrecompiledHeader.h)
endif() endif()

View File

@ -69,6 +69,8 @@ set(x86emitterFinalLibs
add_pcsx2_lib(x86emitter "${x86emitterFinalSources}" "${x86emitterFinalLibs}" "${x86emitterFinalFlags}") add_pcsx2_lib(x86emitter "${x86emitterFinalSources}" "${x86emitterFinalLibs}" "${x86emitterFinalFlags}")
target_compile_features(x86emitter PUBLIC cxx_std_17) target_compile_features(x86emitter PUBLIC cxx_std_17)
target_include_directories(x86emitter PUBLIC ../../include PRIVATE ../../include/x86emitter) target_include_directories(x86emitter PUBLIC ../../include PRIVATE ../../include/x86emitter)
target_compile_definitions(x86emitter PUBLIC "${PCSX2_DEFS}")
target_compile_options(x86emitter PRIVATE "${PCSX2_WARNINGS}")
if(COMMAND target_precompile_headers) if(COMMAND target_precompile_headers)
target_precompile_headers(x86emitter PRIVATE PrecompiledHeader.h) target_precompile_headers(x86emitter PRIVATE PrecompiledHeader.h)

View File

@ -1295,6 +1295,8 @@ endif()
add_pcsx2_executable(${Output} "${pcsx2FinalSources}" "${pcsx2FinalLibs}" "${pcsx2FinalFlags}") add_pcsx2_executable(${Output} "${pcsx2FinalSources}" "${pcsx2FinalLibs}" "${pcsx2FinalFlags}")
target_compile_features(${Output} PRIVATE cxx_std_17) target_compile_features(${Output} PRIVATE cxx_std_17)
target_compile_definitions(${Output} PUBLIC "${PCSX2_DEFS}")
target_compile_options(${Output} PRIVATE "${PCSX2_WARNINGS}")
# additonal include directories # additonal include directories
target_include_directories(${Output} PRIVATE target_include_directories(${Output} PRIVATE