Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/CMakeLists.txt
4179 views
1
cmake_minimum_required(VERSION 3.19)
2
project(duckstation C CXX)
3
4
# Policy settings.
5
cmake_policy(SET CMP0069 NEW)
6
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
7
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(FATAL_ERROR "CMAKE_BUILD_TYPE not set. Please set it first.")
16
endif()
17
18
# Pull in modules.
19
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/CMakeModules/")
20
include(DuckStationUtils)
21
22
# Detect system attributes.
23
detect_operating_system()
24
detect_compiler()
25
detect_architecture()
26
detect_page_size()
27
detect_cache_line_size()
28
29
# Build options. Depends on system attributes.
30
include(DuckStationBuildOptions)
31
include(DuckStationDependencies)
32
include(DuckStationCompilerRequirement)
33
34
# Enable PIC on Linux, otherwise the builds do not support ASLR.
35
if(LINUX OR BSD)
36
include(CheckPIESupported)
37
check_pie_supported()
38
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
39
endif()
40
41
# Release build optimizations for MSVC.
42
if(MSVC)
43
add_definitions("/D_CRT_SECURE_NO_WARNINGS")
44
foreach(config CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
45
# Set warning level 3 instead of 4.
46
string(REPLACE "/W3" "/W4" ${config} "${${config}}")
47
48
# Enable intrinsic functions, disable minimal rebuild, UTF-8 source, set __cplusplus version.
49
set(${config} "${${config}} /Oi /Gm- /utf-8 /Zc:__cplusplus")
50
endforeach()
51
52
# RelWithDebInfo is set to Ob1 instead of Ob2.
53
string(REPLACE "/Ob1" "/Ob2" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
54
string(REPLACE "/Ob1" "/Ob2" CMAKE_C_FLAGS_DEVEL "${CMAKE_C_FLAGS_DEVEL}")
55
string(REPLACE "/Ob1" "/Ob2" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
56
string(REPLACE "/Ob1" "/Ob2" CMAKE_CXX_FLAGS_DEVEL "${CMAKE_CXX_FLAGS_DEVEL}")
57
58
# Disable incremental linking in RelWithDebInfo.
59
string(REPLACE "/INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
60
string(REPLACE "/INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_DEVEL "${CMAKE_EXE_LINKER_FLAGS_DEVEL}")
61
62
# COMDAT folding/remove unused functions.
63
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /OPT:REF /OPT:ICF")
64
set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /OPT:REF /OPT:ICF")
65
set(CMAKE_EXE_LINKER_FLAGS_DEVEL "${CMAKE_EXE_LINKER_FLAGS_DEVEL} /OPT:REF /OPT:ICF")
66
else()
67
# Force debug symbols for Linux builds.
68
add_debug_symbol_flag(CMAKE_C_FLAGS_RELEASE)
69
add_debug_symbol_flag(CMAKE_CXX_FLAGS_RELEASE)
70
endif()
71
72
# Warning disables.
73
if(COMPILER_CLANG OR COMPILER_CLANG_CL OR COMPILER_GCC)
74
include(CheckCXXFlag)
75
check_cxx_flag(-Wall COMPILER_SUPPORTS_WALL)
76
check_cxx_flag(-Wno-class-memaccess COMPILER_SUPPORTS_MEMACCESS)
77
check_cxx_flag(-Wno-invalid-offsetof COMPILER_SUPPORTS_OFFSETOF)
78
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-switch")
79
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-switch")
80
endif()
81
82
# We don't need exceptions, disable them to save a bit of code size.
83
if(MSVC)
84
string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
85
string(REPLACE "/GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
86
87
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_HAS_EXCEPTIONS=0 /permissive-")
88
if(COMPILER_CLANG_CL)
89
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /clang:-fno-rtti")
90
endif()
91
else()
92
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
93
endif()
94
95
# Write binaries to a seperate directory.
96
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
97
98
# Installation directories. Everything goes into one directory.
99
if(ALLOW_INSTALL)
100
set(CMAKE_INSTALL_BINDIR "${CMAKE_INSTALL_PREFIX}")
101
set(CMAKE_INSTALL_LIBDIR "${CMAKE_INSTALL_PREFIX}")
102
endif()
103
104
# Enable large file support on Linux 32-bit platforms.
105
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
106
add_definitions("-D_FILE_OFFSET_BITS=64")
107
endif()
108
109
# Optional unit tests.
110
if(BUILD_TESTS)
111
enable_testing()
112
endif()
113
114
# Prevent fmt from being built with exceptions, or being thrown at call sites.
115
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DFMT_EXCEPTIONS=0")
116
117
# Recursively include the source tree.
118
add_subdirectory(dep)
119
add_subdirectory(src)
120
121
# Output build summary.
122
include(DuckStationBuildSummary)
123
124