xref: /4.0.0/platform/CMakeLists.txt (revision 94d56619)
1PROJECT(Platform)
2CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
3
4IF (${CMAKE_MAJOR_VERSION} GREATER 2)
5    CMAKE_POLICY(SET CMP0042 NEW)
6ENDIF (${CMAKE_MAJOR_VERSION} GREATER 2)
7
8INCLUDE(CheckIncludeFileCXX)
9INCLUDE(CheckSymbolExists)
10
11CHECK_SYMBOL_EXISTS(backtrace execinfo.h HAVE_BACKTRACE)
12
13CMAKE_PUSH_CHECK_STATE(RESET)
14SET(CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
15SET(CMAKE_REQUIRED_LIBRARIES "dl")
16CHECK_SYMBOL_EXISTS(dladdr dlfcn.h HAVE_DLADDR)
17CMAKE_POP_CHECK_STATE()
18
19CHECK_SYMBOL_EXISTS(gethrtime sys/time.h CB_DONT_NEED_GETHRTIME)
20CHECK_SYMBOL_EXISTS(htonll arpa/inet.h CB_DONT_NEED_BYTEORDER)
21
22CONFIGURE_FILE (${CMAKE_CURRENT_SOURCE_DIR}/src/config.cmake.h
23                ${CMAKE_CURRENT_BINARY_DIR}/src/config.h)
24
25CONFIGURE_FILE (${CMAKE_CURRENT_SOURCE_DIR}/include/platform/internal_config.cmake.h
26                ${CMAKE_CURRENT_BINARY_DIR}/include/platform/internal_config.h)
27
28CONFIGURE_FILE (${CMAKE_CURRENT_SOURCE_DIR}/include/platform/dynamic.in.h
29                ${CMAKE_CURRENT_BINARY_DIR}/include/platform/dynamic.h)
30
31#
32# Set the include path
33#
34INCLUDE_DIRECTORIES(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/include
35                           ${CMAKE_CURRENT_BINARY_DIR}/src)
36
37ADD_LIBRARY(cJSON SHARED src/cJSON.c include/cJSON.h)
38SET_TARGET_PROPERTIES(cJSON PROPERTIES SOVERSION 1.0.0)
39SET_TARGET_PROPERTIES(cJSON PROPERTIES COMPILE_FLAGS -DBUILDING_CJSON=1)
40
41ADD_LIBRARY(JSON_checker SHARED src/JSON_checker.c include/JSON_checker.h)
42SET_TARGET_PROPERTIES(JSON_checker PROPERTIES SOVERSION 1.0.0)
43
44IF (WIN32)
45   INCLUDE_DIRECTORIES(AFTER ${CMAKE_CURRENT_SOURCE_DIR}/include/win32)
46   ADD_DEFINITIONS(-D_CRT_SECURE_NO_WARNINGS)
47   SET(PLATFORM_FILES src/getopt.c
48                      src/sockets.c
49                      src/strings.c
50                      src/cb_win32.c
51                      src/winrandom.c
52                      include/win32/getopt.h
53                      include/win32/strings.h
54                      include/win32/unistd.h)
55   INCLUDE(FindCouchbaseDbgHelp)
56   SET(BACKTRACE_LIBS ${DBGHELP_LIBRARY})
57   INSTALL(FILES ${DBGHELP_DLL} DESTINATION bin)
58ELSE (WIN32)
59   SET(PLATFORM_FILES src/cb_pthreads.c src/urandom.c)
60   SET(THREAD_LIBS "pthread")
61   SET(DLOPENLIB "dl")
62
63   IF (NOT APPLE)
64      SET(RTLIB "rt")
65   ENDIF(NOT APPLE)
66
67   TARGET_LINK_LIBRARIES(cJSON m)
68ENDIF (WIN32)
69
70IF (NOT DEFINED COUCHBASE_NETWORK_LIBS)
71   IF (WIN32)
72      SET(COUCHBASE_NETWORK_LIBS "Ws2_32")
73   ELSEIF ("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")
74      SET(COUCHBASE_NETWORK_LIBS socket nsl)
75   ENDIF (WIN32)
76ENDIF (NOT DEFINED COUCHBASE_NETWORK_LIBS)
77
78IF (CB_DONT_NEED_GETHRTIME)
79   SET(PLATFORM_FILES ${PLATFORM_FILES} src/gethrtime.c)
80ELSE (CB_DONT_NEED_GETHRTIME)
81   CHECK_INCLUDE_FILE_CXX(chrono HAVE_CHRONO)
82   IF (NOT HAVE_CHRONO)
83      MESSAGE(FATAL_ERROR "You need chrono to build a high resolution timer")
84   ENDIF (NOT HAVE_CHRONO)
85   SET(PLATFORM_FILES ${PLATFORM_FILES} src/cxxtime.cc)
86ENDIF (CB_DONT_NEED_GETHRTIME)
87
88#
89# Add all of the libraries
90#
91ADD_LIBRARY(platform SHARED ${PLATFORM_FILES}
92                            ${CMAKE_CURRENT_BINARY_DIR}/src/config.h
93                            src/random.cc
94                            src/backtrace.c
95                            src/byteorder.c
96                            src/cb_time.c
97                            src/cb_mktemp.c
98                            src/cbassert.c
99                            include/platform/platform.h
100                            include/platform/random.h
101                            include/platform/visibility.h)
102
103TARGET_LINK_LIBRARIES(platform ${THREAD_LIBS} ${COUCHBASE_NETWORK_LIBS} ${DLOPENLIB} ${RTLIB} ${BACKTRACE_LIBS})
104SET_TARGET_PROPERTIES(platform PROPERTIES SOVERSION 0.1.0)
105
106ADD_LIBRARY(dirutils SHARED src/dirutils.cc include/platform/dirutils.h)
107SET_TARGET_PROPERTIES(dirutils PROPERTIES SOVERSION 0.1.0)
108
109ADD_EXECUTABLE(platform-dirutils-test tests/dirutils_test.cc)
110TARGET_LINK_LIBRARIES(platform-dirutils-test dirutils)
111
112ADD_EXECUTABLE(platform-gettimeofday-test tests/gettimeofday_test.cc)
113TARGET_LINK_LIBRARIES(platform-gettimeofday-test platform)
114
115IF (INSTALL_HEADER_FILES)
116   INSTALL (FILES
117            include/platform/cbassert.h
118            include/platform/dirutils.h
119            include/platform/platform.h
120            include/platform/random.h
121            include/platform/visibility.h
122            include/platform/dirutils.h
123            DESTINATION include/platform)
124
125   INSTALL(FILES include/cJSON.h include/JSON_checker.h DESTINATION include)
126
127   IF (WIN32)
128      INSTALL(FILES
129             include/win32/getopt.h
130             include/win32/strings.h
131             include/win32/unistd.h
132             DESTINATION include)
133   ENDIF (WIN32)
134ENDIF (INSTALL_HEADER_FILES)
135
136INSTALL(TARGETS cJSON JSON_checker platform dirutils
137        RUNTIME DESTINATION bin
138        LIBRARY DESTINATION lib
139        ARCHIVE DESTINATION lib)
140
141#
142# Test targets
143#
144ADD_EXECUTABLE(platform-gethrtime-test
145               tests/gethrtime_test.c
146               include/platform/platform.h)
147TARGET_LINK_LIBRARIES(platform-gethrtime-test platform)
148
149ADD_EXECUTABLE(platform-getopt-test tests/getopt_test.cc)
150TARGET_LINK_LIBRARIES(platform-getopt-test platform)
151
152ADD_EXECUTABLE(platform-random-test
153               tests/random_test.cc
154               include/platform/random.h)
155TARGET_LINK_LIBRARIES(platform-random-test platform)
156
157ADD_EXECUTABLE(platform-mktemp-test
158               tests/cb_mktemp_test.c
159               include/platform/platform.h)
160TARGET_LINK_LIBRARIES(platform-mktemp-test platform)
161
162ADD_TEST(platform-dirutils-test platform-dirutils-test)
163ADD_TEST(platform-gettimeofday-test platform-gettimeofday-test)
164ADD_TEST(platform-gethrtime-test platform-gethrtime-test)
165ADD_TEST(platform-getopt-test-0 platform-getopt-test 0)
166ADD_TEST(platform-getopt-test-1 platform-getopt-test 1)
167ADD_TEST(platform-getopt-test-2 platform-getopt-test 2)
168ADD_TEST(platform-random-test platform-random-test)
169ADD_TEST(platform-mktemp-test platform-mktemp-test)
170
171ADD_EXECUTABLE(platform-cjson-test include/cJSON.h tests/cjson_test.c)
172TARGET_LINK_LIBRARIES(platform-cjson-test cJSON)
173ADD_TEST(platform-cjson-test platform-cjson-test)
174
175ADD_EXECUTABLE(platform-cjson-parse-test
176               include/cJSON.h
177               tests/cjson_parse_test.c)
178TARGET_LINK_LIBRARIES(platform-cjson-parse-test cJSON platform)
179ADD_TEST(platform-cjson-parse-test
180         platform-cjson-parse-test -f ${PROJECT_SOURCE_DIR}/tests/testdata.json
181                                   -n 1)
182
183ADD_EXECUTABLE(platform-json-checker-test tests/json_checker_test.cc)
184TARGET_LINK_LIBRARIES(platform-json-checker-test JSON_checker)
185ADD_TEST(platform-json-checker-test platform-json-checker-test)
186
187ADD_EXECUTABLE(platform-strings-test tests/strings_test.c)
188TARGET_LINK_LIBRARIES(platform-strings-test platform)
189ADD_TEST(platform-strings-test platform-strings-test)
190
191ADD_EXECUTABLE(platform-backtrace-test tests/backtrace_test.c)
192TARGET_LINK_LIBRARIES(platform-backtrace-test platform)
193ADD_TEST(platform-backtrace-test platform-backtrace-test)
194
195
196IF (${CMAKE_MAJOR_VERSION} LESS 3)
197   SET_TARGET_PROPERTIES(cJSON PROPERTIES INSTALL_NAME_DIR
198                         ${CMAKE_INSTALL_PREFIX}/lib)
199   SET_TARGET_PROPERTIES(JSON_checker PROPERTIES INSTALL_NAME_DIR
200                         ${CMAKE_INSTALL_PREFIX}/lib)
201   SET_TARGET_PROPERTIES(platform PROPERTIES INSTALL_NAME_DIR
202                         ${CMAKE_INSTALL_PREFIX}/lib)
203   SET_TARGET_PROPERTIES(dirutils PROPERTIES INSTALL_NAME_DIR
204                         ${CMAKE_INSTALL_PREFIX}/lib)
205ENDIF (${CMAKE_MAJOR_VERSION} LESS 3)
206