126fa459cSmrg#!/usr/bin/env bash
226fa459cSmrg
326fa459cSmrg# Autotools-style (./configure) wrapper for CMake
426fa459cSmrg# <https://github.com/nemequ/configure-cmake>
526fa459cSmrg#
626fa459cSmrg#   *** IMPORTANT ***
726fa459cSmrg#
826fa459cSmrg#   You must include the GNUInstallDirs module (which comes with
926fa459cSmrg#   CMake) in your project.  Just put "include (GNUInstallDirs)" in
1026fa459cSmrg#   you CMakeLists.txt and you should be good.
1126fa459cSmrg#
1226fa459cSmrg# This script was originally written for Squash
1326fa459cSmrg# <https://quixdb.github.io/squash/> by Evan Nemerson
1426fa459cSmrg# <evan@nemerson.com>, but has been spun off into a separate
1526fa459cSmrg# repository.  Please feel free to copy it into your own repository,
1626fa459cSmrg# though I would appreciate it if you would post improvements, bugs,
1726fa459cSmrg# feature requests, etc. to the issue tracker at
1826fa459cSmrg# <https://github.com/nemequ/configure-cmake/issues>.
1926fa459cSmrg#
2026fa459cSmrg# To the extent possible under law, the author(s) hereby waive all
2126fa459cSmrg# copyright and related or neighboring rights to this work.  For
2226fa459cSmrg# details, see <https://creativecommons.org/publicdomain/zero/1.0/>
2326fa459cSmrg
2426fa459cSmrgTOP_SRCDIR="$(dirname $0)"
2526fa459cSmrg
2626fa459cSmrgif [ "${CMAKE_CMD}" = "" ]; then
2726fa459cSmrg    CMAKE_CMD="cmake"
2826fa459cSmrgfi
2926fa459cSmrg
3026fa459cSmrgBUILD_TYPE="Debug"
3126fa459cSmrgPREFIX=/usr/local
3226fa459cSmrgLIBDIR=
3326fa459cSmrgCMAKE_ARGS=
3426fa459cSmrg
3526fa459cSmrgif [ -e "${TOP_SRCDIR}/scripts/.configure-custom.sh" ]; then
3626fa459cSmrg    . "${TOP_SRCDIR}/scripts/.configure-custom.sh"
3726fa459cSmrgfi
3826fa459cSmrg
3926fa459cSmrgquote() {
4026fa459cSmrg    echo "$1" | sed -e "s|'|'\\\\''|g; 1s/^/'/; \$s/\$/'/"
4126fa459cSmrg}
4226fa459cSmrg
4326fa459cSmrgextract_var_string() {
4426fa459cSmrg    VAR_NAME=$1
4526fa459cSmrg    VAR_NAME=$(echo $1 | sed -e 's/[ \t]*$//')
4626fa459cSmrg    if [ "x$2" != "x" ]; then
4726fa459cSmrg        VAR_VALUE=$2
4826fa459cSmrg    else
4926fa459cSmrg        VAR_VALUE=yes
5026fa459cSmrg    fi
5126fa459cSmrg
5226fa459cSmrg    if [ "x$3" != "x" ]; then
5326fa459cSmrg        VAR_UC_NAME=$3
5426fa459cSmrg    else
5526fa459cSmrg        VAR_UC_NAME=$(echo "$1" | tr '[:lower:]' '[:upper:]' | tr -c '[:alnum:]' '_' | sed 's/_$//g')
5626fa459cSmrg    fi
5726fa459cSmrg}
5826fa459cSmrg
5926fa459cSmrgset_config_var() {
6026fa459cSmrg    is_with=n
6126fa459cSmrg    case "$1" in
6226fa459cSmrg        "--enable-"*)
6326fa459cSmrg            name="${1#--enable-}"
6426fa459cSmrg            cfg="${ENABLE_VARS}"
6526fa459cSmrg            ;;
6626fa459cSmrg        "--disable-"*)
6726fa459cSmrg            name="${1#--disable-}";
6826fa459cSmrg            cfg="${DISABLE_VARS}";
6926fa459cSmrg            ;;
7026fa459cSmrg        "--with-"*)
7126fa459cSmrg            # IFS="=" read -ra WITHARGS <<< "${1}"
7226fa459cSmrg            name="${1#--with-}"
7326fa459cSmrg            cfg="${WITH_VARS}"
7426fa459cSmrg            is_with=y
7526fa459cSmrg            ;;
7626fa459cSmrg    esac
7726fa459cSmrg
7826fa459cSmrg    found=n
7926fa459cSmrg    for varstring in $cfg; do
8026fa459cSmrg        extract_var_string $(echo "${varstring}" | tr '|' ' ')
8126fa459cSmrg        if [ "x$VAR_NAME" = "x$name" ]; then
8226fa459cSmrg            found=y
8326fa459cSmrg            break;
8426fa459cSmrg        fi
8526fa459cSmrg    done
8626fa459cSmrg
8726fa459cSmrg    if [ "$found" = "y" ]; then
8826fa459cSmrg        if [ "x$is_with" = "xy" ]; then
8926fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -D${VAR_UC_NAME}=$(quote "$2")"
9026fa459cSmrg        else
9126fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -D${VAR_UC_NAME}=$(quote "${VAR_VALUE}")"
9226fa459cSmrg        fi
9326fa459cSmrg    else
9426fa459cSmrg        echo "Unknown parameter: ${1}"
9526fa459cSmrg        exit 1
9626fa459cSmrg    fi
9726fa459cSmrg}
9826fa459cSmrg
9926fa459cSmrgprefix_to_offset() {
10026fa459cSmrg    expr $(echo "${1}" | awk '{ print length }') + 1
10126fa459cSmrg}
10226fa459cSmrg
10326fa459cSmrgprint_help() {
10426fa459cSmrg    cat <<EOF >&2
10526fa459cSmrg  -h, --help              display this help and exit
10626fa459cSmrg  --disable-debug         disable debugging mode
10726fa459cSmrg  --pass-thru             pass remaining arguments through to CMake
10826fa459cSmrg
10926fa459cSmrg  --prefix=PREFIX         install architecture-independent files in PREFIX
11026fa459cSmrg                          [$PREFIX]
11126fa459cSmrg  --bindir=DIR            user executables [PREFIX/bin]
11226fa459cSmrg  --sbindir=DIR           system admin executables [PREFIX/sbin]
11326fa459cSmrg  --libexecdir=DIR        program executables [PREFIX/libexec]
11426fa459cSmrg  --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]
11526fa459cSmrg  --sharedstatedir=DIR    modifiable architecture-independent data [PREFIX/com]
11626fa459cSmrg  --localstatedir=DIR     modifiable single-machine data [PREFIX/var]
11726fa459cSmrg  --libdir=DIR            object code libraries [PREFIX/lib]
11826fa459cSmrg  --includedir=DIR        C header files [PREFIX/include]
11926fa459cSmrg  --oldincludedir=DIR     C header files for non-gcc [/usr/include]
12026fa459cSmrg  --datarootdir=DIR       read-only arch.-independent data root [PREFIX/share]
12126fa459cSmrg  --datadir=DIR           read-only architecture-independent data [DATAROOTDIR]
12226fa459cSmrg  --infodir=DIR           info documentation [DATAROOTDIR/info]
12326fa459cSmrg  --localedir=DIR         locale-dependent data [DATAROOTDIR/locale]
12426fa459cSmrg  --mandir=DIR            man documentation [DATAROOTDIR/man]
12526fa459cSmrg  --docdir=DIR            documentation root [DATAROOTDIR/doc/PROJECT_NAME]
12626fa459cSmrgEOF
12726fa459cSmrg
12826fa459cSmrg    first=y
12926fa459cSmrg    for varstring in ${ENABLE_VARS}; do
13026fa459cSmrg        if [ $first = 'y' ]; then
13126fa459cSmrg            echo ""
13226fa459cSmrg            first=n
13326fa459cSmrg        fi
13426fa459cSmrg        extract_var_string $(echo "${varstring}" | tr '|' ' ')
13526fa459cSmrg        var_doc_name="ENABLE_${VAR_UC_NAME}_DOC"
13626fa459cSmrg        eval "docstring=\$$var_doc_name"
13726fa459cSmrg        if [ "x${docstring}" = "x" ]; then
13826fa459cSmrg            printf "  --enable-%-14s enable %s support\n" "${VAR_NAME}" "$(echo -n "${VAR_NAME}" | tr '-' ' ')"
13926fa459cSmrg        else
14026fa459cSmrg            printf "  --enable-%-14s %s\n" "${VAR_NAME}" "$docstring"
14126fa459cSmrg        fi
14226fa459cSmrg    done
14326fa459cSmrg
14426fa459cSmrg    first=y
14526fa459cSmrg    for varstring in ${DISABLE_VARS}; do
14626fa459cSmrg        if [ $first = 'y' ]; then
14726fa459cSmrg            echo ""
14826fa459cSmrg            first=n
14926fa459cSmrg        fi
15026fa459cSmrg        extract_var_string $(echo "${varstring}" | tr '|' ' ')
15126fa459cSmrg        var_doc_name="DISABLE_${VAR_UC_NAME}_DOC"
15226fa459cSmrg        eval "docstring=\$$var_doc_name"
15326fa459cSmrg        if [ "x${docstring}" = "x" ]; then
15426fa459cSmrg            printf "  --disable-%-13s disable %s support\n" "${VAR_NAME}" "$(echo -n "${VAR_NAME}" | tr '-' ' ')"
15526fa459cSmrg        else
15626fa459cSmrg            printf "  --disable-%-13s %s\n" "${VAR_NAME}" "$docstring"
15726fa459cSmrg        fi
15826fa459cSmrg    done
15926fa459cSmrg
16026fa459cSmrg    first=y
16126fa459cSmrg    for varstring in ${WITH_VARS}; do
16226fa459cSmrg        if [ $first = 'y' ]; then
16326fa459cSmrg            echo ""
16426fa459cSmrg            first=n
16526fa459cSmrg        fi
16626fa459cSmrg        extract_var_string $(echo "${varstring}" | tr '|' ' ')
16726fa459cSmrg        var_doc_name="WITH_${VAR_UC_NAME}_DOC"
16826fa459cSmrg        eval "docstring=\$$var_doc_name"
16926fa459cSmrg        paraminfo="${VAR_NAME}=${VAR_VALUE}"
17026fa459cSmrg        if [ "x${docstring}" = "x" ]; then
17126fa459cSmrg            printf "  --with-%-16s enable %s support\n" "$paraminfo" "$(echo -n "${VAR_NAME}" | tr '-' ' ')"
17226fa459cSmrg        else
17326fa459cSmrg            printf "  --with-%-16s %s\n" "$paraminfo" "$docstring"
17426fa459cSmrg        fi
17526fa459cSmrg    done
17626fa459cSmrg
17726fa459cSmrg    exit 0
17826fa459cSmrg}
17926fa459cSmrg
18026fa459cSmrgwhile [ $# != 0 ]; do
18126fa459cSmrg    case "$1" in
18226fa459cSmrg        "--prefix="*)
18326fa459cSmrg            PREFIX="${1#*=}";;
18426fa459cSmrg        "--prefix")
18526fa459cSmrg            PREFIX="${2}"; shift;;
18626fa459cSmrg        "--bindir="*)
18726fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_BINDIR=$(quote "${1#*=}")";;
18826fa459cSmrg        "--bindir")
18926fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_BINDIR=$(quote "$2")"; shift;;
19026fa459cSmrg        "--sbindir="*)
19126fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SBINDIR=$(quote "${1#*=}")";;
19226fa459cSmrg        "--sbindir")
19326fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SBINDIR=$(quote "$2")"; shift;;
19426fa459cSmrg        "--libexecdir="*)
19526fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LIBEXECDIR=$(quote "${1#*=}")";;
19626fa459cSmrg        "--libexecdir")
19726fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LIBEXECDIR=$(quote "$2")"; shift;;
19826fa459cSmrg        "--sysconfdir="*)
19926fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SYSCONFDIR=$(quote "${1#*=}")";;
20026fa459cSmrg        "--sysconfdir")
20126fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SYSCONFDIR=$(quote "$2")"; shift;;
20226fa459cSmrg        "--sharedstatedir="*)
20326fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SHAREDSTATEDIR=$(quote "${1#*=}")";;
20426fa459cSmrg        "--sharedstatedir")
20526fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_SHAREDSTATEDIR=$(quote "$2")"; shift;;
20626fa459cSmrg        "--localstatedir="*)
20726fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LOCALSTATEDIR=$(quote "${1#*=}")";;
20826fa459cSmrg        "--localstatedir")
20926fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LOCALSTATEDIR=$(quote "$2")"; shift;;
21026fa459cSmrg        "--libdir="*)
21126fa459cSmrg            LIBDIR="${1#*=}";;
21226fa459cSmrg        "--libdir")
21326fa459cSmrg            LIBDIR="${2}"; shift;;
21426fa459cSmrg        "--includedir="*)
21526fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_INCLUDEDIR=$(quote "${1#*=}")";;
21626fa459cSmrg        "--includedir")
21726fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_INCLUDEDIR=$(quote "$2")"; shift;;
21826fa459cSmrg        "--oldincludedir="*)
21926fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_OLDINCLUDEDIR=$(quote "${1#*=}")";;
22026fa459cSmrg        "--oldincludedir")
22126fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_OLDINCLUDEDIR=$(quote "$2")"; shift;;
22226fa459cSmrg        "--datarootdir="*)
22326fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DATAROOTDIR=$(quote "${1#*=}")";;
22426fa459cSmrg        "--datarootdir")
22526fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DATAROOTDIR=$(quote "$2")"; shift;;
22626fa459cSmrg        "--datadir="*)
22726fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DATADIR=$(quote "${1#*=}")";;
22826fa459cSmrg        "--datadir")
22926fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DATADIR=$(quote "$2")"; shift;;
23026fa459cSmrg        "--infodir="*)
23126fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_INFODIR=$(quote "${1#*=}")";;
23226fa459cSmrg        "--infodir")
23326fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_INFODIR=$(quote "$2")"; shift;;
23426fa459cSmrg        "--localedir="*)
23526fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LOCALEDIR=$(quote "${1#*=}")";;
23626fa459cSmrg        "--localedir")
23726fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_LOCALEDIR=$(quote "$2")"; shift;;
23826fa459cSmrg        "--mandir="*)
23926fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_MANDIR=$(quote "${1#*=}")";;
24026fa459cSmrg        "--mandir")
24126fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_MANDIR=$(quote "$2")"; shift;;
24226fa459cSmrg        "--docdir="*)
24326fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DOCDIR=$(quote "${1#*=}")";;
24426fa459cSmrg        "--docdir")
24526fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_DOCDIR=$(quote "$2")"; shift;;
24626fa459cSmrg
24726fa459cSmrg        "CC="*)
24826fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_C_COMPILER=$(quote "${1#*=}")";;
24926fa459cSmrg        "CXX="*)
25026fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_CXX_COMPILER=$(quote "${1#*=}")";;
25126fa459cSmrg        "CFLAGS="*)
25226fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_C_FLAGS=$(quote "${1#*=}")";;
25326fa459cSmrg        "CXXFLAGS="*)
25426fa459cSmrg            CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_CXX_FLAGS=$(quote "${1#*=}")";;
25526fa459cSmrg        "LDFLAGS="*)
25626fa459cSmrg            LDFLAGS="$LDFLAGS ${1#*=}";;
25726fa459cSmrg
25826fa459cSmrg        "--help")
25926fa459cSmrg            print_help;;
26026fa459cSmrg        "-h")
26126fa459cSmrg            print_help;;
26226fa459cSmrg
26326fa459cSmrg        # This flag is the only one which may be a bit surprising to
26426fa459cSmrg        # people.  Autotools always builds with debugging symbols enabled
26526fa459cSmrg        # (AFAIK), but for cmake you have to do -DCMAKE_BUILD_TYPE=Debug.
26626fa459cSmrg        # Unfortunately this can change other things as well, so although
26726fa459cSmrg        # I realize there is no --disable-debug flag I thought it would be
26826fa459cSmrg        # prudent to support one here.
26926fa459cSmrg        "--disable-debug")
27026fa459cSmrg            BUILD_TYPE="Release";;
27126fa459cSmrg
27226fa459cSmrg        "--pass-thru")
27326fa459cSmrg            shift;
27426fa459cSmrg            while [ $# != 0 ]; do
27526fa459cSmrg                CMAKE_ARGS="$CMAKE_ARGS $(quote "${1}")";
27626fa459cSmrg                shift;
27726fa459cSmrg            done;;
27826fa459cSmrg
27926fa459cSmrg        "--enable-"*)
28026fa459cSmrg            set_config_var "$1"
28126fa459cSmrg            ;;
28226fa459cSmrg
28326fa459cSmrg        "--disable-"*)
28426fa459cSmrg            set_config_var "$1"
28526fa459cSmrg            ;;
28626fa459cSmrg
28726fa459cSmrg        "--with-"*)
28826fa459cSmrg            name=$(echo "${1#--with-}" | awk '{split($1,v,"="); print v[1]}')
28926fa459cSmrg            case "${1}" in
29026fa459cSmrg                "--with-${name}="*)
29126fa459cSmrg                    set_config_var "--with-${name}" "${1#--with-${name}=}";;
29226fa459cSmrg                "--with-${name}")
29326fa459cSmrg                    set_config_var "$1" "$2";
29426fa459cSmrg                    shift;;
29526fa459cSmrg            esac
29626fa459cSmrg            ;;
29726fa459cSmrg
29826fa459cSmrg        *)
29926fa459cSmrg            echo "$0: error: unrecognized option: \`$1'" >&2
30026fa459cSmrg            echo "Try \`$0 --help' for more information" >&2
30126fa459cSmrg            exit -1
30226fa459cSmrg    esac;
30326fa459cSmrg    shift
30426fa459cSmrgdone
30526fa459cSmrg
30626fa459cSmrgif [ "x${LIBDIR}" = "x" ]; then
30726fa459cSmrg    LIBDIR="${PREFIX}/lib"
30826fa459cSmrgfi
30926fa459cSmrg
31026fa459cSmrg# Unlike CFLAGS/CXXFLAGS/CC/CXX, LDFLAGS isn't handled by CMake, so we
31126fa459cSmrg# need to parse it here.
31226fa459cSmrgif [ "x${LDFLAGS}" != "x" ]; then
31326fa459cSmrg    for varname in EXE MODULE SHARED STATIC; do
31426fa459cSmrg        CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_${varname}_LINKER_FLAGS=$(quote "$LDFLAGS")"
31526fa459cSmrg    done
31626fa459cSmrgfi
31726fa459cSmrg
31826fa459cSmrgeval "${CMAKE_CMD}" "${TOP_SRCDIR}" -DCMAKE_BUILD_TYPE="${BUILD_TYPE}" -DCMAKE_INSTALL_PREFIX="${PREFIX}" -DCMAKE_INSTALL_LIBDIR="${LIBDIR}" ${CMAKE_ARGS}
319