xref: /5.5.2/platform/CMakeLists.txt (revision 5c6a39bc)
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/processclock.cc
194                            src/strerror.cc
195                            src/string.cc
196                            src/strnstr.cc
197                            src/sysinfo.cc
198                            src/thread.cc
199                            src/timeutils.cc
200                            src/uuid.cc
201                            include/platform/atomic_duration.h
202                            include/platform/backtrace.h
203                            include/platform/base64.h
204                            include/platform/bitset.h
205                            include/platform/byte_buffer_dump.h
206                            include/platform/cacheline_padded.h
207                            include/platform/cb_malloc.h
208                            include/platform/cbassert.h
209                            include/platform/checked_snprintf.h
210                            include/platform/corestore.h
211                            include/platform/crc32c.h
212                            include/platform/make_unique.h
213                            include/platform/memorymap.h
214                            include/platform/non_negative_counter.h
215                            include/platform/platform.h
216                            include/platform/pipe.h
217                            include/platform/processclock.h
218                            include/platform/random.h
219                            include/platform/ring_buffer.h
220                            include/platform/rwlock.h
221                            include/platform/sized_buffer.h
222                            include/platform/strerror.h
223                            include/platform/string.h
224                            include/platform/sysinfo.h
225                            include/platform/thread.h
226                            include/platform/timeutils.h
227                            include/platform/uuid.h
228                            ${Platform_BINARY_DIR}/include/platform/visibility.h)
229GENERATE_EXPORT_HEADER(platform_so
230                       EXPORT_MACRO_NAME PLATFORM_PUBLIC_API
231                       EXPORT_FILE_NAME ${Platform_BINARY_DIR}/include/platform/visibility.h)
232
233LIST(APPEND PLATFORM_LIBRARIES "phosphor")
234LIST(REMOVE_DUPLICATES PLATFORM_LIBRARIES)
235TARGET_LINK_LIBRARIES(platform_so PRIVATE
236        platform_headers
237        gsl_lite
238        ${MALLOC_LIBRARIES}
239        ${PLATFORM_LIBRARIES})
240
241# Link publically with COUCHBASE_NETWORK_LIBS - strictly speaking this
242# shouldn't be necessary, however a number of downstream components
243# (e.g. couchstore targets) rely on platform to make available
244# htons/htonl (via Ws2_32) etc when building on Windows.
245# Until all such targets are # updated to link directly to Ws2_32, we
246# link publically.
247TARGET_LINK_LIBRARIES(platform_so PUBLIC
248        ${COUCHBASE_NETWORK_LIBS})
249
250TARGET_INCLUDE_DIRECTORIES(platform_so PUBLIC include)
251# TODO: Add a TARGET_INCLUDE_DIRECTORIES(PUBLIC) to phosphor; then the
252# following line can be removed:
253TARGET_INCLUDE_DIRECTORIES(platform_so PRIVATE ${phosphor_SOURCE_DIR}/include)
254
255# 'platform' - the library clients should normally link to.
256#
257# 'platform' is a static library, which itself links to the main platform_so
258# shared library. This is to ensure that any clients of platform always link
259# in the C++ global operator new/delete replacements if they use platform at
260# all. If they didn't then this would cause problems:
261# 1) Memory usage from the client will not be tracked.
262# 2) If a C++ object allocated in platform (using cbmalloc via new) is
263#    delete'd in the client (using default free) then we have a mismatched
264#    new/delete pair.
265# By giving all clients of platform the global new/delete automatically we
266# avoid these problems.
267ADD_LIBRARY(platform STATIC src/global_new_replacement.cc)
268SET_TARGET_PROPERTIES(platform PROPERTIES POSITION_INDEPENDENT_CODE true)
269TARGET_LINK_LIBRARIES(platform platform_headers platform_so)
270
271# On OS X 10.11 we need to remove the default rpath settings in order to all
272# us to load the platform library from Python scripts that are run out of
273# /usr/bin/python. We can remove this once we ship our own Python.
274IF (APPLE)
275    SET_TARGET_PROPERTIES(platform_so
276                          PROPERTIES
277                          INSTALL_RPATH "")
278ENDIF ()
279
280SET_TARGET_PROPERTIES(platform_so PROPERTIES SOVERSION 0.1.0)
281
282# TODO: Given dirutils now links with platform (for cbmalloc &
283# global_new_replacement) we can probably just merge it into platform.
284ADD_LIBRARY(dirutils SHARED
285            src/dirutils.cc
286            include/platform/dirutils.h)
287SET_TARGET_PROPERTIES(dirutils PROPERTIES SOVERSION 0.1.0)
288TARGET_LINK_LIBRARIES(dirutils platform)
289GENERATE_EXPORT_HEADER(dirutils
290                       EXPORT_MACRO_NAME DIRUTILS_PUBLIC_API
291                       EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/include/platform/dirutils-visibility.h)
292
293IF (BREAKPAD_FOUND)
294   ADD_DEFINITIONS(-DHAVE_BREAKPAD)
295   INCLUDE_DIRECTORIES(AFTER ${BREAKPAD_INCLUDE_DIR})
296   # Breakpad has headers (which are dependencies for our breakpad integration
297   # code) that don't compile cleanly as C++11, so the GNU standard is
298   # used instead.
299   IF (UNIX)
300      set_source_files_properties(src/breakpad_wrapper.cc PROPERTIES COMPILE_FLAGS ${CB_GNU_CXX11_OPTION})
301   ENDIF (UNIX)
302ENDIF (BREAKPAD_FOUND)
303
304# To simplify usage we always build breakpad_wrapper; it'll just
305# do nothing on targets which don't have Breakpad.
306ADD_LIBRARY(breakpad_wrapper SHARED
307            src/breakpad_wrapper.cc
308            include/breakpad_wrapper/breakpad_wrapper.h)
309SET_TARGET_PROPERTIES(breakpad_wrapper PROPERTIES COMPILE_FLAGS "-Dplatform_so_EXPORTS")
310TARGET_LINK_LIBRARIES(breakpad_wrapper
311                      ${BREAKPAD_LIBRARIES} platform_headers)
312
313INSTALL(TARGETS breakpad_wrapper cJSON JSON_checker platform_so dirutils
314        RUNTIME DESTINATION bin
315        LIBRARY DESTINATION lib
316        ARCHIVE DESTINATION lib)
317
318ADD_SUBDIRECTORY(cbcompress)
319ADD_SUBDIRECTORY(cbsocket)
320
321if (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
322    ADD_SUBDIRECTORY(tests)
323endif (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
324