Skip to content

Commit 8867bb1

Browse files
committed
CMake: Tidy up build system
And fix Windows CMake... who knows how long for.
1 parent a88ee93 commit 8867bb1

File tree

14 files changed

+287
-252
lines changed

14 files changed

+287
-252
lines changed

CMakeLists.txt

Lines changed: 30 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ project(duckstation C CXX)
55
cmake_policy(SET CMP0069 NEW)
66
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
77

8+
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
9+
message(FATAL_ERROR "DuckStation does not support in-tree builds. Please make a build directory that is not the source"
10+
"directory and generate your CMake project there using either `cmake -B build_directory` or by "
11+
"running cmake from the build directory.")
12+
endif()
13+
14+
if(NOT CMAKE_BUILD_TYPE MATCHES "Debug|Devel|MinSizeRel|RelWithDebInfo|Release")
15+
message(STATUS "CMAKE_BUILD_TYPE not set, defaulting to Release.")
16+
set(CMAKE_BUILD_TYPE "Release")
17+
endif()
18+
819
message(STATUS "CMake Version: ${CMAKE_VERSION}")
920
message(STATUS "CMake System Name: ${CMAKE_SYSTEM_NAME}")
1021
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
@@ -13,58 +24,19 @@ message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
1324
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules/")
1425
include(DuckStationUtils)
1526

16-
# Platform detection.
17-
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
18-
set(LINUX TRUE)
19-
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
20-
set(FREEBSD TRUE)
21-
endif()
22-
23-
# Renderer options.
24-
option(ENABLE_OPENGL "Build with OpenGL renderer" ON)
25-
option(ENABLE_VULKAN "Build with Vulkan renderer" ON)
26-
27-
# Global options.
28-
if(NOT ANDROID)
29-
option(BUILD_NOGUI_FRONTEND "Build the NoGUI frontend" OFF)
30-
option(BUILD_QT_FRONTEND "Build the Qt frontend" ON)
31-
option(BUILD_REGTEST "Build regression test runner" OFF)
32-
option(BUILD_TESTS "Build unit tests" OFF)
33-
34-
set(ENABLE_CUBEB ON)
35-
set(ENABLE_DISCORD_PRESENCE ON)
36-
set(ENABLE_SDL2 ON)
37-
38-
if(LINUX OR FREEBSD)
39-
option(ENABLE_X11 "Support X11 window system" ON)
40-
option(ENABLE_WAYLAND "Support Wayland window system" ON)
41-
endif()
42-
if(APPLE)
43-
option(SKIP_POSTPROCESS_BUNDLE "Disable bundle post-processing, including Qt additions" OFF)
44-
endif()
45-
endif()
46-
47-
# Everything except Windows/Mac use EGL.
48-
if(ENABLE_OPENGL AND (LINUX OR FREEBSD OR ANDROID))
49-
set(ENABLE_EGL TRUE)
50-
endif()
51-
52-
if(ENABLE_X11)
53-
find_package(X11 REQUIRED)
54-
if (NOT X11_Xrandr_FOUND)
55-
message(FATAL_ERROR "XRandR extension is required")
56-
endif()
57-
endif()
58-
if(ENABLE_WAYLAND)
59-
message(STATUS "Wayland support enabled")
60-
endif()
27+
# Detect system attributes.
28+
detect_operating_system()
29+
detect_compiler()
30+
detect_architecture()
31+
detect_page_size()
6132

33+
# Build options. Depends on system attributes.
34+
include(DuckStationBuildOptions)
6235

6336
# Set _DEBUG macro for Debug builds.
6437
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG")
6538
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG")
6639

67-
6840
# Release build optimizations for MSVC.
6941
if(MSVC)
7042
add_definitions("/D_CRT_SECURE_NO_WARNINGS")
@@ -88,101 +60,16 @@ if(MSVC)
8860
set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /OPT:REF /OPT:ICF")
8961
endif()
9062

91-
92-
# Default symbol visibility to hidden, that way we don't go through the PLT for intra-library calls.
93-
if(ANDROID)
94-
cmake_policy(SET CMP0063 NEW)
95-
set(CMAKE_POLICY_DEFAULT_CMP0063 NEW)
96-
set(CMAKE_C_VISIBILITY_PRESET hidden)
97-
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
98-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-semantic-interposition")
99-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-semantic-interposition")
100-
endif()
101-
102-
10363
# Warning disables.
104-
if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
105-
include(CheckCXXFlag)
106-
check_cxx_flag(-Wall COMPILER_SUPPORTS_WALL)
107-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-switch")
108-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-switch")
109-
if(NOT ANDROID)
110-
check_cxx_flag(-Wno-class-memaccess COMPILER_SUPPORTS_MEMACCESS)
111-
check_cxx_flag(-Wno-invalid-offsetof COMPILER_SUPPORTS_OFFSETOF)
112-
endif()
113-
endif()
114-
115-
116-
# Detect processor type.
117-
if(APPLE AND NOT "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "")
118-
# Universal binaries.
119-
if("x86_64" IN_LIST CMAKE_OSX_ARCHITECTURES)
120-
set(CPU_ARCH_X64 TRUE)
121-
message(STATUS "Building x86_64 MacOS binaries.")
122-
endif()
123-
if("arm64" IN_LIST CMAKE_OSX_ARCHITECTURES)
124-
set(CPU_ARCH_ARM64 TRUE)
125-
message(STATUS "Building ARM64 MacOS binaries.")
126-
endif()
127-
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "amd64" OR
128-
"${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64" OR "${CMAKE_OSX_ARCHITECTURES}" STREQUAL "x86_64")
129-
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
130-
set(CPU_ARCH_X64 TRUE)
131-
message(STATUS "Building x86_64 binaries.")
132-
else()
133-
# Cross-compiling 32-bit build. 32-bit hosts are not supported.
134-
set(CPU_ARCH_X86 TRUE)
135-
message(FATAL_ERROR "Building x86_32 binaries is not supported.")
136-
endif()
137-
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i386" OR
138-
"${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686")
139-
set(CPU_ARCH_X86 TRUE)
140-
message(FATAL_ERROR "Building x86_32 binaries is not supported.")
141-
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64")
142-
if(CMAKE_SIZEOF_VOID_P EQUAL 8) # Might have an A64 kernel, e.g. Raspbian.
143-
set(CPU_ARCH_ARM64 TRUE)
144-
message(STATUS "Building ARM64 binaries.")
145-
else()
146-
set(CPU_ARCH_ARM32 TRUE)
147-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -marm -march=armv7-a")
148-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -marm -march=armv7-a")
149-
message(STATUS "Building ARM32 binaries on ARM64.")
150-
endif()
151-
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7-a" OR
152-
"${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7l")
153-
set(CPU_ARCH_ARM32 TRUE)
154-
message(STATUS "Building ARM32 binaries.")
155-
if(ANDROID)
156-
# Force ARM mode, since apparently ANDROID_ARM_MODE isn't working..
157-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -marm")
158-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -marm")
159-
else()
160-
# Enable NEON.
161-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -marm -march=armv7-a")
162-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -marm -march=armv7-a")
163-
endif()
164-
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "riscv64")
165-
set(CPU_ARCH_RISCV64 TRUE)
166-
message(STATUS "Building RISC-V 64 binaries.")
167-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -finline-atomics")
168-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -finline-atomics")
169-
170-
# Still need this, apparently.
171-
link_libraries("-latomic")
172-
173-
if(NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
174-
# Frame pointers generate an annoying amount of code on leaf functions.
175-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fomit-frame-pointer")
176-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fomit-frame-pointer")
177-
endif()
178-
else()
179-
message(FATAL_ERROR "Unknown system processor: ${CMAKE_SYSTEM_PROCESSOR}")
64+
if(COMPILER_CLANG OR COMPILER_CLANG_CL OR COMPILER_GCC)
65+
include(CheckCXXFlag)
66+
check_cxx_flag(-Wall COMPILER_SUPPORTS_WALL)
67+
check_cxx_flag(-Wno-class-memaccess COMPILER_SUPPORTS_MEMACCESS)
68+
check_cxx_flag(-Wno-invalid-offsetof COMPILER_SUPPORTS_OFFSETOF)
69+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-switch")
70+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-switch")
18071
endif()
18172

182-
# Detect page size if needed.
183-
detect_page_size()
184-
185-
18673
# We don't need exceptions, disable them to save a bit of code size.
18774
if(MSVC)
18875
string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
@@ -193,29 +80,15 @@ else()
19380
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
19481
endif()
19582

196-
19783
# Write binaries to a seperate directory.
198-
if(WIN32)
199-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin/${CPU_ARCH}")
200-
else()
201-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
202-
endif()
203-
204-
# Needed for Linux - put shared libraries in the binary directory.
205-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
206-
207-
208-
# Enable threads everywhere.
209-
set(THREADS_PREFER_PTHREAD_FLAG ON)
210-
find_package(Threads REQUIRED)
211-
84+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
21285

21386
# Enable large file support on Linux 32-bit platforms.
214-
# Android is deliberately ommitted here as it didn't support 64-bit ops on files until Android 7/N.
215-
if((LINUX OR FREEBSD) AND (CPU_ARCH_X86 OR CPU_ARCH_ARM32))
87+
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
21688
add_definitions("-D_FILE_OFFSET_BITS=64")
21789
endif()
21890

91+
# Optional unit tests.
21992
if(BUILD_TESTS)
22093
enable_testing()
22194
endif()
@@ -232,6 +105,5 @@ include(DuckStationDependencies)
232105
add_subdirectory(dep)
233106
add_subdirectory(src)
234107

235-
if(ANDROID)
236-
add_subdirectory(android/app/src/cpp)
237-
endif()
108+
# Output build summary.
109+
include(DuckStationBuildSummary)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Renderer options.
2+
option(ENABLE_OPENGL "Build with OpenGL renderer" ON)
3+
option(ENABLE_VULKAN "Build with Vulkan renderer" ON)
4+
option(BUILD_NOGUI_FRONTEND "Build the NoGUI frontend" OFF)
5+
option(BUILD_QT_FRONTEND "Build the Qt frontend" ON)
6+
option(BUILD_REGTEST "Build regression test runner" OFF)
7+
option(BUILD_TESTS "Build unit tests" OFF)
8+
9+
if(LINUX OR BSD)
10+
option(ENABLE_X11 "Support X11 window system" ON)
11+
option(ENABLE_WAYLAND "Support Wayland window system" ON)
12+
endif()
13+
if(APPLE)
14+
option(SKIP_POSTPROCESS_BUNDLE "Disable bundle post-processing, including Qt additions" OFF)
15+
endif()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
if(ENABLE_OPENGL)
2+
message(STATUS "Building with OpenGL support.")
3+
endif()
4+
if(ENABLE_VULKAN)
5+
message(STATUS "Building with Vulkan support.")
6+
endif()
7+
if(ENABLE_X11)
8+
message(STATUS "Building with X11 support.")
9+
endif()
10+
if(ENABLE_WAYLAND)
11+
message(STATUS "Building with Wayland support.")
12+
endif()
13+
14+
if(BUILD_QT_FRONTEND)
15+
message(STATUS "Building Qt frontend.")
16+
endif()
17+
if(BUILD_NOGUI_FRONTEND)
18+
message(STATUS "Building NoGUI frontend.")
19+
endif()
20+
if(BUILD_REGTEST)
21+
message(STATUS "Building RegTest frontend.")
22+
endif()
23+
if(BUILD_TESTS)
24+
message(STATUS "Building unit tests.")
25+
endif()
26+
27+
if(NOT IS_SUPPORTED_COMPILER)
28+
message(WARNING "
29+
*************** UNSUPPORTED CONFIGURATION ***************
30+
You are not compiling DuckStation with a supported compiler.
31+
It may not even build successfully.
32+
DuckStation only supports the Clang and MSVC compilers.
33+
No support will be provided, continue at your own risk.
34+
*********************************************************")
35+
endif()
36+
37+
if(WIN32)
38+
message(WARNING "
39+
*************** UNSUPPORTED CONFIGURATION ***************
40+
You are compiling DuckStation with CMake on Windows.
41+
It may not even build successfully.
42+
DuckStation only supports MSBuild on Windows.
43+
No support will be provided, continue at your own risk.
44+
*********************************************************")
45+
endif()
Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,53 @@
1-
if(ENABLE_SDL2)
2-
find_package(SDL2 2.30.2 REQUIRED)
3-
endif()
4-
if(NOT WIN32 AND NOT ANDROID)
5-
# From PCSX2: On macOS, Mono.framework contains an ancient version of libpng. We don't want that.
6-
# Avoid it by telling cmake to avoid finding frameworks while we search for libpng.
7-
if(APPLE)
8-
set(FIND_FRAMEWORK_BACKUP ${CMAKE_FIND_FRAMEWORK})
9-
set(CMAKE_FIND_FRAMEWORK NEVER)
10-
endif()
1+
# From PCSX2: On macOS, Mono.framework contains an ancient version of libpng. We don't want that.
2+
# Avoid it by telling cmake to avoid finding frameworks while we search for libpng.
3+
if(APPLE)
4+
set(FIND_FRAMEWORK_BACKUP ${CMAKE_FIND_FRAMEWORK})
5+
set(CMAKE_FIND_FRAMEWORK NEVER)
6+
endif()
7+
8+
# Enable threads everywhere.
9+
set(THREADS_PREFER_PTHREAD_FLAG ON)
10+
find_package(Threads REQUIRED)
1111

12-
find_package(Zstd 1.5.5 REQUIRED)
13-
find_package(WebP REQUIRED) # v1.3.2, spews an error on Linux because no pkg-config.
14-
find_package(ZLIB REQUIRED) # 1.3, but Mac currently doesn't use it.
15-
find_package(PNG 1.6.40 REQUIRED)
16-
find_package(JPEG REQUIRED) # No version because flatpak uses libjpeg-turbo.
12+
find_package(SDL2 2.30.2 REQUIRED)
13+
find_package(Zstd 1.5.5 REQUIRED)
14+
find_package(WebP REQUIRED) # v1.3.2, spews an error on Linux because no pkg-config.
15+
find_package(ZLIB REQUIRED) # 1.3, but Mac currently doesn't use it.
16+
find_package(PNG 1.6.40 REQUIRED)
17+
find_package(JPEG REQUIRED) # No version because flatpak uses libjpeg-turbo.
18+
find_package(Freetype 2.13.1 REQUIRED)
19+
20+
if(NOT WIN32)
1721
find_package(CURL REQUIRED)
18-
find_package(Freetype 2.13.1 REQUIRED)
22+
endif()
1923

20-
if(ENABLE_VULKAN OR APPLE)
21-
find_package(Shaderc REQUIRED)
24+
if(ENABLE_X11)
25+
find_package(X11 REQUIRED)
26+
if (NOT X11_Xrandr_FOUND)
27+
message(FATAL_ERROR "XRandR extension is required")
2228
endif()
29+
endif()
2330

24-
if(APPLE)
25-
# SPIRV-Cross is currently only used on MacOS.
26-
find_package(spirv_cross_c_shared REQUIRED)
31+
if(ENABLE_VULKAN OR APPLE)
32+
find_package(Shaderc REQUIRED)
33+
endif()
2734

28-
set(CMAKE_FIND_FRAMEWORK ${FIND_FRAMEWORK_BACKUP})
29-
endif()
35+
if(APPLE)
36+
# SPIRV-Cross is currently only used on MacOS.
37+
find_package(spirv_cross_c_shared REQUIRED)
3038
endif()
31-
if(LINUX AND NOT ANDROID)
39+
40+
if(LINUX)
3241
find_package(UDEV REQUIRED)
3342
endif()
34-
if(UNIX AND NOT APPLE)
43+
44+
if(NOT WIN32 AND NOT APPLE)
3545
find_package(Libbacktrace)
3646
if(NOT LIBBACKTRACE_FOUND)
3747
message(WARNING "libbacktrace not found, crashes will not produce backtraces.")
3848
endif()
3949
endif()
50+
51+
if(APPLE)
52+
set(CMAKE_FIND_FRAMEWORK ${FIND_FRAMEWORK_BACKUP})
53+
endif()

0 commit comments

Comments
 (0)