3 # Autotools-style (./configure) wrapper for CMake
4 # <https://github.com/nemequ/configure-cmake>
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.
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>.
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/>
24 TOP_SRCDIR="$(dirname $0)"
25 CMAKE_CMD="cmake ${TOP_SRCDIR}"
29 DATA_DIR=${PREFIX}/share
33 if [ -e "${TOP_SRCDIR}/.configure-custom.sh" ]; then
34 . "${TOP_SRCDIR}/.configure-custom.sh"
38 echo "$1" | sed -e "s|'|'\\\\''|g; 1s/^/'/; \$s/\$/'/"
41 extract_var_string() {
43 VAR_NAME=$(echo $1 | sed -e 's/[ \t]*$//')
44 if [ "x$2" != "x" ]; then
50 if [ "x$3" != "x" ]; then
53 VAR_UC_NAME=$(echo "$1" | tr '[:lower:]' '[:upper:]' | tr -c '[:alnum:]' '_' | sed 's/_$//g')
65 name="${1#--disable-}";
66 cfg="${DISABLE_VARS}";
69 # IFS="=" read -ra WITHARGS <<< "${1}"
77 for varstring in $cfg; do
78 extract_var_string $(echo "${varstring}" | tr '|' ' ')
79 if [ "x$VAR_NAME" = "x$name" ]; then
85 if [ "$found" = "y" ]; then
86 if [ "x$is_with" = "xy" ]; then
87 CMAKE_ARGS="$CMAKE_ARGS -D${VAR_UC_NAME}=$(quote "$2")"
89 CMAKE_ARGS="$CMAKE_ARGS -D${VAR_UC_NAME}=$(quote "${VAR_VALUE}")"
92 echo "Unknown parameter: ${1}"
98 expr $(echo "${1}" | awk '{ print length }') + 1
103 -h, --help display this help and exit
104 --disable-debug disable debugging mode
105 --pass-thru pass remaining arguments through to CMake
106 --prefix=PREFIX install architecture-independent files in PREFIX
108 --bindir=DIR user executables [PREFIX/bin]
109 --sbindir=DIR system admin executables [PREFIX/sbin]
110 --libexecdir=DIR program executables [PREFIX/libexec]
111 --sysconfdir=DIR read-only single-machine data [PREFIX/etc]
112 --sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
113 --localstatedir=DIR modifiable single-machine data [PREFIX/var]
114 --libdir=DIR object code libraries [PREFIX/lib]
115 --includedir=DIR C header files [PREFIX/include]
116 --oldincludedir=DIR C header files for non-gcc [/usr/include]
117 --datarootdir=DIR read-only arch.-independent data root [PREFIX/share]
118 --datadir=DIR read-only architecture-independent data [DATAROOTDIR]
119 --infodir=DIR info documentation [DATAROOTDIR/info]
120 --localedir=DIR locale-dependent data [DATAROOTDIR/locale]
121 --mandir=DIR man documentation [DATAROOTDIR/man]
122 --docdir=DIR documentation root [DATAROOTDIR/doc/PROJECT_NAME]
126 for varstring in ${ENABLE_VARS}; do
127 if [ $first = 'y' ]; then
131 extract_var_string $(echo "${varstring}" | tr '|' ' ')
132 var_doc_name="ENABLE_${VAR_UC_NAME}_DOC"
133 eval "docstring=\$$var_doc_name"
134 if [ "x${docstring}" = "x" ]; then
135 printf " --enable-%-14s enable %s support\n" "${VAR_NAME}" "$(echo -n "${VAR_NAME}" | tr '-' ' ')"
137 printf " --enable-%-14s %s\n" "${VAR_NAME}" "$docstring"
142 for varstring in ${DISABLE_VARS}; do
143 if [ $first = 'y' ]; then
147 extract_var_string $(echo "${varstring}" | tr '|' ' ')
148 var_doc_name="DISABLE_${VAR_UC_NAME}_DOC"
149 eval "docstring=\$$var_doc_name"
150 if [ "x${docstring}" = "x" ]; then
151 printf " --disable-%-13s disable %s support\n" "${VAR_NAME}" "$(echo -n "${VAR_NAME}" | tr '-' ' ')"
153 printf " --disable-%-13s %s\n" "${VAR_NAME}" "$docstring"
158 for varstring in ${WITH_VARS}; do
159 if [ $first = 'y' ]; then
163 extract_var_string $(echo "${varstring}" | tr '|' ' ')
164 var_doc_name="WITH_${VAR_UC_NAME}_DOC"
165 eval "docstring=\$$var_doc_name"
166 paraminfo="${VAR_NAME}=${VAR_VALUE}"
167 if [ "x${docstring}" = "x" ]; then
168 printf " --with-%-16s enable %s support\n" "$paraminfo" "$(echo -n "${VAR_NAME}" | tr '-' ' ')"
170 printf " --with-%-16s %s\n" "$paraminfo" "$docstring"
177 while [ $# != 0 ]; do
182 PREFIX="${2}"; shift;;
184 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_BINDIR=$(quote "${1#*=}")";;
186 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_BINDIR=$(quote "$2")"; shift;;
188 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SBINDIR=$(quote "${1#*=}")";;
190 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SBINDIR=$(quote "$2")"; shift;;
192 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LIBEXECDIR=$(quote "${1#*=}")";;
194 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LIBEXECDIR=$(quote "$2")"; shift;;
196 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SYSCONFDIR=$(quote "${1#*=}")";;
198 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SYSCONFDIR=$(quote "$2")"; shift;;
199 "--sharedstatedir="*)
200 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SHAREDSTATEDIR=$(quote "${1#*=}")";;
202 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SHAREDSTATEDIR=$(quote "$2")"; shift;;
204 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LOCALSTATEDIR=$(quote "${1#*=}")";;
206 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LOCALSTATEDIR=$(quote "$2")"; shift;;
210 LIBDIR="${2}"; shift;;
212 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_INCLUDEDIR=$(quote "${1#*=}")";;
214 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_INCLUDEDIR=$(quote "$2")"; shift;;
216 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_OLDINCLUDEDIR=$(quote "${1#*=}")";;
218 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_OLDINCLUDEDIR=$(quote "$2")"; shift;;
220 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DATAROOTDIR=$(quote "${1#*=}")";;
222 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DATAROOTDIR=$(quote "$2")"; shift;;
224 DATA_DIR="$(quote "${1#*=}")"; shift;;
226 DATA_DIR="$(quote "$2")"; shift;;
228 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_INFODIR=$(quote "${1#*=}")";;
230 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_INFODIR=$(quote "$2")"; shift;;
232 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LOCALEDIR=$(quote "${1#*=}")";;
234 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LOCALEDIR=$(quote "$2")"; shift;;
236 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_MANDIR=$(quote "${1#*=}")";;
238 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_MANDIR=$(quote "$2")"; shift;;
240 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DOCDIR=$(quote "${1#*=}")";;
242 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DOCDIR=$(quote "$2")"; shift;;
245 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_C_COMPILER=$(quote "${1#*=}")";;
247 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_CXX_COMPILER=$(quote "${1#*=}")";;
249 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_C_FLAGS=$(quote "${1#*=}")";;
251 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_CXX_FLAGS=$(quote "${1#*=}")";;
253 LDFLAGS="$LDFLAGS ${1#*=}";;
260 # This flag is the only one which may be a bit surprising to
261 # people. Autotools always builds with debugging symbols enabled
262 # (AFAIK), but for cmake you have to do -DCMAKE_BUILD_TYPE=Debug.
263 # Unfortunately this can change other things as well, so although
264 # I realize there is no --disable-debug flag I thought it would be
265 # prudent to support one here.
267 BUILD_TYPE="Release";;
271 while [ $# != 0 ]; do
272 CMAKE_ARGS="$CMAKE_ARGS $(quote "${1}")";
285 name=$(echo "${1#--with-}" | awk '{split($1,v,"="); print v[1]}')
288 set_config_var "--with-${name}" "${1#--with-${name}=}";;
290 set_config_var "$1" "$2";
296 echo "$0: error: unrecognized option: \`$1'" >&2
297 echo "Try \`$0 --help' for more information" >&2
303 if [ "x${LIBDIR}" = "x" ]; then
304 LIBDIR="${PREFIX}/lib"
307 # Unlike CFLAGS/CXXFLAGS/CC/CXX, LDFLAGS isn't handled by CMake, so we
308 # need to parse it here.
309 if [ "x${LDFLAGS}" != "x" ]; then
310 for varname in EXE MODULE SHARED STATIC; do
311 CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_${varname}_LINKER_FLAGS=$(quote "$LDFLAGS")"
315 eval "cmake -Bbuild -H${TOP_SRCDIR} -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=${PREFIX} -DCMAKE_INSTALL_DATADIR=${DATA_DIR} -DCMAKE_INSTALL_LIBDIR=${LIBDIR} ${CMAKE_ARGS}"