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