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
12OPTION(PLATFORM_ENABLE_CBSOCKET "Build cbsocket support" ON)
13
14check_unit_test_enabled(platform COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
15if (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
16    ENABLE_CODE_COVERAGE_REPORT()
17endif (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
18
19CMAKE_PUSH_CHECK_STATE(RESET)
20  FIND_LIBRARY(EXECINFO_LIBRARY NAMES execinfo)
21  IF (EXECINFO_LIBRARY)
22    SET(CMAKE_REQUIRED_LIBRARIES "${EXECINFO_LIBRARY}")
23    LIST(APPEND PLATFORM_LIBRARIES "${EXECINFO_LIBRARY}")
24  ENDIF(EXECINFO_LIBRARY)
25  CHECK_SYMBOL_EXISTS(backtrace execinfo.h HAVE_BACKTRACE)
26CMAKE_POP_CHECK_STATE()
27
28CMAKE_PUSH_CHECK_STATE(RESET)
29  SET(CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
30  FIND_LIBRARY(DL_LIBRARY NAMES dl)
31  IF (DL_LIBRARY)
32    SET(CMAKE_REQUIRED_LIBRARIES "${DL_LIBRARY}")
33    LIST(APPEND PLATFORM_LIBRARIES "${DL_LIBRARY}")
34  ENDIF (DL_LIBRARY)
35  CHECK_SYMBOL_EXISTS(dladdr dlfcn.h HAVE_DLADDR)
36CMAKE_POP_CHECK_STATE()
37
38CMAKE_PUSH_CHECK_STATE(RESET)
39SET(CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
40SET(CMAKE_REQUIRED_LIBRARIES "pthread")
41CHECK_SYMBOL_EXISTS(pthread_setname_np pthread.h HAVE_PTHREAD_SETNAME_NP)
42CHECK_SYMBOL_EXISTS(pthread_getname_np pthread.h HAVE_PTHREAD_GETNAME_NP)
43CHECK_SYMBOL_EXISTS(sched_getaffinity sched.h HAVE_SCHED_GETAFFINITY)
44CHECK_SYMBOL_EXISTS(sched_getcpu sched.h HAVE_SCHED_GETCPU)
45CMAKE_POP_CHECK_STATE()
46
47CMAKE_PUSH_CHECK_STATE(RESET)
48
49CHECK_SYMBOL_EXISTS(htonll arpa/inet.h CB_DONT_NEED_BYTEORDER)
50CHECK_INCLUDE_FILE(cpuid.h HAVE_CPUID_H)
51
52if (HAVE_BACKTRACE)
53  add_definitions(-DHAVE_BACKTRACE=1)
54endif()
55
56if (HAVE_DLADDR)
57  add_definitions(-DHAVE_DLADDR=1)
58endif()
59
60if (HAVE_PTHREAD_SETNAME_NP)
61  add_definitions(-DHAVE_PTHREAD_SETNAME_NP=1)
62endif()
63
64if (HAVE_PTHREAD_GETNAME_NP)
65  add_definitions(-DHAVE_PTHREAD_GETNAME_NP=1)
66endif()
67
68if (HAVE_SCHED_GETAFFINITY)
69  add_definitions(-DHAVE_SCHED_GETAFFINITY=1)
70endif()
71
72if (HAVE_SCHED_GETCPU)
73  add_definitions(-DHAVE_SCHED_GETCPU=1)
74endif()
75
76if (HAVE_CPUID_H)
77  add_definitions(-DHAVE_CPUID_H=1)
78endif()
79
80CONFIGURE_FILE (${CMAKE_CURRENT_SOURCE_DIR}/include/platform/dynamic.in.h
81                ${CMAKE_CURRENT_BINARY_DIR}/include/platform/dynamic.h)
82
83#
84# Set the include path
85#
86INCLUDE_DIRECTORIES(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/external)
87
88IF (MEMORY_ALLOCATOR)
89  INCLUDE_DIRECTORIES(AFTER ${MALLOC_INCLUDE_DIR})
90ENDIF (MEMORY_ALLOCATOR)
91
92ADD_LIBRARY(JSON_checker SHARED src/JSON_checker.cc include/JSON_checker.h)
93SET_TARGET_PROPERTIES(JSON_checker PROPERTIES SOVERSION 1.0.0)
94TARGET_LINK_LIBRARIES(JSON_checker PRIVATE platform_headers)
95GENERATE_EXPORT_HEADER(JSON_checker
96                       EXPORT_MACRO_NAME JSON_CHECKER_PUBLIC_API
97                       EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/include/platform/JSON_checker-visibility.h)
98
99IF (WIN32)
100   INCLUDE_DIRECTORIES(AFTER ${CMAKE_CURRENT_SOURCE_DIR}/include/win32)
101   ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
102   SET(PLATFORM_FILES src/sockets.c
103                      src/cb_win32.cc
104                      src/crc32c_sse4_2.cc
105                      include/win32/getopt.h
106                      include/win32/strings.h
107                      include/win32/unistd.h)
108   ADD_DEFINITIONS(-DHAVE_BACKTRACE_SUPPORT=1)
109   LIST(APPEND PLATFORM_LIBRARIES DbgHelp.lib)
110ELSE (WIN32)
111   SET(PLATFORM_FILES src/cb_pthreads.cc)
112   # We don't know the output of uname -p on all of our supported platforms
113   # so check if it's PPC64
114   if (NOT CMAKE_SYSTEM_PROCESSOR EQUAL "^ppc64")
115      list(APPEND PLATFORM_FILES src/crc32c_sse4_2.cc)
116      set_source_files_properties(src/crc32c_sse4_2.cc
117                                  PROPERTIES COMPILE_FLAGS -msse4.2)
118   endif()
119   LIST(APPEND PLATFORM_LIBRARIES "pthread")
120
121   IF (NOT APPLE)
122      LIST(APPEND PLATFORM_LIBRARIES "rt")
123   ENDIF(NOT APPLE)
124
125   SET(LIB_M m)
126ENDIF (WIN32)
127LIST(REMOVE_DUPLICATES PLATFORM_LIBRARIES)
128
129IF (NOT DEFINED COUCHBASE_NETWORK_LIBS)
130   IF (WIN32)
131      SET(COUCHBASE_NETWORK_LIBS "Ws2_32")
132   ENDIF (WIN32)
133ENDIF (NOT DEFINED COUCHBASE_NETWORK_LIBS)
134
135CHECK_FUNCTION_EXISTS(malloc_usable_size HAVE_MALLOC_USABLE_SIZE)
136if(HAVE_MALLOC_USABLE_SIZE)
137  ADD_DEFINITIONS(-DHAVE_MALLOC_USABLE_SIZE)
138endif()
139#
140# Add all of the libraries
141#
142
143# 'platform_headers' - header-only part of platform.
144#
145# This should typically only be used for tests / benchmarks which want the
146# definitions of platform functions; but don't want to link against the
147# complete library. For example, a unit test might want to just link against
148# the single .cc file for the code it is testing, but needs the prototype(s)
149# of that .cc file.
150ADD_LIBRARY(platform_headers INTERFACE)
151TARGET_INCLUDE_DIRECTORIES(platform_headers SYSTEM INTERFACE
152                           ${FOLLY_INCLUDE_DIR})
153
154TARGET_INCLUDE_DIRECTORIES(platform_headers INTERFACE
155        include
156        ${Platform_BINARY_DIR}/include)
157
158LIST(APPEND PLATFORM_FILES
159  src/base64.cc
160  src/random.cc
161  src/backtrace.c
162  src/byte_buffer_dump.cc
163  src/cb_malloc.cc
164  src/cb_time.cc
165  src/cbassert.c
166  src/checked_snprintf.cc
167  src/crc32c.cc
168  src/crc32c_private.h
169  src/getopt.cc
170  src/global_new_replacement.cc
171  src/histogram.cc
172  src/interrupt.cc
173  src/memorymap.cc
174  src/pipe.cc
175  src/processclock.cc
176  src/sized_buffer.cc
177  src/strerror.cc
178  src/string_hex.cc
179  src/sysinfo.cc
180  src/thread.cc
181  src/timeutils.cc
182  src/uuid.cc
183  include/platform/atomic_duration.h
184  include/platform/backtrace.h
185  include/platform/base64.h
186  include/platform/bitset.h
187  include/platform/byte_buffer_dump.h
188  include/platform/cb_malloc.h
189  include/platform/cbassert.h
190  include/platform/checked_snprintf.h
191  include/platform/corestore.h
192  include/platform/crc32c.h
193  include/platform/getopt.h
194  include/platform/interrupt.h
195  include/platform/memorymap.h
196  include/platform/non_negative_counter.h
197  include/platform/platform_socket.h
198  include/platform/platform_thread.h
199  include/platform/platform_time.h
200  include/platform/pipe.h
201  include/platform/processclock.h
202  include/platform/random.h
203  include/platform/ring_buffer.h
204  include/platform/rwlock.h
205  include/platform/sized_buffer.h
206  include/platform/strerror.h
207  include/platform/string_hex.h
208  include/platform/sysinfo.h
209  include/platform/thread.h
210  include/platform/timeutils.h
211  include/platform/uuid.h
212  ${Platform_BINARY_DIR}/include/platform/visibility.h
213)
214
215# platform_so - the 'real' platform shared library containing our various
216# platform abstraction functionality.
217# Note that in general external users should link 'platform' and not
218# 'platform_so' - see below.
219add_library(platform_so SHARED)
220target_link_libraries(platform_so PUBLIC ${FOLLY_LIBRARIES})
221target_link_libraries(platform_so PRIVATE phosphor)
222
223# 'platform' - the library clients should normally link to.
224#
225# 'platform' is a static library, which itself links to the main platform_so
226# shared library. This is to ensure that any clients of platform always link
227# in the C++ global operator new/delete replacements if they use platform at
228# all. If they didn't then this would cause problems:
229# 1) Memory usage from the client will not be tracked.
230# 2) If a C++ object allocated in platform (using cbmalloc via new) is
231#    delete'd in the client (using default free) then we have a mismatched
232#    new/delete pair.
233# By giving all clients of platform the global new/delete automatically we
234# avoid these problems.
235add_library(platform STATIC)
236
237if(CB_SANITIZERS)
238  # Define an additional variant of platform libs which has the
239  # sanitizers disabled.  This allows targets which require platform,
240  # but which are incompatible with the sanitizers (e.g. Erlang NIFs)
241  # to still be built and run when we are building the rest of the
242  # Server with Sanitizer support.
243  add_library(platform_unsanitized_so SHARED)
244  target_link_libraries(platform_unsanitized_so PRIVATE phosphor_unsanitized)
245  remove_sanitizers(platform_unsanitized_so)
246
247  add_library(platform_unsanitized STATIC)
248  remove_sanitizers(platform_unsanitized)
249
250  set(platform_VARIANTS platform platform_unsanitized)
251else()
252  # For convenience define a 'platform_unsanitized' alias even when
253  # the sanitizers are disabled (so targets which need an unsanitized
254  # platform can unconditionally link against this).
255  add_library(platform_unsanitized ALIAS platform)
256
257  set(platform_VARIANTS platform)
258endif()
259
260foreach(platform ${platform_VARIANTS})
261  set(platform_so ${platform}_so)
262  target_sources(${platform_so} PRIVATE ${PLATFORM_FILES})
263
264  TARGET_LINK_LIBRARIES(${platform_so} PRIVATE
265    platform_headers
266    gsl_lite
267    ${MALLOC_LIBRARIES}
268    ${PLATFORM_LIBRARIES})
269
270  # Link publically with COUCHBASE_NETWORK_LIBS - strictly speaking this
271  # shouldn't be necessary, however a number of downstream components
272  # (e.g. couchstore targets) rely on platform to make available
273  # htons/htonl (via Ws2_32) etc when building on Windows.
274  # Until all such targets are updated to link directly to Ws2_32, we
275  # link publically.
276  TARGET_LINK_LIBRARIES(${platform_so} PUBLIC
277    ${COUCHBASE_NETWORK_LIBS})
278
279
280  TARGET_INCLUDE_DIRECTORIES(${platform_so} SYSTEM PUBLIC ${FOLLY_INCLUDE_DIR})
281  TARGET_INCLUDE_DIRECTORIES(${platform_so} PUBLIC include)
282  # TODO: Add a TARGET_INCLUDE_DIRECTORIES(PUBLIC) to phosphor; then the
283  # following line can be removed:
284  TARGET_INCLUDE_DIRECTORIES(${platform_so} PRIVATE ${phosphor_SOURCE_DIR}/include)
285
286  # On OS X 10.11 we need to remove the default rpath settings in order to all
287  # us to load the platform library from Python scripts that are run out of
288  # /usr/bin/python. We can remove this once we ship our own Python.
289  IF (APPLE)
290    SET_TARGET_PROPERTIES(${platform_so}
291      PROPERTIES
292      INSTALL_RPATH "")
293  ENDIF ()
294  SET_TARGET_PROPERTIES(${platform_so} PROPERTIES SOVERSION 0.1.0)
295
296  INSTALL(TARGETS ${platform_so}
297    RUNTIME DESTINATION bin
298    LIBRARY DESTINATION lib
299    ARCHIVE DESTINATION lib)
300
301
302  target_sources(${platform} PRIVATE src/global_new_replacement.cc)
303  SET_TARGET_PROPERTIES(${platform} PROPERTIES POSITION_INDEPENDENT_CODE true)
304  TARGET_LINK_LIBRARIES(${platform} platform_headers ${platform_so})
305endforeach()
306
307GENERATE_EXPORT_HEADER(platform_so
308                       EXPORT_MACRO_NAME PLATFORM_PUBLIC_API
309                       EXPORT_FILE_NAME ${Platform_BINARY_DIR}/include/platform/visibility.h)
310
311# TODO: Given dirutils now links with platform (for cbmalloc &
312# global_new_replacement) we can probably just merge it into platform.
313ADD_LIBRARY(dirutils SHARED
314            src/dirutils.cc
315            include/platform/dirutils.h)
316SET_TARGET_PROPERTIES(dirutils PROPERTIES SOVERSION 0.1.0)
317TARGET_LINK_LIBRARIES(dirutils platform ${CMAKE_DL_LIBS})
318GENERATE_EXPORT_HEADER(dirutils
319                       EXPORT_MACRO_NAME DIRUTILS_PUBLIC_API
320                       EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/include/platform/dirutils-visibility.h)
321
322# To simplify usage we always build breakpad_wrapper; it'll just
323# do nothing on targets which don't have Breakpad.
324add_library(breakpad_wrapper SHARED
325            src/breakpad_wrapper.cc
326            include/breakpad_wrapper/breakpad_wrapper.h)
327set_target_properties(breakpad_wrapper PROPERTIES COMPILE_FLAGS "-Dplatform_so_EXPORTS")
328if (BREAKPAD_FOUND)
329   target_compile_definitions(breakpad_wrapper PRIVATE -DHAVE_BREAKPAD)
330   target_include_directories(breakpad_wrapper PRIVATE SYSTEM AFTER ${BREAKPAD_INCLUDE_DIR})
331endif (BREAKPAD_FOUND)
332
333if (UNIX)
334   # Breakpad has headers (which are dependencies for our breakpad integration
335   # code) that don't compile cleanly as C++11, so the GNU standard is
336   # used instead.
337   set_source_files_properties(src/breakpad_wrapper.cc PROPERTIES COMPILE_FLAGS ${CB_GNU_CXX11_OPTION})
338endif (UNIX)
339target_link_libraries(breakpad_wrapper
340                      ${BREAKPAD_LIBRARIES} platform_headers)
341
342INSTALL(TARGETS breakpad_wrapper JSON_checker dirutils
343        RUNTIME DESTINATION bin
344        LIBRARY DESTINATION lib
345        ARCHIVE DESTINATION lib)
346
347ADD_SUBDIRECTORY(cbcompress)
348
349IF (PLATFORM_ENABLE_CBSOCKET)
350   ADD_SUBDIRECTORY(cbsocket)
351ENDIF (PLATFORM_ENABLE_CBSOCKET)
352
353if (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
354    ADD_SUBDIRECTORY(tests)
355endif (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
356