forked from flashlight/flashlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
280 lines (238 loc) · 10.2 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
cmake_minimum_required(VERSION 3.10)
project(flashlight LANGUAGES CXX C VERSION 0.3)
include(CTest)
include(CMakeDependentOption)
# ----------------------------- Setup -----------------------------
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
endif()
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Default directories for installation
set(FL_INSTALL_INC_DIR "include" CACHE PATH "Install path for headers")
set(FL_INSTALL_INC_DIR_HEADER_LOC ${FL_INSTALL_INC_DIR}/flashlight)
set(FL_INSTALL_LIB_DIR "lib" CACHE PATH "Install path for libraries")
set(FL_INSTALL_BIN_DIR "bin" CACHE PATH "Install path for binaries")
# Other assets
set(FL_INSTALL_ASSETS_BASE_DIR "share/flashlight")
set(FL_INSTALL_CMAKE_DIR "${FL_INSTALL_ASSETS_BASE_DIR}/cmake" CACHE PATH "Install path for CMake files")
set(FL_INSTALL_EXAMPLES_DIR "${FL_INSTALL_ASSETS_BASE_DIR}/examples" CACHE PATH "Install path for example files")
set(FL_INSTALL_DOC_DIR "${FL_INSTALL_ASSETS_BASE_DIR}/doc" CACHE PATH "Install path for documentation")
include(CheckCXXCompilerFlag)
# All libraries should have their symbols exported so plugins can lazily
# symbols from any of them
check_cxx_compiler_flag("-rdynamic" COMPILER_SUPPORTS_RDYNAMIC)
if(${COMPILER_SUPPORTS_RDYNAMIC})
message(STATUS "-rdynamic supported.")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -rdynamic")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -rdynamic")
else()
message(WARNING
"This compiler doesn't support dynamic symbol exports. "
"Plugin functionality likely won't work.")
endif()
include(InternalUtils)
# ----------------------------- Configuration -----------------------------
option(FL_BUILD_TESTS "Build tests" ON)
option(FL_BUILD_EXAMPLES "Build examples" ON)
option(FL_BUILD_EXPERIMENTAL "Build internal experimental components" OFF)
option(FL_BUILD_SCRIPTS "Build internal scripts for wav2letter++" OFF)
option(FL_BUILD_RECIPES "Build recipes" ON)
option(FL_BUILD_STANDALONE "Build standalone installation" ON)
option(FL_BUILD_LIBRARIES "Build flashlight libraries" ON)
option(FL_BUILD_CORE "Build flashlight core" ON)
cmake_dependent_option(FL_BUILD_CONTRIB
"Build and link additional flashlight contrib assets." ON
"FL_BUILD_CORE" OFF)
# Flashlight backend
set(FL_BACKEND "CUDA" CACHE STRING "Backend with which to build flashlight")
# Select from exactly one backend
set_property(CACHE FL_BACKEND PROPERTY STRINGS CPU CUDA OPENCL)
# Map to flags
set(FL_USE_CPU OFF)
set(FL_USE_CUDA OFF)
set(FL_USE_OPENCL OFF)
if (FL_BACKEND STREQUAL "CPU")
set(FL_USE_CPU ON)
elseif (FL_BACKEND STREQUAL "CUDA")
set(FL_USE_CUDA ON)
elseif (FL_BACKEND STREQUAL "OPENCL")
set(FL_USE_OPENCL ON)
else ()
message(FATAL_ERROR "Invalid FLASHLIGHT backend specified")
endif ()
# Profiling
# TODO: for now, profiling is only enabled with the CUDA backend -
# can enable other profiling once an implementation is in place
cmake_dependent_option(FL_BUILD_PROFILING
"Enable profiling with Flashlight" OFF
"FL_USE_CUDA" OFF)
# List of installable targets
set(INSTALLABLE_TARGETS "")
set(FL_ROOT_DIR ${PROJECT_SOURCE_DIR}/flashlight)
set(FL_BUILD_BINARY_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/bin")
# CUDA/nvcc setup
if (FL_USE_CUDA)
enable_language(CUDA)
# The CUDA standard is still C++14 to enable interopability with
# slightly older and still well-supported versions of CUDA/nvcc
# (e.g. CUDA < 11).
set(CMAKE_CUDA_STANDARD 14)
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
include(${CMAKE_MODULE_PATH}/CUDAUtils.cmake)
set_cuda_arch_nvcc_flags()
# remove me? add -fPIC to nvcc flags if needed
if (FL_LIBRARIES_BUILD_FOR_PYTHON OR CMAKE_POSITION_INDEPENDENT_CODE)
cuda_enable_position_independent_code()
endif ()
endif()
# ------------------------ Tests ------------------------
if (FL_BUILD_TESTS)
enable_testing()
include(TestUtils)
endif ()
# ------------------------ flashlight Libraries ------------------------
# FIXME: libraries are required to build core tests - https://git.io/JL955
if (FL_BUILD_LIBRARIES OR (FL_BUILD_CORE AND FL_BUILD_TESTS))
message(STATUS "Will build flashlight libraries.")
option(FL_LIBRARIES_USE_CUDA "Use CUDA in flashlight libraries build" ON)
option(FL_LIBRARIES_USE_KENLM "Use KenLM in flashlight libraries build" ON)
option(FL_LIBRARIES_USE_MKL "Use MKL in flashlight libraries build" ON)
option(FL_LIBRARIES_BUILD_FOR_PYTHON "Build Python bindings" OFF)
set(FL_COMPILE_DEFINITIONS
$<$<BOOL:${FL_LIBRARIES_USE_CUDA}>:FL_LIBRARIES_USE_CUDA>
$<$<BOOL:${FL_LIBRARIES_USE_MKL}>:FL_LIBRARIES_USE_MKL>
$<$<BOOL:${FL_LIBRARIES_USE_KENLM}>:FL_LIBRARIES_USE_KENLM>
)
set(FL_LIB_DIR "${FL_ROOT_DIR}/lib")
include(${FL_LIB_DIR}/CMakeLists.txt)
if (FL_LIBRARIES_BUILD_FOR_PYTHON)
set(FL_BINDING_PYTHON "${PROJECT_SOURCE_DIR}/bindings/python")
include(${FL_BINDING_PYTHON}/CMakeLists.txt)
endif ()
setup_install_headers(${FL_LIB_DIR} ${FL_INSTALL_INC_DIR_HEADER_LOC})
endif() # if FL_BUILD_LIBRARIES
# --------------------------- flashlight ---------------------------
if (FL_BUILD_CORE)
message(STATUS "Will build flashlight core and extensions.")
# Primary lib target. All non-library code is linked here, including app
add_library(flashlight "")
set_target_properties(
flashlight
PROPERTIES
LINKER_LANGUAGE CXX
CXX_STANDARD 14
)
# FIXME: libraries are required to build core tests - https://git.io/JL955
if (FL_BUILD_LIBRARIES OR (FL_BUILD_CORE AND FL_BUILD_TESTS))
target_link_libraries(flashlight PUBLIC fl-libraries)
endif()
# ------------------------ Global External Dependencies ------------------------
# ArrayFire
find_package(ArrayFire REQUIRED)
if (ArrayFire_FOUND AND ArrayFire_VERSION VERSION_LESS 3.7.1)
message(FATAL_ERROR "ArrayFire versions < 3.7.1 are no longer supported "
"with flashlight. To build flashlight with a version of ArrayFire "
"< 3.7.1, use commit <= 5518d91b7f4fd5b400cbc802cfbecc0df57836bd.")
endif()
if (ArrayFire_FOUND)
message(STATUS "ArrayFire found (include: ${ArrayFire_INCLUDE_DIRS}, library: ${ArrayFire_LIBRARIES})")
else()
message(FATAL_ERROR "ArrayFire not found")
endif()
# Check the proper ArrayFire backend is present
if (FL_USE_CPU AND NOT ArrayFire_CPU_FOUND)
message(FATAL_ERROR "ArrayFire CPU not found: cannot build CPU backend")
elseif (FL_USE_CUDA AND NOT ArrayFire_CUDA_FOUND)
message(FATAL_ERROR "ArrayFire CUDA not found: cannot build CUDA backend")
elseif (FL_USE_OPENCL AND NOT ArrayFire_OpenCL_FOUND)
message(FATAL_ERROR "ArrayFire OpenCL not found: cannot build OpenCL backend")
endif()
# If cereal is found in a user-defined location, use it rather than
# downloading from source
find_package(cereal)
if (NOT TARGET cereal AND NOT cereal_FOUND AND FL_BUILD_STANDALONE)
message(STATUS "cereal NOT found. Will download from source")
set(CEREAL_INSTALL_PATH ${FL_INSTALL_INC_DIR}/cereal)
include(${CMAKE_MODULE_PATH}/BuildCereal.cmake)
add_dependencies(flashlight cereal)
# Move cereal headers at install time
install(DIRECTORY ${CEREAL_SOURCE_DIR}/include/cereal
DESTINATION ${CEREAL_INSTALL_PATH}
COMPONENT cereal
FILES_MATCHING
PATTERN "*.hpp"
PATTERN "*.h"
PATTERN ".git" EXCLUDE
)
install(FILES ${CEREAL_SOURCE_DIR}/LICENSE ${CEREAL_SOURCE_DIR}/README.md
DESTINATION ${CEREAL_INSTALL_PATH}
)
target_include_directories(flashlight PUBLIC ${cereal_INCLUDE_DIRS})
else()
message(STATUS "Found cereal")
target_link_libraries(flashlight PRIVATE cereal)
endif()
setup_install_find_module(${CMAKE_MODULE_PATH}/Findcereal.cmake)
# -------------------- Locate Backend-specific Dependencies --------------------
# TODO: rather than conditionally searching for backend-specific dependencies,
# always search for all dependencies, and dynamically build all backends for
# which all required dependencies are found.
if (FL_USE_CUDA)
find_package(CUDA 9.2 QUIET) # CUDA 9.2 is required for >= ArrayFire 3.6.1
if (CUDA_FOUND)
message(STATUS "CUDA found (library: ${CUDA_LIBRARIES} include: ${CUDA_INCLUDE_DIRS})")
else()
message(STATUS "CUDA not found")
message(FATAL_ERROR "CUDA required to build CUDA backend")
endif()
endif()
if (FL_USE_OPENCL)
find_package(OpenCL)
if (OpenCL_FOUND)
message(STATUS "OpenCL found (library: ${OpenCL_LIBRARIES} include: ${OpenCL_INCLUDE_DIRS})")
else()
message(STATUS "OpenCL not found")
if (FL_USE_OPENCL)
message(FATAL_ERROR "OpenCL required to build OpenCL backend")
endif ()
endif()
endif()
# Set up variables to link the right ArrayFire backend
if (FL_USE_CPU)
set(FL_AF_BACKEND "afcpu")
elseif (FL_USE_CUDA)
set(FL_AF_BACKEND "afcuda")
elseif (FL_USE_OPENCL)
set(FL_AF_BACKEND "afopencl")
else()
message(FATAL_ERROR "flashlight backend ill-specified")
endif()
# Link ArrayFire to flashlight
target_link_libraries(flashlight PUBLIC ArrayFire::${FL_AF_BACKEND})
# ------------------------ flashlight Core ------------------------
set(FL_CORE_DIR "${FL_ROOT_DIR}/fl")
include(${FL_CORE_DIR}/CMakeLists.txt)
set(INSTALLABLE_TARGETS ${INSTALLABLE_TARGETS} flashlight)
# flashlight core components keep their relative paths with respect to project
setup_install_headers(${FL_CORE_DIR} ${FL_INSTALL_INC_DIR_HEADER_LOC})
target_compile_definitions(flashlight
PUBLIC
FL_BACKEND_CPU=$<BOOL:${FL_USE_CPU}>
FL_BACKEND_CUDA=$<BOOL:${FL_USE_CUDA}>
FL_BACKEND_OPENCL=$<BOOL:${FL_USE_OPENCL}>
FL_BUILD_PROFILING=$<BOOL:${FL_BUILD_PROFILING}>
)
# ------------------------ Extensions ------------------------
set(FL_EXT_DIR "${FL_ROOT_DIR}/ext")
include(${FL_EXT_DIR}/CMakeLists.txt)
# Make #include "flashlight/lib" work by adding to the inner incl dir
setup_install_headers(${FL_EXT_DIR} ${FL_INSTALL_INC_DIR_HEADER_LOC})
endif() # if FL_BUILD_CORE
# --------------------------- Apps ---------------------------
set(FL_APPS_DIR "${FL_ROOT_DIR}/app")
include(${FL_APPS_DIR}/CMakeLists.txt)
# --------------------------- Cleanup ---------------------------
setup_install_targets(INSTALL_TARGETS ${INSTALLABLE_TARGETS})