xref: /6.0.3/platform/CMakeLists.txt (revision cc62f3a2)
1CMAKE_MINIMUM_REQUIRED(VERSION 3.2)
2PROJECT(Platform LANGUAGES C CXX)
3
4INCLUDE(CheckFunctionExists)
5INCLUDE(CheckIncludeFile)
6INCLUDE(CheckIncludeFileCXX)
7INCLUDE(CheckSymbolExists)
8INCLUDE(CTest)
9INCLUDE(GenerateExportHeader)
10INCLUDE(check_unit_test_enabled)
11
12check_unit_test_enabled(platform COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
13if (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
14    ENABLE_CODE_COVERAGE_REPORT()
15endif (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
16
17CMAKE_PUSH_CHECK_STATE(RESET)
18  FIND_LIBRARY(EXECINFO_LIBRARY NAMES execinfo)
19  IF (EXECINFO_LIBRARY)
20    SET(CMAKE_REQUIRED_LIBRARIES "${EXECINFO_LIBRARY}")
21    LIST(APPEND PLATFORM_LIBRARIES "${EXECINFO_LIBRARY}")
22  ENDIF(EXECINFO_LIBRARY)
23  CHECK_SYMBOL_EXISTS(backtrace execinfo.h HAVE_BACKTRACE)
24CMAKE_POP_CHECK_STATE()
25
26CMAKE_PUSH_CHECK_STATE(RESET)
27  SET(CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
28  FIND_LIBRARY(DL_LIBRARY NAMES dl)
29  IF (DL_LIBRARY)
30    SET(CMAKE_REQUIRED_LIBRARIES "${DL_LIBRARY}")
31    LIST(APPEND PLATFORM_LIBRARIES "${DL_LIBRARY}")
32  ENDIF (DL_LIBRARY)
33  CHECK_SYMBOL_EXISTS(dladdr dlfcn.h HAVE_DLADDR)
34CMAKE_POP_CHECK_STATE()
35
36CMAKE_PUSH_CHECK_STATE(RESET)
37SET(CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
38SET(CMAKE_REQUIRED_LIBRARIES "pthread")
39CHECK_SYMBOL_EXISTS(pthread_setname_np pthread.h HAVE_PTHREAD_SETNAME_NP)
40CHECK_SYMBOL_EXISTS(pthread_getname_np pthread.h HAVE_PTHREAD_GETNAME_NP)
41CHECK_SYMBOL_EXISTS(sched_getaffinity sched.h HAVE_SCHED_GETAFFINITY)
42CHECK_SYMBOL_EXISTS(sched_getcpu sched.h HAVE_SCHED_GETCPU)
43CMAKE_POP_CHECK_STATE()
44
45CMAKE_PUSH_CHECK_STATE(RESET)
46
47CHECK_CXX_SOURCE_COMPILES("
48    #include <memory>
49    int main() {
50        return *std::make_unique<int>(0);
51    }" HAVE_MAKE_UNIQUE)
52CMAKE_POP_CHECK_STATE()
53
54CHECK_SYMBOL_EXISTS(gethrtime sys/time.h CB_DONT_NEED_GETHRTIME)
55CHECK_SYMBOL_EXISTS(htonll arpa/inet.h CB_DONT_NEED_BYTEORDER)
56CHECK_INCLUDE_FILE(cpuid.h HAVE_CPUID_H)
57CONFIGURE_FILE (${CMAKE_CURRENT_SOURCE_DIR}/src/config.cmake.h
58                ${CMAKE_CURRENT_BINARY_DIR}/src/config.h)
59
60CONFIGURE_FILE (${CMAKE_CURRENT_SOURCE_DIR}/include/platform/dynamic.in.h
61                ${CMAKE_CURRENT_BINARY_DIR}/include/platform/dynamic.h)
62
63#
64# Set the include path
65#
66INCLUDE_DIRECTORIES(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/external)
67
68IF (MEMORY_ALLOCATOR)
69  INCLUDE_DIRECTORIES(AFTER ${MALLOC_INCLUDE_DIR})
70ENDIF (MEMORY_ALLOCATOR)
71
72ADD_LIBRARY(cJSON SHARED
73            include/cJSON.h
74            include/cJSON_utils.h
75            src/cJSON.cc
76            src/cJSON_utils.cc)
77SET_TARGET_PROPERTIES(cJSON PROPERTIES SOVERSION 1.1.0)
78GENERATE_EXPORT_HEADER(cJSON
79                       EXPORT_MACRO_NAME CJSON_PUBLIC_API
80                       EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/include/platform/cJSON-visibility.h)
81TARGET_INCLUDE_DIRECTORIES(cJSON INTERFACE include)
82TARGET_LINK_LIBRARIES(cJSON PRIVATE gsl_lite)
83
84ADD_LIBRARY(JSON_checker SHARED src/JSON_checker.cc include/JSON_checker.h)
85SET_TARGET_PROPERTIES(JSON_checker PROPERTIES SOVERSION 1.0.0)
86TARGET_LINK_LIBRARIES(JSON_checker PRIVATE platform_headers)
87GENERATE_EXPORT_HEADER(JSON_checker
88                       EXPORT_MACRO_NAME JSON_CHECKER_PUBLIC_API
89                       EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/include/platform/JSON_checker-visibility.h)
90
91IF (WIN32)
92   INCLUDE_DIRECTORIES(AFTER ${CMAKE_CURRENT_SOURCE_DIR}/include/win32)
93   ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
94   SET(PLATFORM_FILES 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/getopt.cc
192                            src/global_new_replacement.cc
193                            src/histogram.cc
194                            src/interrupt.cc
195                            src/processclock.cc
196                            src/strerror.cc
197                            src/string.cc
198                            src/strnstr.cc
199                            src/sysinfo.cc
200                            src/thread.cc
201                            src/timeutils.cc
202                            src/uuid.cc
203                            include/platform/atomic_duration.h
204                            include/platform/backtrace.h
205                            include/platform/base64.h
206                            include/platform/bitset.h
207                            include/platform/byte_buffer_dump.h
208                            include/platform/cacheline_padded.h
209                            include/platform/cb_malloc.h
210                            include/platform/cbassert.h
211                            include/platform/checked_snprintf.h
212                            include/platform/corestore.h
213                            include/platform/crc32c.h
214                            include/platform/getopt.h
215                            include/platform/interrupt.h
216                            include/platform/make_unique.h
217                            include/platform/memorymap.h
218                            include/platform/non_negative_counter.h
219                            include/platform/platform.h
220                            include/platform/pipe.h
221                            include/platform/processclock.h
222                            include/platform/random.h
223                            include/platform/ring_buffer.h
224                            include/platform/rwlock.h
225                            include/platform/sized_buffer.h
226                            include/platform/strerror.h
227                            include/platform/string.h
228                            include/platform/sysinfo.h
229                            include/platform/thread.h
230                            include/platform/timeutils.h
231                            include/platform/uuid.h
232                            ${Platform_BINARY_DIR}/include/platform/visibility.h)
233GENERATE_EXPORT_HEADER(platform_so
234                       EXPORT_MACRO_NAME PLATFORM_PUBLIC_API
235                       EXPORT_FILE_NAME ${Platform_BINARY_DIR}/include/platform/visibility.h)
236
237LIST(APPEND PLATFORM_LIBRARIES "phosphor")
238LIST(REMOVE_DUPLICATES PLATFORM_LIBRARIES)
239TARGET_LINK_LIBRARIES(platform_so PRIVATE
240        platform_headers
241        gsl_lite
242        ${MALLOC_LIBRARIES}
243        ${PLATFORM_LIBRARIES})
244
245# Link publically with COUCHBASE_NETWORK_LIBS - strictly speaking this
246# shouldn't be necessary, however a number of downstream components
247# (e.g. couchstore targets) rely on platform to make available
248# htons/htonl (via Ws2_32) etc when building on Windows.
249# Until all such targets are # updated to link directly to Ws2_32, we
250# link publically.
251TARGET_LINK_LIBRARIES(platform_so PUBLIC
252        ${COUCHBASE_NETWORK_LIBS})
253
254TARGET_INCLUDE_DIRECTORIES(platform_so PUBLIC include)
255# TODO: Add a TARGET_INCLUDE_DIRECTORIES(PUBLIC) to phosphor; then the
256# following line can be removed:
257TARGET_INCLUDE_DIRECTORIES(platform_so PRIVATE ${phosphor_SOURCE_DIR}/include)
258
259# 'platform' - the library clients should normally link to.
260#
261# 'platform' is a static library, which itself links to the main platform_so
262# shared library. This is to ensure that any clients of platform always link
263# in the C++ global operator new/delete replacements if they use platform at
264# all. If they didn't then this would cause problems:
265# 1) Memory usage from the client will not be tracked.
266# 2) If a C++ object allocated in platform (using cbmalloc via new) is
267#    delete'd in the client (using default free) then we have a mismatched
268#    new/delete pair.
269# By giving all clients of platform the global new/delete automatically we
270# avoid these problems.
271ADD_LIBRARY(platform STATIC src/global_new_replacement.cc)
272SET_TARGET_PROPERTIES(platform PROPERTIES POSITION_INDEPENDENT_CODE true)
273TARGET_LINK_LIBRARIES(platform platform_headers platform_so)
274
275# On OS X 10.11 we need to remove the default rpath settings in order to all
276# us to load the platform library from Python scripts that are run out of
277# /usr/bin/python. We can remove this once we ship our own Python.
278IF (APPLE)
279    SET_TARGET_PROPERTIES(platform_so
280                          PROPERTIES
281                          INSTALL_RPATH "")
282ENDIF ()
283
284SET_TARGET_PROPERTIES(platform_so PROPERTIES SOVERSION 0.1.0)
285
286# TODO: Given dirutils now links with platform (for cbmalloc &
287# global_new_replacement) we can probably just merge it into platform.
288ADD_LIBRARY(dirutils SHARED
289            src/dirutils.cc
290            include/platform/dirutils.h)
291SET_TARGET_PROPERTIES(dirutils PROPERTIES SOVERSION 0.1.0)
292TARGET_LINK_LIBRARIES(dirutils platform)
293GENERATE_EXPORT_HEADER(dirutils
294                       EXPORT_MACRO_NAME DIRUTILS_PUBLIC_API
295                       EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/include/platform/dirutils-visibility.h)
296
297IF (BREAKPAD_FOUND)
298   ADD_DEFINITIONS(-DHAVE_BREAKPAD)
299   INCLUDE_DIRECTORIES(AFTER ${BREAKPAD_INCLUDE_DIR})
300   # Breakpad has headers (which are dependencies for our breakpad integration
301   # code) that don't compile cleanly as C++11, so the GNU standard is
302   # used instead.
303   IF (UNIX)
304      set_source_files_properties(src/breakpad_wrapper.cc PROPERTIES COMPILE_FLAGS ${CB_GNU_CXX11_OPTION})
305   ENDIF (UNIX)
306ENDIF (BREAKPAD_FOUND)
307
308# To simplify usage we always build breakpad_wrapper; it'll just
309# do nothing on targets which don't have Breakpad.
310ADD_LIBRARY(breakpad_wrapper SHARED
311            src/breakpad_wrapper.cc
312            include/breakpad_wrapper/breakpad_wrapper.h)
313SET_TARGET_PROPERTIES(breakpad_wrapper PROPERTIES COMPILE_FLAGS "-Dplatform_so_EXPORTS")
314TARGET_LINK_LIBRARIES(breakpad_wrapper
315                      ${BREAKPAD_LIBRARIES} platform_headers)
316
317INSTALL(TARGETS breakpad_wrapper cJSON JSON_checker platform_so dirutils
318        RUNTIME DESTINATION bin
319        LIBRARY DESTINATION lib
320        ARCHIVE DESTINATION lib)
321
322ADD_SUBDIRECTORY(cbcompress)
323ADD_SUBDIRECTORY(cbsocket)
324
325if (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
326    ADD_SUBDIRECTORY(tests)
327endif (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
328