blob: 6dfb92c4e4616690538278f34597e10b6cee2cb7 [file] [log] [blame]
Eugene Kliuchnikov58144382016-12-20 17:45:40 +01001#!/usr/bin/env bash
2
3# Autotools-style (./configure) wrapper for CMake
4# <https://github.com/nemequ/configure-cmake>
5#
6# *** IMPORTANT ***
7#
8# You must include the GNUInstallDirs module (which comes with
9# CMake) in your project. Just put "include (GNUInstallDirs)" in
10# you CMakeLists.txt and you should be good.
11#
12# This script was originally written for Squash
13# <https://quixdb.github.io/squash/> by Evan Nemerson
14# <evan@nemerson.com>, but has been spun off into a separate
15# repository. Please feel free to copy it into your own repository,
16# though I would appreciate it if you would post improvements, bugs,
17# feature requests, etc. to the issue tracker at
18# <https://github.com/nemequ/configure-cmake/issues>.
19#
20# To the extent possible under law, the author(s) hereby waive all
21# copyright and related or neighboring rights to this work. For
22# details, see <https://creativecommons.org/publicdomain/zero/1.0/>
23
24TOP_SRCDIR="$(dirname $0)"
25
26if [ "${CMAKE_CMD}" = "" ]; then
27 CMAKE_CMD="cmake"
28fi
29
30BUILD_TYPE="Debug"
31PREFIX=/usr/local
32LIBDIR=
33CMAKE_ARGS=
34
Eugene Kliuchnikov6ece1d82017-04-23 14:07:08 +020035if [ -e "${TOP_SRCDIR}/scripts/.configure-custom.sh" ]; then
36 . "${TOP_SRCDIR}/scripts/.configure-custom.sh"
Eugene Kliuchnikov58144382016-12-20 17:45:40 +010037fi
38
39quote() {
40 echo "$1" | sed -e "s|'|'\\\\''|g; 1s/^/'/; \$s/\$/'/"
41}
42
43extract_var_string() {
44 VAR_NAME=$1
45 VAR_NAME=$(echo $1 | sed -e 's/[ \t]*$//')
46 if [ "x$2" != "x" ]; then
47 VAR_VALUE=$2
48 else
49 VAR_VALUE=yes
50 fi
51
52 if [ "x$3" != "x" ]; then
53 VAR_UC_NAME=$3
54 else
55 VAR_UC_NAME=$(echo "$1" | tr '[:lower:]' '[:upper:]' | tr -c '[:alnum:]' '_' | sed 's/_$//g')
56 fi
57}
58
59set_config_var() {
60 is_with=n
61 case "$1" in
62 "--enable-"*)
63 name="${1#--enable-}"
64 cfg="${ENABLE_VARS}"
65 ;;
66 "--disable-"*)
67 name="${1#--disable-}";
68 cfg="${DISABLE_VARS}";
69 ;;
70 "--with-"*)
71 # IFS="=" read -ra WITHARGS <<< "${1}"
72 name="${1#--with-}"
73 cfg="${WITH_VARS}"
74 is_with=y
75 ;;
76 esac
77
78 found=n
79 for varstring in $cfg; do
80 extract_var_string $(echo "${varstring}" | tr '|' ' ')
81 if [ "x$VAR_NAME" = "x$name" ]; then
82 found=y
83 break;
84 fi
85 done
86
87 if [ "$found" = "y" ]; then
88 if [ "x$is_with" = "xy" ]; then
89 CMAKE_ARGS="$CMAKE_ARGS -D${VAR_UC_NAME}=$(quote "$2")"
90 else
91 CMAKE_ARGS="$CMAKE_ARGS -D${VAR_UC_NAME}=$(quote "${VAR_VALUE}")"
92 fi
93 else
94 echo "Unknown parameter: ${1}"
95 exit 1
96 fi
97}
98
99prefix_to_offset() {
100 expr $(echo "${1}" | awk '{ print length }') + 1
101}
102
103print_help() {
104 cat <<EOF >&2
105 -h, --help display this help and exit
106 --disable-debug disable debugging mode
107 --pass-thru pass remaining arguments through to CMake
108
109 --prefix=PREFIX install architecture-independent files in PREFIX
110 [$PREFIX]
111 --bindir=DIR user executables [PREFIX/bin]
112 --sbindir=DIR system admin executables [PREFIX/sbin]
113 --libexecdir=DIR program executables [PREFIX/libexec]
114 --sysconfdir=DIR read-only single-machine data [PREFIX/etc]
115 --sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
116 --localstatedir=DIR modifiable single-machine data [PREFIX/var]
117 --libdir=DIR object code libraries [PREFIX/lib]
118 --includedir=DIR C header files [PREFIX/include]
119 --oldincludedir=DIR C header files for non-gcc [/usr/include]
120 --datarootdir=DIR read-only arch.-independent data root [PREFIX/share]
121 --datadir=DIR read-only architecture-independent data [DATAROOTDIR]
122 --infodir=DIR info documentation [DATAROOTDIR/info]
123 --localedir=DIR locale-dependent data [DATAROOTDIR/locale]
124 --mandir=DIR man documentation [DATAROOTDIR/man]
125 --docdir=DIR documentation root [DATAROOTDIR/doc/PROJECT_NAME]
126EOF
127
128 first=y
129 for varstring in ${ENABLE_VARS}; do
130 if [ $first = 'y' ]; then
131 echo ""
132 first=n
133 fi
134 extract_var_string $(echo "${varstring}" | tr '|' ' ')
135 var_doc_name="ENABLE_${VAR_UC_NAME}_DOC"
136 eval "docstring=\$$var_doc_name"
137 if [ "x${docstring}" = "x" ]; then
138 printf " --enable-%-14s enable %s support\n" "${VAR_NAME}" "$(echo -n "${VAR_NAME}" | tr '-' ' ')"
139 else
140 printf " --enable-%-14s %s\n" "${VAR_NAME}" "$docstring"
141 fi
142 done
143
144 first=y
145 for varstring in ${DISABLE_VARS}; do
146 if [ $first = 'y' ]; then
147 echo ""
148 first=n
149 fi
150 extract_var_string $(echo "${varstring}" | tr '|' ' ')
151 var_doc_name="DISABLE_${VAR_UC_NAME}_DOC"
152 eval "docstring=\$$var_doc_name"
153 if [ "x${docstring}" = "x" ]; then
154 printf " --disable-%-13s disable %s support\n" "${VAR_NAME}" "$(echo -n "${VAR_NAME}" | tr '-' ' ')"
155 else
156 printf " --disable-%-13s %s\n" "${VAR_NAME}" "$docstring"
157 fi
158 done
159
160 first=y
161 for varstring in ${WITH_VARS}; do
162 if [ $first = 'y' ]; then
163 echo ""
164 first=n
165 fi
166 extract_var_string $(echo "${varstring}" | tr '|' ' ')
167 var_doc_name="WITH_${VAR_UC_NAME}_DOC"
168 eval "docstring=\$$var_doc_name"
169 paraminfo="${VAR_NAME}=${VAR_VALUE}"
170 if [ "x${docstring}" = "x" ]; then
171 printf " --with-%-16s enable %s support\n" "$paraminfo" "$(echo -n "${VAR_NAME}" | tr '-' ' ')"
172 else
173 printf " --with-%-16s %s\n" "$paraminfo" "$docstring"
174 fi
175 done
176
177 exit 0
178}
179
180while [ $# != 0 ]; do
181 case "$1" in
182 "--prefix="*)
183 PREFIX="${1#*=}";;
184 "--prefix")
185 PREFIX="${2}"; shift;;
186 "--bindir="*)
187 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_BINDIR=$(quote "${1#*=}")";;
188 "--bindir")
189 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_BINDIR=$(quote "$2")"; shift;;
190 "--sbindir="*)
191 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SBINDIR=$(quote "${1#*=}")";;
192 "--sbindir")
193 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SBINDIR=$(quote "$2")"; shift;;
194 "--libexecdir="*)
195 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LIBEXECDIR=$(quote "${1#*=}")";;
196 "--libexecdir")
197 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LIBEXECDIR=$(quote "$2")"; shift;;
198 "--sysconfdir="*)
199 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SYSCONFDIR=$(quote "${1#*=}")";;
200 "--sysconfdir")
201 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SYSCONFDIR=$(quote "$2")"; shift;;
202 "--sharedstatedir="*)
203 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SHAREDSTATEDIR=$(quote "${1#*=}")";;
204 "--sharedstatedir")
205 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SHAREDSTATEDIR=$(quote "$2")"; shift;;
206 "--localstatedir="*)
207 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LOCALSTATEDIR=$(quote "${1#*=}")";;
208 "--localstatedir")
209 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LOCALSTATEDIR=$(quote "$2")"; shift;;
210 "--libdir="*)
211 LIBDIR="${1#*=}";;
212 "--libdir")
213 LIBDIR="${2}"; shift;;
214 "--includedir="*)
215 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_INCLUDEDIR=$(quote "${1#*=}")";;
216 "--includedir")
217 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_INCLUDEDIR=$(quote "$2")"; shift;;
218 "--oldincludedir="*)
219 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_OLDINCLUDEDIR=$(quote "${1#*=}")";;
220 "--oldincludedir")
221 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_OLDINCLUDEDIR=$(quote "$2")"; shift;;
222 "--datarootdir="*)
223 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DATAROOTDIR=$(quote "${1#*=}")";;
224 "--datarootdir")
225 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DATAROOTDIR=$(quote "$2")"; shift;;
226 "--datadir="*)
227 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DATADIR=$(quote "${1#*=}")";;
228 "--datadir")
229 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DATADIR=$(quote "$2")"; shift;;
230 "--infodir="*)
231 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_INFODIR=$(quote "${1#*=}")";;
232 "--infodir")
233 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_INFODIR=$(quote "$2")"; shift;;
234 "--localedir="*)
235 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LOCALEDIR=$(quote "${1#*=}")";;
236 "--localedir")
237 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LOCALEDIR=$(quote "$2")"; shift;;
238 "--mandir="*)
239 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_MANDIR=$(quote "${1#*=}")";;
240 "--mandir")
241 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_MANDIR=$(quote "$2")"; shift;;
242 "--docdir="*)
243 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DOCDIR=$(quote "${1#*=}")";;
244 "--docdir")
245 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DOCDIR=$(quote "$2")"; shift;;
246
247 "CC="*)
248 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_C_COMPILER=$(quote "${1#*=}")";;
249 "CXX="*)
250 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_CXX_COMPILER=$(quote "${1#*=}")";;
251 "CFLAGS="*)
252 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_C_FLAGS=$(quote "${1#*=}")";;
253 "CXXFLAGS="*)
254 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_CXX_FLAGS=$(quote "${1#*=}")";;
255 "LDFLAGS="*)
256 LDFLAGS="$LDFLAGS ${1#*=}";;
257
258 "--help")
259 print_help;;
260 "-h")
261 print_help;;
262
263 # This flag is the only one which may be a bit surprising to
264 # people. Autotools always builds with debugging symbols enabled
265 # (AFAIK), but for cmake you have to do -DCMAKE_BUILD_TYPE=Debug.
266 # Unfortunately this can change other things as well, so although
267 # I realize there is no --disable-debug flag I thought it would be
268 # prudent to support one here.
269 "--disable-debug")
270 BUILD_TYPE="Release";;
271
272 "--pass-thru")
273 shift;
274 while [ $# != 0 ]; do
275 CMAKE_ARGS="$CMAKE_ARGS $(quote "${1}")";
276 shift;
277 done;;
278
279 "--enable-"*)
280 set_config_var "$1"
281 ;;
282
283 "--disable-"*)
284 set_config_var "$1"
285 ;;
286
287 "--with-"*)
288 name=$(echo "${1#--with-}" | awk '{split($1,v,"="); print v[1]}')
289 case "${1}" in
290 "--with-${name}="*)
291 set_config_var "--with-${name}" "${1#--with-${name}=}";;
292 "--with-${name}")
293 set_config_var "$1" "$2";
294 shift;;
295 esac
296 ;;
297
298 *)
299 echo "$0: error: unrecognized option: \`$1'" >&2
300 echo "Try \`$0 --help' for more information" >&2
301 exit -1
302 esac;
303 shift
304done
305
306if [ "x${LIBDIR}" = "x" ]; then
307 LIBDIR="${PREFIX}/lib"
308fi
309
310# Unlike CFLAGS/CXXFLAGS/CC/CXX, LDFLAGS isn't handled by CMake, so we
311# need to parse it here.
312if [ "x${LDFLAGS}" != "x" ]; then
313 for varname in EXE MODULE SHARED STATIC; do
314 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_${varname}_LINKER_FLAGS=$(quote "$LDFLAGS")"
315 done
316fi
317
318eval "${CMAKE_CMD}" "${TOP_SRCDIR}" -DCMAKE_BUILD_TYPE="${BUILD_TYPE}" -DCMAKE_INSTALL_PREFIX="${PREFIX}" -DCMAKE_INSTALL_LIBDIR="${LIBDIR}" ${CMAKE_ARGS}