summaryrefslogtreecommitdiffstats
path: root/scripts/gtest-config.in
blob: 50b18c9a43bd07a1ffe1aa4e165deea2e1093600 (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
#!/bin/sh

# These variables are automatically filled in by the configure script.
prefix="@prefix@"
exec_prefix="@exec_prefix@"
libdir="@libdir@"
includedir="@includedir@"
name="@PACKAGE_TARNAME@"
version="@PACKAGE_VERSION@"

gtest_ldflags="-L${libdir}"
gtest_libs="-l${name}"
gtest_cppflags="-I${includedir}"
gtest_cxxflags=""

show_usage()
{
  cat <<EOF
Usage: gtest-config [OPTIONS...]
EOF
}

show_help()
{
  show_usage
  cat <<EOF

The \`gtest-config' script provides access to the necessary compile and linking
flags to connect with Google C++ Testing framework. The installation queries
may only be issued one at a time, and may not be issued with any other types of
queries. The version queries and compiler flag queries may be combined as
desired but not mixed. Different version queries are always combined with "and"
logical semantics, and only the last of any particular query is used and all
previous ones ignored. All versions must be specified as a sequence of numbers
separated by periods. Compiler flag queries output the union of the sets of
flags when combined.

 Examples:
  gtest-config --min-version=1.0 || echo "Insufficient Google Test version."
  
  gcc \$(gtest-config --cppflags --cxxflags) -o foo.o -c foo.cpp
  gcc \$(gtest-config --ldflags --libs) -o foo foo.o

 Help:
  --usage                    brief usage information
  --help                     display this help message

 Installation Queries:
  --prefix                   installation prefix
  --exec-prefix              executable installation prefix
  --libdir                   library installation directory
  --includedir               header file installation directory
  --version                  the version of the INC installation

 Version Queries:
  --min-version=VERSION      return 0 if the version is at least VERSION
  --exact-version=VERSION    return 0 if the version is exactly VERSION
  --max-version=VERSION      return 0 if the version is at most VERSION

 Compilation Flag Queries:
  --cppflags                 compile flags specific to the C-like preprocessors
  --cxxflags                 compile flags appropriate for C++ programs
  --ldflags                  linker flags
  --libs                     libraries for linking

EOF
}

# This function bounds our version with a min and a max. It uses some clever
# POSIX-compliant variable expansion to portably do all the work in the shell
# and avoid any dependency on a particular "sed" implementation. Notable is
# that it will only ever compare the first 3 components of versions. Further
# components will be cleanly stripped off. All versions must be unadorned, so
# "v1.0" will *not* work. The minimum version must be in $1, and the max in
# $2.
check_versions()
{
  major_version=${version%%.*}
  minor_version="0"
  point_version="0"
  if test "${version#*.}" != "${version}"; then
    minor_version=${version#*.}
    minor_version=${minor_version%%.*}
  fi
  if test "${version#*.*.}" != "${version}"; then
    point_version=${version#*.*.}
    point_version=${point_version%%.*}
  fi

  min_version="$1"
  min_major_version=${min_version%%.*}
  min_minor_version="0"
  min_point_version="0"
  if test "${min_version#*.}" != "${min_version}"; then
    min_minor_version=${min_version#*.}
    min_minor_version=${min_minor_version%%.*}
  fi
  if test "${min_version#*.*.}" != "${min_version}"; then
    min_point_version=${min_version#*.*.}
    min_point_version=${min_point_version%%.*}
  fi

  max_version="$2"
  max_major_version=${max_version%%.*}
  max_minor_version="0"
  max_point_version="0"
  if test "${max_version#*.}" != "${max_version}"; then
    max_minor_version=${max_version#*.}
    max_minor_version=${max_minor_version%%.*}
  fi
  if test "${max_version#*.*.}" != "${max_version}"; then
    max_point_version=${max_version#*.*.}
    max_point_version=${max_point_version%%.*}
  fi

  test $(($major_version)) -lt $(($min_major_version)) && exit 1
  if test $(($major_version)) -eq $(($min_major_version)); then
    test $(($minor_version)) -lt $(($min_minor_version)) && exit 1
    if test $(($minor_version)) -eq $(($min_minor_version)); then
      test $(($point_version)) -lt $(($min_point_version)) && exit 1
    fi
  fi

  test $(($major_version)) -gt $(($max_major_version)) && exit 1
  if test $(($major_version)) -eq $(($max_major_version)); then
    test $(($minor_version)) -gt $(($max_minor_version)) && exit 1
    if test $(($minor_version)) -eq $(($max_minor_version)); then
      test $(($point_version)) -gt $(($max_point_version)) && exit 1
    fi
  fi

  exit 0
}

# Show the usage line when no arguments are specified.
if test $# -eq 0; then
  show_usage
  exit 1
fi

while test $# -gt 0; do
  case $1 in
    --usage)        show_usage;         exit 0;;
    --help)         show_help;          exit 0;;
    --prefix)       echo $prefix;       exit 0;;
    --exec-prefix)  echo $exec_prefix;  exit 0;;
    --libdir)       echo $libdir;       exit 0;;
    --includedir)   echo $includedir;   exit 0;;
    --version)      echo $version;      exit 0;;
    --min-version=*)
      do_check_versions=yes
      min_version=${1#--min-version=}
      ;;
    --max-version=*)
      do_check_versions=yes
      max_version=${1#--max-version=}
      ;;
    --exact-version=*)
      do_check_versions=yes
      exact_version=${1#--exact-version=}
      ;;
    --cppflags)     echo_cppflags=yes;;
    --cxxflags)     echo_cxxflags=yes;;
    --ldflags)      echo_ldflags=yes;;
    --libs)         echo_libs=yes;;

    # Everything else is an error
    *)              show_usage;         exit 1;;
  esac
  shift
done

# Do a version check if requested.
if test "$do_check_versions" = "yes"; then
  # Make sure we didn't receive a bad combination of parameters.
  test "$echo_cppflags" = "yes" && show_usage && exit 1
  test "$echo_cxxflags" = "yes" && show_usage && exit 1
  test "$echo_ldflags" = "yes"  && show_usage && exit 1
  test "$echo_libs" = "yes"     && show_usage && exit 1

  if test "$exact_version" != ""; then
    check_versions $exact_version $exact_version
    # unreachable
  else
    check_versions ${min_version:-0.0.0} ${max_version:-9999.9999.9999}
    # unreachable
  fi
fi

# Do the output in the correct order so that these can be used in-line of
# a compiler invocation.
output=""
test "$echo_cppflags" = "yes" && output="$output $gtest_cppflags"
test "$echo_cxxflags" = "yes" && output="$output $gtest_cxxflags"
test "$echo_ldflags" = "yes"  && output="$output $gtest_ldflags"
test "$echo_libs" = "yes"     && output="$output $gtest_libs"
echo $output

exit 0