summaryrefslogtreecommitdiff
path: root/configure
blob: ce32ac76980dfba1feb01e607b9b7621d9947330 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/sh

VERSION=$(cat .version)
DATE=$(date +%Y%m%d)

# All absolutely required tools, one per line to ease diff.
# See function 'has_or_abort, below, for syntax
#  - Hopefully, if gcc is present, then all associated tools will be
#  - awk must be GNU awk
#  - makeinfo for building docs, even if discarded later on
#  - others obvious... :-/
#
# Format of a pattern to check for, one per line:
#   pattern := tool_test  OR  pattern|tool_test
#   tool_test := tool/regexp
#   tool := name of the tool  OR  absolute pathname to the tool
#   regexp := valid grep(1) extended regular expression  OR  empty
#
# In case a pattern list is given (eg foo|bar|buz), then tests are performed
# from left to right, stopping at the first matching test (like the shell
# would parse 'foo || bar || buz' ).
#
# Examples:
#    /bin/bash/^GNU bash, version 3\.
#       will ensure that /bin/bash exists, and that $(/bin/bash --version)
#       matches the regexp '^GNU bash, version 3\.'
#    autoconf/(GNU Autoconf)|autoconf2.50/
#       will ensure that:
#         - 'autoconf' is to be found in the PATH, and that $(autoconf
#           --version) matches the regexp '(GNU Autoconf)' (which btw is
#           the signature of autoconf >= 2.50),
#       OR that:
#         - 'autoconf2.50' is to be found in the PATH
#
TOOLS_TO_CHECK='
/bin/bash/^GNU bash, version 3\.
make/^GNU Make
gcc/
gawk/^GNU Awk
sed/
bison/
flex/
makeinfo/
automake/
libtool/
patch/
tar/
gzip/
bzip2/
'

PREFIX_DEFAULT=/usr/local

BINDIR_set=
LIBDIR_set=
DOCDIR_set=
MANDIR_set=
LOCAL_set=

FORCE=0

do_quit=
CONTRIB_list=

# Simply print the error message, and exit. Obvious, he?
do_error() {
    echo "${@}"
    exit 1
}

# A small function to test for existence of various tools
# Usage: has_or_abort test_pattern (see top of file, TOOLS_TO_CHECK, for
#                                   complete pattern format)
has_or_abort() {
    { IFS="|"; for item in ${1}; do
          tool="${item%/*}"
          regexp="${item##*/}"
          printf "Checking for '${tool}'... "
          where=$(which "${tool}" 2>/dev/null || true)
          if [ -z "${where}" ]; then
              echo "not found"
              where=
              continue
          else
              if [ -n "${regexp}" ]; then
                  tool_version=$(${tool} --version 2>&1)
                  str=$(echo "${tool_version}" |grep -E "${regexp}" |head -n 1)
                  if [ -z "${str}" ]; then
                      echo "wrong version string: expecting regexp '${regexp}'"
                      where=""
                      continue
                  fi
              fi
              echo "${where}"
              return 0
          fi
      done;
    }
    [ ${FORCE} -eq 0 ] && do_error "Bailing out..."
    return 0
}

# Given an option string and the following argument,
# echoes the value of the option.
# If --var=val => echoes val and returns 0, meaning second arg was not consumed
# If --var val => echoes val and returns non null, meaning second arg was used
get_optval(){
    local ret
    case "$1" in
        --*=?*)
            echo "${1}" |cut -d '=' -f 2-
            ret=0
            ;;
        *)
            echo "${2}"
            ret=1
            ;;
    esac
    return ${ret}
}

# The set_xxx functions will set the corresponding configuration variable
# They return 0 if second arg was not consumed, and non-zero if it was consumed.
set_prefix() {
    PREFIX=$(get_optval "$1" "$2")
    return $?
}
set_bindir() {
    BINDIR_set=1
    BINDIR=$(get_optval "$1" "$2")
    return $?
}
set_libdir() {
    LIBDIR_set=1
    LIBDIR=$(get_optval "$1" "$2")
    return $?
}
set_docdir() {
    DOCDIR_set=1
    DOCDIR=$(get_optval "$1" "$2")
    return $?
}
set_mandir() {
    MANDIR_set=1
    MANDIR=$(get_optval "$1" "$2")
    return $?
}

# The set_contrib function is different in that it will work like the others,
# except in two cases:
# If all => replaces all with the list of all available contribs
# If list => just echoes the list of all available contribs, and instructs
# caller to quit immediately by setting do_quit to non null.
# (can't use the return code, see above).
set_contrib() {
    opt_val=$(get_optval "$1" "$2")
    local ret=$?
    case "${opt_val}" in
        all)
            CONTRIB_list=$(LC_ALL=C ls -1 contrib/*.patch.lzma  \
                           |xargs -I {} basename {} .patch.lzma \
                           |sed -r -e ':a; /$/N; s/\n/,/; ta;'
                          )
            ;;
        list)
            do_quit=1
            echo "Available contributions:"
            LC_ALL=C ls -1 contrib/*.patch.lzma     \
            |xargs -I {} basename {} .patch.lzma    \
            |sed -r -e 's/^/  /;'
            ;;
        *)  CONTRIB_list="${CONTRIB_list},${opt_val}";;
    esac
    return $ret
}

do_help() {
    cat <<__EOF__
\`configure' configures crosstool-NG ${VERSION} to adapt to many kind of systems.

USAGE: ./configure [OPTION]...

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit                                                                                                                  
      --force             force ./configure to complete, even if one or more
                          tools were not found. Use at your own risk, only if
                          you know what you are doing!

Installation directories:
  --prefix=PREFIX         install files in PREFIX [${PREFIX_DEFAULT}]
  --local                 don't install, and use current directory

By default, \`make install' will install all the files in
\`${PREFIX_DEFAULT}/bin', \`${PREFIX_DEFAULT}/lib' etc.  You can specify
an installation prefix other than \`${PREFIX_DEFAULT}' using \`--prefix',
for instance \`--prefix=\${HOME}'.

For better control, use the options below.

Fine tuning of the installation directories:
  --bindir=DIR            user executables [PREFIX/bin]
  --libdir=DIR            object code libraries [PREFIX/lib]
  --docdir=DIR            info documentation [PREFIX/share/doc]
  --mandir=DIR            man documentation [PREFIX/share/man]

Optional Features:
  --with-contrib=XXX      Include externally contributed features found in the
                          contrib/ sub-directory. Set to a comma-separated list
                          of features. Use 'all' to use all contributions, and
                          'list' to see which are available.
__EOF__
}

#---------------------------------------------------------------------
# Set user's options

while [ $# -ne 0 ]; do
    case "$1" in
        --prefix*)  set_prefix "$1" "$2" && shift || shift 2;;
        --bindir*)  set_bindir "$1" "$2" && shift || shift 2;;
        --libdir*)  set_libdir "$1" "$2" && shift || shift 2;;
        --docdir*)  set_docdir "$1" "$2" && shift || shift 2;;
        --mandir*)  set_mandir "$1" "$2" && shift || shift 2;;
        --local)    LOCAL_set=1; shift;;
        --force)    FORCE=1; shift;;
        --with-contrib*)
                    set_contrib "$1" "$2" && shift || shift 2
                    [ "${do_quit}" = "1" ] && exit 0
                    ;;
        --help|-h)  do_help; exit 0;;
        *)          do_help; exit 1;;
    esac
done

# Use defaults
[ -z "${PREFIX}" ] && set_prefix "" "${PREFIX_DEFAULT}"

# Special case when installing locally
if [ "${LOCAL_set}" = "1" ]; then
    set_prefix "" $(pwd)
    set_bindir "" $(pwd)
    set_libdir "" $(pwd)
    set_docdir "" $(pwd)/docs
    set_mandir "" $(pwd)/docs
fi

#---------------------------------------------------------------------
# Some sanity checks, now

# If this version is a svn snapshot, try to get the revision number
# If we can't get the revision number, use date
printf "Computing version string... "
case "${VERSION}" in
  *+svn)
    REVISION=$(LC_ALL=C svnversion)
    case "${REVISION}" in
      exported)
        VERSION="${VERSION}unknown@$(date +%Y%m%d.%H%M%S)";;
      *)
        URL=$(LC_ALL=C svn info 2>/dev/null |egrep 'URL: ' |cut -d ' ' -f 2-)
        ROOT=$(LC_ALL=C svn info 2>/dev/null |egrep 'Repository Root: ' |cut -d ' ' -f 3-)
        VERSION="${VERSION}${URL#${ROOT}}@${REVISION}"
        ;;
    esac
    # Arrange to have no / in the directory name, no need to create an
    # arbitrarily deep directory structure
    VERSION=$(echo "${VERSION}" |sed -r -e 's|/+|_|g;')
    ;;
esac
echo "${VERSION}"

# Now we have the version string, we can build up the paths
[ -z "${BINDIR_set}" ] && BINDIR="${PREFIX}/bin"
[ -z "${LIBDIR_set}" ] && LIBDIR="${PREFIX}/lib/ct-ng-${VERSION}"
[ -z "${DOCDIR_set}" ] && DOCDIR="${PREFIX}/share/doc/ct-ng-${VERSION}"
[ -z "${MANDIR_set}" ] && MANDIR="${PREFIX}/share/man/man1"

# Check that install PATHs are absolute
for p in BIN LIB DOC MAN; do
  var="${p}DIR"
  eval v="\${${var}}"
  case "${v}" in
    /*) ;;
    *)  do_error "'${var}' is not an absolute path: '${v}'"
  esac
done

# Check the existence of absolutely required tools
{ IFS='
';
  for tool in ${TOOLS_TO_CHECK}; do
      has_or_abort "${tool}"
  done;
}

# It's safer to change all ',' to spaces rather than setting IFS
CONTRIB_list=$(echo "${CONTRIB_list}" |sed -r -e 's/,+/ /g; s/ +/ /g; s/^ //g; s/ $//g;')
if [ -n "${CONTRIB_list}" ]; then
    has_or_abort 'lzcat/'
    printf "Applying contributed code: "
    for c in ${CONTRIB_list}; do
        printf "${c}, "
        if [ ! -f "contrib/${c}.patch.lzma" ]; then
            do_error "Contribution '${c}' does not exist"
        fi
        lzcat "contrib/${c}.patch.lzma" |patch -p1 >/dev/null 2>&1
    done
    echo "done"
fi

printf "Building up Makefile... "
sed -r -e "s,@@BINDIR@@,${BINDIR},g;"   \
       -e "s,@@LIBDIR@@,${LIBDIR},g;"   \
       -e "s,@@DOCDIR@@,${DOCDIR},g;"   \
       -e "s,@@MANDIR@@,${MANDIR},g;"   \
       -e "s,@@VERSION@@,${VERSION},g;" \
       -e "s,@@DATE@@,${DATE},g;"       \
       -e "s,@@LOCAL@@,${LOCAL_set},g;" \
       Makefile.in >Makefile
echo "done"

cat <<__EOF__

crosstool-NG configured as follows:
  PREFIX='${PREFIX}'
  BINDIR='${BINDIR}'
  LIBDIR='${LIBDIR}'
  DOCDIR='${DOCDIR}'
  MANDIR='${MANDIR}'
  CONTRIB='${CONTRIB_list}'
__EOF__