-
Notifications
You must be signed in to change notification settings - Fork 22
/
CMakeLists.txt
434 lines (352 loc) · 12.3 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# Copyright 2019-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
cmake_minimum_required(VERSION 3.13)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
# ------------------------------------------------------------
# Project
# ------------------------------------------------------------
project(MLIO VERSION 0.9.0 LANGUAGES C CXX)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY VALUE RelWithDebInfo)
endif()
# ------------------------------------------------------------
# Build Options
# ------------------------------------------------------------
include(CMakeDependentOption)
option(BUILD_SHARED_LIBS "If set, builds as a shared library." ON)
option(MLIO_INCLUDE_LIB "If set, generates build target for the library." ON)
option(MLIO_INCLUDE_CONTRIB "If set, generates build target for the contrib library.")
option(MLIO_INCLUDE_PYTHON_EXTENSION "If set, generates build target for the Python C extension.")
cmake_dependent_option(
MLIO_INCLUDE_ARROW_INTEGRATION "If set, generates build target for the Apache Arrow integration." OFF
MLIO_INCLUDE_PYTHON_EXTENSION OFF
)
option(MLIO_INCLUDE_TESTS "If set, generates build target for the tests." ON)
option(MLIO_INCLUDE_DOC "If set, generates build target for the documentation.")
option(MLIO_BUILD_S3 "If set, builds with Amazon S3 support.")
option(MLIO_BUILD_IMAGE_READER "If set, builds with image reader support.")
option(MLIO_TREAT_WARNINGS_AS_ERRORS "If set, treats compilation warnings as errors.")
# Optimizations
option(MLIO_BUILD_FOR_NATIVE_ARCHITECTURE "If set, builds for the processor type of the compiling machine.")
option(MLIO_ENABLE_LTO "If set, enables link time optimization." ON)
# Sanitizers
option(MLIO_ENABLE_ASAN "If set, enables ASan.")
option(MLIO_ENABLE_UBSAN "If set, enables UBSan.")
option(MLIO_ENABLE_TSAN "If set, enables TSan.")
# Static Analyzers
option(MLIO_USE_CLANG_TIDY "If set, uses clang-tidy as static analyzer.")
option(MLIO_USE_IWYU "If set, uses include-what-you-use.")
# ------------------------------------------------------------
# Global Settings
# ------------------------------------------------------------
include(LTO)
include(GNUInstallDirs)
add_compile_definitions(MLIO_COMPILE_LIB)
if(BUILD_SHARED_LIBS)
set(MLIO_STATIC_LIB OFF)
else()
set(MLIO_STATIC_LIB ON)
endif()
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_C_VISIBILITY_PRESET hidden)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
if(MLIO_ENABLE_LTO)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
endif()
if(MLIO_INCLUDE_TESTS)
enable_testing()
endif()
# Several dependencies transitively use pthreads library. We make sure
# that they use the -pthread flag if available.
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
# ------------------------------------------------------------
# Compiler Settings
# ------------------------------------------------------------
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
add_compile_options(
-fasynchronous-unwind-tables
-fstack-protector-strong
)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(
-Wall
-Wcast-align
-Wconversion
-Wdouble-promotion
-Wextra
-Wfloat-equal
-Wformat=2
-Winit-self
-Wlogical-op
-Wno-unknown-pragmas
-Wpointer-arith
-Wshadow
-Wsign-conversion
-Wswitch-enum
-Wunused
$<$<COMPILE_LANGUAGE:CXX>:-Wnon-virtual-dtor>
$<$<COMPILE_LANGUAGE:CXX>:-Wold-style-cast>
$<$<COMPILE_LANGUAGE:CXX>:-Woverloaded-virtual>
$<$<COMPILE_LANGUAGE:CXX>:-Wuseless-cast>
)
# See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57709.
# See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61489.
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5)
add_compile_options(-Wno-shadow -Wno-missing-field-initializers)
endif()
# See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43407.
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6)
add_compile_options(-Wno-attributes)
endif()
add_compile_definitions(_GLIBCXX_ASSERTIONS)
else()
add_compile_options(
-fsized-deallocation
-Weverything
-Wno-c++98-compat
-Wno-c++98-compat-pedantic
-Wno-exit-time-destructors
-Wno-global-constructors
-Wno-padded
-Wno-shadow-uncaptured-local
)
endif()
if(MLIO_TREAT_WARNINGS_AS_ERRORS)
add_compile_options(-Werror)
endif()
if(MLIO_BUILD_FOR_NATIVE_ARCHITECTURE)
add_compile_options(-march=native -mtune=native)
endif()
add_compile_options(
$<$<NOT:$<CONFIG:Debug>>:-U_FORTIFY_SOURCE>
$<$<NOT:$<CONFIG:Debug>>:-D_FORTIFY_SOURCE=2>
)
else()
message(FATAL_ERROR "Unsupported compiler!")
endif()
# ------------------------------------------------------------
# Linker Settings
# ------------------------------------------------------------
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
add_link_options(
LINKER:--as-needed
LINKER:--build-id=sha1
LINKER:-z,noexecstack
LINKER:-z,now
LINKER:-z,relro
LINKER:-z,defs
)
if(MLIO_TREAT_WARNINGS_AS_ERRORS)
add_link_options(
LINKER:--fatal-warnings
)
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
add_link_options(
LINKER:-bind_at_load
LINKER:-undefined,error
)
if(MLIO_TREAT_WARNINGS_AS_ERRORS)
add_link_options(
LINKER:--fatal-warnings
)
endif()
endif()
# ------------------------------------------------------------
# Sanitizers
# ------------------------------------------------------------
if(MLIO_ENABLE_ASAN OR MLIO_ENABLE_UBSAN OR MLIO_ENABLE_TSAN)
add_compile_options(-fno-omit-frame-pointer)
endif()
if(MLIO_ENABLE_ASAN)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(-D_GLIBCXX_SANITIZE_VECTOR)
endif()
add_compile_options(-fsanitize=address)
add_link_options (-fsanitize=address)
endif()
if(MLIO_ENABLE_UBSAN)
add_compile_options(-fsanitize=undefined)
add_link_options (-fsanitize=undefined)
endif()
if(MLIO_ENABLE_TSAN)
add_compile_options(-fsanitize=thread)
add_link_options (-fsanitize=thread)
endif()
# ------------------------------------------------------------
# Static Analyzers
# ------------------------------------------------------------
if(MLIO_USE_CLANG_TIDY)
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
message(FATAL_ERROR "clang-tidy is only supported with Clang!")
endif()
find_program(MLIO_CLANG_TIDY NAMES clang-tidy)
if(NOT MLIO_CLANG_TIDY)
message(FATAL_ERROR "clang-tidy is not found!")
endif()
set(CMAKE_CXX_CLANG_TIDY ${MLIO_CLANG_TIDY})
endif()
if(MLIO_USE_IWYU)
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
message(FATAL_ERROR "include-what-you-use is only supported with Clang!")
endif()
find_program(MLIO_IWYU NAMES include-what-you-use)
if(NOT MLIO_IWYU)
message(FATAL_ERROR "include-what-you-use is not found!")
endif()
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${MLIO_IWYU};-Xiwyu;--mapping_file=${PROJECT_SOURCE_DIR}/iwyu.imp)
endif()
# ------------------------------------------------------------
# Dependencies
# ------------------------------------------------------------
find_package(dlpack 0.1.0 REQUIRED CONFIG)
find_package(fmt 6.0 REQUIRED CONFIG)
if(MLIO_INCLUDE_LIB)
find_package(absl REQUIRED CONFIG)
find_package(Iconv REQUIRED)
find_package(natsort REQUIRED CONFIG)
find_package(Protobuf 3.8 REQUIRED)
find_package(TBB REQUIRED CONFIG COMPONENTS tbb)
find_package(Threads REQUIRED)
find_package(ZLIB REQUIRED)
if(MLIO_BUILD_S3)
find_package(AWSSDK 1.7 REQUIRED CONFIG COMPONENTS s3)
endif()
if(MLIO_BUILD_IMAGE_READER)
find_package(OpenCV 4.0 REQUIRED COMPONENTS core imgproc imgcodecs)
endif()
if(MLIO_INCLUDE_TESTS)
find_package(GTest REQUIRED)
endif()
else()
find_package(mlio ${PROJECT_VERSION} REQUIRED CONFIG)
endif()
if(MLIO_INCLUDE_PYTHON_EXTENSION)
find_package(pybind11 2.4.2 REQUIRED CONFIG)
if(${PYTHON_VERSION_STRING} VERSION_LESS "3.6")
message(FATAL_ERROR "Only CPython 3.6 or later is supported.")
endif()
if(MLIO_INCLUDE_ARROW_INTEGRATION)
find_package(Arrow 14.0.1 REQUIRED CONFIG)
endif()
include(PythonExtension)
endif()
if(MLIO_INCLUDE_DOC)
find_package(Doxygen 1.8 REQUIRED)
endif()
# ------------------------------------------------------------
# Targets
# ------------------------------------------------------------
if(MLIO_INCLUDE_LIB)
add_subdirectory(src/mlio)
add_library(mlio::mlio ALIAS mlio)
if(MLIO_INCLUDE_TESTS)
add_subdirectory(tests/mlio-test)
endif()
endif()
if(MLIO_INCLUDE_PYTHON_EXTENSION)
add_subdirectory(src/mlio-py)
endif()
# ------------------------------------------------------------
# Documentation
# ------------------------------------------------------------
if(MLIO_INCLUDE_DOC)
set(DOXYGEN_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/doc)
set(DOXYGEN_EXTRACT_ALL YES)
set(DOXYGEN_QUIET YES)
set(DOXYGEN_GENERATE_HTML YES)
set(DOXYGEN_GENERATE_XML YES)
set(DOXYGEN_JAVADOC_AUTOBRIEF YES)
set(DOXYGEN_EXCLUDE_PATTERNS platform;config.h)
set(DOXYGEN_EXCLUDE_SYMBOLS std;mlio::*::detail;data_type_traits<*>;operator<<)
doxygen_add_docs(
#TARGET
mlio-doc
#SOURCE_DIR
include/mlio
)
install(
DIRECTORY
${PROJECT_BINARY_DIR}/doc/
DESTINATION
${CMAKE_INSTALL_DATAROOTDIR}/doc/mlio-${PROJECT_VERSION}
COMPONENT
devel
OPTIONAL
)
set_property(DIRECTORY APPEND PROPERTY
ADDITIONAL_MAKE_CLEAN_FILES
${PROJECT_BINARY_DIR}/doc/.
)
endif()
# ------------------------------------------------------------
# Package Config
# ------------------------------------------------------------
if(MLIO_INCLUDE_LIB)
include(CMakePackageConfigHelpers)
configure_package_config_file(
#INPUT
${PROJECT_SOURCE_DIR}/src/mlio-config.cmake.in
#OUTPUT
${PROJECT_BINARY_DIR}/lib/cmake/mlio/mlio-config.cmake
INSTALL_DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/mlio-${PROJECT_VERSION}
NO_SET_AND_CHECK_MACRO
)
write_basic_package_version_file(
#OUTPUT
${PROJECT_BINARY_DIR}/lib/cmake/mlio/mlio-config-version.cmake
VERSION
${PROJECT_VERSION}
COMPATIBILITY
AnyNewerVersion
)
install(
FILES
${PROJECT_BINARY_DIR}/lib/cmake/mlio/mlio-config.cmake
${PROJECT_BINARY_DIR}/lib/cmake/mlio/mlio-config-version.cmake
DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/mlio-${PROJECT_VERSION}
COMPONENT
devel
)
install(
EXPORT
mlio-targets
FILE
mlio-targets.cmake
DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/mlio-${PROJECT_VERSION}
COMPONENT
devel
NAMESPACE
mlio::
)
export(
EXPORT
mlio-targets
FILE
${PROJECT_BINARY_DIR}/lib/cmake/mlio/mlio-targets.cmake
NAMESPACE
mlio::
)
endif()