forked from OpenEnroth/OpenEnroth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
167 lines (145 loc) · 8.44 KB
/
CMakeLists.txt
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
project("OpenEnroth")
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules)
include(Git)
include(Detection)
include(ExternalProject)
include(CppLint)
include(Dependencies)
include(CheckLinkerFlag)
# Defaults.
if (CMAKE_BUILD_TYPE)
set(OE_USE_PREBUILT_DEPENDENCIES_DEFAULT ON)
else()
set(OE_USE_PREBUILT_DEPENDENCIES_DEFAULT OFF)
endif()
# User-settable options.
set(OE_BUILD_TESTS ON CACHE BOOL "Build OpenEnroth tests.")
set(OE_USE_PREBUILT_DEPENDENCIES ${OE_USE_PREBUILT_DEPENDENCIES_DEFAULT} CACHE BOOL "Use prebuilt dependencies.")
set(OE_USE_CCACHE ON CACHE BOOL "Use ccache if available.")
set(OE_USE_SCCACHE ON CACHE BOOL "Use sccache if available, note that ccache takes precedence.")
set(OE_USE_LD_MOLD ON CACHE BOOL "Use mold linker if available.")
set(OE_USE_LD_LLD ON CACHE BOOL "Use lld linker if available, note that mold takes precedence.")
set(OE_USE_LD_GOLD ON CACHE BOOL "Use GNU gold linker if available, note that lld takes precedence.")
# Find ccache / sccache.
if(OE_USE_CCACHE)
find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND)
message(STATUS "Using ccache at ${CCACHE_FOUND}.")
set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_FOUND})
set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_FOUND})
else()
message(STATUS "ccache is not found.")
endif()
endif()
if(OE_USE_SCCACHE AND NOT CCACHE_FOUND)
find_program(SCCACHE_FOUND sccache)
if(SCCACHE_FOUND)
message(STATUS "Using sccache at ${SCCACHE_FOUND}.")
set(CMAKE_C_COMPILER_LAUNCHER ${SCCACHE_FOUND})
set(CMAKE_CXX_COMPILER_LAUNCHER ${SCCACHE_FOUND})
else()
message(STATUS "Neither sccache or ccache is found.")
endif()
endif()
# Detect available linkers.
check_linker_flag(CXX "-fuse-ld=mold" COMPILER_RT_HAS_FUSE_LD_MOLD_FLAG)
check_linker_flag(CXX "-fuse-ld=lld" COMPILER_RT_HAS_FUSE_LD_LLD_FLAG)
check_linker_flag(CXX "-fuse-ld=gold" COMPILER_RT_HAS_FUSE_LD_GOLD_FLAG)
# Set compiler options.
# We do a lot of tweaks here.
add_compile_definitions(FMT_USE_NONTYPE_TEMPLATE_ARGS) # Enable _cf literals
if(BUILD_COMPILER STREQUAL "gcc" OR BUILD_COMPILER STREQUAL "clang")
add_compile_options(-Werror=return-type) # Control reaches the end of non-void function, this is an error on MSVC
add_compile_options(-Werror=unused-result) # Ignoring return value of function declared with 'nodiscard' attribute
add_compile_options(-Werror=unused-value) # Expression result unused
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Werror=suggest-override>) # 'func' overrides a member function but is not marked 'override'
# -Og is a better choice than -O0 for producing debuggable code because some compiler passes that collect debug information are disabled at -O0.
string(REPLACE " -O0 " "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
string(REPLACE " -O0 " "" CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
string(REPLACE " -g " " -g -Og " CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
string(REPLACE " -g " " -g -Og " CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
if(OE_USE_LD_MOLD AND COMPILER_RT_HAS_FUSE_LD_MOLD_FLAG)
message(STATUS "Using mold linker.")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=mold")
elseif(OE_USE_LD_LLD AND COMPILER_RT_HAS_FUSE_LD_LLD_FLAG)
message(STATUS "Using lld linker.")
# "-Wl,-z,notext" is needed because on x86 linux prebuilt ffmpeg is built with --enable-asm, and asm code there
# isn't position-independent. Thus, when linking a position-independent binary, LLD has two options:
# 1. Choke.
# 2. Generate a bunch of relocations in the .text segment.
# Default is #1, so we need to instruct it not to choke. Note that LLD behaves differently from GNU LD here,
# which picks #2 by default.
if (BUILD_PLATFORM STREQUAL "linux")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld -Wl,-z,notext")
endif ()
elseif(OE_USE_LD_GOLD AND COMPILER_RT_HAS_FUSE_LD_GOLD_FLAG)
message(STATUS "Using gold linker.")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold")
else()
message(STATUS "Fallback to default linker.")
endif()
if(BUILD_ARCHITECTURE STREQUAL "x86")
add_compile_options(-msse2 -mfpmath=sse -march=pentium4) # Don't use x87 fpu in 32-bit builds.
endif()
# Don't set errno for math functions. This is the default on Darwin, so makes sense to pass this flag everywhere.
add_compile_options(-fno-math-errno)
# We don't use fp traps, so compiler can assume that they are disabled.
add_compile_options(-fno-trapping-math)
# Clang & gcc implementations of fp-contract differ, and we are getting diffs in tests. Better to disable it.
add_compile_options(-ffp-contract=off)
endif()
if(BUILD_COMPILER STREQUAL "gcc")
# Flags that are available only in gcc but not in clang go here.
add_compile_definitions($<$<CONFIG:Debug>:_GLIBCXX_ASSERTIONS>) # Enable assertions in libstdc++.
elseif(BUILD_COMPILER STREQUAL "clang")
# Flags that are available only in clang but not in gcc go here.
add_compile_options(-Werror=unused-comparison) # Comparison result unused.
add_compile_options(-Werror=inconsistent-missing-override) # 'override' only used on some of the overridden functions.
add_compile_options(-Werror=invalid-noreturn) # function declared 'noreturn' should not return.
add_compile_options(-Werror=uninitialized) # variable 'x' is used uninitialized.
add_compile_definitions($<$<CONFIG:Debug>:_LIBCPP_ENABLE_ASSERTIONS=1>) # Enable assertions in libcxx.
elseif(BUILD_COMPILER STREQUAL "msvc")
# /Zi -> /Z7: Pack debug info into .obj files, this is needed for ccache to work. Note that pdb for the binary
# itself is still generated.
string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
add_compile_definitions(NOMINMAX) # please don't pull in these macros from <Windows.h>
add_compile_definitions(_CRT_SECURE_NO_WARNINGS) # STL security warnings are just noise
add_compile_definitions(_CRT_NONSTDC_NO_DEPRECATE) # POSIX deprecation warnings are also just noise
add_compile_definitions(_USE_MATH_DEFINES) # Pull in M_PI and other <cmath> defines
add_compile_definitions(FMT_CONSTEVAL=) # MSVC chokes on fmt consteval formatting, so we define FMT_CONSTEVAL=<empty>
add_compile_options(/MP) # Multi-threaded build
add_compile_options(/Zc:preprocessor) # Use standard compliant preprocessor
add_compile_options(/we4834) # Discarding return value of function with 'nodiscard' attribute
add_link_options(/OPT:REF) # Eliminate unreferenced data & functions.
add_link_options(/OPT:ICF) # Enable COMDAT folding, should decrease binary sizes?
if (BUILD_ARCHITECTURE STREQUAL "x86")
add_link_options(/LARGEADDRESSAWARE) # Enable heap size over 2gb for x86 builds
add_link_options(/SAFESEH:NO) # /SAFESEH is x86-only, don't produce safe exception handler tables
endif()
# Allow the compiler to package individual functions in the form of packaged functions (COMDATs)
add_compile_options("$<$<CONFIG:RELEASE>:/Gy>")
# Retains almost all relevant debug information for debugging, but makes a very big difference in performance and is debug safe
# /Ob controls inline expansions and /Ob2 expands any function not explicitly marked for no inlining.
string(REPLACE "/Ob0" "/Ob2" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
string(REPLACE "/Ob0" "/Ob2" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
# Prebuilt deps are built with MT/MTd.
if (OE_USE_PREBUILT_DEPENDENCIES)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
endif()
# We're getting linker errors on Android if we don't pass -Bsymbolic.
# See https://github.com/google/ExoPlayer/issues/9933#issuecomment-1029775358.
if(BUILD_PLATFORM STREQUAL "android")
add_link_options("-Wl,-Bsymbolic")
endif()
resolve_dependencies()
add_subdirectory(thirdparty)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/test)
add_subdirectory(test)
add_subdirectory(src)