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