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