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