1#!/bin/sh
2# Build all of the am-utils package in a directory A.<cpu-company-system>
3# Used by am-utils users.
4# Erez Zadok <ezk AT am-utils.org>
5#
6# run "buildall -h" to get usage
7#
8#set -x
9
10##############################################################################
11# first test if we are in the right directory to run this script
12# change to the right directory
13test -f ../config.guess && cd ..
14test -f ../../config.guess && cd ../..
15pwd=$(pwd)
16host_alias=$(cd /tmp; $pwd/config.guess.long)
17if test -z "$host_alias" ; then
18          echo "$0: must run from the source or the A. directory."
19          echo "$0: cannot find $pwd/config.guess"
20          exit 1
21else
22          :
23fi
24
25##############################################################################
26# pattern of lines to remove from config.cache (for developers only)
27# Example: if you change TRY_COMPILE_NFS, redo these:
28#pat='fhandle|nfs_fh|nfs_args|struct_nfs|opt'
29#pat='style_|mount_trap|mtype|transport|dref'
30#pat='mntent|mnttab'
31#pat='nfs_args|fh_len|irix|proto|vers'
32#pat='3|proto|tcp|ver|nfs_prot|mtype'
33#pat='trap|style|mtype|transport|os_libs|restartable|unmount_args|yp_order'
34#pat='yp_all|nsl|nis'
35
36##############################################################################
37# initialize variables (build command, config command, variables)
38bld_cmd=""
39bld_flags=""
40cnf_cmd=""
41cnf_flags="-C"                          # use config.cache cache file
42inst_cmd=""
43inst_flags=""
44mkcnf_cmd=""
45vars=""
46expvars=""
47default=yes
48
49##############################################################################
50# check if CFLAGS or AM_CFLAGS was passed
51test -z "$CFLAGS" || vars="$vars CFLAGS=\"${CFLAGS}\""
52test -z "$CFLAGS" || expvars="$expvars CFLAGS"
53test -z "$AM_CFLAGS" || vars="$vars AM_CFLAGS=\"${AM_CFLAGS}\""
54
55##############################################################################
56# functions
57
58add_gcc_flags1 () {
59    case "${CC}" in
60          cc | */cc )
61                    # do nothing
62                    ;;
63              * )
64                    vars="$vars AM_CFLAGS=\"-Werror\""
65                    AM_CFLAGS="-Werror"
66                    expvars="$expvars AM_CFLAGS"
67                    ;;
68    esac
69}
70
71add_gcc_flags2 () {
72    case "${CC}" in
73          cc | */cc )
74                    # do nothing
75                    ;;
76              * )
77                    vars="$vars AM_CFLAGS=\"-Wall -Werror\""
78                    AM_CFLAGS="-Wall -Werror"
79                    expvars="$expvars AM_CFLAGS"
80                    ;;
81    esac
82}
83
84add_shared_flags () {
85    cnf_cmd="$cnf_cmd --enable-shared --disable-static"
86}
87
88add_prefix_flags () {
89    cnf_cmd="$cnf_cmd --prefix=/usr/local/AMD"
90}
91
92##############################################################################
93# iterate over all options, and set the command to run with any variables
94while [ $# != 0 ]; do
95case "$1" in
96    -b )
97          # look for GNU make if possible
98          gmake --version -f /dev/null > /dev/null 2>&1
99          if [ $? = 0 ]
100          then
101              bld_cmd="${MAKE:-gmake}"
102          else
103              bld_cmd="${MAKE:-make}"
104          fi
105          default=no
106          shift
107    ;;
108
109    -i )
110          # look for GNU make if possible
111          gmake --version -f /dev/null > /dev/null 2>&1
112          if [ $? = 0 ]
113          then
114              inst_cmd="${MAKE:-gmake} install"
115          else
116              inst_cmd="${MAKE:-make} install"
117          fi
118          default=no
119          shift
120    ;;
121
122    -c )
123          cnf_cmd="../configure --enable-debug=yes"
124#         add_gcc_flags1
125          default=no
126          shift
127    ;;
128
129    -cs )
130          cnf_cmd="../configure --enable-debug=yes"
131          add_shared_flags
132#         add_gcc_flags1
133          default=no
134          shift
135    ;;
136
137    -C )
138          cnf_cmd="../configure --enable-debug=yes"
139          add_gcc_flags2
140          default=no
141          shift
142    ;;
143
144    -Cs )
145          cnf_cmd="../configure --enable-debug=yes"
146          add_shared_flags
147          add_gcc_flags2
148          default=no
149          shift
150    ;;
151
152    -d )
153          cnf_cmd="../configure --enable-debug=yes"
154          add_prefix_flags
155#         add_gcc_flags1
156          default=no
157          shift
158    ;;
159
160    -ds )
161          cnf_cmd="../configure --enable-debug=yes"
162          add_prefix_flags
163          add_shared_flags
164#         add_gcc_flags1
165          default=no
166          shift
167    ;;
168
169    -D )
170          cnf_cmd="../configure --enable-debug=yes"
171          add_prefix_flags
172          add_gcc_flags2
173          default=no
174          shift
175    ;;
176
177    -Ds )
178          cnf_cmd="../configure -enable-debug=yes"
179#         cnf_cmd="../configure -enable-debug=mem"
180#         cnf_cmd="../configure --prefix=/usr/local/AMD --enable-debug=yes --enable-shared --disable-static \
181#             --enable-cppflags=\"-I${HOME}/ldap/include -I${HOME}/hesiod/include\" \
182#             --enable-ldflags=\"-L${HOME}/ldap/lib -L${HOME}/hesiod/lib\""
183#         cnf_cmd="$cnf_cmd \
184#             --enable-cppflags=-I${HOME}/ldap/include \
185#             --enable-ldflags=-L${HOME}/ldap/lib"
186#         cnf_cmd="../configure -enable-debug=yes --enable-cppflags=-I/usr/local/include --enable-ldflags=-L/usr/local/lib"
187          add_prefix_flags
188          add_shared_flags
189          add_gcc_flags2
190          default=no
191          shift
192    ;;
193
194    -K )
195#         mkcnf_cmd="../m4/mkconf"
196          mkcnf_cmd="../bootstrap"
197          if test -f bootstrap ; then
198              :
199          else
200              echo "am-utils maintainer option only!"
201              exit 1
202          fi
203          default=no
204          shift
205    ;;
206
207    -q )
208          cnf_cmd="./config.status"
209          default=no
210          shift
211    ;;
212
213    -- )
214          shift
215          cmdline_cnf_flags="$*"
216          break     # from while loop
217    ;;
218
219    -h | * )
220cat <<EOF
221Usage: buildall [-b] [-[cCdD][s]] [-K] [-q] [-h] [-- configopts]
222     -b: build only
223     -c:  configure (debugging)
224     -cs: configure (debugging, shared libs)
225     -C:  configure (strict compile, debugging)
226     -Cs: configure (strict compile, debugging, shared libs)
227     -d:  configure in /usr/local/AMD (debugging)
228     -ds: configure in /usr/local/AMD (debugging, shared libs)
229     -D:  configure in /usr/local/AMD (strict compile, debugging)
230     -Ds: configure in /usr/local/AMD (strict compile, debugging, shared libs)
231     -K: run mkconf to update *.in files (developers only)
232     -i: build and install
233     -q: quick configure only (run config.status)
234     -h: print usage
235     configopts: options to pass to configure (must be last and after a --)
236You may pass variables: CFLAGS for build, MAKE for your make program
237and AM_CFLAGS for additional build flags.
238EOF
239    exit 1
240    ;;
241
242esac
243done
244
245# if AM_CFLAGS was set before, then add it to the configure option
246if test -n "${AM_CFLAGS}"; then
247    extra_cnf_flags="--enable-am-cflags=\"${AM_CFLAGS}\""
248else
249    :
250fi
251
252# check if no options were given, and set to defaults
253if test "$default" = "yes"; then
254    # look for GNU make if possible
255    gmake --version -f /dev/null > /dev/null 2>&1
256    if [ $? = 0 ]
257    then
258          bld_cmd="${MAKE:-gmake}"
259    else
260          bld_cmd="${MAKE:-make}"
261    fi
262    cnf_cmd="../configure"
263else
264    :
265fi
266
267##############################################################################
268# make build directory if needed
269if test -d ./A.${host_alias} ; then
270    :
271else
272    mkdir ./A.${host_alias}
273fi
274echo "Configuring/building am-utils in directory ./A.${host_alias} ..."
275echo cd ./A.${host_alias}
276cd ./A.${host_alias} || exit 1
277
278##############################################################################
279# this is for developers only (remove config.cache entries)
280if test -n "$pat"; then
281    if test -f config.cache; then
282          egrep $pat config.cache | while read i; do echo ' '$i;done
283          egrep -v $pat config.cache > tmp.$$ && \
284                    mv config.cache config.cache.old && mv tmp.$$ config.cache
285    else
286          :
287    fi
288else
289    :
290fi
291
292##############################################################################
293# Some system's /bin/sh has limits/bugs which prevent it from being used
294# with configure
295case "${host_alias}" in
296    *hpux9* | *aix5.1* )
297          if test -n "$cnf_cmd"; then
298              if test -f /bin/bash; then
299                    cnf_cmd="/bin/bash $cnf_cmd"
300              elif test -f /bin/ksh; then
301                    cnf_cmd="/bin/ksh $cnf_cmd"
302              fi
303          else
304              :
305          fi
306          echo "WARNING: do not use /bin/make under this system."
307          echo "Instead, use GNU make or 'ksh ./configure' directly."
308          ;;
309    mips-sgi-irix5.2)
310          echo "WARNING: do not use /bin/make under this system."
311          echo "Instead, use GNU make or ./configure directly."
312          ;;
313esac
314
315##############################################################################
316# see if need to run mkconf
317if test -n "$mkcnf_cmd"; then
318    echo $mkcnf_cmd
319    $mkcnf_cmd || exit 1
320else
321    :
322fi
323
324##############################################################################
325# see if need to [re]configure
326if test -n "$cnf_cmd"; then
327    if test -n "$vars"; then
328          echo $vars
329          eval $vars
330          echo export $expvars
331          export $expvars
332    else
333          :
334    fi
335    if test -z "${cnf_flags}${extra_cnf_flags}"; then
336          echo $cnf_cmd $cmdline_cnf_flags
337          eval $cnf_cmd $cmdline_cnf_flags || exit 1
338    else
339          echo $cnf_cmd $cnf_flags $extra_cnf_flags $cmdline_cnf_flags
340          eval $cnf_cmd $cnf_flags $extra_cnf_flags $cmdline_cnf_flags || exit 1
341    fi
342else
343    :
344fi
345
346##############################################################################
347# if need to [re]build
348if test -n "$bld_cmd"; then
349    echo $bld_cmd $bld_flags
350    $bld_cmd $bld_flags || exit 1
351else
352    :
353fi
354
355##############################################################################
356# if need to install
357if test -n "$inst_cmd"; then
358    echo $inst_cmd $inst_flags
359    $inst_cmd $inst_flags || exit 1
360else
361    :
362fi
363
364##############################################################################
365