1SET(CMAKE_TEST_SHARED_OBJECT "libm.so")
2IF (WIN32)
3    SET(CMAKE_TEST_SHARED_OBJECT "kernel32.dll.")
4ELSEIF (APPLE)
5    SET(CMAKE_TEST_SHARED_OBJECT "libm.dylib")
6ELSEIF (CMAKE_SYSTEM_NAME STREQUAL "Linux")
7    EXECUTE_PROCESS(COMMAND ${CMAKE_LINKER} -lm -t
8            OUTPUT_VARIABLE LIBM_SO_TRACE
9            ERROR_QUIET
10            OUTPUT_STRIP_TRAILING_WHITESPACE)
11    STRING(REGEX MATCH [-_a-z0-9/]+libm.so[.0-9]+ LIBM_SO_PATH ${LIBM_SO_TRACE})
12    IF (EXISTS "${LIBM_SO_PATH}")
13        GET_FILENAME_COMPONENT(CMAKE_TEST_SHARED_OBJECT ${LIBM_SO_PATH} NAME)
14    ENDIF()
15ENDIF()
16
17CONFIGURE_FILE(
18        ${PROJECT_SOURCE_DIR}/tests/start_mock.sh.in
19        ${PROJECT_SOURCE_DIR}/tests/start_mock.sh)
20
21# For Windows+GCC we need to link statically
22IF (NOT (WIN32 AND CMAKE_COMPILER_IS_GNUCC))
23    SET(gtest_force_shared_crt ON CACHE BOOL "" )
24    SET(BUILD_SHARED_LIBS ON)
25ENDIF()
26ADD_SUBDIRECTORY(${PROJECT_SOURCE_DIR}/contrib/gtest-1.8.1 gtest)
27INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
28
29ADD_EXECUTABLE(check-all EXCLUDE_FROM_ALL
30    check-all.cc $<TARGET_OBJECTS:cliopts> mocksupport/procutil.c)
31
32SET_PROPERTY(TARGET check-all
33    APPEND PROPERTY INCLUDE_DIRECTORIES
34    ${lcb_plat_includes}
35    ${SOURCE_ROOT})
36TARGET_LINK_LIBRARIES(check-all couchbaseS)
37
38
39FILE(GLOB T_BASIC_SRC basic/*.cc)
40FILE(GLOB T_MC_SRC mc/*.cc)
41FILE(GLOB T_RDB_SRC rdb/*.cc)
42FILE(GLOB T_SOCK_SRC socktests/*.cc)
43FILE(GLOB T_IOSERVER_SRC ioserver/*.cc)
44FILE(GLOB T_MOCKSUPPORT_SRC mocksupport/*.c mocksupport/*.cc)
45FILE(GLOB T_VBTEST_SRC vbucket/*.cc)
46
47ADD_LIBRARY(ioserver OBJECT EXCLUDE_FROM_ALL ${T_IOSERVER_SRC})
48IF(NOT LCB_NO_SSL)
49    GET_TARGET_PROPERTY(_ioserver_includes ioserver INCLUDE_DIRECTORIES)
50    SET_TARGET_PROPERTIES(ioserver PROPERTIES INCLUDE_DIRECTORIES "${_ioserver_includes};${OPENSSL_INCLUDE_DIR}")
51ENDIF()
52
53
54ADD_EXECUTABLE(nonio-tests EXCLUDE_FROM_ALL nonio_tests.cc ${T_BASIC_SRC})
55
56ADD_EXECUTABLE(mc-tests EXCLUDE_FROM_ALL nonio_tests.cc ${T_MC_SRC}
57    $<TARGET_OBJECTS:mcreq> $<TARGET_OBJECTS:mcreq-cxx> $<TARGET_OBJECTS:netbuf> $<TARGET_OBJECTS:vbucket-lcb>)
58
59ADD_EXECUTABLE(mc-malloc-tests EXCLUDE_FROM_ALL nonio_tests.cc ${T_MC_SRC}
60    $<TARGET_OBJECTS:mcreq> $<TARGET_OBJECTS:mcreq-cxx> $<TARGET_OBJECTS:netbuf-malloc> $<TARGET_OBJECTS:vbucket-lcb>)
61
62ADD_EXECUTABLE(netbuf-tests
63    EXCLUDE_FROM_ALL nonio_tests.cc basic/t_netbuf.cc $<TARGET_OBJECTS:netbuf>)
64
65ADD_EXECUTABLE(rdb-tests EXCLUDE_FROM_ALL nonio_tests.cc
66    ${T_RDB_SRC} $<TARGET_OBJECTS:rdb> ${SOURCE_ROOT}/src/list.c)
67
68ADD_EXECUTABLE(sock-tests EXCLUDE_FROM_ALL nonio_tests.cc
69    ${T_SOCK_SRC} $<TARGET_OBJECTS:ioserver>)
70
71ADD_EXECUTABLE(vbucket-tests EXCLUDE_FROM_ALL nonio_tests.cc ${T_VBTEST_SRC})
72ADD_EXECUTABLE(htparse-tests EXCLUDE_FROM_ALL nonio_tests.cc htparse/t_basic.cc)
73
74FILE(GLOB T_IO_SRC iotests/*.cc)
75IF(LCB_NO_MOCK)
76    ADD_EXECUTABLE(unit-tests EXCLUDE_FROM_ALL unit_tests.cc)
77    SET_TARGET_PROPERTIES(unit-tests PROPERTIES COMPILE_DEFINITIONS NO_COUCHBASEMOCK)
78ELSE()
79    ADD_LIBRARY(mocksupport OBJECT EXCLUDE_FROM_ALL ${T_MOCKSUPPORT_SRC})
80    ADD_EXECUTABLE(unit-tests EXCLUDE_FROM_ALL ${T_IO_SRC} unit_tests.cc $<TARGET_OBJECTS:mocksupport>)
81ENDIF()
82
83IF(CMAKE_COMPILER_IS_GNUCXX)
84    SET_TARGET_PROPERTIES(unit-tests
85        PROPERTIES
86        COMPILE_FLAGS "-Wno-sign-compare -Wno-missing-field-initializers")
87ENDIF(CMAKE_COMPILER_IS_GNUCXX)
88
89IF(NOT LCB_NO_MOCK)
90    IF(NOT EXISTS ${PROJECT_SOURCE_DIR}/tests/CouchbaseMock.jar)
91        DOWNLOAD_LCB_DEP("${COUCHBASE_MOCK_URL}" "${PROJECT_SOURCE_DIR}/tests/CouchbaseMock.jar")
92    ENDIF()
93    FIND_PROGRAM(JAVA java)
94    EXECUTE_PROCESS(COMMAND "${JAVA}" -jar "${PROJECT_SOURCE_DIR}/tests/CouchbaseMock.jar" --version
95                    RESULT_VARIABLE RET OUTPUT_VARIABLE VER OUTPUT_STRIP_TRAILING_WHITESPACE)
96    IF(RET EQUAL 0)
97        MESSAGE(STATUS "Using ${VER}")
98    ELSE()
99        MESSAGE(FATAL_ERROR "Unable to use ${PROJECT_SOURCE_DIR}/tests/CouchbaseMock.jar as CouchbaseMock")
100    ENDIF()
101ENDIF()
102
103# We're a library using the shared CRT. Don't use the static one
104INCLUDE_DIRECTORIES(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
105TARGET_LINK_LIBRARIES(unit-tests couchbaseS gtest)
106TARGET_LINK_LIBRARIES(nonio-tests couchbaseS gtest)
107TARGET_LINK_LIBRARIES(mc-tests couchbaseS gtest)
108TARGET_LINK_LIBRARIES(mc-malloc-tests couchbaseS gtest)
109TARGET_LINK_LIBRARIES(netbuf-tests gtest)
110TARGET_LINK_LIBRARIES(rdb-tests gtest)
111TARGET_LINK_LIBRARIES(sock-tests couchbaseS gtest)
112TARGET_LINK_LIBRARIES(vbucket-tests gtest couchbaseS)
113TARGET_LINK_LIBRARIES(htparse-tests gtest couchbaseS)
114
115IF(WIN32)
116    TARGET_LINK_LIBRARIES(mc-tests ws2_32.lib)
117    TARGET_LINK_LIBRARIES(mc-malloc-tests ws2_32.lib)
118ENDIF()
119
120FILE(GENERATE
121        OUTPUT ${LCB_GENSRCDIR}/$<CONFIG>/check_config.h
122        CONTENT
123        "
124#ifndef CHECK_CONFIG_H
125#define CHECK_CONFIG_H
126#define TEST_SHARED_OBJECT \"${CMAKE_TEST_SHARED_OBJECT}\"
127#define TEST_SRC_DIR \"${PROJECT_SOURCE_DIR}\"
128#define TEST_BUILD_DIR \"${PROJECT_BINARY_DIR}\"
129#define TEST_TEST_DIR \"$<TARGET_FILE_DIR:check-all>\"
130#define TEST_LIB_DIR \"$<TARGET_FILE_DIR:couchbase>\"
131#endif
132        "
133        )
134INCLUDE_DIRECTORIES(${LCB_GENSRCDIR}/$<CONFIG>)
135
136ADD_CUSTOM_TARGET(alltests DEPENDS check-all unit-tests nonio-tests
137    rdb-tests sock-tests vbucket-tests mc-tests htparse-tests)
138
139
140ADD_TEST(NAME BUILD-TESTS COMMAND ${CMAKE_COMMAND} --build "${PROJECT_BINARY_DIR}" --target alltests)
141
142MACRO(DEFINE_MOCKTEST plugin test)
143    ADD_TEST(
144        NAME
145            check-${plugin}-${test}
146        COMMAND
147            $<TARGET_FILE:check-all>
148            --srcdir "${PROJECT_SOURCE_DIR}"
149            --testdir "$<TARGET_FILE_DIR:check-all>"
150            --libdir "$<TARGET_FILE_DIR:couchbase>"
151            --plugins ${plugin}
152            --tests ${test}
153            --verbose
154            --
155            --gtest_filter="-ContaminatingUnitTest.*"
156            --gtest_throw_on_failure=1
157            --gtest_print_time=1
158            --gtest_output=xml:"${PROJECT_BINARY_DIR}/REPORT_${plugin}_${test}.xml")
159    SET_TESTS_PROPERTIES(check-${plugin}-${test} PROPERTIES LABELS "normal" )
160
161    ADD_TEST(
162            NAME
163            check-contaminating-${plugin}-${test}
164            COMMAND
165            $<TARGET_FILE:check-all>
166            --srcdir "${PROJECT_SOURCE_DIR}"
167            --testdir "$<TARGET_FILE_DIR:check-all>"
168            --libdir "$<TARGET_FILE_DIR:couchbase>"
169            --plugins ${plugin}
170            --tests ${test}
171            --verbose
172            --
173            --gtest_filter="ContaminatingUnitTest.*"
174            --gtest_throw_on_failure=1
175            --gtest_print_time=1
176            --gtest_output=xml:"${PROJECT_BINARY_DIR}/REPORT_${plugin}_${test}.xml")
177    SET_TESTS_PROPERTIES(check-contaminating-${plugin}-${test} PROPERTIES LABELS "contaminating" )
178ENDMACRO()
179
180# Since we need a plugin name, we'll use 'select'. However none of these
181# actually do any I/O
182DEFINE_MOCKTEST("select" "nonio-tests")
183DEFINE_MOCKTEST("select" "rdb-tests")
184DEFINE_MOCKTEST("select" "vbucket-tests")
185DEFINE_MOCKTEST("select" "mc-tests")
186DEFINE_MOCKTEST("select" "htparse-tests")
187
188
189DEFINE_MOCKTEST("select" "unit-tests")
190DEFINE_MOCKTEST("select" "sock-tests")
191IF(WIN32)
192    DEFINE_MOCKTEST("iocp" "unit-tests")
193    DEFINE_MOCKTEST("iocp" "sock-tests")
194ENDIF()
195IF(HAVE_LIBEVENT AND LCB_BUILD_LIBEVENT)
196    DEFINE_MOCKTEST("libevent" "unit-tests")
197    DEFINE_MOCKTEST("libevent" "sock-tests")
198ENDIF()
199IF(HAVE_LIBEV AND LCB_BUILD_LIBEV)
200    DEFINE_MOCKTEST("libev" "unit-tests")
201    DEFINE_MOCKTEST("libev" "sock-tests")
202ENDIF()
203IF(HAVE_LIBUV AND LCB_BUILD_LIBUV)
204    DEFINE_MOCKTEST("libuv" "unit-tests")
205    DEFINE_MOCKTEST("libuv" "sock-tests")
206ENDIF()
207