xref: /4.0.0/platform/CMakeLists.txt (revision 4d17a68e)
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   SET(BACKTRACE_LIBS "Dbghelp")
56ELSE (WIN32)
57   SET(PLATFORM_FILES src/cb_pthreads.c src/urandom.c)
58   SET(THREAD_LIBS "pthread")
59   SET(DLOPENLIB "dl")
60
61   IF (NOT APPLE)
62      SET(RTLIB "rt")
63   ENDIF(NOT APPLE)
64
65   TARGET_LINK_LIBRARIES(cJSON m)
66ENDIF (WIN32)
67
68IF (NOT DEFINED COUCHBASE_NETWORK_LIBS)
69   IF (WIN32)
70      SET(COUCHBASE_NETWORK_LIBS "Ws2_32")
71   ELSEIF ("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")
72      SET(COUCHBASE_NETWORK_LIBS socket nsl)
73   ENDIF (WIN32)
74ENDIF (NOT DEFINED COUCHBASE_NETWORK_LIBS)
75
76IF (CB_DONT_NEED_GETHRTIME)
77   SET(PLATFORM_FILES ${PLATFORM_FILES} src/gethrtime.c)
78ELSE (CB_DONT_NEED_GETHRTIME)
79   CHECK_INCLUDE_FILE_CXX(chrono HAVE_CHRONO)
80   IF (NOT HAVE_CHRONO)
81      MESSAGE(FATAL_ERROR "You need chrono to build a high resolution timer")
82   ENDIF (NOT HAVE_CHRONO)
83   SET(PLATFORM_FILES ${PLATFORM_FILES} src/cxxtime.cc)
84ENDIF (CB_DONT_NEED_GETHRTIME)
85
86#
87# Add all of the libraries
88#
89ADD_LIBRARY(platform SHARED ${PLATFORM_FILES}
90                            ${CMAKE_CURRENT_BINARY_DIR}/src/config.h
91                            src/random.cc
92                            src/backtrace.c
93                            src/byteorder.c
94                            src/cb_time.c
95                            src/cb_mktemp.c
96                            include/platform/platform.h
97                            include/platform/random.h
98                            include/platform/visibility.h)
99
100TARGET_LINK_LIBRARIES(platform ${THREAD_LIBS} ${COUCHBASE_NETWORK_LIBS} ${DLOPENLIB} ${RTLIB} ${BACKTRACE_LIBS})
101SET_TARGET_PROPERTIES(platform PROPERTIES SOVERSION 0.1.0)
102
103ADD_LIBRARY(dirutils SHARED src/dirutils.cc include/platform/dirutils.h)
104SET_TARGET_PROPERTIES(dirutils PROPERTIES SOVERSION 0.1.0)
105
106ADD_EXECUTABLE(platform-dirutils-test tests/dirutils_test.cc)
107TARGET_LINK_LIBRARIES(platform-dirutils-test dirutils)
108
109ADD_EXECUTABLE(platform-gettimeofday-test tests/gettimeofday_test.cc)
110TARGET_LINK_LIBRARIES(platform-gettimeofday-test platform)
111
112IF (INSTALL_HEADER_FILES)
113   INSTALL (FILES
114            include/platform/cbassert.h
115            include/platform/dirutils.h
116            include/platform/platform.h
117            include/platform/random.h
118            include/platform/visibility.h
119            include/platform/dirutils.h
120            DESTINATION include/platform)
121
122   INSTALL(FILES include/cJSON.h include/JSON_checker.h DESTINATION include)
123
124   IF (WIN32)
125      INSTALL(FILES
126             include/win32/getopt.h
127             include/win32/strings.h
128             include/win32/unistd.h
129             DESTINATION include)
130   ENDIF (WIN32)
131ENDIF (INSTALL_HEADER_FILES)
132
133INSTALL(TARGETS cJSON JSON_checker platform dirutils
134        RUNTIME DESTINATION bin
135        LIBRARY DESTINATION lib
136        ARCHIVE DESTINATION lib)
137
138#
139# Test targets
140#
141ADD_EXECUTABLE(platform-gethrtime-test
142               tests/gethrtime_test.c
143               include/platform/platform.h)
144TARGET_LINK_LIBRARIES(platform-gethrtime-test platform)
145
146ADD_EXECUTABLE(platform-getopt-test tests/getopt_test.cc)
147TARGET_LINK_LIBRARIES(platform-getopt-test platform)
148
149ADD_EXECUTABLE(platform-random-test
150               tests/random_test.cc
151               include/platform/random.h)
152TARGET_LINK_LIBRARIES(platform-random-test platform)
153
154ADD_EXECUTABLE(platform-mktemp-test
155               tests/cb_mktemp_test.c
156               include/platform/platform.h)
157TARGET_LINK_LIBRARIES(platform-mktemp-test platform)
158
159ADD_TEST(platform-dirutils-test platform-dirutils-test)
160ADD_TEST(platform-gettimeofday-test platform-gettimeofday-test)
161ADD_TEST(platform-gethrtime-test platform-gethrtime-test)
162ADD_TEST(platform-getopt-test-0 platform-getopt-test 0)
163ADD_TEST(platform-getopt-test-1 platform-getopt-test 1)
164ADD_TEST(platform-getopt-test-2 platform-getopt-test 2)
165ADD_TEST(platform-random-test platform-random-test)
166ADD_TEST(platform-mktemp-test platform-mktemp-test)
167
168ADD_EXECUTABLE(platform-cjson-test include/cJSON.h tests/cjson_test.c)
169TARGET_LINK_LIBRARIES(platform-cjson-test cJSON)
170ADD_TEST(platform-cjson-test platform-cjson-test)
171
172ADD_EXECUTABLE(platform-cjson-parse-test
173               include/cJSON.h
174               tests/cjson_parse_test.c)
175TARGET_LINK_LIBRARIES(platform-cjson-parse-test cJSON platform)
176ADD_TEST(platform-cjson-parse-test
177         platform-cjson-parse-test -f ${PROJECT_SOURCE_DIR}/tests/testdata.json
178                                   -n 1)
179
180ADD_EXECUTABLE(platform-json-checker-test tests/json_checker_test.cc)
181TARGET_LINK_LIBRARIES(platform-json-checker-test JSON_checker)
182ADD_TEST(platform-json-checker-test platform-json-checker-test)
183
184ADD_EXECUTABLE(platform-strings-test tests/strings_test.c)
185TARGET_LINK_LIBRARIES(platform-strings-test platform)
186ADD_TEST(platform-strings-test platform-strings-test)
187
188ADD_EXECUTABLE(platform-backtrace-test tests/backtrace_test.c)
189TARGET_LINK_LIBRARIES(platform-backtrace-test platform)
190ADD_TEST(platform-backtrace-test platform-backtrace-test)
191
192
193IF (${CMAKE_MAJOR_VERSION} LESS 3)
194   SET_TARGET_PROPERTIES(cJSON PROPERTIES INSTALL_NAME_DIR
195                         ${CMAKE_INSTALL_PREFIX}/lib)
196   SET_TARGET_PROPERTIES(JSON_checker PROPERTIES INSTALL_NAME_DIR
197                         ${CMAKE_INSTALL_PREFIX}/lib)
198   SET_TARGET_PROPERTIES(platform PROPERTIES INSTALL_NAME_DIR
199                         ${CMAKE_INSTALL_PREFIX}/lib)
200   SET_TARGET_PROPERTIES(dirutils PROPERTIES INSTALL_NAME_DIR
201                         ${CMAKE_INSTALL_PREFIX}/lib)
202ENDIF (${CMAKE_MAJOR_VERSION} LESS 3)
203