configure.sh: Cleanup argument parsing. Thanks, Timothy!
[mes.git] / configure.sh
1 #! /bin/sh
2
3 # GNU Mes --- Maxwell Equations of Software
4 # Copyright © 2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
5 #
6 # This file is part of GNU Mes.
7 #
8 # GNU Mes is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or (at
11 # your option) any later version.
12 #
13 # GNU Mes is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with GNU Mes.  If not, see <http://www.gnu.org/licenses/>.
20
21 set -e
22
23 VERSION=0.20
24 srcdir=${srcdir-$(dirname $0)}
25 . ${srcdest}build-aux/trace.sh
26
27 # parse arguments
28 while [ $# -gt 0 ]; do
29     case $1 in
30         (--with-courage)
31             courageous=true
32             ;;
33         (--with-system-libc)
34             mes_libc=system
35             ;;
36         (--build=*)
37             build=${1#--build=}
38             ;;
39         (--host=*)
40             host=${1#--host=}
41             ;;
42         (--prefix=*)
43             prefix=${1#--prefix=}
44             ;;
45         (--program-prefix=*)
46             program_prefix=${1#--program-prefix=}
47             ;;
48     esac
49     shift
50 done
51
52 prefix=${prefix-/usr/local}
53 mes_libc=${mes_libc-mes}
54 courageous=${courageous-false}
55 host=${host-$build}
56
57 AR=${AR-$(command -v ar)} || true
58 BASH=${BASH-$(command -v bash)}
59 BLOOD_ELF=${BLOOD_ELF-$(command -v blood-elf)}
60 CC=${CC-$(command -v gcc)} || true
61 DIFF=${DIFF-$(command -v diff || echo $PWD/pre-inst-env diff.scm)}
62 GUILD=${GUILD-$(command -v guild)} || true
63 GUILE_TOOLS=${GUILE_TOOLS-$(command -v guile-tools)} || true
64 if test ! "$GUILD"; then
65     if test "$GUILE_TOOLS"; then
66         GUILD=$GUILE_TOOLS
67     else
68         GUILD=true
69     fi
70 fi
71 GUILE=${GUILE-$(command -v guile)} || true
72 HEX2=${HEX2-$(command -v hex2)}
73 M1=${M1-$(command -v M1)}
74 MES_FOR_BUILD=${MES_FOR_BUILD-$(command -v mes || command -v guile || echo mes)}
75 GIT=${GIT-$(command -v git)} || true
76 PERL=${PERL-$(command -v perl)} || true
77 SHELL=${BASH-$SHELL}
78
79 MES_SEED=${MES_SEED-../mes-seed}
80
81 if test "$srcdir" = .; then
82     top_builddir=.
83 else
84     srcdest=${srcdest}
85     top_builddir=$PWD
86 fi
87 abs_top_srcdir=${abs_top_srcdir-$(cd ${srcdir} && pwd)}
88 abs_top_builddir=$PWD
89
90 if test -z "$GUILE" -o "$GUILE" = true; then
91     GUILE_EFFECTIVE_VERSION=${GUILE_EFFECTIVE_VERSION-2.2}
92 else
93     GUILE_EFFECTIVE_VERSION=${GUILE_EFFECTIVE_VERSION-$(guile -c '(display (effective-version))')}
94 fi
95 bindir=$(eval echo ${bindir-$prefix/bin})
96 datadir=$(eval echo ${datadir-$prefix/share})
97 docdir=$(eval echo ${docdir-$datadir/doc/mes-$VERSION})
98 infodir=$(eval echo ${infodir-$datadir/info})
99 includedir=$(eval echo ${includedir-$prefix/include})
100 libdir=$(eval echo ${libdir-$prefix/lib})
101 pkgdatadir=$(eval echo ${pkgdatadir-$datadir/mes})
102 mandir=$(eval echo ${mandir-$datadir/man})
103 guile_site_dir=$(eval echo ${guile_site_dir-$prefix/share/guile/site/$GUILE_EFFECTIVE_VERSION})
104 guile_site_ccache_dir=$(eval echo ${guile_site_ccache_dir-$prefix/lib/guile/$GUILE_EFFECTIVE_VERSION/site-ccache})
105
106 subst () {
107     echo "creating $2"
108     sed \
109     -e s,"@PACKAGE@,$PACKAGE,"\
110     -e s,"@PACKAGE_NAME@,$PACKAGE_NAME,"\
111     -e s,"@PACKAGE_BUGREPORT@,$PACKAGE_BUGREPORT,"\
112     -e s,"@VERSION@,$VERSION,"\
113     -e s,"@build@,$build,"\
114     -e s,"@host@,$host,"\
115     -e s,"@compiler@,$compiler,"\
116     -e s,"@courageous@,$courageous,"\
117     -e s,"@mes_bits@,$mes_bits,"\
118     -e s,"@mes_kernel@,$mes_kernel,"\
119     -e s,"@mes_cpu@,$mes_cpu,"\
120     -e s,"@mes_libc@,$mes_libc,"\
121     -e s,"@mes_system@,$mes_system,"\
122     -e s,"@abs_top_srcdir@,$abs_top_srcdir,"\
123     -e s,"@abs_top_builddir@,$abs_top_builddir,"\
124     -e s,"@top_builddir@,$top_builddir,"\
125     -e s,"@srcdest@,$srcdest,"\
126     -e s,"@srcdir@,$srcdir,"\
127     -e s,"@prefix@,$prefix,"\
128     -e s,"@program_prefix@,$program_prefix,"\
129     -e s,"@bindir@,$bindir,"\
130     -e s,"@datadir@,$datadir,"\
131     -e s,"@docdir@,$docdir,"\
132     -e s,"@guile_site_dir@,$guile_site_dir,"\
133     -e s,"@guile_site_ccache_dir@,$guile_site_ccache_dir,"\
134     -e s,"@infodir@,$infodir,"\
135     -e s,"@includedir@,$includedir,"\
136     -e s,"@libdir@,$libdir,"\
137     -e s,"@mandir@,$mandir,"\
138     -e s,"@pkgdatadir@,$pkgdatadir,"\
139     -e s,"@sysconfdir@,$sysconfdir,"\
140     -e s,"@GUILE_EFFECTIVE_VERSION@,$GUILE_EFFECTIVE_VERSION,"\
141     -e s,"@V@,$V,"\
142     -e s,"@AR@,$AR,"\
143     -e s,"@BASH@,$BASH,"\
144     -e s,"@BLOOD_ELF@,$BLOOD_ELF,"\
145     -e s,"@CC@,$CC,"\
146     -e s,"@DIFF@,$DIFF,"\
147     -e s,"@GIT@,$GIT,"\
148     -e s,"@GUILD@,$GUILD,"\
149     -e s,"@GUILE@,$GUILE,"\
150     -e s,"@PERL@,$PERL,"\
151     -e s,"@CFLAGS@,$CFLAGS,"\
152     -e s,"@HEX2@,$HEX2,"\
153     -e s,"@HEX2FLAGS@,$HEX2FLAGS,"\
154     -e s,"@M1@,$M1,"\
155     -e s,"@M1FLAGS@,$M1FLAGS,"\
156     -e s,"@MES_FOR_BUILD@,$MES_FOR_BUILD,"\
157     -e s,"@MES_SEED@,$MES_SEED,"\
158     -e s,"@MES_SEED@,$MES_SEED,"\
159     -e s,"@SHELL@,$SHELL,"\
160     $1 > $2
161 }
162
163 host=${host-$($CC -dumpmachine 2>/dev/null)}
164 if test -z "$host$host_type"; then
165     mes_cpu=${arch-$(get_machine || uname -m)}
166 else
167     mes_cpu=${host%%-*}
168 fi
169 if test "$mes_cpu" = i386\
170         || test "$mes_cpu" = i486\
171         || test "$mes_cpu" = i586\
172         || test "$mes_cpu" = i686; then
173     mes_cpu=x86
174 fi
175 if test "$mes_cpu" = armv4\
176         || test "$arch" = armv7l; then
177     mes_cpu=arm
178 fi
179 if test "$mes_cpu" = amd64; then
180     mes_cpu=x86_64
181 fi
182
183 case "$host" in
184     *linux-gnu|*linux)
185         mes_kernel=linux;;
186     *gnu)
187         mes_kernel=gnu;;
188     *)
189         mes_kernel=linux;;
190 esac
191
192 case "$mes_cpu" in
193     x86_64)
194         mes_bits=64;;
195     *)
196         mes_bits=32;;
197 esac
198 #
199 if $CC --version | grep gcc; then #2>/dev/null; then
200     compiler=gcc
201 elif $CC --version | grep tcc; then #2>/dev/null; then
202     compiler=gcc
203 else
204     compiler=mescc
205 fi
206
207 mes_system=$mes_cpu-$mes_kernel-mes
208
209 subst ${srcdest}build-aux/GNUmakefile.in GNUmakefile
210 subst ${srcdest}build-aux/config.sh.in config.sh
211 subst ${srcdest}build-aux/bootstrap.sh.in bootstrap.sh
212 chmod +x bootstrap.sh
213 subst ${srcdest}build-aux/build.sh.in build.sh
214 chmod +x build.sh
215 subst ${srcdest}build-aux/check.sh.in check.sh
216 chmod +x check.sh
217 subst ${srcdest}build-aux/install.sh.in install.sh
218 chmod +x install.sh
219 subst ${srcdest}build-aux/pre-inst-env.in pre-inst-env
220 chmod +x pre-inst-env
221 subst ${srcdest}scripts/mesar.in scripts/mesar
222 chmod +x scripts/mesar
223 subst ${srcdest}scripts/mescc.scm.in scripts/mescc.scm
224 chmod +x scripts/mescc.scm
225 subst ${srcdest}scripts/mescc.in scripts/mescc
226 chmod +x scripts/mescc
227 subst ${srcdest}build-aux/uninstall.sh.in uninstall.sh
228 chmod +x uninstall.sh
229
230 mkdir -p include/mes
231 rm -f include/mes/config.h
232 if test $mes_libc = system; then
233     cat >> include/mes/config.h <<EOF
234 #define SYSTEM_LIBC 1
235 EOF
236 else
237     cat >> include/mes/config.h <<EOF
238 #undef SYSTEM_LIBC
239 EOF
240 fi
241 cat >> include/mes/config.h <<EOF
242 #define MES_VERSION "$VERSION"
243 #define MES_PKGDATADIR "$pkgdatadir"
244 EOF
245
246 cat <<EOF
247 GNU Mes is configured for
248    compiler: $compiler
249    cpu:      $mes_cpu
250    bits:     $mes_bits
251    libc:     $mes_libc
252    kernel:   $mes_kernel
253    system:   $mes_system
254
255 Run:
256   ./build.sh      to build mes
257   ./check.sh      to check mes
258   ./install.sh    to install mes
259 EOF