1#!/bin/sh
2
3# These variables are automatically filled in by the configure script.
4name="@PACKAGE_TARNAME@"
5version="@PACKAGE_VERSION@"
6
7show_usage()
8{
9  echo "Usage: gtest-config [OPTIONS...]"
10}
11
12show_help()
13{
14  show_usage
15  cat <<\EOF
16
17The `gtest-config' script provides access to the necessary compile and linking
18flags to connect with Google C++ Testing Framework, both in a build prior to
19installation, and on the system proper after installation. The installation
20overrides may be issued in combination with any other queries, but will only
21affect installation queries if called on a built but not installed gtest. The
22installation queries may not be issued with any other types of queries, and
23only one installation query may be made at a time. The version queries and
24compiler flag queries may be combined as desired but not mixed. Different
25version queries are always combined with logical "and" semantics, and only the
26last of any particular query is used while all previous ones ignored. All
27versions must be specified as a sequence of numbers separated by periods.
28Compiler flag queries output the union of the sets of flags when combined.
29
30 Examples:
31  gtest-config --min-version=1.0 || echo "Insufficient Google Test version."
32
33  g++ $(gtest-config --cppflags --cxxflags) -o foo.o -c foo.cpp
34  g++ $(gtest-config --ldflags --libs) -o foo foo.o
35
36  # When using a built but not installed Google Test:
37  g++ $(../../my_gtest_build/scripts/gtest-config ...) ...
38
39  # When using an installed Google Test, but with installation overrides:
40  export GTEST_PREFIX="/opt"
41  g++ $(gtest-config --libdir="/opt/lib64" ...) ...
42
43 Help:
44  --usage                    brief usage information
45  --help                     display this help message
46
47 Installation Overrides:
48  --prefix=<dir>             overrides the installation prefix
49  --exec-prefix=<dir>        overrides the executable installation prefix
50  --libdir=<dir>             overrides the library installation prefix
51  --includedir=<dir>         overrides the header file installation prefix
52
53 Installation Queries:
54  --prefix                   installation prefix
55  --exec-prefix              executable installation prefix
56  --libdir                   library installation directory
57  --includedir               header file installation directory
58  --version                  the version of the Google Test installation
59
60 Version Queries:
61  --min-version=VERSION      return 0 if the version is at least VERSION
62  --exact-version=VERSION    return 0 if the version is exactly VERSION
63  --max-version=VERSION      return 0 if the version is at most VERSION
64
65 Compilation Flag Queries:
66  --cppflags                 compile flags specific to the C-like preprocessors
67  --cxxflags                 compile flags appropriate for C++ programs
68  --ldflags                  linker flags
69  --libs                     libraries for linking
70
71EOF
72}
73
74# This function bounds our version with a min and a max. It uses some clever
75# POSIX-compliant variable expansion to portably do all the work in the shell
76# and avoid any dependency on a particular "sed" or "awk" implementation.
77# Notable is that it will only ever compare the first 3 components of versions.
78# Further components will be cleanly stripped off. All versions must be
79# unadorned, so "v1.0" will *not* work. The minimum version must be in $1, and
80# the max in $2. TODO(chandlerc@google.com): If this ever breaks, we should
81# investigate expanding this via autom4te from AS_VERSION_COMPARE rather than
82# continuing to maintain our own shell version.
83check_versions()
84{
85  major_version=${version%%.*}
86  minor_version="0"
87  point_version="0"
88  if test "${version#*.}" != "${version}"; then
89    minor_version=${version#*.}
90    minor_version=${minor_version%%.*}
91  fi
92  if test "${version#*.*.}" != "${version}"; then
93    point_version=${version#*.*.}
94    point_version=${point_version%%.*}
95  fi
96
97  min_version="$1"
98  min_major_version=${min_version%%.*}
99  min_minor_version="0"
100  min_point_version="0"
101  if test "${min_version#*.}" != "${min_version}"; then
102    min_minor_version=${min_version#*.}
103    min_minor_version=${min_minor_version%%.*}
104  fi
105  if test "${min_version#*.*.}" != "${min_version}"; then
106    min_point_version=${min_version#*.*.}
107    min_point_version=${min_point_version%%.*}
108  fi
109
110  max_version="$2"
111  max_major_version=${max_version%%.*}
112  max_minor_version="0"
113  max_point_version="0"
114  if test "${max_version#*.}" != "${max_version}"; then
115    max_minor_version=${max_version#*.}
116    max_minor_version=${max_minor_version%%.*}
117  fi
118  if test "${max_version#*.*.}" != "${max_version}"; then
119    max_point_version=${max_version#*.*.}
120    max_point_version=${max_point_version%%.*}
121  fi
122
123  test $(($major_version)) -lt $(($min_major_version)) && exit 1
124  if test $(($major_version)) -eq $(($min_major_version)); then
125    test $(($minor_version)) -lt $(($min_minor_version)) && exit 1
126    if test $(($minor_version)) -eq $(($min_minor_version)); then
127      test $(($point_version)) -lt $(($min_point_version)) && exit 1
128    fi
129  fi
130
131  test $(($major_version)) -gt $(($max_major_version)) && exit 1
132  if test $(($major_version)) -eq $(($max_major_version)); then
133    test $(($minor_version)) -gt $(($max_minor_version)) && exit 1
134    if test $(($minor_version)) -eq $(($max_minor_version)); then
135      test $(($point_version)) -gt $(($max_point_version)) && exit 1
136    fi
137  fi
138
139  exit 0
140}
141
142# Show the usage line when no arguments are specified.
143if test $# -eq 0; then
144  show_usage
145  exit 1
146fi
147
148while test $# -gt 0; do
149  case $1 in
150    --usage)          show_usage;         exit 0;;
151    --help)           show_help;          exit 0;;
152
153    # Installation overrides
154    --prefix=*)       GTEST_PREFIX=${1#--prefix=};;
155    --exec-prefix=*)  GTEST_EXEC_PREFIX=${1#--exec-prefix=};;
156    --libdir=*)       GTEST_LIBDIR=${1#--libdir=};;
157    --includedir=*)   GTEST_INCLUDEDIR=${1#--includedir=};;
158
159    # Installation queries
160    --prefix|--exec-prefix|--libdir|--includedir|--version)
161      if test -n "${do_query}"; then
162        show_usage
163        exit 1
164      fi
165      do_query=${1#--}
166      ;;
167
168    # Version checking
169    --min-version=*)
170      do_check_versions=yes
171      min_version=${1#--min-version=}
172      ;;
173    --max-version=*)
174      do_check_versions=yes
175      max_version=${1#--max-version=}
176      ;;
177    --exact-version=*)
178      do_check_versions=yes
179      exact_version=${1#--exact-version=}
180      ;;
181
182    # Compiler flag output
183    --cppflags)       echo_cppflags=yes;;
184    --cxxflags)       echo_cxxflags=yes;;
185    --ldflags)        echo_ldflags=yes;;
186    --libs)           echo_libs=yes;;
187
188    # Everything else is an error
189    *)                show_usage;         exit 1;;
190  esac
191  shift
192done
193
194# These have defaults filled in by the configure script but can also be
195# overridden by environment variables or command line parameters.
196prefix="${GTEST_PREFIX:-@prefix@}"
197exec_prefix="${GTEST_EXEC_PREFIX:-@exec_prefix@}"
198libdir="${GTEST_LIBDIR:-@libdir@}"
199includedir="${GTEST_INCLUDEDIR:-@includedir@}"
200
201# We try and detect if our binary is not located at its installed location. If
202# it's not, we provide variables pointing to the source and build tree rather
203# than to the install tree. This allows building against a just-built gtest
204# rather than an installed gtest.
205bindir="@bindir@"
206this_relative_bindir=`dirname $0`
207this_bindir=`cd ${this_relative_bindir}; pwd -P`
208if test "${this_bindir}" = "${this_bindir%${bindir}}"; then
209  # The path to the script doesn't end in the bindir sequence from Autoconf,
210  # assume that we are in a build tree.
211  build_dir=`dirname ${this_bindir}`
212  src_dir=`cd ${this_bindir}/@top_srcdir@; pwd -P`
213
214  # TODO(chandlerc@google.com): This is a dangerous dependency on libtool, we
215  # should work to remove it, and/or remove libtool altogether, replacing it
216  # with direct references to the library and a link path.
217  gtest_libs="${build_dir}/lib/libgtest.la"
218  gtest_ldflags=""
219
220  # We provide hooks to include from either the source or build dir, where the
221  # build dir is always preferred. This will potentially allow us to write
222  # build rules for generated headers and have them automatically be preferred
223  # over provided versions.
224  gtest_cppflags="-I${build_dir}/include -I${src_dir}/include"
225  gtest_cxxflags=""
226else
227  # We're using an installed gtest, although it may be staged under some
228  # prefix. Assume (as our own libraries do) that we can resolve the prefix,
229  # and are present in the dynamic link paths.
230  gtest_ldflags="-L${libdir}"
231  gtest_libs="-l${name}"
232  gtest_cppflags="-I${includedir}"
233  gtest_cxxflags=""
234fi
235
236# Do an installation query if requested.
237if test -n "$do_query"; then
238  case $do_query in
239    prefix)           echo $prefix;       exit 0;;
240    exec-prefix)      echo $exec_prefix;  exit 0;;
241    libdir)           echo $libdir;       exit 0;;
242    includedir)       echo $includedir;   exit 0;;
243    version)          echo $version;      exit 0;;
244    *)                show_usage;         exit 1;;
245  esac
246fi
247
248# Do a version check if requested.
249if test "$do_check_versions" = "yes"; then
250  # Make sure we didn't receive a bad combination of parameters.
251  test "$echo_cppflags" = "yes" && show_usage && exit 1
252  test "$echo_cxxflags" = "yes" && show_usage && exit 1
253  test "$echo_ldflags" = "yes"  && show_usage && exit 1
254  test "$echo_libs" = "yes"     && show_usage && exit 1
255
256  if test "$exact_version" != ""; then
257    check_versions $exact_version $exact_version
258    # unreachable
259  else
260    check_versions ${min_version:-0.0.0} ${max_version:-9999.9999.9999}
261    # unreachable
262  fi
263fi
264
265# Do the output in the correct order so that these can be used in-line of
266# a compiler invocation.
267output=""
268test "$echo_cppflags" = "yes" && output="$output $gtest_cppflags"
269test "$echo_cxxflags" = "yes" && output="$output $gtest_cxxflags"
270test "$echo_ldflags" = "yes"  && output="$output $gtest_ldflags"
271test "$echo_libs" = "yes"     && output="$output $gtest_libs"
272echo $output
273
274exit 0
275