summaryrefslogtreecommitdiffstats
path: root/m4macros/ax_gtest.m4
blob: 479cc1896b97a1988f747ce04053a3ab4d7ecc50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
AC_DEFUN([AX_ISC_GTEST], [

enable_gtest="no"
GTEST_INCLUDES=

AC_ARG_WITH([gtest-source],
            [AS_HELP_STRING([--with-gtest-source[[=PATH]]],
                            [location of the Googletest source])],
            [enable_gtest="yes" ; GTEST_SOURCE="$withval"])

AC_ARG_WITH([gtest],
            [AS_HELP_STRING([--with-gtest[[=PATH]]],
                            [specify a path to gtest header files (PATH/include) and library (PATH/lib)])],
        [gtest_path="$withval"; enable_gtest="yes"], [gtest_path="no"])

AC_ARG_WITH([lcov],
            [AS_HELP_STRING([--with-lcov[[=PROGRAM]]],
                            [enable gtest and coverage target using the specified lcov])],
                            [lcov="$withval"],
                            [lcov="no"])

USE_LCOV="no"
if test "$lcov" != "no"; then
        # force gtest if not set
        if test "$enable_gtest" = "no"; then
#               AC_MSG_ERROR("lcov needs gtest for test coverage report")
                AC_MSG_NOTICE([gtest support is now enabled, because used by coverage tests])
                enable_gtest="yes"
        fi
        if test "$lcov" != "yes"; then
                LCOV=$lcov
        else
                AC_PATH_PROG([LCOV], [lcov])
        fi
        if test -x "${LCOV}"; then
                USE_LCOV="yes"
        else
                AC_MSG_ERROR([Cannot find lcov.])
        fi
        # is genhtml always in the same directory?
        GENHTML=`echo "$LCOV" | ${SED} s/lcov$/genhtml/`
        if test ! -x $GENHTML; then
                AC_MSG_ERROR([genhtml not found, needed for lcov])
        fi
        # GCC specific?
        CXXFLAGS="$CXXFLAGS -fprofile-arcs -ftest-coverage"
        LIBS=" $LIBS -lgcov"
        AC_SUBST(CPPFLAGS)
        AC_SUBST(LIBS)
        AC_SUBST(LCOV)
        AC_SUBST(GENHTML)
fi
AC_SUBST(USE_LCOV)

#
# Check availability of gtest, which will be used for unit tests.
#
GTEST_LDFLAGS=
GTEST_LDADD=
DISTCHECK_GTEST_CONFIGURE_FLAG=
GTEST_VERSION="unknown"

if test "x$enable_gtest" = "xyes" ; then

    DISTCHECK_GTEST_CONFIGURE_FLAG="--with-gtest=$gtest_path"

    if test -n "$with_gtest_source" ; then

        AC_MSG_CHECKING([for gtest source])

        if test "x$GTEST_SOURCE" = "xyes" ; then

            # If not specified, try some common paths.
            GTEST_SOURCE=
            for d in /usr/src/googletest /usr/src/gtest /usr/local /usr/pkg /opt /opt/local ; do
                cmakelists="$d/CMakeLists.txt"
                if test ! -d "$d/src" -a -d "$d/googletest"; then
                    d="$d/googletest"
                fi
                if test -f "$d/src/gtest-all.cc" -a -f "$d/src/gtest_main.cc"; then
                    GTEST_SOURCE="$d"
                    AC_MSG_RESULT([$GTEST_SOURCE])
                    break
                fi
            done
            if test -z "$GTEST_SOURCE" ; then
                AC_MSG_ERROR([no gtest sources found])
            fi
        else
            # Trim trailing slashes.
            GTEST_SOURCE=$(echo "$GTEST_SOURCE" | ${SED} 's:/*$::')
            cmakelists="$GTEST_SOURCE/CMakeLists.txt"
            if test ! -d "$GTEST_SOURCE/src" -a -d "$GTEST_SOURCE/googletest"; then
                GTEST_SOURCE="$GTEST_SOURCE/googletest"
            fi
            if test -f "$GTEST_SOURCE/src/gtest-all.cc" -a -f "$GTEST_SOURCE/src/gtest_main.cc"; then
                AC_MSG_RESULT([$GTEST_SOURCE])
            else
                AC_MSG_ERROR([no gtest source at $GTEST_SOURCE])
            fi
        fi
        have_gtest_source=yes

        GTEST_LDADD="\$(top_builddir)/ext/gtest/libgtest.a"
        DISTCHECK_GTEST_CONFIGURE_FLAG="--with-gtest-source=$GTEST_SOURCE"
        GTEST_INCLUDES="-I$GTEST_SOURCE -I$GTEST_SOURCE/include"
        gtest_src_basename="$(basename $GTEST_SOURCE)"

        # Versions starting from 1.8.0 are put in the googletest directory. If the basename
        # returns googletest string, we need to cut it off and try basename again.
        if test "$gtest_src_basename" = "googletest"; then
            gtest_src_parent_dir=${GTEST_SOURCE%"/googletest"}
            if test -f "$gtest_src_parent_dir/CMakeLists.txt" ; then
                cmakelists="$gtest_src_parent_dir/CMakeLists.txt"
            fi
            gtest_src_basename=$(basename "$gtest_src_parent_dir")
        fi
        gtest_src_basename="${gtest_src_basename#googletest-release-}"
        gtest_src_basename="${gtest_src_basename#gtest-}"
        gtest_src_basename="${gtest_src_basename#googletest-}"

        gtest_version_candidate=$(echo "$gtest_src_basename" | grep -Eo '[[0-9]+\.[0-9]+\.[0-9]+]')

        if test -z "$gtest_version_candidate" ; then
            # If the GTEST_VERSION is still not correct semver, we need to determine googletest version in other way.
            # Let's try to extract it from CMake build script used by Google Test starting from version 1.8.0.
            if test -f "$cmakelists" ; then
                gtest_version_candidate=$(grep -F 'set(GOOGLETEST_VERSION' "$cmakelists" | grep -Eo '[[0-9]+\.[0-9]+\.[0-9]+]')
                if test -n "$gtest_version_candidate"; then
                    GTEST_VERSION="$gtest_version_candidate"
                fi
            fi
            if test "$GTEST_VERSION" = "unknown" ; then
                # Try to get googletest version from debian/ubuntu package
                AC_PATH_PROG(DPKG, dpkg)
                AC_PATH_PROG(DPKGQUERY, dpkg-query)
                if test -n "${DPKG}" -a -n "${DPKGQUERY}"; then
                    # Let's check if there is a googletest package owning files under given GTEST_SOURCE path
                    ${DPKG} -S "$GTEST_SOURCE" 2>/dev/null | grep googletest >/dev/null 2>&1
                    if test $? -eq 0; then
                        GTEST_VERSION="$(${DPKGQUERY} --showformat='${Version}' --show googletest | cut -d'-' -f1)"
                    fi
                fi
            fi
        else
            GTEST_VERSION="$gtest_version_candidate"
        fi
    fi

    if test "$gtest_path" != "no" ; then
        if test "$gtest_path" != "yes"; then
            GTEST_PATHS=$gtest_path
            AC_PATH_PROG([GTEST_CONFIG], [gtest-config], [], [${gtest_path}/bin])
        else
            AC_PATH_PROG([GTEST_CONFIG], [gtest-config])
        fi
        if test -x "${GTEST_CONFIG}" ; then :
            # using cppflags instead of cxxflags
            GTEST_INCLUDES=`${GTEST_CONFIG} --cppflags`
            GTEST_LDFLAGS=`${GTEST_CONFIG} --ldflags`
            GTEST_LDADD=`${GTEST_CONFIG} --libs`
            GTEST_VERSION=`${GTEST_CONFIG} --version`
            GTEST_FOUND="true"
        else
            if test -z "${GTEST_PATHS}" ; then
                GTEST_PATHS="/usr /usr/local"
            fi
            GTEST_FOUND="false"
        fi
        if test "${GTEST_FOUND}" != "true"; then
            AC_MSG_CHECKING([for gtest lib path])
            GTEST_FOUND="false"
            for dir in $GTEST_PATHS; do
                if test -f "$dir/include/gtest/gtest.h"; then
                    if test -f "$dir/lib/libgtest.a" || \
                       test -f "$dir/lib/libgtest.so"; then
                        GTEST_INCLUDES="-I$dir/include"
                        GTEST_LDFLAGS="-L$dir/lib"
                        GTEST_LDADD="-lgtest"
                        GTEST_FOUND="true"
                        AC_MSG_RESULT([$dir/lib])
                        if test -f "$dir/lib/pkgconfig/gtest.pc" ; then
                            AX_FIND_LIBRARY([gtest], ["$dir/lib/pkgconfig/gtest.pc"])
                            if "${LIBRARY_FOUND}"; then
                              GTEST_VERSION="${LIBRARY_VERSION}"
                            fi
                        fi
                        break
                    elif test -f "$dir/lib/$dumpmachine/libgtest.a" || \
                         test -f "$dir/lib/$dumpmachine/libgtest.so"; then
                        GTEST_INCLUDES="-I$dir/include"
                        # check also multiarch dir in debian/ubuntu distributions
                        GTEST_LDFLAGS="-L$dir/lib/$dumpmachine"
                        GTEST_LDADD="-lgtest"
                        GTEST_FOUND="true"
                        AC_MSG_RESULT([$dir/lib/$dumpmachine])
                        if test -f "$dir/lib/$dumpmachine/pkgconfig/gtest.pc" ; then
                            AX_FIND_LIBRARY([gtest], ["$dir/lib/$dumpmachine/pkgconfig/gtest.pc"])
                            if "${LIBRARY_FOUND}"; then
                              GTEST_VERSION="${LIBRARY_VERSION}"
                            fi
                        fi
                        break
                    else
                        AC_MSG_WARN([Found Google Test include but not the library in $dir.])
                    fi
                fi
            done
        fi
        if test "${GTEST_FOUND}" != "true"; then
            AC_MSG_ERROR([Cannot find gtest in: $GTEST_PATHS])
        fi

    fi
fi
AM_CONDITIONAL(HAVE_GTEST, test $enable_gtest != "no")
AM_CONDITIONAL(HAVE_GTEST_SOURCE, test "X$have_gtest_source" = "Xyes")
AC_SUBST(DISTCHECK_GTEST_CONFIGURE_FLAG)
AC_SUBST(GTEST_INCLUDES)
AC_SUBST(GTEST_LDFLAGS)
AC_SUBST(GTEST_LDADD)
AC_SUBST(GTEST_SOURCE)

])