xref: /5.5.2/platform/CMakeLists.txt (revision e40463a5)
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/getopt.c
95                      src/sockets.c
96                      src/cb_win32.cc
97                      src/winrandom.c
98                      src/memorymap_win32.cc
99                      include/win32/getopt.h
100                      include/win32/strings.h
101                      include/win32/unistd.h)
102
103   # Allow to build on unsupported platorms (currently with MSVC 2017CE)
104   # and the FindCouchbaseDbgHelp macro didn't pick it up in the new location
105   # stuff has been moved around for the CE version. The library is only used
106   # for backtrace support which we can live without
107   CB_GET_SUPPORTED_PLATFORM(_supported_platform)
108   if (_supported_platform)
109       ADD_DEFINITIONS(-DHAVE_BACKTRACE_SUPPORT=1)
110       INCLUDE(FindCouchbaseDbgHelp)
111       LIST(APPEND PLATFORM_LIBRARIES "${DBGHELP_LIBRARY}")
112       INSTALL(FILES ${DBGHELP_DLL} DESTINATION bin)
113   endif ()
114ELSE (WIN32)
115   SET(PLATFORM_FILES src/cb_pthreads.cc src/urandom.c src/memorymap_posix.cc)
116   SET_SOURCE_FILES_PROPERTIES(src/crc32c_sse4_2.cc PROPERTIES COMPILE_FLAGS -msse4.2)
117   LIST(APPEND PLATFORM_LIBRARIES "pthread")
118
119   IF (NOT APPLE)
120      LIST(APPEND PLATFORM_LIBRARIES "rt")
121   ENDIF(NOT APPLE)
122
123   SET(LIB_M m)
124ENDIF (WIN32)
125
126TARGET_LINK_LIBRARIES(cJSON PRIVATE platform ${LIB_M})
127
128IF (NOT DEFINED COUCHBASE_NETWORK_LIBS)
129   IF (WIN32)
130      SET(COUCHBASE_NETWORK_LIBS "Ws2_32")
131   ELSEIF ("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")
132      SET(COUCHBASE_NETWORK_LIBS socket nsl)
133   ENDIF (WIN32)
134ENDIF (NOT DEFINED COUCHBASE_NETWORK_LIBS)
135
136IF ("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
137    # The implementation you get with chrono on a default FreeBSD 10
138    # is dead slow and considered useless...
139    SET(HAVE_DEAD_SLOW_CHRONO true)
140ENDIF ("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
141
142IF (CB_DONT_NEED_GETHRTIME OR HAVE_DEAD_SLOW_CHRONO)
143   SET(PLATFORM_FILES ${PLATFORM_FILES} src/gethrtime.c)
144ELSE (CB_DONT_NEED_GETHRTIME OR HAVE_DEAD_SLOW_CHRONO)
145   SET(PLATFORM_FILES ${PLATFORM_FILES} src/cxxtime.cc)
146ENDIF (CB_DONT_NEED_GETHRTIME OR HAVE_DEAD_SLOW_CHRONO)
147
148CHECK_FUNCTION_EXISTS(malloc_usable_size HAVE_MALLOC_USABLE_SIZE)
149if(HAVE_MALLOC_USABLE_SIZE)
150  ADD_DEFINITIONS(-DHAVE_MALLOC_USABLE_SIZE)
151endif()
152#
153# Add all of the libraries
154#
155
156# 'platform_headers' - header-only part of platform.
157#
158# This should typically only be used for tests / benchmarks which want the
159# definitions of platform functions; but don't want to link against the
160# complete library. For example, a unit test might want to just link against
161# the single .cc file for the code it is testing, but needs the prototype(s)
162# of that .cc file.
163ADD_LIBRARY(platform_headers INTERFACE)
164TARGET_INCLUDE_DIRECTORIES(platform_headers INTERFACE
165        include
166        # "config.h" (which resides in ${Platform_BINARY_DIR}/src) is needed
167        # indirectly by any targets linking against platform_headers (because
168        # a number of platform's public headers include it).
169        ${Platform_BINARY_DIR}/src
170        ${Platform_BINARY_DIR}/include)
171
172# platform_so - the 'real' platform shared library containing our various
173# platform abstraction functionality.
174# Note that in general external users should link 'platform' and not
175# 'platform_so' - see below.
176ADD_LIBRARY(platform_so SHARED ${PLATFORM_FILES}
177                            ${CMAKE_CURRENT_BINARY_DIR}/src/config.h
178                            src/base64.cc
179                            src/getpid.c
180                            src/random.cc
181                            src/backtrace.c
182                            src/byte_buffer_dump.cc
183                            src/byteorder.c
184                            src/cb_malloc.cc
185                            src/cb_mktemp.cc
186                            src/cb_time.cc
187                            src/cbassert.c
188                            src/checked_snprintf.cc
189                            src/crc32c.cc
190                            src/crc32c_sse4_2.cc
191                            src/crc32c_private.h
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/interrupt.h
215                            include/platform/make_unique.h
216                            include/platform/memorymap.h
217                            include/platform/non_negative_counter.h
218                            include/platform/platform.h
219                            include/platform/pipe.h
220                            include/platform/processclock.h
221                            include/platform/random.h
222                            include/platform/ring_buffer.h
223                            include/platform/rwlock.h
224                            include/platform/sized_buffer.h
225                            include/platform/strerror.h
226                            include/platform/string.h
227                            include/platform/sysinfo.h
228                            include/platform/thread.h
229                            include/platform/timeutils.h
230                            include/platform/uuid.h
231                            ${Platform_BINARY_DIR}/include/platform/visibility.h)
232GENERATE_EXPORT_HEADER(platform_so
233                       EXPORT_MACRO_NAME PLATFORM_PUBLIC_API
234                       EXPORT_FILE_NAME ${Platform_BINARY_DIR}/include/platform/visibility.h)
235
236LIST(APPEND PLATFORM_LIBRARIES "phosphor")
237LIST(REMOVE_DUPLICATES PLATFORM_LIBRARIES)
238TARGET_LINK_LIBRARIES(platform_so PRIVATE
239        platform_headers
240        gsl_lite
241        ${MALLOC_LIBRARIES}
242        ${PLATFORM_LIBRARIES})
243
244# Link publically with COUCHBASE_NETWORK_LIBS - strictly speaking this
245# shouldn't be necessary, however a number of downstream components
246# (e.g. couchstore targets) rely on platform to make available
247# htons/htonl (via Ws2_32) etc when building on Windows.
248# Until all such targets are # updated to link directly to Ws2_32, we
249# link publically.
250TARGET_LINK_LIBRARIES(platform_so PUBLIC
251        ${COUCHBASE_NETWORK_LIBS})
252
253TARGET_INCLUDE_DIRECTORIES(platform_so PUBLIC include)
254# TODO: Add a TARGET_INCLUDE_DIRECTORIES(PUBLIC) to phosphor; then the
255# following line can be removed:
256TARGET_INCLUDE_DIRECTORIES(platform_so PRIVATE ${phosphor_SOURCE_DIR}/include)
257
258# 'platform' - the library clients should normally link to.
259#
260# 'platform' is a static library, which itself links to the main platform_so
261# shared library. This is to ensure that any clients of platform always link
262# in the C++ global operator new/delete replacements if they use platform at
263# all. If they didn't then this would cause problems:
264# 1) Memory usage from the client will not be tracked.
265# 2) If a C++ object allocated in platform (using cbmalloc via new) is
266#    delete'd in the client (using default free) then we have a mismatched
267#    new/delete pair.
268# By giving all clients of platform the global new/delete automatically we
269# avoid these problems.
270ADD_LIBRARY(platform STATIC src/global_new_replacement.cc)
271SET_TARGET_PROPERTIES(platform PROPERTIES POSITION_INDEPENDENT_CODE true)
272TARGET_LINK_LIBRARIES(platform platform_headers platform_so)
273
274# On OS X 10.11 we need to remove the default rpath settings in order to all
275# us to load the platform library from Python scripts that are run out of
276# /usr/bin/python. We can remove this once we ship our own Python.
277IF (APPLE)
278    SET_TARGET_PROPERTIES(platform_so
279                          PROPERTIES
280                          INSTALL_RPATH "")
281ENDIF ()
282
283SET_TARGET_PROPERTIES(platform_so PROPERTIES SOVERSION 0.1.0)
284
285# TODO: Given dirutils now links with platform (for cbmalloc &
286# global_new_replacement) we can probably just merge it into platform.
287ADD_LIBRARY(dirutils SHARED
288            src/dirutils.cc
289            include/platform/dirutils.h)
290SET_TARGET_PROPERTIES(dirutils PROPERTIES SOVERSION 0.1.0)
291TARGET_LINK_LIBRARIES(dirutils platform)
292GENERATE_EXPORT_HEADER(dirutils
293                       EXPORT_MACRO_NAME DIRUTILS_PUBLIC_API
294                       EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/include/platform/dirutils-visibility.h)
295
296IF (BREAKPAD_FOUND)
297   ADD_DEFINITIONS(-DHAVE_BREAKPAD)
298   INCLUDE_DIRECTORIES(AFTER ${BREAKPAD_INCLUDE_DIR})
299   # Breakpad has headers (which are dependencies for our breakpad integration
300   # code) that don't compile cleanly as C++11, so the GNU standard is
301   # used instead.
302   IF (UNIX)
303      set_source_files_properties(src/breakpad_wrapper.cc PROPERTIES COMPILE_FLAGS ${CB_GNU_CXX11_OPTION})
304   ENDIF (UNIX)
305ENDIF (BREAKPAD_FOUND)
306
307# To simplify usage we always build breakpad_wrapper; it'll just
308# do nothing on targets which don't have Breakpad.
309ADD_LIBRARY(breakpad_wrapper SHARED
310            src/breakpad_wrapper.cc
311            include/breakpad_wrapper/breakpad_wrapper.h)
312SET_TARGET_PROPERTIES(breakpad_wrapper PROPERTIES COMPILE_FLAGS "-Dplatform_so_EXPORTS")
313TARGET_LINK_LIBRARIES(breakpad_wrapper
314                      ${BREAKPAD_LIBRARIES} platform_headers)
315
316INSTALL(TARGETS breakpad_wrapper cJSON JSON_checker platform_so dirutils
317        RUNTIME DESTINATION bin
318        LIBRARY DESTINATION lib
319        ARCHIVE DESTINATION lib)
320
321ADD_SUBDIRECTORY(cbcompress)
322ADD_SUBDIRECTORY(cbsocket)
323
324if (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
325    ADD_SUBDIRECTORY(tests)
326endif (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
327