Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/CMakeLists.txt
7486 views
1
# SPDX-FileCopyrightText: 2019-2026 Connor McLaughlin <[email protected]>
2
# SPDX-License-Identifier: CC-BY-NC-ND-4.0 + Packaging Restriction
3
#
4
# NOTE: In addition to the terms of CC-BY-NC-ND-4.0, you may not use this file to create
5
# packages or build recipes without explicit permission from the copyright holder.
6
#
7
# Unless otherwise specified, other files supporting the build system are covered under
8
# the same terms.
9
#
10
11
cmake_minimum_required(VERSION 3.19)
12
project(duckstation C CXX)
13
14
# Policy settings.
15
cmake_policy(SET CMP0069 NEW)
16
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
17
18
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
19
message(FATAL_ERROR "DuckStation does not support in-tree builds. Please make a build directory that is not the source"
20
"directory and generate your CMake project there using either `cmake -B build_directory` or by "
21
"running cmake from the build directory.")
22
endif()
23
24
if(NOT CMAKE_BUILD_TYPE MATCHES "Debug|Devel|MinSizeRel|RelWithDebInfo|Release")
25
message(FATAL_ERROR "CMAKE_BUILD_TYPE not set. Please set it first.")
26
endif()
27
28
# Pull in modules.
29
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules/")
30
include(DuckStationUtils)
31
32
# Detect system attributes.
33
detect_operating_system()
34
detect_compiler()
35
detect_architecture()
36
detect_page_size()
37
detect_cache_line_size()
38
39
# Build options. Depends on system attributes.
40
include(DuckStationBuildOptions)
41
include(DuckStationDependencies)
42
include(DuckStationCompilerRequirement)
43
44
# Enable PIC on Linux, otherwise the builds do not support ASLR.
45
if(LINUX OR BSD)
46
include(CheckPIESupported)
47
check_pie_supported()
48
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
49
endif()
50
51
# Release build optimizations for MSVC.
52
if(MSVC)
53
add_compile_definitions("_UNICODE" "UNICODE" "_CRT_NONSTDC_NO_DEPRECATE" "_CRT_SECURE_NO_WARNINGS")
54
foreach(config CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
55
# Enable intrinsic functions, disable minimal rebuild, UTF-8 source, set __cplusplus version.
56
set(${config} "${${config}} /Oi /Gm- /utf-8 /Zc:__cplusplus")
57
endforeach()
58
59
# RelWithDebInfo is set to Ob1 instead of Ob2.
60
string(REPLACE "/Ob1" "/Ob2" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
61
string(REPLACE "/Ob1" "/Ob2" CMAKE_C_FLAGS_DEVEL "${CMAKE_C_FLAGS_DEVEL}")
62
string(REPLACE "/Ob1" "/Ob2" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
63
string(REPLACE "/Ob1" "/Ob2" CMAKE_CXX_FLAGS_DEVEL "${CMAKE_CXX_FLAGS_DEVEL}")
64
65
# Disable incremental linking in RelWithDebInfo.
66
string(REPLACE "/INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
67
string(REPLACE "/INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_DEVEL "${CMAKE_EXE_LINKER_FLAGS_DEVEL}")
68
69
# COMDAT folding/remove unused functions.
70
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /OPT:REF /OPT:ICF")
71
set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /OPT:REF /OPT:ICF")
72
set(CMAKE_EXE_LINKER_FLAGS_DEVEL "${CMAKE_EXE_LINKER_FLAGS_DEVEL} /OPT:REF /OPT:ICF")
73
else()
74
# Force debug symbols for Linux builds.
75
add_debug_symbol_flag(CMAKE_C_FLAGS_RELEASE)
76
add_debug_symbol_flag(CMAKE_CXX_FLAGS_RELEASE)
77
endif()
78
79
# Warning disables.
80
if(COMPILER_CLANG OR COMPILER_CLANG_CL OR COMPILER_GCC)
81
include(CheckCXXFlag)
82
check_cxx_flag(-Wall COMPILER_SUPPORTS_WALL)
83
check_cxx_flag(-Wno-class-memaccess COMPILER_SUPPORTS_MEMACCESS)
84
check_cxx_flag(-Wno-invalid-offsetof COMPILER_SUPPORTS_OFFSETOF)
85
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-switch")
86
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-switch")
87
endif()
88
89
# We don't need exceptions, disable them to save a bit of code size.
90
if(MSVC)
91
string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
92
string(REPLACE "/GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
93
94
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_HAS_EXCEPTIONS=0 /permissive-")
95
if(COMPILER_CLANG_CL)
96
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /clang:-fno-rtti")
97
endif()
98
else()
99
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
100
endif()
101
102
# Rewrite paths in macros to be relative to the source directory.
103
# Helpful for reproducible builds.
104
if("${CMAKE_BUILD_TYPE}" STREQUAL "Release" AND NOT CMAKE_GENERATOR MATCHES "Xcode" AND
105
(COMPILER_CLANG OR COMPILER_CLANG_CL OR COMPILER_GCC))
106
file(RELATIVE_PATH source_dir_remap "${CMAKE_BINARY_DIR}" "${CMAKE_SOURCE_DIR}")
107
string(REGEX REPLACE "\/+$" "" source_dir_remap "${source_dir_remap}")
108
set(source_dir_remap_str "\"${CMAKE_SOURCE_DIR}\"=\"${source_dir_remap}\"")
109
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffile-prefix-map=${source_dir_remap_str}")
110
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffile-prefix-map=${source_dir_remap_str}")
111
endif()
112
113
# Write binaries to a seperate directory.
114
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
115
116
# Enable large file support on Linux 32-bit platforms.
117
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
118
add_definitions("-D_FILE_OFFSET_BITS=64")
119
endif()
120
121
# Optional unit tests.
122
if(BUILD_TESTS)
123
enable_testing()
124
endif()
125
126
# Recursively include the source tree.
127
add_subdirectory(dep)
128
add_subdirectory(src)
129
130
# Output build summary.
131
include(DuckStationBuildSummary)
132
133