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