forked from realm/realm-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
263 lines (221 loc) · 10.4 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
if(WIN32)
cmake_minimum_required(VERSION 3.12)
else()
cmake_minimum_required(VERSION 3.15)
endif()
set(CMAKE_BUILD_TYPE Debug CACHE STRING "")
project(RealmCore)
list(APPEND CMAKE_MODULE_PATH "${RealmCore_SOURCE_DIR}/tools/cmake")
# Include general CMake modules
include(GNUInstallDirs)
include(CheckIncludeFiles)
include(GetGitRevisionDescription)
include(Utilities)
include(SpecialtyBuilds)
include(GetVersion)
include(CheckCXXCompilerFlag)
include(CheckCXXSourceRuns)
include(CheckSymbolExists)
# Get accurate git-describe version
git_describe(REALM_VERSION)
# Project-wide build flags
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if(ANDROID)
# TODO: Core APIs should always built for internal usage. But there seems to be issues with cocoa. Enable it only for Android for now.
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
elseif(APPLE)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
endif()
function(add_cxx_flag_if_supported flag)
if(flag MATCHES "^-Wno-")
# Compilers ignore unknown -Wno-foo flags, so look for -Wfoo instead.
string(REPLACE "-Wno-" "-W" check_flag ${flag})
else()
set(check_flag ${flag})
endif()
check_cxx_compiler_flag(${check_flag} HAVE${check_flag})
if(HAVE${check_flag})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
endif()
endfunction()
function(use_faster_linker)
# If a linker has already been set, don't override.
if ("${CMAKE_EXE_LINKER_FLAGS}" MATCHES "-fuse-ld=")
return()
endif()
foreach(LINKER lld gold) # lld > gold > default
# there is no link equivalent to check_cxx_compiler_flag()
set(CMAKE_REQUIRED_LINK_OPTIONS "-fuse-ld=${LINKER}")
check_cxx_source_compiles("int main() {}" HAVE_LINKER_${LINKER})
if(HAVE_LINKER_${LINKER})
foreach(KIND EXE SHARED MODULE)
set(CMAKE_${KIND}_LINKER_FLAGS "${CMAKE_${KIND}_LINKER_FLAGS} -fuse-ld=${LINKER}" PARENT_SCOPE)
endforeach()
return()
endif()
endforeach()
endfunction()
# Set global warnings settings
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /D_CRT_SECURE_NO_WARNINGS /D_SCL_SECURE_NO_WARNINGS")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Wundef -Wshadow")
# TODO: Remove this when fixed
if(ANDROID)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-uninitialized")
elseif(${CMAKE_CXX_COMPILER_ID} MATCHES ".*[Cc]lang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunreachable-code -Wshorten-64-to-32 -Wold-style-cast -Wconditional-uninitialized -Wextra-semi -Wno-nested-anon-types -Wdocumentation")
endif()
# This warning is too agressive. It warns about moves that are not redundant on older
# compilers that we still support. It is also harmless, unlike pessimizing moves.
add_cxx_flag_if_supported(-Wno-redundant-move)
# Ninja buffers output so we need to tell the compiler to use colors even though stdout isn't a tty.
if("${CMAKE_GENERATOR}" STREQUAL "Ninja")
add_cxx_flag_if_supported(-fdiagnostics-color)
endif()
use_faster_linker()
endif()
if(ANDROID)
# Optimize for size vs. performance for Android. The has the following implications:
# - Replace `-O3` with `-Oz`.
# - Add `-ffunction-sections` and `-fdata-sections`. This requires that `-Wl,-gc-sections` are used when creating
# the final .so file.
# - `-fstrict-aliasing` is inherited from NDK r10e.
# - `-fomit-frame-pointer` is inherited from NDK r10e.
#
# On some architectures char is unsigned by default. Make it signed
set(CMAKE_CXX_FLAGS_RELEASE "-Oz -DNDEBUG")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdata-sections -ffunction-sections -fomit-frame-pointer -fsigned-char -fstrict-aliasing -funwind-tables -no-canonical-prefixes")
endif()
set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS
$<$<CONFIG:MinSizeDebug>:-g>
)
set(OPENSSL_VERSION ${DEP_OPENSSL_VERSION})
set(CMAKE_DEBUG_POSTFIX "-dbg")
set(CMAKE_MINSIZEDEBUG_POSTFIX "-dbg")
set(CMAKE_POSITION_INDEPENDENT_CODE true)
if(CMAKE_SYSTEM_NAME MATCHES "^Windows")
add_definitions(
/D_SCL_SECURE_NO_WARNINGS
/DWIN32_LEAN_AND_MEAN
/DUNICODE
/D_UNICODE
)
endif()
if(MSVC)
add_compile_options(
/MP # Enable multi-processor compilation
)
add_definitions(
/D_DISABLE_EXTENDED_ALIGNED_STORAGE #https://developercommunity.visualstudio.com/comments/279328/view.html
)
if(WINDOWS_STORE)
# Removing LNK4075 warnings for debug UWP builds
# "LNK4075: ignoring '/INCREMENTAL' due to '/OPT:ICF' specification"
# "LNK4075: ignoring '/INCREMENTAL' due to '/OPT:REF' specification"
# Optional verification checks since we don't know existing contents of variables below
string(REPLACE "/OPT:ICF " "/OPT:NOICF " CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
string(REPLACE "/OPT:REF " "/OPT:NOREF " CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
string(REPLACE "/INCREMENTAL:YES " "/INCREMENTAL:NO " CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
string(REPLACE "/INCREMENTAL " "/INCREMENTAL:NO " CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
# Mandatory
set(CMAKE_MODULE_LINKER_FLAGS_DEBUG "${CMAKE_MODULE_LINKER_FLAGS_DEBUG} /INCREMENTAL:NO /OPT:NOREF /OPT:NOICF")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /INCREMENTAL:NO /OPT:NOREF /OPT:NOICF")
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} /INCREMENTAL:NO /OPT:NOREF /OPT:NOICF")
else()
# Statically link the run-time library
# TODO: replace with CMAKE_MSVC_RUNTIME_LIBRARY once we switch to CMake 3.15 as the minimum
# https://docs.microsoft.com/bg-bg/cpp/build/reference/md-mt-ld-use-run-time-library
# https://cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
foreach(flag_var
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
if(${flag_var} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endif()
# Disable runtime stack bound checks, because these slow down unit tests
# disproportionately.
if(${flag_var} MATCHES "/RTC")
string(REGEX REPLACE "/RTC(su|[1su])" "" ${flag_var} "${${flag_var}}")
endif()
endforeach()
endif()
# Let the compiler optimize debug builds ever so slightly.
# /Ob1: This allows the compiler to inline functions marked __inline.
# /Oi: This enables (faster) intrinsic functions.
string(REPLACE "/Ob0" "/Ob1" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
string(CONCAT CMAKE_CXX_FLAGS_DEBUG "/Oi " "${CMAKE_CXX_FLAGS_DEBUG}")
endif()
# Platform-specific build configuration
if(APPLE)
find_library(Foundation Foundation)
elseif(ANDROID)
list(APPEND PLATFORM_LIBRARIES log android)
endif()
if(UNIX)
# Enable access to large file APIs, but don't make them the default.
add_compile_definitions("_LARGEFILE_SOURCE" "_LARGEFILE64_SOURCE")
set(CMAKE_REQUIRED_DEFINITIONS "-D_LARGEFILE_SOURCE" "-D_LARGEFILE64_SOURCE")
# Use readdir64 if available.
check_symbol_exists(readdir64 "dirent.h" REALM_HAVE_READDIR64)
endif()
# Find dependencies
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
list(APPEND PLATFORM_LIBRARIES Threads::Threads)
# Options (passed to CMake)
option(REALM_ENABLE_ASSERTIONS "Enable assertions in release mode." OFF)
option(REALM_ENABLE_ALLOC_SET_ZERO "Zero all allocations." OFF)
option(REALM_ENABLE_ENCRYPTION "Enable encryption." ON)
option(REALM_ENABLE_MEMDEBUG "Add additional memory checks" OFF)
option(REALM_VALGRIND "Tell the test suite we are running with valgrind" OFF)
option(REALM_METRICS "Enable various metric tracking" ON)
set(REALM_MAX_BPNODE_SIZE "1000" CACHE STRING "Max B+ tree node size.")
check_include_files(malloc.h HAVE_MALLOC_H)
# Store configuration in header file
configure_file(src/realm/util/config.h.in src/realm/util/config.h)
# Configure source code to use right version number
configure_file(src/realm/version_numbers.hpp.in src/realm/version_numbers.hpp)
set(DEPRECATED_CONFIG_FILE "${RealmCore_SOURCE_DIR}/src/realm/util/config.h")
if(EXISTS "${DEPRECATED_CONFIG_FILE}")
message(FATAL_ERROR "${DEPRECATED_CONFIG_FILE} exists in the source directory, and will take precedence over the generated configuration in the build directory. Please remove this file before continuing. Alternatively, you can also clean your realm-core to remove this and other stale files: git clean -xfd")
endif()
set(PEGTL_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/external/pegtl/include/tao/)
# Tell the build system where to find the sources (and generated sources)
include_directories(src)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/src) # For generated files (like config.h)
# Include additional CMakeLists
add_subdirectory(src)
# Enable CTest and include unit tests
if(NOT REALM_BUILD_LIB_ONLY AND NOT REALM_NO_TESTS)
enable_testing()
add_subdirectory(test)
endif()
# Install the licence and changelog files
install(FILES LICENSE CHANGELOG.md DESTINATION "doc/realm" COMPONENT devel)
# Make the project importable from the build directory
export(TARGETS Storage QueryParser NAMESPACE RealmCore:: FILE RealmCoreTargets.cmake)
configure_file(${CMAKE_CURRENT_LIST_DIR}/tools/cmake/RealmCoreConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/RealmCoreConfig.cmake @ONLY)
# Make the project importable from the install directory
install(EXPORT realm
NAMESPACE RealmCore::
FILE RealmCoreTargets.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/RealmCore
COMPONENT devel
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/RealmCoreConfig.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/RealmCore
COMPONENT devel
)
# CPack
set(CPACK_GENERATOR "TGZ")
set(CPACK_PACKAGE_NAME "realm-core-${CMAKE_BUILD_TYPE}")
set(CPACK_PACKAGE_VERSION ${REALM_VERSION})
set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)
include(CPack)
cpack_add_component(runtime DEPENDS runtime)
cpack_add_component(devel DEPENDS devel)