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