Skip to content

Commit

Permalink
CMake: Tidy up platform/architecture detection
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Dec 30, 2023
1 parent 1823753 commit 0e15de7
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 438 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/zstd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ add_library(pcsx2-zstd
zstd/lib/decompress/zstd_decompress.c
)

if(NOT MSVC AND ${PCSX2_TARGET_ARCHITECTURES} MATCHES "x86_64")
if(_M_X86 AND (NOT MSVC OR USE_CLANG_CL))
target_sources(pcsx2-zstd PRIVATE zstd/lib/decompress/huf_decompress_amd64.S)
endif()

Expand Down
13 changes: 5 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Setting it to a range tells it that it supports the features on the newer
# versions as well, avoiding setting policies.
cmake_minimum_required(VERSION 3.16...3.24)
cmake_minimum_required(VERSION 3.16...3.25)

#Enabling this cmake policy gets rid of warnings regarding LTO.
# Enabling this cmake policy gets rid of warnings regarding LTO.
cmake_policy(SET CMP0069 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)

Expand All @@ -11,7 +11,7 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
endif()

# Project Name
project(Pcsx2)
project(Pcsx2 C CXX)

# Variable to check that people use the good file
set(TOP_CMAKE_WAS_SOURCED TRUE)
Expand All @@ -30,14 +30,11 @@ endif()
include(Pcsx2Utils)

check_no_parenthesis_in_path()
detectOperatingSystem()
check_compiler_version("7.0" "7.0")
detect_operating_system()
detect_compiler()

#-------------------------------------------------------------------------------
# Include specific module
# GNUInstallDirs must be done before BuildParameters
include(GNUInstallDirs)
# BuildParameters Must be done before SearchForStuff
include(BuildParameters)
include(SearchForStuff)

Expand Down
69 changes: 1 addition & 68 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@
"patch": 0
},
"configurePresets": [
{
"name": "gcc-base",
"displayName": "GCC Base",
"description": "Base preset for GCC. Only for inheriting from.",
"hidden": true,
"binaryDir": "${sourceDir}/build"
},
{
"name": "clang-base",
"displayName": "Base",
Expand All @@ -32,69 +25,9 @@
"name": "ninja-multi",
"displayName": "Ninja Multi Config",
"description": "Generate multiple ninja build files.",
"inherits": "gcc-base",
"inherits": "clang-base",
"generator": "Ninja Multi-Config"
},
{
"name": "gcc-debug",
"displayName": "GCC Debug",
"description": "GCC Debug build with make.",
"inherits": "gcc-base",
"generator": "Unix Makefiles",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "gcc-devel",
"displayName": "GCC Devel",
"description": "GCC Developer build using make.",
"inherits": "gcc-base",
"generator": "Unix Makefiles",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Devel"
}
},
{
"name": "gcc-release",
"displayName": "GCC Release",
"description": "GCC Release build using make.",
"inherits": "gcc-base",
"generator": "Unix Makefiles",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "gcc-debug-ninja",
"displayName": "GCC Debug Ninja",
"description": "Debug build using ninja.",
"inherits": "gcc-base",
"generator": "Ninja",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "gcc-devel-ninja",
"displayName": "GCC Devel Ninja",
"description": "GCC Developer build using ninja.",
"inherits": "gcc-base",
"generator": "Ninja",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Devel"
}
},
{
"name": "gcc-release-ninja",
"displayName": "GCC Release Ninja",
"description": "GCC Release build using ninja.",
"inherits": "gcc-base",
"generator": "Ninja",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "clang-debug",
"displayName": "Clang Debug",
Expand Down
77 changes: 24 additions & 53 deletions cmake/BuildParameters.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,6 @@ endif()
#-------------------------------------------------------------------------------
option(USE_ASAN "Enable address sanitizer")

if(MSVC AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(USE_CLANG_CL TRUE)
message(STATUS "Building with Clang-CL.")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
set(USE_CLANG TRUE)
message(STATUS "Building with Clang/LLVM.")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(USE_GCC TRUE)
message(STATUS "Building with GNU GCC")
elseif(MSVC)
message(STATUS "Building with MSVC")
else()
message(FATAL_ERROR "Unknown compiler: ${CMAKE_CXX_COMPILER_ID}")
endif()

#-------------------------------------------------------------------------------
# if no build type is set, use Devel as default
# Note without the CMAKE_BUILD_TYPE options the value is still defined to ""
Expand Down Expand Up @@ -81,52 +66,38 @@ mark_as_advanced(CMAKE_C_FLAGS_DEVEL CMAKE_CXX_FLAGS_DEVEL CMAKE_LINKER_FLAGS_DE
#-------------------------------------------------------------------------------
# Select the architecture
#-------------------------------------------------------------------------------
option(DISABLE_ADVANCE_SIMD "Disable advance use of SIMD (SSE2+ & AVX)" OFF)

# Print if we are cross compiling.
if(CMAKE_CROSSCOMPILING)
message(STATUS "Cross compilation is enabled.")
else()
message(STATUS "Cross compilation is disabled.")
endif()
if("${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "x86_64" OR "${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "amd64" OR
"${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "AMD64" OR "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "x86_64")
# Multi-ISA only exists on x86.
option(DISABLE_ADVANCE_SIMD "Disable advance use of SIMD (SSE2+ & AVX)" OFF)

# Architecture bitness detection
include(TargetArch)
target_architecture(PCSX2_TARGET_ARCHITECTURES)
if(${PCSX2_TARGET_ARCHITECTURES} MATCHES "x86_64")
message(STATUS "Compiling a ${PCSX2_TARGET_ARCHITECTURES} build on a ${CMAKE_HOST_SYSTEM_PROCESSOR} host.")
list(APPEND PCSX2_DEFS _M_X86=1)
set(_M_X86 TRUE)
if(DISABLE_ADVANCE_SIMD)
message(STATUS "Building for x86-64 (Multi-ISA).")
else()
message(STATUS "Building for x86-64.")
endif()

# x86_64 requires -fPIC
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(MSVC)
# SSE4.1 is not set by MSVC, it uses _M_SSE instead.
list(APPEND PCSX2_DEFS __SSE4_1__=1)

if(NOT DEFINED ARCH_FLAG AND NOT MSVC)
if(USE_CLANG_CL)
# clang-cl => need to explicitly enable SSE4.1.
add_compile_options("-msse4.1")
endif()
else()
# Multi-ISA => SSE4, otherwise native.
if (DISABLE_ADVANCE_SIMD)
set(ARCH_FLAG "-msse -msse2 -msse4.1 -mfxsr")
add_compile_options("-msse" "-msse2" "-msse4.1" "-mfxsr")
else()
#set(ARCH_FLAG "-march=native -fabi-version=6")
set(ARCH_FLAG "-march=native")
add_compile_options("-march=native")
endif()
elseif(NOT DEFINED ARCH_FLAG AND USE_CLANG_CL)
set(ARCH_FLAG "-msse4.1")
endif()
list(APPEND PCSX2_DEFS _M_X86=1)
set(_M_X86 1)

# SSE4.1 is not set by MSVC, it uses _M_SSE instead.
if(MSVC)
list(APPEND PCSX2_DEFS __SSE4_1__=1)
endif()
else()
message(FATAL_ERROR "Unsupported architecture: ${PCSX2_TARGET_ARCHITECTURES}")
message(FATAL_ERROR "Unsupported architecture: ${CMAKE_HOST_SYSTEM_PROCESSOR}")
endif()
string(REPLACE " " ";" ARCH_FLAG_LIST "${ARCH_FLAG}")
add_compile_options("${ARCH_FLAG_LIST}")

#-------------------------------------------------------------------------------
# Set some default compiler flags
#-------------------------------------------------------------------------------
option(USE_PGO_GENERATE "Enable PGO optimization (generate profile)")
option(USE_PGO_OPTIMIZE "Enable PGO optimization (use profile)")

# Require C++20.
set(CMAKE_CXX_STANDARD 20)
Expand Down Expand Up @@ -183,7 +154,7 @@ endif()

# Enable debug information in release builds for Linux.
# Makes the backtrace actually meaningful.
if(UNIX AND NOT APPLE)
if(LINUX)
add_compile_options($<$<CONFIG:Release>:-g1>)
endif()

Expand Down
32 changes: 0 additions & 32 deletions cmake/FindLibc.cmake

This file was deleted.

103 changes: 32 additions & 71 deletions cmake/Pcsx2Utils.cmake
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
#-------------------------------------------------------------------------------
# detectOperatingSystem
#-------------------------------------------------------------------------------
# This function detects on which OS cmake is run and set a flag to control the
# build process. Supported OS: Linux, MacOSX, Windows
#
# On linux, it also set a flag for specific distribution (ie Fedora)
#-------------------------------------------------------------------------------
function(detectOperatingSystem)
function(detect_operating_system)
# LINUX wasn't added until CMake 3.25.
if (CMAKE_VERSION VERSION_LESS 3.25.0 AND CMAKE_SYSTEM_NAME MATCHES "Linux")
set(LINUX TRUE PARENT_SCOPE)
endif()

if(WIN32)
set(Windows TRUE PARENT_SCOPE)
elseif(UNIX AND APPLE)
if(IOS)
message(WARNING "iOS isn't supported, the build will most likely fail")
endif()
set(MacOSX TRUE PARENT_SCOPE)
elseif(UNIX)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
set(Linux TRUE PARENT_SCOPE)
if (EXISTS /etc/os-release)
# Read the file without CR character
file(STRINGS /etc/os-release OS_RELEASE)
if("${OS_RELEASE}" MATCHES "^.*ID=fedora.*$")
set(Fedora TRUE PARENT_SCOPE)
message(STATUS "Build Fedora specific")
elseif("${OS_RELEASE}" MATCHES "^.*ID=.*suse.*$")
set(openSUSE TRUE PARENT_SCOPE)
message(STATUS "Build openSUSE specific")
endif()
endif()
elseif(CMAKE_SYSTEM_NAME MATCHES "kFreeBSD")
set(kFreeBSD TRUE PARENT_SCOPE)
elseif(CMAKE_SYSTEM_NAME STREQUAL "GNU")
set(GNU TRUE PARENT_SCOPE)
endif()
message(STATUS "Building for Windows.")
elseif(APPLE AND NOT IOS)
message(STATUS "Building for MacOS.")
elseif(LINUX)
message(STATUS "Building for Linux.")
elseif(BSD)
message(STATUS "Building for *BSD.")
else()
message(FATAL_ERROR "Unsupported platform.")
endif()
endfunction()

function(detect_compiler)
if(MSVC AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(USE_CLANG_CL TRUE PARENT_SCOPE)
message(STATUS "Building with Clang-CL.")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
set(USE_CLANG TRUE PARENT_SCOPE)
message(STATUS "Building with Clang/LLVM.")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(USE_GCC TRUE PARENT_SCOPE)
message(STATUS "Building with GNU GCC. THIS IS NOT A SUPPORTED CONFIGURATION.")
elseif(MSVC)
message(STATUS "Building with MSVC.")
else()
message(FATAL_ERROR "Unknown compiler: ${CMAKE_CXX_COMPILER_ID}")
endif()
endfunction()

Expand Down Expand Up @@ -119,24 +117,6 @@ function(write_svnrev_h)
endif()
endfunction()

function(check_compiler_version version_warn version_err)
if(CMAKE_COMPILER_IS_GNUCXX)
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION)
string(STRIP "${GCC_VERSION}" GCC_VERSION)
if(GCC_VERSION VERSION_LESS ${version_err})
message(FATAL_ERROR "PCSX2 doesn't support your old GCC ${GCC_VERSION}! Please upgrade it!
The minimum supported version is ${version_err} but ${version_warn} is warmly recommended")
else()
if(GCC_VERSION VERSION_LESS ${version_warn})
message(WARNING "PCSX2 will stop supporting GCC ${GCC_VERSION} in the near future. Please upgrade to at least GCC ${version_warn}.")
endif()
endif()

set(GCC_VERSION "${GCC_VERSION}" PARENT_SCOPE)
endif()
endfunction()

function(check_no_parenthesis_in_path)
if ("${CMAKE_BINARY_DIR}" MATCHES "[()]" OR "${CMAKE_SOURCE_DIR}" MATCHES "[()]")
message(FATAL_ERROR "Your path contains some parenthesis. Unfortunately Cmake doesn't support them correctly.\nPlease rename your directory to avoid '(' and ')' characters\n")
Expand All @@ -163,25 +143,6 @@ function(alias_library new old)
add_library(${new} ALIAS _alias_${library_no_namespace})
endfunction()

# Helper macro to generate resources on linux (based on glib)
macro(add_custom_glib_res out xml prefix)
set(RESOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/res")
set(RESOURCE_FILES "${ARGN}")
# Note: trying to combine --generate-source and --generate-header doesn't work.
# It outputs whichever one comes last into the file named by the first
add_custom_command(
OUTPUT ${out}.h
COMMAND glib-compile-resources --sourcedir "${RESOURCE_DIR}" --generate-header
--c-name ${prefix} "${RESOURCE_DIR}/${xml}" --target=${out}.h
DEPENDS res/${xml} ${RESOURCE_FILES})

add_custom_command(
OUTPUT ${out}.cpp
COMMAND glib-compile-resources --sourcedir "${RESOURCE_DIR}" --generate-source
--c-name ${prefix} "${RESOURCE_DIR}/${xml}" --target=${out}.cpp
DEPENDS res/${xml} ${RESOURCE_FILES})
endmacro()

function(source_groups_from_vcxproj_filters file)
file(READ "${file}" filecontent)
get_filename_component(parent "${file}" DIRECTORY)
Expand Down
Loading

0 comments on commit 0e15de7

Please sign in to comment.