1#! /bin/sh
2
3# configure
4# Copyright 1984-2017 Cisco Systems, Inc.
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18machs=""; last=""; sep0=""; sep1=""; sep2=""; sep3=""; sep4=" and ";
19for fn in boot/*/scheme.boot ; do
20  machs=$machs$sep0$last
21  last=`echo $fn | sed -e 's/boot\/\(.*\)\/scheme.boot/\1/'`
22  sep0=$sep1; sep1=", "; sep2=$sep3; sep3=$sep4; sep4=", and "
23done
24machs=$machs$sep2$last
25
26m=""
27w=""
28threads=no
29temproot=""
30help=no
31gzipmanpages=yes
32installowner=""
33installgroup=""
34installbin=""
35installlib=""
36installman=""
37installschemename="scheme"
38installpetitename="petite"
39installscriptname="scheme-script"
40disablex11=no
41disablecurses=no
42: ${CC:="gcc"}
43: ${CPPFLAGS:=""}
44: ${CFLAGS:=""}
45: ${LD:="ld"}
46: ${LDFLAGS:=""}
47: ${AR:="ar"}
48: ${ARFLAGS:="rc"}
49: ${RANLIB:="ranlib"}
50: ${WINDRES:="windres"}
51zlibInc=-I../zlib
52LZ4Inc=-I../lz4/lib
53zlibDep=../zlib/libz.a
54LZ4Dep=../lz4/lib/liblz4.a
55zlibLib=../zlib/libz.a
56LZ4Lib=../lz4/lib/liblz4.a
57zlibHeaderDep="../zlib/zconf.h ../zlib/zlib.h"
58LZ4HeaderDep="../lz4/lib/lz4.h ../lz4/lib/lz4frame.h"
59Kernel=KernelO
60installkerneltarget=installkernelobj
61installzlibtarget=
62installlz4target=
63
64# On WSL, set OS to "Windows_NT" to create a Windows
65# build instead of a Linux (on Windows) build:
66if [ "$OS" = "Windows_NT" ] ; then
67    CONFIG_UNAME="CYGWIN_NT-"
68else
69    CONFIG_UNAME=`uname`
70fi
71
72case "${CONFIG_UNAME}" in
73  Linux)
74    if uname -a | egrep 'i386|i686|amd64|athlon|x86_64' > /dev/null 2>&1 ; then
75      m32=i3le
76      m64=a6le
77      tm32=ti3le
78      tm64=ta6le
79    elif uname -a | grep -i power > /dev/null 2>&1 ; then
80      m32=ppc32le
81      m64=""
82      tm32=tppc32le
83      tm64=""
84    fi
85    installprefix=/usr
86    installmansuffix=share/man
87    ;;
88  QNX)
89    if uname -a | egrep 'x86' > /dev/null 2>&1 ; then
90      m32=i3qnx
91      tm32=ti3qnx
92    fi
93    installprefix=/usr/local
94    installmansuffix=man
95    ;;
96  FreeBSD|DragonFly)
97    if uname -a | egrep 'i386|i686|amd64|athlon|x86_64' > /dev/null 2>&1 ; then
98      m32=i3fb
99      m64=a6fb
100      tm32=ti3fb
101      tm64=ta6fb
102    fi
103    installprefix=/usr/local
104    installmansuffix=man
105    ;;
106  OpenBSD)
107    if uname -a | egrep 'i386|i686|amd64|athlon|x86_64' > /dev/null 2>&1 ; then
108      m32=i3ob
109      m64=a6ob
110      tm32=ti3ob
111      tm64=ta6ob
112    fi
113    installprefix=/usr/local
114    installmansuffix=man
115    ;;
116  NetBSD)
117    if uname -a | egrep 'i386|i686|amd64|athlon|x86_64' > /dev/null 2>&1 ; then
118      m32=i3nb
119      m64=a6nb
120      tm32=ti3nb
121      tm64=ta6nb
122    fi
123    installprefix=/usr
124    installmansuffix=share/man
125    gzipmanpages=no
126    ;;
127  Darwin)
128    if uname -a | egrep 'i386|i686|amd64|athlon|x86_64' > /dev/null 2>&1 ; then
129      m32=i3osx
130      m64=a6osx
131      tm32=ti3osx
132      tm64=ta6osx
133    fi
134    installprefix=/usr/local
135    installmansuffix=share/man
136    ;;
137  SunOS)
138    if uname -a | egrep 'i386|i686|amd64|athlon|x86_64' > /dev/null 2>&1 ; then
139      m32=i3s2
140      m64=a6s2
141      tm32=ti3s2
142      tm64=ta6s2
143      installprefix=/usr
144      installmansuffix=share/man
145      gzipmanpages=no
146    fi
147    ;;
148  CYGWIN_NT-*)
149    if uname -a | egrep 'i386|i686|amd64|athlon|x86_64' > /dev/null 2>&1 ; then
150      m32=i3nt
151      m64=a6nt
152      tm32=ti3nt
153      tm64=ta6nt
154      installprefix=/usr/local
155      installmansuffix=share/man
156    fi
157    ;;
158esac
159
160threads=""
161bits=""
162
163while [ $# != 0 ] ; do
164  case $1 in
165    -m=*)
166      m=`echo $1 | sed -e 's/^-m=//'`
167      ;;
168    --machine=*)
169      m=`echo $1 | sed -e 's/^--machine=//'`
170      ;;
171    --threads)
172      threads=yes
173      ;;
174    --64)
175      bits=64
176      ;;
177    --32)
178      bits=32
179      ;;
180    --installprefix=*)
181      installprefix=`echo $1 | sed -e 's/^--installprefix=//'`
182      ;;
183    --installlib=*)
184      installlib=`echo $1 | sed -e 's/^--installlib=//'`
185      ;;
186    --installbin=*)
187      installbin=`echo $1 | sed -e 's/^--installbin=//'`
188      ;;
189    --installman=*)
190      installman=`echo $1 | sed -e 's/^--installman=//'`
191      ;;
192    --installowner=*)
193      installowner=`echo $1 | sed -e 's/^--installowner=//'`
194      ;;
195    --installgroup=*)
196      installgroup=`echo $1 | sed -e 's/^--installgroup=//'`
197      ;;
198    --installschemename=*)
199      installschemename=`echo $1 | sed -e 's/^--installschemename=//'`
200      ;;
201    --installpetitename=*)
202      installpetitename=`echo $1 | sed -e 's/^--installpetitename=//'`
203      ;;
204    --installscriptname=*)
205      installscriptname=`echo $1 | sed -e 's/^--installscriptname=//'`
206      ;;
207    --toolprefix=*)
208      toolprefix=`echo $1 | sed -e 's/^--toolprefix=//'`
209      CC="${toolprefix}${CC}"
210      LD="${toolprefix}${LD}"
211      AR="${toolprefix}${AR}"
212      RANLIB="${toolprefix}${RANLIB}"
213      WINDRES="${toolprefix}${WINDRES}"
214      ;;
215    --gzip-man-pages)
216      gzipmanpages=yes
217      ;;
218    --nogzip-man-pages)
219      gzipmanpages=no
220      ;;
221    --temproot=*)
222      temproot=`echo $1 | sed -e 's/^--temproot=//'`
223      ;;
224    --workarea=*)
225      w=`echo $1 | sed -e 's/^--workarea=//'`
226      ;;
227    --help)
228      help=yes
229      ;;
230    --disable-x11)
231      disablex11=yes
232      ;;
233    --disable-curses)
234      disablecurses=yes
235      ;;
236    --libkernel)
237      Kernel=KernelLib
238      installkerneltarget=installkernellib
239      if [ "$zlibInc" != "" ]; then
240          installzlibtarget=installzlib
241      fi
242      if [ "$LZ4Inc" != "" ]; then
243          installlz4target=installlz4
244      fi
245      ;;
246    --kernelobj)
247      Kernel=KernelO
248      installkerneltarget=installkernelobj
249      installzlibtarget=
250      installlz4target=
251      ;;
252    CC=*)
253      CC=`echo $1 | sed -e 's/^CC=//'`
254      ;;
255    CPPFLAGS=*)
256      CPPFLAGS=`echo $1 | sed -e 's/^CPPFLAGS=//'`
257      ;;
258    CFLAGS=*)
259      CFLAGS=`echo $1 | sed -e 's/^CFLAGS=//'`
260      ;;
261    LD=*)
262      LD=`echo $1 | sed -e 's/^LD=//'`
263      ;;
264    LDFLAGS=*)
265      LDFLAGS=`echo $1 | sed -e 's/^LDFLAGS=//'`
266      ;;
267    AR=*)
268      AR=`echo $1 | sed -e 's/^AR=//'`
269      ;;
270    ARFLAGS=*)
271      ARFLAGS=`echo $1 | sed -e 's/^ARFLAGS=//'`
272      ;;
273    RANLIB=*)
274      RANLIB=`echo $1 | sed -e 's/^RANLIB=//'`
275      ;;
276    WINDRES=*)
277      WINDRES=`echo $1 | sed -e 's/^WINDRES=//'`
278      ;;
279    ZLIB=*)
280      zlibLib=`echo $1 | sed -e 's/^ZLIB=//'`
281      zlibInc=
282      zlibDep=
283      zlibHeaderDep=
284      installzlibtarget=
285      ;;
286    LZ4=*)
287      LZ4Lib=`echo $1 | sed -e 's/^LZ4=//'`
288      LZ4Inc=
289      LZ4Dep=
290      LZ4HeaderDep=
291      installlz4target=
292      ;;
293    *)
294      echo "option '$1' unrecognized or missing an argument; try $0 --help"
295      exit 1
296      ;;
297  esac
298  shift
299done
300
301if [ "$bits" = "" ] ; then
302  if uname -a | egrep 'amd64|x86_64' > /dev/null 2>&1 ; then
303    bits=64
304  else
305    bits=32
306  fi
307fi
308
309if [ "$threads" = "" ] ; then
310  threads=no
311fi
312
313if [ "$m" = "" ] ; then
314  if [ $bits = 64 ] ; then
315    if [ $threads = yes ] ; then m=$tm64 ; else m=$m64 ; fi
316  else
317    if [ $threads = yes ] ; then m=$tm32 ; else m=$m32 ; fi
318  fi
319fi
320
321if [ "$w" = "" ] ; then
322  w=$m
323fi
324
325if [ "$installbin" = "" ] ; then
326  installbin=$installprefix/bin
327fi
328
329if [ "$installlib" = "" ] ; then
330  installlib=$installprefix/lib
331fi
332
333if [ "$installman" = "" ] ; then
334  installman=$installprefix/$installmansuffix
335fi
336
337if [ "$disablex11" = "no" ] ; then
338  if [ $m = a6osx ] || [ $m = ta6osx ] ; then
339    if [ ! -d /opt/X11/include/ ] ; then
340      disablex11=yes
341    fi
342  fi
343fi
344
345if [ "$help" = "yes" ]; then
346  echo "Purpose:"
347  echo "  $0 determines the machine type and constructs a custom Makefile"
348  echo "  and Mf-install, taking into account the options below."
349  echo ""
350  echo "Options (defaults shown in parens):"
351  echo "  --machine=<machine type>          explicitly specify machine type ($m)"
352  echo "  -m=<machine type>                 same as --machine <machine type> ($m)"
353  echo "  --threads                         specify threaded version ($threads)"
354  echo "  --32|--64                         specify 32/64-bit version ($bits)"
355  echo "  --disable-x11                     disable X11 support"
356  echo "  --disable-curses                  disable [n]curses support"
357  echo "  --libkernel                       build libkernel.a instead of kernel.o"
358  echo "  --kernelobj                       build kernel.o (the default)"
359  echo "  --installprefix=<pathname>        final installation root ($installprefix)"
360  echo "  --installbin=<pathname>           bin directory ($installbin)"
361  echo "  --installlib=<pathname>           lib directory ($installlib)"
362  echo "  --installman=<pathname>           manpage directory ($installman)"
363  echo "  --temproot=<pathname>             staging root ($temproot)"
364  echo "  --installowner=<ownername>        install with owner ($installowner)"
365  echo "  --installgroup=<groupname>        install with group ($installgroup)"
366  echo "  --installschemename=<schemename>  install with group ($installschemename)"
367  echo "  --installpetitename=<petitename>  install with group ($installpetitename)"
368  echo "  --installscriptname=<scriptname>  install with group ($installscriptname)"
369  echo "  --toolprefix=<prefix>             prefix tool (compiler, linker, ...) names"
370  echo "  --[no]gzip-man-pages              compress manual pages ($gzipmanpages)"
371  echo "  --workarea=<pathname>             build directory ($w)"
372  echo "  CC=<C compiler>                   C compiler"
373  echo "  CPPFLAGS=<C preprocessor flags>   additional C preprocessor flags ($CPPFLAGS)"
374  echo "  CFLAGS=<C compiler flags>         additional C compiler flags ($CFLAGS)"
375  echo "  LD=<linker>                       linker"
376  echo "  LDFLAGS=<linker flags>            additional linker flags ($LDFLAGS)"
377  echo "  AR=<archiver>                     archiver"
378  echo "  ARFLAGS=<archiver flgs>           archiver flags"
379  echo "  RANLIB=<archive indexer>          archive indexer"
380  echo "  WINDRES=<resource compiler>       resource compiler"
381  echo "  ZLIB=<lib>                        link to <lib> instead of own zlib"
382  echo "  LZ4=<lib>                         link to <lib> instead of own LZ4"
383  echo ""
384  echo "Available machine types: $machs"
385  echo ""
386  echo "Examples:"
387  echo "  $0 --machine=i3le"
388  echo ""
389  echo "  set machine-type to i3le rather than to determined type"
390  echo ""
391  echo "  $0 --threads --installprefix=/usr/local"
392  echo ""
393  echo "  specify threaded version and set installation directory to /usr/local."
394  echo ""
395  echo "  $0 --installprefix=/usr/local --temproot=/tmp"
396  echo ""
397  echo "  declare the final destination to be /usr/local but staging area"
398  echo "  to be /tmp/usr/local.  Make will record the final destination in the"
399  echo "  installed manual pages but actually install the system and manual"
400  echo "  pages in the staging area."
401  echo ""
402  exit 0
403fi
404
405if [ "$m" = "" -o ! -f boot/$m/scheme.boot ] ; then
406  echo "no suitable machine type found"
407  echo "try rerunning as $0 -m=<machine type>"
408  echo "available machine types: $machs"
409  exit 1
410fi
411
412if [ -d '.git' ] && command -v git >/dev/null 2>&1 ; then
413  git submodule init && git submodule update || exit 1
414else
415  if [ ! -f 'nanopass/nanopass.ss' ] ; then
416    rmdir nanopass > /dev/null 2>&1
417    (curl  -L -o v1.9.1.tar.gz https://github.com/nanopass/nanopass-framework-scheme/archive/v1.9.1.tar.gz && tar -zxf v1.9.1.tar.gz && mv nanopass-framework-scheme-1.9.1 nanopass && rm v1.9.1.tar.gz) || exit 1
418  fi
419
420  if [ "${zlibDep}" != "" ] ; then
421    if [ ! -f 'zlib/configure' ] ; then
422      rmdir zlib > /dev/null 2>&1
423      (curl -L -o v1.2.11.tar.gz https://github.com/madler/zlib/archive/v1.2.11.tar.gz && tar -xzf v1.2.11.tar.gz && mv zlib-1.2.11 zlib && rm v1.2.11.tar.gz) || exit 1
424    fi
425  fi
426
427  if [ "${LZ4Dep}" != "" ] ; then
428    if [ ! -f 'lz4/lib/Makefile' ] ; then
429      rmdir lz4 > /dev/null 2>&1
430      (curl -L -o v1.9.2.tar.gz https://github.com/lz4/lz4/archive/v1.9.2.tar.gz && tar -xzf v1.9.2.tar.gz && mv lz4-1.9.2 lz4 && rm v1.9.2.tar.gz) || exit 1
431    fi
432  fi
433
434  if [ ! -f 'stex/Mf-stex' ] ; then
435    rmdir stex > /dev/null 2>&1
436    (curl -L -o v1.2.2.tar.gz https://github.com/dybvig/stex/archive/v1.2.2.tar.gz && tar -zxf v1.2.2.tar.gz && mv stex-1.2.2 stex && rm v1.2.2.tar.gz) || exit 1
437  fi
438fi
439
440./workarea $m $w
441
442sed -e 's/$(m)/'$m'/g'\
443    -e 's/$(workarea)/'$w'/g'\
444  makefiles/Makefile.in > Makefile
445
446sed -e 's/$(m)/'$m'/g'\
447    makefiles//Makefile-csug.in > csug/Makefile
448
449sed -e 's/$(m)/'$m'/g'\
450    makefiles//Makefile-release_notes.in > release_notes/Makefile
451
452cat makefiles/Makefile-workarea.in > $w/Makefile
453
454sed -e 's/$(m)/'$m'/g'\
455    -e 's/$(workarea)/'$w'/g'\
456    makefiles/Mf-boot.in > $w/Mf-boot
457
458sed -e "s;^m=none\$;m=$m;"\
459    -e "s;^InstallBin=.*\$;InstallBin=$installbin;"\
460    -e "s;^InstallLib=.*\$;InstallLib=$installlib;"\
461    -e "s;^InstallMan=.*\$;InstallMan=$installman/man1;"\
462    -e "s;^InstallOwner=.*\$;InstallOwner=$installowner;"\
463    -e "s;^InstallGroup=.*\$;InstallGroup=$installgroup;"\
464    -e "s;^TempRoot=.*;TempRoot=$temproot;"\
465    -e "s;^GzipManPages=.*$;GzipManPages=$gzipmanpages;"\
466    -e "s;^InstallSchemeName=.*$;InstallSchemeName=$installschemename;"\
467    -e "s;^InstallPetiteName=.*$;InstallPetiteName=$installpetitename;"\
468    -e "s;^InstallScriptName=.*$;InstallScriptName=$installscriptname;"\
469    -e "s;^InstallKernelTarget=.*$;InstallKernelTarget=$installkerneltarget;"\
470    -e "s;^InstallZlibTarget=.*$;InstallZlibTarget=$installzlibtarget;"\
471    -e "s;^InstallLZ4Target=.*$;InstallLZ4Target=$installlz4target;"\
472    makefiles/Mf-install.in > $w/Mf-install
473
474cat > $w/c/config.h << END
475#define SCHEME_SCRIPT "$installscriptname"
476#ifndef WIN32
477#define DEFAULT_HEAP_PATH "$installlib/csv%v/%m"
478#endif
479END
480
481if [ "$disablex11" = "yes" ]; then
482  echo '#define DISABLE_X11' >> $w/c/config.h
483fi
484
485cursesLib=-lcurses
486ncursesLib=-lncurses
487
488if [ "$disablecurses" = "yes" ]; then
489  echo '#define DISABLE_CURSES' >> $w/c/config.h
490  cursesLib=
491  ncursesLib=
492fi
493
494cat > $w/c/Mf-config << END
495CC=$CC
496CPPFLAGS=$CPPFLAGS
497CFLAGS=$CFLAGS
498LD=$LD
499LDFLAGS=$LDFLAGS
500AR=$AR
501ARFLAGS=$ARFLAGS
502RANLIB=$RANLIB
503WINDRES=$WINDRES
504cursesLib=$cursesLib
505ncursesLib=$ncursesLib
506zlibInc=$zlibInc
507LZ4Inc=$LZ4Inc
508zlibDep=$zlibDep
509LZ4Dep=$LZ4Dep
510zlibLib=$zlibLib
511LZ4Lib=$LZ4Lib
512zlibHeaderDep=$zlibHeaderDep
513LZ4HeaderDep=$LZ4HeaderDep
514Kernel=\${${Kernel}}
515KernelLinkDeps=\${${Kernel}LinkDeps}
516KernelLinkLibs=\${${Kernel}LinkLibs}
517END
518