80fb3807f2a946a8ab392b3b3d9a91f0106b2035
[libcds.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 2.8.12)
2
3 cmake_policy(SET CMP0016 NEW)
4 cmake_policy(SET CMP0042 NEW)
5
6 set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/build/cmake ${CMAKE_MODULE_PATH})
7 include(TargetArch)
8
9 project(cds)
10
11 set(PROJECT_VERSION 2.2.0)
12
13 # Options
14 option(WITH_TESTS "Build unit tests" OFF)
15 option(WITH_TESTS_COVERAGE "Analyze test coverage using gcov (only for gcc)" OFF)
16 option(WITH_BOOST_ATOMIC "Use boost atomics (only for boost >= 1.54)" OFF)
17 option(WITH_ASAN "Build AddressSantinizer instrumented code" OFF)
18 option(WITH_TSAN "Build ThreadSantinizer instrumented code" OFF)
19 set(CMAKE_TARGET_ARCHITECTURE "" CACHE string "Target build architecture")
20
21 find_package(Threads)
22
23 if(TARGET boost::system AND TARGET boost::thread)
24     link_libraries(boost::system boost::thread)
25 else()
26     find_package(Boost 1.50 COMPONENTS system thread)
27 endif()
28
29 include_directories(${Boost_INCLUDE_DIRS})
30
31 if(NOT CMAKE_TARGET_ARCHITECTURE)
32     target_architecture(CMAKE_TARGET_ARCHITECTURE)
33 endif()
34
35 if(WITH_BOOST_ATOMIC)
36     if(Boost_FOUND)
37         if(${Boost_MINOR_VERSION} GREATER 53)
38             set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCDS_USE_BOOST_ATOMIC")
39             message(STATUS "Boost version allows using of boost.atomic: activated")
40         endif()
41     else()
42         if(TARGET boost::atomic)
43             link_libraries(boost::atomic)
44         endif()
45     endif()
46 endif(WITH_BOOST_ATOMIC)
47
48 if(WITH_ASAN)
49     if(CMAKE_COMPILER_IS_GNUCXX)
50         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O1 -fPIC -fsanitize=address -g -fno-omit-frame-pointer -fno-optimize-sibling-calls")
51         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O1 -fsanitize=address -g -fno-omit-frame-pointer -fno-optimize-sibling-calls")
52         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address -pie")
53     else()
54         message(WARNING "Compiler does not support AddressSanitizer")
55     endif()
56 endif(WITH_ASAN)
57
58 if(WITH_TSAN)
59     if(CMAKE_COMPILER_IS_GNUCXX)
60         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O1 -fPIC -fsanitize=thread -g -DCDS_THREAD_SANITIZER_ENABLED -fno-omit-frame-pointer")
61         set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -O1 -fPIC -fsanitize=thread -g -DCDS_THREAD_SANITIZER_ENABLED -fno-omit-frame-pointer")
62         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread -pie")
63     else()
64         message(WARNING "Compiler does not support ThreadSanitizer")
65     endif()
66 endif(WITH_TSAN)
67
68 if(WITH_TESTS_COVERAGE)
69     if(CMAKE_COMPILER_IS_GNUCXX)
70         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
71         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
72         set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
73         message(STATUS "Test coverage analysis: activated")
74     else()
75         message(WARNING "Compiler is not GNU gcc! Test coverage couldn't be analyzed")
76     endif()
77 endif(WITH_TESTS_COVERAGE)
78
79 set(CDS_SHARED_LIBRARY ${PROJECT_NAME})
80 set(CDS_STATIC_LIBRARY ${PROJECT_NAME}-s)
81
82 set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
83 set(CMAKE_INCLUDE_CURRENT_DIR ON)
84
85 if(CDS_BIN_DIR)
86     set(EXECUTABLE_OUTPUT_PATH ${CDS_BIN_DIR})
87     set(LIBRARY_OUTPUT_PATH ${CDS_BIN_DIR})
88 else()
89     set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
90     set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
91 endif()
92 message(STATUS "Binary output path: ${EXECUTABLE_OUTPUT_PATH}")
93
94 if(NOT CMAKE_BUILD_TYPE)
95     set(CMAKE_BUILD_TYPE Debug CACHE STRING "Default build type to Debug" FORCE)
96 endif()
97
98 if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
99     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -pedantic -fno-strict-aliasing")
100     if(CMAKE_TARGET_ARCHITECTURE STREQUAL "x86_64")
101         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcx16")
102         set(LIB_SUFFIX "64")
103     endif()
104     if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "7.0.0")
105         #   gcc 4.8 - 6: disable noise -Wunused-local-typedefs
106         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-local-typedefs")
107     endif()
108 endif()
109
110 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG")
111
112 message("Build type -- ${CMAKE_BUILD_TYPE}")
113 message("Compiler version: ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
114 message("Target architecture: ${CMAKE_TARGET_ARCHITECTURE}")
115 if(CMAKE_BUILD_TYPE STREQUAL "DEBUG")
116     message("Compiler flags: ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG}")
117 else()
118     message("Compiler flags: ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE}")
119 endif()
120 if(Boost_FOUND)
121     message("Boost: ${Boost_LIB_VERSION} in ${Boost_INCLUDE_DIRS}, lib ${Boost_LIBRARY_DIRS}")
122 else()
123     message("Boost: Using CMake-fied boost")
124 endif()
125
126
127 include_directories(${PROJECT_SOURCE_DIR})
128
129 # Component names for separate distribution in rpms, debs etc.
130 set(LIBRARIES_COMPONENT lib)
131 set(HEADERS_COMPONENT devel)
132
133 set(SOURCES src/hp_gc.cpp
134         src/init.cpp
135         src/dhp_gc.cpp
136         src/urcu_gp.cpp
137         src/urcu_sh.cpp
138         src/michael_heap.cpp
139         src/topology_hpux.cpp
140         src/topology_linux.cpp
141         src/topology_osx.cpp
142         src/dllmain.cpp)
143
144 add_library(${CDS_SHARED_LIBRARY} SHARED ${SOURCES})
145 set_target_properties(${CDS_SHARED_LIBRARY} PROPERTIES VERSION ${PROJECT_VERSION}
146         DEBUG_POSTFIX "_d")
147 add_library(${CDS_STATIC_LIBRARY} STATIC ${SOURCES})
148 set_target_properties(${CDS_STATIC_LIBRARY} PROPERTIES DEBUG_POSTFIX "_d")
149 target_link_libraries(${CDS_SHARED_LIBRARY} PRIVATE ${CMAKE_THREAD_LIBS_INIT})
150 target_link_libraries(${CDS_STATIC_LIBRARY} PRIVATE ${CMAKE_THREAD_LIBS_INIT})
151
152 install(TARGETS ${CDS_SHARED_LIBRARY} LIBRARY DESTINATION lib${LIB_SUFFIX} COMPONENT ${LIBRARIES_COMPONENT} NAMELINK_SKIP)
153 install(TARGETS ${CDS_SHARED_LIBRARY} LIBRARY DESTINATION lib${LIB_SUFFIX} COMPONENT ${HEADERS_COMPONENT} NAMELINK_ONLY)
154 install(TARGETS ${CDS_STATIC_LIBRARY} DESTINATION lib${LIB_SUFFIX} COMPONENT ${LIBRARIES_COMPONENT})
155 install(DIRECTORY ${PROJECT_SOURCE_DIR}/cds DESTINATION include COMPONENT ${HEADERS_COMPONENT})
156
157 if(WITH_TESTS)
158     enable_testing()
159     add_subdirectory(${PROJECT_SOURCE_DIR}/test)
160     message(STATUS "Build tests: activated")
161 endif(WITH_TESTS)
162
163 ### FOR PACKAGING in RPM, TGZ, DEB, NSYS...###############################################################################
164 set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
165 set(CPACK_PACKAGE_NAME ${PROJECT_NAME})
166 set(CPACK_PACKAGE_CONTACT "Max Khizhinsky <libcds-user@lists.sourceforge.net>")
167 set(CPACK_PACKAGE_RELEASE 1)
168 set(CPACK_PACKAGE_INSTALL_DIRECTORY "cds")
169 set(CPACK_PACKAGE_DESCRIPTION_FILE "${PROJECT_SOURCE_DIR}/build/cmake/description.txt")
170 set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Library of concurrent data structures")
171 set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_PACKAGE_RELEASE}")
172 set(DEPLOY_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}")
173
174 # TGZ specific
175 set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)
176
177 # RPM specific
178 set(CPACK_RPM_COMPONENT_INSTALL ON)
179 set(CPACK_RPM_PACKAGE_RELEASE ${CPACK_PACKAGE_RELEASE})
180 set(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${PROJECT_SOURCE_DIR}/build/cmake/post_install_script.sh")
181 set(CPACK_RPM_POST_UNINSTALL_SCRIPT_FILE "${PROJECT_SOURCE_DIR}/build/cmake/post_uninstall_script.sh")
182 set(CPACK_RPM_PACKAGE_URL https://github.com/khizmax/libcds)
183 set(CPACK_RPM_PACKAGE_LICENSE GPL)
184 set(CPACK_RPM_PACKAGE_GROUP "System Environment/Base")
185 set(CPACK_RPM_PACKAGE_REQUIRES "boost >= 1.50")
186 set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION ${CPACK_PACKAGING_INSTALL_PREFIX})
187 set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION /usr/local)
188 set(CPACK_RPM_devel_PACKAGE_REQUIRES "boost >= 1.50, cds-lib = ${PROJECT_VERSION}")
189
190 # DEB specific
191 set(CPACK_DEB_COMPONENT_INSTALL ON)
192 set(CPACK_DEBIAN_PACKAGE_DEPENDS "boost (>= 1.50)")
193 set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://github.com/khizmax/libcds")
194 set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${PROJECT_SOURCE_DIR}/build/cmake/post_install_script.sh;;${PROJECT_SOURCE_DIR}/build/cmake/post_uninstall_script.sh;")
195
196 # NSYS specific
197 set(CPACK_NSIS_PACKAGE_NAME "${CPACK_PACKAGE_NAME}")
198 set(CPACK_NSIS_DISPLAY_NAME "${CPACK_PACKAGE_NAME}")
199 set(CPACK_NSIS_CONTACT ${CPACK_PACKAGE_CONTACT})
200 set(CPACK_NSIS_ENABLE_UNINSTALL_BEFORE_INSTALL ON)
201 set(CPACK_NSIS_MODIFY_PATH ON)
202
203 # Components grouping for Mac OS X and Windows installers
204 set(CPACK_COMPONENT_${LIBRARIES_COMPONENT}_GROUP "Runtime")
205 set(CPACK_COMPONENT_${HEADERS_COMPONENT}_GROUP "Development")
206 set(CPACK_COMPONENT_${LIBRARIES_COMPONENT}_DISPLAY_NAME "Libraries")
207 set(CPACK_COMPONENT_${HEADERS_COMPONENT}_DISPLAY_NAME "C++ Headers")
208 set(CPACK_COMPONENT_${HEADERS_COMPONENT}_DEPENDS ${LIBRARIES_COMPONENT})
209 set(CPACK_COMPONENT_GROUP_DEVELOPMENT_DESCRIPTION "All of the tools you'll ever need to develop lock-free oriented software with libcds")
210 set(CPACK_COMPONENT_GROUP_RUNTIME_DESCRIPTION "Only libcds library for runtime")
211
212 include(CPack)