-
Notifications
You must be signed in to change notification settings - Fork 663
/
Copy pathStandardProjectSettings.cmake
126 lines (115 loc) · 4.47 KB
/
StandardProjectSettings.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Set a default build type if none was specified
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Setting build type to 'RelWithDebInfo' as none was specified.")
set(CMAKE_BUILD_TYPE
"RelWithDebInfo"
CACHE STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui, ccmake
set_property(
CACHE CMAKE_BUILD_TYPE
PROPERTY STRINGS
"Debug"
"Release"
"MinSizeRel"
"RelWithDebInfo")
endif()
option(ENABLE_IPO "Enable Interprocedural Optimization, aka Link Time Optimization (LTO)" ON)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION OFF)
if(ENABLE_IPO AND NOT CMAKE_BUILD_TYPE STREQUAL Debug)
include(CheckIPOSupported)
check_ipo_supported(
RESULT
result
OUTPUT
output)
if(result)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
else()
message(STATUS "IPO is not supported: ${output}")
endif()
endif()
message(STATUS "CMAKE_INTERPROCEDURAL_OPTIMIZATION: ${CMAKE_INTERPROCEDURAL_OPTIMIZATION} (this implies /GL or -flto)")
# Snippet from GLM: https://github.com/g-truc/glm (MIT)
# NOTE: fast-math was on by default before the CMake build refactoring!
option(ENABLE_FAST_MATH "Enable fast math optimizations" ON)
if(ENABLE_FAST_MATH)
message(STATUS "ENABLE_FAST_MATH: ON")
if((CMAKE_CXX_COMPILER_ID MATCHES "Clang") OR (CMAKE_CXX_COMPILER_ID MATCHES "GNU"))
add_compile_options(-ffast-math)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
add_compile_options(/fp:fast)
endif()
else()
message(STATUS "ENABLE_FAST_MATH: OFF")
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
add_compile_options(/fp:precise)
endif()
endif()
if(MSVC)
list(APPEND FLAGS_AND_DEFINES
-D_CONSOLE
-D_MBCS
-DNOMINMAX
-D_CRT_SECURE_NO_WARNINGS
-D_CRT_NONSTDC_NO_DEPRECATE
# TODO: This is being overwritten by /Ob0
# /Ob2 # Inline Function Expansion
/Oy- # Frame-Pointer Omission
/MP # Build with Multiple Processes
/bigobj # Allow bigger .obj files, which we have from mainly the sol templating
/utf-8 # Treat source files as UTF-8. This is needed because of certain symbols inside fmtlib's code. u-second, etc.
)
if(CMAKE_BUILD_TYPE STREQUAL Debug)
# /EDITANDCONTINUE isn't supported, it messes with Tracy
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /INCREMENTAL /SAFESEH:NO")
list(APPEND FLAGS_AND_DEFINES
/Zi
/GR # Enable RTTI
)
else()
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /INCREMENTAL:NO /LTCG /OPT:REF /OPT:ICF")
list(APPEND FLAGS_AND_DEFINES
/Oi # Generate Intrinsic Functions
/GL # Whole Program Optimization
/Gy # Enable Function Level Linking
/TP # C++ Source Files
)
endif()
link_libraries(WS2_32 dbghelp Shlwapi)
endif()
if(UNIX)
link_libraries(dl)
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
message(FATAL_ERROR
"GCC version must be at least 9.0! Detected: ${CMAKE_CXX_COMPILER_VERSION}")
endif()
endif()
# TODO: These should be applied on a per-target level, not globally like this!
string(REPLACE ";" " " FLAGS_AND_DEFINES_STR "${FLAGS_AND_DEFINES}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAGS_AND_DEFINES_STR}")
function(set_target_output_directory target)
message(STATUS "Setting output directory for ${target} to ${CMAKE_SOURCE_DIR}")
set_target_properties(${target} PROPERTIES
VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_SOURCE_DIR}"
RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_SOURCE_DIR}"
RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_SOURCE_DIR}"
RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL "${CMAKE_SOURCE_DIR}"
)
endfunction()
function(disable_lto target)
target_compile_options(${target} PRIVATE -fno-lto)
target_link_options(${target} PRIVATE -fno-lto)
endfunction()
# If we're on Unix and the system is 32-bit (void* is 4-bytes wide),
# then there's a good chance we're compiling for Raspberry Pi.
# Currently, CMake doesn't detect this properly and needs some help
# to link libatomic.
# Source: https://gitlab.kitware.com/cmake/cmake/-/issues/21174
#
# TODO: Use include(CheckCXXSourceCompiles) to make this check better.
if(UNIX AND CMAKE_SIZEOF_VOID_P EQUAL 4)
set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -latomic")
endif()