xref: /6.6.0/platform/CMakeLists.txt (revision 58590f55)
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/byteorder.c
164  src/cb_malloc.cc
165  src/cb_time.cc
166  src/cbassert.c
167  src/checked_snprintf.cc
168  src/crc32c.cc
169  src/crc32c_private.h
170  src/getopt.cc
171  src/global_new_replacement.cc
172  src/histogram.cc
173  src/interrupt.cc
174  src/memorymap.cc
175  src/pipe.cc
176  src/processclock.cc
177  src/sized_buffer.cc
178  src/strerror.cc
179  src/string_hex.cc
180  src/sysinfo.cc
181  src/thread.cc
182  src/timeutils.cc
183  src/uuid.cc
184  include/platform/atomic_duration.h
185  include/platform/backtrace.h
186  include/platform/base64.h
187  include/platform/bitset.h
188  include/platform/byte_buffer_dump.h
189  include/platform/cb_malloc.h
190  include/platform/cbassert.h
191  include/platform/checked_snprintf.h
192  include/platform/corestore.h
193  include/platform/crc32c.h
194  include/platform/getopt.h
195  include/platform/interrupt.h
196  include/platform/memorymap.h
197  include/platform/non_negative_counter.h
198  include/platform/platform_socket.h
199  include/platform/platform_thread.h
200  include/platform/platform_time.h
201  include/platform/pipe.h
202  include/platform/processclock.h
203  include/platform/random.h
204  include/platform/ring_buffer.h
205  include/platform/rwlock.h
206  include/platform/sized_buffer.h
207  include/platform/strerror.h
208  include/platform/string_hex.h
209  include/platform/sysinfo.h
210  include/platform/thread.h
211  include/platform/timeutils.h
212  include/platform/uuid.h
213  ${Platform_BINARY_DIR}/include/platform/visibility.h
214)
215
216# platform_so - the 'real' platform shared library containing our various
217# platform abstraction functionality.
218# Note that in general external users should link 'platform' and not
219# 'platform_so' - see below.
220add_library(platform_so SHARED)
221target_link_libraries(platform_so PUBLIC ${FOLLY_LIBRARIES})
222target_link_libraries(platform_so PRIVATE phosphor)
223
224# 'platform' - the library clients should normally link to.
225#
226# 'platform' is a static library, which itself links to the main platform_so
227# shared library. This is to ensure that any clients of platform always link
228# in the C++ global operator new/delete replacements if they use platform at
229# all. If they didn't then this would cause problems:
230# 1) Memory usage from the client will not be tracked.
231# 2) If a C++ object allocated in platform (using cbmalloc via new) is
232#    delete'd in the client (using default free) then we have a mismatched
233#    new/delete pair.
234# By giving all clients of platform the global new/delete automatically we
235# avoid these problems.
236add_library(platform STATIC)
237
238if(CB_SANITIZERS)
239  # Define an additional variant of platform libs which has the
240  # sanitizers disabled.  This allows targets which require platform,
241  # but which are incompatible with the sanitizers (e.g. Erlang NIFs)
242  # to still be built and run when we are building the rest of the
243  # Server with Sanitizer support.
244  add_library(platform_unsanitized_so SHARED)
245  target_link_libraries(platform_unsanitized_so PRIVATE phosphor_unsanitized)
246  remove_sanitizers(platform_unsanitized_so)
247
248  add_library(platform_unsanitized STATIC)
249  remove_sanitizers(platform_unsanitized)
250
251  set(platform_VARIANTS platform platform_unsanitized)
252else()
253  # For convenience define a 'platform_unsanitized' alias even when
254  # the sanitizers are disabled (so targets which need an unsanitized
255  # platform can unconditionally link against this).
256  add_library(platform_unsanitized ALIAS platform)
257
258  set(platform_VARIANTS platform)
259endif()
260
261foreach(platform ${platform_VARIANTS})
262  set(platform_so ${platform}_so)
263  target_sources(${platform_so} PRIVATE ${PLATFORM_FILES})
264
265  TARGET_LINK_LIBRARIES(${platform_so} PRIVATE
266    platform_headers
267    gsl_lite
268    ${MALLOC_LIBRARIES}
269    ${PLATFORM_LIBRARIES})
270
271  # Link publically with COUCHBASE_NETWORK_LIBS - strictly speaking this
272  # shouldn't be necessary, however a number of downstream components
273  # (e.g. couchstore targets) rely on platform to make available
274  # htons/htonl (via Ws2_32) etc when building on Windows.
275  # Until all such targets are updated to link directly to Ws2_32, we
276  # link publically.
277  TARGET_LINK_LIBRARIES(${platform_so} PUBLIC
278    ${COUCHBASE_NETWORK_LIBS})
279
280
281  TARGET_INCLUDE_DIRECTORIES(${platform_so} SYSTEM PUBLIC ${FOLLY_INCLUDE_DIR})
282  TARGET_INCLUDE_DIRECTORIES(${platform_so} PUBLIC include)
283  # TODO: Add a TARGET_INCLUDE_DIRECTORIES(PUBLIC) to phosphor; then the
284  # following line can be removed:
285  TARGET_INCLUDE_DIRECTORIES(${platform_so} PRIVATE ${phosphor_SOURCE_DIR}/include)
286
287  # On OS X 10.11 we need to remove the default rpath settings in order to all
288  # us to load the platform library from Python scripts that are run out of
289  # /usr/bin/python. We can remove this once we ship our own Python.
290  IF (APPLE)
291    SET_TARGET_PROPERTIES(${platform_so}
292      PROPERTIES
293      INSTALL_RPATH "")
294  ENDIF ()
295  SET_TARGET_PROPERTIES(${platform_so} PROPERTIES SOVERSION 0.1.0)
296
297  INSTALL(TARGETS ${platform_so}
298    RUNTIME DESTINATION bin
299    LIBRARY DESTINATION lib
300    ARCHIVE DESTINATION lib)
301
302
303  target_sources(${platform} PRIVATE src/global_new_replacement.cc)
304  SET_TARGET_PROPERTIES(${platform} PROPERTIES POSITION_INDEPENDENT_CODE true)
305  TARGET_LINK_LIBRARIES(${platform} platform_headers ${platform_so})
306endforeach()
307
308GENERATE_EXPORT_HEADER(platform_so
309                       EXPORT_MACRO_NAME PLATFORM_PUBLIC_API
310                       EXPORT_FILE_NAME ${Platform_BINARY_DIR}/include/platform/visibility.h)
311
312# TODO: Given dirutils now links with platform (for cbmalloc &
313# global_new_replacement) we can probably just merge it into platform.
314ADD_LIBRARY(dirutils SHARED
315            src/dirutils.cc
316            include/platform/dirutils.h)
317SET_TARGET_PROPERTIES(dirutils PROPERTIES SOVERSION 0.1.0)
318TARGET_LINK_LIBRARIES(dirutils platform ${CMAKE_DL_LIBS})
319GENERATE_EXPORT_HEADER(dirutils
320                       EXPORT_MACRO_NAME DIRUTILS_PUBLIC_API
321                       EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/include/platform/dirutils-visibility.h)
322
323# To simplify usage we always build breakpad_wrapper; it'll just
324# do nothing on targets which don't have Breakpad.
325add_library(breakpad_wrapper SHARED
326            src/breakpad_wrapper.cc
327            include/breakpad_wrapper/breakpad_wrapper.h)
328set_target_properties(breakpad_wrapper PROPERTIES COMPILE_FLAGS "-Dplatform_so_EXPORTS")
329if (BREAKPAD_FOUND)
330   target_compile_definitions(breakpad_wrapper PRIVATE -DHAVE_BREAKPAD)
331   target_include_directories(breakpad_wrapper PRIVATE SYSTEM AFTER ${BREAKPAD_INCLUDE_DIR})
332endif (BREAKPAD_FOUND)
333
334if (UNIX)
335   # Breakpad has headers (which are dependencies for our breakpad integration
336   # code) that don't compile cleanly as C++11, so the GNU standard is
337   # used instead.
338   set_source_files_properties(src/breakpad_wrapper.cc PROPERTIES COMPILE_FLAGS ${CB_GNU_CXX11_OPTION})
339endif (UNIX)
340target_link_libraries(breakpad_wrapper
341                      ${BREAKPAD_LIBRARIES} platform_headers)
342
343INSTALL(TARGETS breakpad_wrapper JSON_checker dirutils
344        RUNTIME DESTINATION bin
345        LIBRARY DESTINATION lib
346        ARCHIVE DESTINATION lib)
347
348ADD_SUBDIRECTORY(cbcompress)
349
350IF (PLATFORM_ENABLE_CBSOCKET)
351   ADD_SUBDIRECTORY(cbsocket)
352ENDIF (PLATFORM_ENABLE_CBSOCKET)
353
354if (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
355    ADD_SUBDIRECTORY(tests)
356endif (COUCHBASE_PLATFORM_BUILD_UNIT_TESTS)
357