xref: /6.0.3/platform/CMakeLists.txt (revision 72cbf62f)
1CMAKE_MINIMUM_REQUIRED(VERSION 3.2)
2PROJECT(Platform LANGUAGES C CXX)
3
4INCLUDE(CheckFunctionExists)
5INCLUDE(CheckIncludeFileCXX)
6INCLUDE(CheckSymbolExists)
7INCLUDE(CTest)
8INCLUDE(GenerateExportHeader)
9INCLUDE(check_unit_test_enabled)
10
11check_unit_test_enabled(platform COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
12if (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
13    ENABLE_CODE_COVERAGE_REPORT()
14endif (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
15
16CMAKE_PUSH_CHECK_STATE(RESET)
17  FIND_LIBRARY(EXECINFO_LIBRARY NAMES execinfo)
18  IF (EXECINFO_LIBRARY)
19    SET(CMAKE_REQUIRED_LIBRARIES "${EXECINFO_LIBRARY}")
20    LIST(APPEND PLATFORM_LIBRARIES "${EXECINFO_LIBRARY}")
21  ENDIF(EXECINFO_LIBRARY)
22  CHECK_SYMBOL_EXISTS(backtrace execinfo.h HAVE_BACKTRACE)
23CMAKE_POP_CHECK_STATE()
24
25CMAKE_PUSH_CHECK_STATE(RESET)
26  SET(CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
27  FIND_LIBRARY(DL_LIBRARY NAMES dl)
28  IF (DL_LIBRARY)
29    SET(CMAKE_REQUIRED_LIBRARIES "${DL_LIBRARY}")
30    LIST(APPEND PLATFORM_LIBRARIES "${DL_LIBRARY}")
31  ENDIF (DL_LIBRARY)
32  CHECK_SYMBOL_EXISTS(dladdr dlfcn.h HAVE_DLADDR)
33CMAKE_POP_CHECK_STATE()
34
35CMAKE_PUSH_CHECK_STATE(RESET)
36SET(CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
37SET(CMAKE_REQUIRED_LIBRARIES "pthread")
38CHECK_SYMBOL_EXISTS(pthread_setname_np pthread.h HAVE_PTHREAD_SETNAME_NP)
39CHECK_SYMBOL_EXISTS(pthread_getname_np pthread.h HAVE_PTHREAD_GETNAME_NP)
40CHECK_SYMBOL_EXISTS(sched_getaffinity sched.h HAVE_SCHED_GETAFFINITY)
41CHECK_SYMBOL_EXISTS(sched_getcpu sched.h HAVE_SCHED_GETCPU)
42CMAKE_POP_CHECK_STATE()
43
44CMAKE_PUSH_CHECK_STATE(RESET)
45
46CHECK_CXX_SOURCE_COMPILES("
47    #include <memory>
48    int main() {
49        return *std::make_unique<int>(0);
50    }" HAVE_MAKE_UNIQUE)
51CMAKE_POP_CHECK_STATE()
52
53CHECK_SYMBOL_EXISTS(gethrtime sys/time.h CB_DONT_NEED_GETHRTIME)
54CHECK_SYMBOL_EXISTS(htonll arpa/inet.h CB_DONT_NEED_BYTEORDER)
55CHECK_INCLUDE_FILE(cpuid.h HAVE_CPUID_H)
56CONFIGURE_FILE (${CMAKE_CURRENT_SOURCE_DIR}/src/config.cmake.h
57                ${CMAKE_CURRENT_BINARY_DIR}/src/config.h)
58
59CONFIGURE_FILE (${CMAKE_CURRENT_SOURCE_DIR}/include/platform/dynamic.in.h
60                ${CMAKE_CURRENT_BINARY_DIR}/include/platform/dynamic.h)
61
62#
63# Set the include path
64#
65INCLUDE_DIRECTORIES(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/external)
66
67IF (MEMORY_ALLOCATOR)
68  INCLUDE_DIRECTORIES(AFTER ${MALLOC_INCLUDE_DIR})
69ENDIF (MEMORY_ALLOCATOR)
70
71ADD_LIBRARY(cJSON SHARED
72            include/cJSON.h
73            include/cJSON_utils.h
74            src/cJSON.cc
75            src/cJSON_utils.cc)
76SET_TARGET_PROPERTIES(cJSON PROPERTIES SOVERSION 1.1.0)
77GENERATE_EXPORT_HEADER(cJSON
78                       EXPORT_MACRO_NAME CJSON_PUBLIC_API
79                       EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/include/platform/cJSON-visibility.h)
80TARGET_INCLUDE_DIRECTORIES(cJSON INTERFACE include)
81TARGET_LINK_LIBRARIES(cJSON PRIVATE gsl_lite)
82
83ADD_LIBRARY(JSON_checker SHARED src/JSON_checker.cc include/JSON_checker.h)
84SET_TARGET_PROPERTIES(JSON_checker PROPERTIES SOVERSION 1.0.0)
85TARGET_LINK_LIBRARIES(JSON_checker PRIVATE platform_headers)
86GENERATE_EXPORT_HEADER(JSON_checker
87                       EXPORT_MACRO_NAME JSON_CHECKER_PUBLIC_API
88                       EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/include/platform/JSON_checker-visibility.h)
89
90IF (WIN32)
91   INCLUDE_DIRECTORIES(AFTER ${CMAKE_CURRENT_SOURCE_DIR}/include/win32)
92   ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
93   SET(PLATFORM_FILES src/getopt.c
94                      src/sockets.c
95                      src/cb_win32.cc
96                      src/winrandom.c
97                      src/memorymap_win32.cc
98                      include/win32/getopt.h
99                      include/win32/strings.h
100                      include/win32/unistd.h)
101
102   # Allow to build on unsupported platorms (currently with MSVC 2017CE)
103   # and the FindCouchbaseDbgHelp macro didn't pick it up in the new location
104   # stuff has been moved around for the CE version. The library is only used
105   # for backtrace support which we can live without
106   CB_GET_SUPPORTED_PLATFORM(_supported_platform)
107   if (_supported_platform)
108       ADD_DEFINITIONS(-DHAVE_BACKTRACE_SUPPORT=1)
109       INCLUDE(FindCouchbaseDbgHelp)
110       LIST(APPEND PLATFORM_LIBRARIES "${DBGHELP_LIBRARY}")
111       INSTALL(FILES ${DBGHELP_DLL} DESTINATION bin)
112   endif ()
113ELSE (WIN32)
114   SET(PLATFORM_FILES src/cb_pthreads.cc src/urandom.c src/memorymap_posix.cc)
115   SET_SOURCE_FILES_PROPERTIES(src/crc32c_sse4_2.cc PROPERTIES COMPILE_FLAGS -msse4.2)
116   LIST(APPEND PLATFORM_LIBRARIES "pthread")
117
118   IF (NOT APPLE)
119      LIST(APPEND PLATFORM_LIBRARIES "rt")
120   ENDIF(NOT APPLE)
121
122   SET(LIB_M m)
123ENDIF (WIN32)
124
125TARGET_LINK_LIBRARIES(cJSON PRIVATE platform ${LIB_M})
126
127IF (NOT DEFINED COUCHBASE_NETWORK_LIBS)
128   IF (WIN32)
129      SET(COUCHBASE_NETWORK_LIBS "Ws2_32")
130   ELSEIF ("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")
131      SET(COUCHBASE_NETWORK_LIBS socket nsl)
132   ENDIF (WIN32)
133ENDIF (NOT DEFINED COUCHBASE_NETWORK_LIBS)
134
135IF ("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
136    # The implementation you get with chrono on a default FreeBSD 10
137    # is dead slow and considered useless...
138    SET(HAVE_DEAD_SLOW_CHRONO true)
139ENDIF ("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
140
141IF (CB_DONT_NEED_GETHRTIME OR HAVE_DEAD_SLOW_CHRONO)
142   SET(PLATFORM_FILES ${PLATFORM_FILES} src/gethrtime.c)
143ELSE (CB_DONT_NEED_GETHRTIME OR HAVE_DEAD_SLOW_CHRONO)
144   SET(PLATFORM_FILES ${PLATFORM_FILES} src/cxxtime.cc)
145ENDIF (CB_DONT_NEED_GETHRTIME OR HAVE_DEAD_SLOW_CHRONO)
146
147CHECK_FUNCTION_EXISTS(malloc_usable_size HAVE_MALLOC_USABLE_SIZE)
148if(HAVE_MALLOC_USABLE_SIZE)
149  ADD_DEFINITIONS(-DHAVE_MALLOC_USABLE_SIZE)
150endif()
151#
152# Add all of the libraries
153#
154
155# 'platform_headers' - header-only part of platform.
156#
157# This should typically only be used for tests / benchmarks which want the
158# definitions of platform functions; but don't want to link against the
159# complete library. For example, a unit test might want to just link against
160# the single .cc file for the code it is testing, but needs the prototype(s)
161# of that .cc file.
162ADD_LIBRARY(platform_headers INTERFACE)
163TARGET_INCLUDE_DIRECTORIES(platform_headers INTERFACE
164        include
165        # "config.h" (which resides in ${Platform_BINARY_DIR}/src) is needed
166        # indirectly by any targets linking against platform_headers (because
167        # a number of platform's public headers include it).
168        ${Platform_BINARY_DIR}/src
169        ${Platform_BINARY_DIR}/include)
170
171# platform_so - the 'real' platform shared library containing our various
172# platform abstraction functionality.
173# Note that in general external users should link 'platform' and not
174# 'platform_so' - see below.
175ADD_LIBRARY(platform_so SHARED ${PLATFORM_FILES}
176                            ${CMAKE_CURRENT_BINARY_DIR}/src/config.h
177                            src/base64.cc
178                            src/getpid.c
179                            src/random.cc
180                            src/backtrace.c
181                            src/byte_buffer_dump.cc
182                            src/byteorder.c
183                            src/cb_malloc.cc
184                            src/cb_mktemp.cc
185                            src/cb_time.cc
186                            src/cbassert.c
187                            src/checked_snprintf.cc
188                            src/crc32c.cc
189                            src/crc32c_sse4_2.cc
190                            src/crc32c_private.h
191                            src/global_new_replacement.cc
192                            src/histogram.cc
193                            src/interrupt.cc
194                            src/processclock.cc
195                            src/strerror.cc
196                            src/string.cc
197                            src/strnstr.cc
198                            src/sysinfo.cc
199                            src/thread.cc
200                            src/timeutils.cc
201                            src/uuid.cc
202                            include/platform/atomic_duration.h
203                            include/platform/backtrace.h
204                            include/platform/base64.h
205                            include/platform/bitset.h
206                            include/platform/byte_buffer_dump.h
207                            include/platform/cacheline_padded.h
208                            include/platform/cb_malloc.h
209                            include/platform/cbassert.h
210                            include/platform/checked_snprintf.h
211                            include/platform/corestore.h
212                            include/platform/crc32c.h
213                            include/platform/interrupt.h
214                            include/platform/make_unique.h
215                            include/platform/memorymap.h
216                            include/platform/non_negative_counter.h
217                            include/platform/platform.h
218                            include/platform/pipe.h
219                            include/platform/processclock.h
220                            include/platform/random.h
221                            include/platform/ring_buffer.h
222                            include/platform/rwlock.h
223                            include/platform/sized_buffer.h
224                            include/platform/strerror.h
225                            include/platform/string.h
226                            include/platform/sysinfo.h
227                            include/platform/thread.h
228                            include/platform/timeutils.h
229                            include/platform/uuid.h
230                            ${Platform_BINARY_DIR}/include/platform/visibility.h)
231GENERATE_EXPORT_HEADER(platform_so
232                       EXPORT_MACRO_NAME PLATFORM_PUBLIC_API
233                       EXPORT_FILE_NAME ${Platform_BINARY_DIR}/include/platform/visibility.h)
234
235LIST(APPEND PLATFORM_LIBRARIES "phosphor")
236LIST(REMOVE_DUPLICATES PLATFORM_LIBRARIES)
237TARGET_LINK_LIBRARIES(platform_so PRIVATE
238        platform_headers
239        gsl_lite
240        ${MALLOC_LIBRARIES}
241        ${PLATFORM_LIBRARIES})
242
243# Link publically with COUCHBASE_NETWORK_LIBS - strictly speaking this
244# shouldn't be necessary, however a number of downstream components
245# (e.g. couchstore targets) rely on platform to make available
246# htons/htonl (via Ws2_32) etc when building on Windows.
247# Until all such targets are # updated to link directly to Ws2_32, we
248# link publically.
249TARGET_LINK_LIBRARIES(platform_so PUBLIC
250        ${COUCHBASE_NETWORK_LIBS})
251
252TARGET_INCLUDE_DIRECTORIES(platform_so PUBLIC include)
253# TODO: Add a TARGET_INCLUDE_DIRECTORIES(PUBLIC) to phosphor; then the
254# following line can be removed:
255TARGET_INCLUDE_DIRECTORIES(platform_so PRIVATE ${phosphor_SOURCE_DIR}/include)
256
257# 'platform' - the library clients should normally link to.
258#
259# 'platform' is a static library, which itself links to the main platform_so
260# shared library. This is to ensure that any clients of platform always link
261# in the C++ global operator new/delete replacements if they use platform at
262# all. If they didn't then this would cause problems:
263# 1) Memory usage from the client will not be tracked.
264# 2) If a C++ object allocated in platform (using cbmalloc via new) is
265#    delete'd in the client (using default free) then we have a mismatched
266#    new/delete pair.
267# By giving all clients of platform the global new/delete automatically we
268# avoid these problems.
269ADD_LIBRARY(platform STATIC src/global_new_replacement.cc)
270SET_TARGET_PROPERTIES(platform PROPERTIES POSITION_INDEPENDENT_CODE true)
271TARGET_LINK_LIBRARIES(platform platform_headers platform_so)
272
273# On OS X 10.11 we need to remove the default rpath settings in order to all
274# us to load the platform library from Python scripts that are run out of
275# /usr/bin/python. We can remove this once we ship our own Python.
276IF (APPLE)
277    SET_TARGET_PROPERTIES(platform_so
278                          PROPERTIES
279                          INSTALL_RPATH "")
280ENDIF ()
281
282SET_TARGET_PROPERTIES(platform_so PROPERTIES SOVERSION 0.1.0)
283
284# TODO: Given dirutils now links with platform (for cbmalloc &
285# global_new_replacement) we can probably just merge it into platform.
286ADD_LIBRARY(dirutils SHARED
287            src/dirutils.cc
288            include/platform/dirutils.h)
289SET_TARGET_PROPERTIES(dirutils PROPERTIES SOVERSION 0.1.0)
290TARGET_LINK_LIBRARIES(dirutils platform)
291GENERATE_EXPORT_HEADER(dirutils
292                       EXPORT_MACRO_NAME DIRUTILS_PUBLIC_API
293                       EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/include/platform/dirutils-visibility.h)
294
295IF (BREAKPAD_FOUND)
296   ADD_DEFINITIONS(-DHAVE_BREAKPAD)
297   INCLUDE_DIRECTORIES(AFTER ${BREAKPAD_INCLUDE_DIR})
298   # Breakpad has headers (which are dependencies for our breakpad integration
299   # code) that don't compile cleanly as C++11, so the GNU standard is
300   # used instead.
301   IF (UNIX)
302      set_source_files_properties(src/breakpad_wrapper.cc PROPERTIES COMPILE_FLAGS ${CB_GNU_CXX11_OPTION})
303   ENDIF (UNIX)
304ENDIF (BREAKPAD_FOUND)
305
306# To simplify usage we always build breakpad_wrapper; it'll just
307# do nothing on targets which don't have Breakpad.
308ADD_LIBRARY(breakpad_wrapper SHARED
309            src/breakpad_wrapper.cc
310            include/breakpad_wrapper/breakpad_wrapper.h)
311SET_TARGET_PROPERTIES(breakpad_wrapper PROPERTIES COMPILE_FLAGS "-Dplatform_so_EXPORTS")
312TARGET_LINK_LIBRARIES(breakpad_wrapper
313                      ${BREAKPAD_LIBRARIES} platform_headers)
314
315INSTALL(TARGETS breakpad_wrapper cJSON JSON_checker platform_so dirutils
316        RUNTIME DESTINATION bin
317        LIBRARY DESTINATION lib
318        ARCHIVE DESTINATION lib)
319
320ADD_SUBDIRECTORY(cbcompress)
321ADD_SUBDIRECTORY(cbsocket)
322
323if (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
324    ADD_SUBDIRECTORY(tests)
325endif (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
326