-
Notifications
You must be signed in to change notification settings - Fork 6
/
CMakeLists.txt
470 lines (399 loc) · 16.5 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
cmake_minimum_required(VERSION 3.0)
#
# options
#
option (BUILD_PACKAGING "Enable build of distribution packages using CPack." OFF)
option (BUILD_SHARED_LIBS "Build shared libraries." OFF)
# to turn on, specify -DBUILD_TESTING=ON in cmake command line
option (BUILD_TESTING "Enable build of the unit tests and their execution using CTest." OFF)
option (BUILD_INTEGRATION_TESTING "Enable integration test execution using CTest." OFF)
option (INSTALL_HEADERS "Request installation of headers and other development files." OFF)
option (REGISTER_BUILD_DIR "Request entry of build directory in CMake's package registry." OFF)
option (UPDATE_CONNECT "Update automatically from the remote repository." OFF)
set (MOUNT_POINT "" CACHE STRING "Mount point (full path) to run integration test.")
set (BUCKET_NAME "" CACHE STRING "Bucket name to run integration test.")
set (ZONE "" CACHE STRING "Zone name to run integration test.")
if (BUILD_INTEGRATION_TESTING)
# check if directory of mount point exist
# notes it should be a full path
if (NOT EXISTS ${MOUNT_POINT})
message(FATAL_ERROR "Mount point ${MOUNT_POINT} is not existing, please use full path and confirm it's existence.")
endif()
if ("${BUCKET_NAME}" STREQUAL "")
message (FATAL_ERROR "Bucket name for integration test is empty, please given an existing bucket name.")
endif()
if ("${ZONE}" STREQUAL "")
message (FATAL_ERROR "Zone name for integration test is empty, please given an existing zone name, such as sh1a, pek3a, etc.")
endif()
endif (BUILD_INTEGRATION_TESTING)
#
# system info
#
execute_process(COMMAND uname -m OUTPUT_VARIABLE MACHINE_HARDWARE OUTPUT_STRIP_TRAILING_WHITESPACE)
if (MACHINE_HARDWARE MATCHES "x86_64" OR MACHINE_HARDWARE MATCHES "x64"
OR MACHINE_HARDWARE MATCHES "amd64")
set(IS_MACH_64 TRUE CACHE BOOL "Machine hardware is 64-bit")
endif()
execute_process(COMMAND whoami OUTPUT_VARIABLE CURRENT_USER)
if(CURRENT_USER MATCHES "root")
set (IS_ROOT TRUE CACHE BOOL "Current user is root")
endif()
if (IS_MACH_64)
# when GCC is built as a 64-bit only compiler
# this only affect configure step, not affect build step
set(ENV{LIBRARY_PATH} "/usr/lib/x86_64-linux-gnu/:$LIBRARY_PATH"
CACHE PATH "library path env variable")
endif()
execute_process(COMMAND head -1 /etc/issue OUTPUT_VARIABLE DISTROS_ISSUE)
string (STRIP "${DISTROS_ISSUE}" DISTROS_ISSUE)
string (FIND "${DISTROS_ISSUE}" " " POS1)
string (SUBSTRING "${DISTROS_ISSUE}" 0 ${POS1} OS_NAME)
string (REGEX MATCH "([0-9]+\\.[0-9]+)" OS_VERSION "${DISTROS_ISSUE}")
if (OS_NAME MATCHES "CentOS" OR OS_NAME MATCHES "RedHat")
set(OS_NAME "el")
string (REPLACE "." "_" OS_VERSION "${OS_VERSION}")
endif ()
string (CONCAT OS_NAME_VERSION ${OS_NAME} ${OS_VERSION})
if (OS_NAME MATCHES "el")
if (OS_VERSION MATCHES "^5")
set (IS_CENTOS_5 TRUE CACHE BOOL "Current OS is CentOS/RedHat 5")
endif()
endif()
#
# project information
#
set (PACKAGE_NAME "qsfs")
set (PACKAGE_VERSION_MAJOR 1)
set (PACKAGE_VERSION_MINOR 0)
set (PACKAGE_VERSION_PATCH 11)
set (PACKAGE_VERSION ${PACKAGE_VERSION_MAJOR}.${PACKAGE_VERSION_MINOR}.${PACKAGE_VERSION_PATCH})
set (PACKAGE_RELEASE alpha)
set (PACKAGE_STRING "${QSFS_NAME} ${QSFS_VERSION}")
set (PACKAGE_TARNAME "${QSFS_NAME}-${QSFS_VERSION}")
set (PACKAGE_BUGREPORT "https://github.com/yunify/qsfs-fuse/issues")
set (PACKAGE_DESCRIPTION "A FUSE based filesystem allows you to mount a qingstor bucket in Linux.")
set (PACKAGE_URL "https://github.com/yunify/qsfs-fuse")
string (TOUPPER "${PACKAGE_NAME}" PACKAGE_PREFIX)
project (${PACKAGE_NAME})
message (STATUS "Building project version: ${PACKAGE_VERSION}")
#
# directories
#
# qsfs source directory
set (QSFS_SOURCE_DIR ${CMAKE_SOURCE_DIR}/src
CACHE PATH "qsfs source directory")
set (THIRD_PARTY_SOURCE_DIR ${CMAKE_SOURCE_DIR}/third_party
CACHE PATH "third party source directory")
# qsfs test directory
set (QSFS_TEST_DIR ${CMAKE_SOURCE_DIR}/test
CACHE PATH "qsfs test directory")
# qsfs integration test run dir
if (${MOUNT_POINT} MATCHES "/$") # ends with /
set (QSFS_INTEGRATION_TEST_RUN_DIR "${MOUNT_POINT}qsfs_integration_test")
else ()
set (QSFS_INTEGRATION_TEST_RUN_DIR "${MOUNT_POINT}/qsfs_integration_test")
endif ()
#
# configure version
#
if (EXISTS ${QSFS_SOURCE_DIR}/configure/VersionConfig.h)
FILE(REMOVE ${QSFS_SOURCE_DIR}/configure/VersionConfig.h)
endif ()
configure_file (cmake/templates/VersionConfig.h.in
"${QSFS_SOURCE_DIR}/configure/VersionConfig.h")
if (EXISTS ${QSFS_SOURCE_DIR}/configure/OSNameConfig.h)
FILE(REMOVE ${QSFS_SOURCE_DIR}/configure/OSNameConfig.h)
endif ()
configure_file (cmake/templates/OSNameConfig.h.in
"${QSFS_SOURCE_DIR}/configure/OSNameConfig.h")
if (EXISTS ${QSFS_SOURCE_DIR}/configure/ArchitectureConfig.h)
FILE(REMOVE ${QSFS_SOURCE_DIR}/configure/ArchitectureConfig.h)
endif ()
configure_file (cmake/templates/ArchitectureConfig.h.in
"${QSFS_SOURCE_DIR}/configure/ArchitectureConfig.h")
#
# dependencies
#
find_package (Threads REQUIRED)
if (NOT Threads_FOUND)
message (FATAL_ERROR "Could not find pthread. Check the log file"
"\n\t${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log")
endif ()
find_package(CURL REQUIRED)
find_package(OpenSSL REQUIRED)
find_program(BASH_PROGRAM bash)
if (NOT BASH_PROGRAM)
message(WARNING "Cound not find bash.")
endif ()
# Not call find_package to check dependencies, instead
# download and install dependencies as static libraries
# under ./third_party/install
set (EXTERNAL_PROJECT_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/third_party/install
CACHE PATH "Installation prefix for dependencies")
if (NOT googletest_INSTALLED)
include (cmake/gtest.cmake)
endif()
if(NOT googleflags_INSTALLED)
include (cmake/gflags.cmake)
endif()
if(NOT googlelog_INSTALLED)
include(cmake/glog.cmake)
endif()
# qingstor sdk static libraries is not available now,
# so its shared libraries will be installed under /usr/local/lib
if (NOT qingstorsdk_INSTALLED)
include (cmake/qingstorsdk.cmake)
endif()
#
# linter target (make lint)
#
add_custom_target (lint COMMAND ${CMAKE_COMMAND} -P
${CMAKE_SOURCE_DIR}/cmake/lint.cmake)
#
# compile set up
#
# As we build gflags as a subproject and gflags is built as static single-threaded
# library by default. As glog depend on the gflags library and should link to gfalgs
# target, and this requires position-independent code.
# As not all systems support '-shared' option to produce a shared object
# which can then be linked with other object to form an executable.
# For predictable results, explicitly specify the -fPIC option.
# For example, on Debain (gcc 6.3.0), without explicit set -fPIC, cmake
# raise error:
# EXPORT glog-target requires target gflags_nothreads_static
# which is not in export set.
add_compile_options (-fPIC)
# add_compile_options (-Wall) # for dev
# disable warnings for boost, this is only for gcc 4.8 or later
add_compile_options (-Wno-deprecated-declarations -Wno-unused-local-typedefs -Wno-delete-non-virtual-dtor)
# do not specify -g, instead specify -DCMAKE_BUILD_TYPE=Debug/Release in command line
# add_compile_options (-g -o0 -fno-inline)
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set (CMAKE_INSTALL_DO_STRIP 0)
endif()
# fuse need to uses 64 bit off_t *
add_compile_options(-D_FILE_OFFSET_BITS=64)
#
# set up include/link directories
#
# Where qsfs's header files can be found
include_directories (${QSFS_SOURCE_DIR})
# Where boost files can be found
include_directories(${THIRD_PARTY_SOURCE_DIR}/boost_1_49_0)
# Where boost lib can be found
if (IS_MACH_64)
# link to boost lib built on centos5.8-64bit with gcc4.1.2
# and boost lib built on macOS10.12.5
link_directories(${THIRD_PARTY_SOURCE_DIR}/boost_1_49_0/lib64)
else (IS_MACH_64)
# link to boost lib built on centos5.8-32bitwith gcc4.1.2
link_directories(${THIRD_PARTY_SOURCE_DIR}/boost_1_49_0/lib)
endif ()
# other dependencies (glog/gtest) built with source code and
# installed under third-party/install in cmake configure.
# Where dependencies's header files can be found.
include_directories(${EXTERNAL_PROJECT_INSTALL_PREFIX}/include)
# Where dependencies's targets can be found.
# On ubuntu x86_64 all libs get installed under lib
link_directories(${EXTERNAL_PROJECT_INSTALL_PREFIX}/lib)
if (IS_MACH_64 AND NOT APPLE)
# On Centos x86_64 glog/gtest libs get installed under lib64
link_directories(${EXTERNAL_PROJECT_INSTALL_PREFIX}/lib64)
endif()
# test on centos6.8 seems not include following default path
link_directories(/usr/local/lib)
#
# Make sure that the required libraries are always found independent
# from LD_LIBRARY_PATH and the install location
#
# use, i.e. don't skip the full RPATH for the build tree
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
# when building, don't use the install RPATH already
# (but later on when installing)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
# the RPATH to be used when installing
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
# add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# the RPATH to be used when installing, but only if it's not a system directory
LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
IF("${isSystemDir}" STREQUAL "-1")
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
ENDIF("${isSystemDir}" STREQUAL "-1")
#
# installation
#
# directories vars should be put before include directories
set (INSTALL_RUNTIME_DIR bin CACHE PATH "Installation directory for executables")
set (INSTALL_LIBRARY_DIR lib CACHE PATH "Installation directory for libraries")
set (INSTALL_INCLUDE_DIR include CACHE PATH "Installation directory for header files")
set (INSTALL_CMAKE_DIR ${INSTALL_LIBRARY_DIR}/cmake/${PROJECT_NAME} CACHE PATH
"Installation directory for CMake files")
set (EXPORT_NAME ${PROJECT_NAME}-targets)
set (INSTALL_PREFIX_REL2CONFIG_DIR .)
file (RELATIVE_PATH INSTALL_PREFIX_REL2CONFIG_DIR
"${CMAKE_INSTALL_PREFIX}/${INSTALL_CMAKE_DIR}" "${CMAKE_INSTALL_PREFIX}")
# for build tree
configure_file (cmake/templates/config.cmake.in
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake" @ONLY)
# for install tree
configure_file (cmake/templates/config.cmake.in
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}-config.cmake" @ONLY)
# for both
configure_file (cmake/templates/version.cmake.in
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake" @ONLY)
if (INSTALL_HEADERS)
install (FILES
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}-config.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake"
DESTINATION "${INSTALL_CMAKE_DIR}" COMPONENT dev
)
install (
EXPORT ${EXPORT_NAME}
DESTINATION "${INSTALL_CMAKE_DIR}" COMPONENT dev
)
endif ()
# support direct use of build tree
export (TARGETS ${TARGETS} FILE "${PROJECT_BINARY_DIR}/${EXPORT_NAME}.cmake")
if (REGISTER_BUILD_DIR)
export (PACKAGE ${PACKAGE_NAME})
endif ()
#
# uninstall
#
include(cmake/UninstallProject.cmake)
setup_uninstall_project(${PROJECT_NAME})
if(TARGET uninstall_${PROJECT_NAME})
set (UNINSTALL_ALL ${UNINSTALL_ALL} uninstall_${PROJECT_NAME})
set (UNINSTALL_DEFAULT ${UNINSTALL_DEFAULT} uninstall_${PROJECT_NAME})
endif()
if(TARGET uninstall_qingstorsdk)
set (UNINSTALL_ALL ${UNINSTALL_ALL} uninstall_qingstorsdk)
set (UNINSTALL_DEFAULT ${UNINSTALL_DEFAULT} uninstall_qingstorsdk)
endif()
if(TARGET uninstall_googletest)
set (UNINSTALL_ALL ${UNINSTALL_ALL} uninstall_googletest)
endif()
if(TARGET uninstall_googlelog)
set (UNINSTALL_ALL ${UNINSTALL_ALL} uninstall_googlelog)
endif()
if(TARGET uninstall_googleflags)
set (UNINSTALL_ALL ${UNINSTALL_ALL} uninstall_googleflags)
endif()
# uninstall only uninstall qsfs and qingstor sdk, as they are
# installed under /usr/local
add_custom_target(
uninstall DEPENDS ${UNINSTALL_DEFAULT}
)
add_custom_target(
uninstall_all DEPENDS ${UNINSTALL_ALL}
)
#
# subdirectories
#
add_subdirectory (src)
#
# testing
#
if (BUILD_TESTING)
include(CTest)
enable_testing ()
add_subdirectory(test)
endif (BUILD_TESTING)
if (BUILD_INTEGRATION_TESTING AND BASH_PROGRAM)
configure_file(cmake/templates/CTestCustom.cmake.in
"${CMAKE_BINARY_DIR}/CTestCustom.cmake" @ONLY)
include(CTest)
enable_testing ()
add_subdirectory(test/sh)
endif (BUILD_INTEGRATION_TESTING AND BASH_PROGRAM)
#
# packaging
#
if (BUILD_PACKAGING)
include (InstallRequiredSystemLibraries)
# unix
set (PACKAGE_GENERATOR "DEB;RPM")
set (PACKAGE_SOURCE_GENERATOR "TGZ;ZIP")
# used package generators
set (CPACK_GENERATOR "${PACKAGE_GENERATOR}" CACHE STRING "List of binary package generators (CPack).")
set (CPACK_SOURCE_GENERATOR "${PACKAGE_SOURCE_GENERATOR}" CACHE STRING "List of source package generators (CPack).")
mark_as_advanced (CPACK_GENERATOR CPACK_SOURCE_GENERATOR)
# common package information
set (CPACK_PACKAGE_VENDOR "yunify")
set (CPACK_PACKAGE_CONTACT "jimhuang@yunify.com")
set (CPACK_PACKAGE_NAME "${PACKAGE_NAME}")
set (CPACK_PACKAGE_VERSION "${PACKAGE_VERSION}")
set (CPACK_PACKAGE_VERSION_MAJOR "${PACKAGE_VERSION_MAJOR}")
set (CPACK_PACKAGE_VERSION_MINOR "${PACKAGE_VERSION_MINOR}")
set (CPACK_PACKAGE_VERSION_PATCH "${PACKAGE_VERSION_PATCH}")
set (CPACK_PACKAGE_RELEASE "${PACKAGE_RELEASE}")
set (CPACK_OS_NAME_VERSION "${OS_NAME_VERSION}") # cpack var must begin with CPACK
set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "${PACKAGE_DESCRIPTION}")
set (CPACK_RESOURCE_FILE_WELCOME "${PROJECT_SOURCE_DIR}/README.md")
set (CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/COPYING")
set (CPACK_PACKAGE_DESCRIPTION_FILE "${PROJECT_SOURCE_DIR}/README.md")
set (CPACK_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
set (CPACK_PACKAGING_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
set (CPACK_OUTPUT_FILE_PREFIX packages)
set (CPACK_PACKAGE_RELOCATABLE TRUE)
set (CPACK_MONOLITHIC_INSTALL TRUE)
# RPM package information -- used in cmake/package.cmake.in also for DEB
# following variables must defined here instead of cmake/package.cmake.in
set (CPACK_RPM_PACKAGE_GROUP "Applications/System")
set (CPACK_RPM_PACKAGE_LICENSE "Apache")
set (CPACK_RPM_PACKAGE_URL "${PACKAGE_URL}")
set (CPACK_RPM_PACKAGE_RELEASE "${CPACK_PACKAGE_RELEASE}")
set (CPACK_RPM_CHANGELOG_FILE "${PROJECT_SOURCE_DIR}/ChangeLog")
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
# to prevent rpmbuild strip debug symbols
set (CPACK_RPM_SPEC_MORE_DEFINE "%define __spec_install_post /bin/true")
endif()
# unix system/architecture
string (TOLOWER "${CMAKE_SYSTEM_NAME}" CPACK_SYSTEM_NAME)
if (CMAKE_CXX_FLAGS MATCHES "-m32")
set (CPACK_PACKAGE_ARCHITECTURE i386)
else ()
execute_process (
COMMAND dpkg --print-architecture
RESULT_VARIABLE RV
OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE
)
if (RV EQUAL 0)
string (STRIP "${CPACK_PACKAGE_ARCHITECTURE}" CPACK_PACKAGE_ARCHITECTURE)
else ()
execute_process (
COMMAND uname -m
OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE
)
if (CPACK_PACKAGE_ARCHITECTURE MATCHES "x86_64")
set (CPACK_PACKAGE_ARCHITECTURE x86_64)
else ()
set (CPACK_PACKAGE_ARCHITECTURE i386)
endif ()
endif ()
endif ()
# source package settings
set (CPACK_SOURCE_TOPLEVEL_TAG "source")
set (CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
set (CPACK_SOURCE_IGNORE_FILES "/\\\\.git/;\\\\.swp$;\\\\.#;/#;\\\\.*~;cscope\\\\.*;/[Bb]uild[.+-_a-zA-Z0-9]*/")
# default binary package settings
set (CPACK_INCLUDE_TOPLEVEL_DIRECTORY TRUE)
set (CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_SYSTEM_NAME}")
if (CPACK_PACKAGE_ARCHITECTURE)
set (CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-${CPACK_PACKAGE_ARCHITECTURE}")
endif ()
# generator specific configuration file
#
# allow package maintainers to use their own configuration file
# $ cmake -DCPACK_PROJECT_CONFIG_FILE:FILE=/path/to/package/config
if (NOT CPACK_PROJECT_CONFIG_FILE)
configure_file (
cmake/templates/package.cmake.in
"${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-package.cmake" @ONLY
)
set (CPACK_PROJECT_CONFIG_FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-package.cmake")
endif ()
include (CPack)
endif (BUILD_PACKAGING) # BUILD_PACKAGING