configure
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Sat Oct 03 18:49:23 2009 +0200 (2009-10-03)
changeset 1560 79a609170a83
parent 1485 d031a67fc494
child 1570 1d43b65599a4
child 1576 906b7509835e
permissions -rwxr-xr-x
configure: split has_or_abort in two: one to check, one to abort

Split the has_or_abort function in two:
- one that checks if the tool if found,
- one that calls the above check, and aborts if not found

The rational behind this is to be able to check for a tool
and if not found, fallback to using our bundled version,
should the need arise (and I get time).
yann@182
     1
#!/bin/sh
yann@182
     2
yann@1311
     3
myname="${0##*/}"
yann@1311
     4
yann@1105
     5
VERSION=$( cat .version )
yann@1105
     6
DATE=$( date +%Y%m%d )
yann@182
     7
yann@183
     8
PREFIX_DEFAULT=/usr/local
yann@182
     9
yann@182
    10
BINDIR_set=
yann@182
    11
LIBDIR_set=
yann@182
    12
DOCDIR_set=
yann@182
    13
MANDIR_set=
yann@285
    14
LOCAL_set=
yann@1311
    15
FORCE=
yann@182
    16
yann@641
    17
do_quit=
yann@614
    18
yann@673
    19
# Simply print the error message, and exit. Obvious, he?
yann@673
    20
do_error() {
yann@1333
    21
    printf "${myname}: ${@}\n"
yann@673
    22
    exit 1
yann@673
    23
}
yann@673
    24
yann@641
    25
# Given an option string and the following argument,
yann@641
    26
# echoes the value of the option.
yann@641
    27
# If --var=val => echoes val and returns 0, meaning second arg was not consumed
yann@641
    28
# If --var val => echoes val and returns non null, meaning second arg was used
yann@182
    29
get_optval(){
yann@182
    30
    case "$1" in
yann@182
    31
        --*=?*)
yann@1333
    32
            printf "${1#*=}"
yann@1105
    33
            return 0
yann@182
    34
            ;;
yann@182
    35
        *)
yann@1333
    36
            printf "${2}"
yann@1105
    37
            return 1
yann@182
    38
            ;;
yann@182
    39
    esac
yann@182
    40
}
yann@182
    41
yann@641
    42
# The set_xxx functions will set the corresponding configuration variable
yann@641
    43
# They return 0 if second arg was not consumed, and non-zero if it was consumed.
yann@182
    44
set_prefix() {
yann@1105
    45
    PREFIX="$( get_optval "$1" "$2" )"
yann@182
    46
}
yann@182
    47
set_bindir() {
yann@376
    48
    BINDIR_set=1
yann@1105
    49
    BINDIR="$( get_optval "$1" "$2" )"
yann@182
    50
}
yann@182
    51
set_libdir() {
yann@376
    52
    LIBDIR_set=1
yann@1105
    53
    LIBDIR="$( get_optval "$1" "$2" )"
yann@182
    54
}
yann@182
    55
set_docdir() {
yann@376
    56
    DOCDIR_set=1
yann@1105
    57
    DOCDIR="$( get_optval "$1" "$2" )"
yann@182
    58
}
yann@182
    59
set_mandir() {
yann@376
    60
    MANDIR_set=1
yann@1105
    61
    MANDIR="$( get_optval "$1" "$2" )"
yann@182
    62
}
yann@1140
    63
set_tool() {
yann@1140
    64
    local var_name="${1%%=*}"
yann@1140
    65
    var_name="${var_name#--with-}"
yann@1140
    66
    eval ${var_name}="\$( get_optval "$1" "$2" )"
yann@614
    67
}
yann@614
    68
yann@1311
    69
# var_list is a list of variables, each one holding a path to a
yann@1311
    70
# tool, either detected by ./configure, or specified by the user.
yann@1311
    71
var_list=""
yann@1311
    72
yann@1311
    73
# This function adds a variable name to the above list of variable names.
yann@1311
    74
# $1: the name of the variable to add to the list
yann@1311
    75
add_to_var_list() {
yann@1311
    76
    var_list="${var_list} ${1}"
yann@1311
    77
}
yann@1311
    78
yann@1311
    79
# A function to test for required tools/headers/libraries
yann@1560
    80
# Return 0 (true) if found, !0 (false) if not found
yann@1560
    81
#
yann@1311
    82
# $*: [prog|inc|lib]=<name[ name...]>
yann@1311
    83
#     the name(s) of tool(s) to test for
yann@1311
    84
#     mandatory
yann@1311
    85
#       eg: prog=bash   prog="curl wget"
yann@1311
    86
# $*: var=<var_name>
yann@1311
    87
#     the name of the variable to test and set
yann@1311
    88
#     optional
yann@1311
    89
#       eg: var=bash    if ${bash} is set and non-null, use that,
yann@1311
    90
#                       else check for bash and set bash=$(which bash)
yann@1311
    91
# $*: ver=<regexp>
yann@1311
    92
#     for each 'prog', test if $(prog --version) matches 'regexp'
yann@1311
    93
#     optional
yann@1311
    94
#       eg: ver='^GNU bash, version [34]\.'
yann@1311
    95
# $*: err=<error_message>
yann@1311
    96
#     the error message to print if tool is missing
yann@1311
    97
#     optional, defaults to: '${prog}: none found'
yann@1311
    98
#       eg: err="'bash' 3.x or above was not found"
yann@1560
    99
check_for() {
yann@1311
   100
    local prog inc lib
yann@1311
   101
    local var ver err
yann@1311
   102
    local val
yann@1311
   103
    local item
yann@1311
   104
    local where
yann@1312
   105
    local status
yann@1311
   106
yann@1311
   107
    for item in "${@}"; do
yann@1311
   108
        case "${item}" in
yann@1311
   109
            prog=*|inc=*|lib=*|var=*|ver=*|err=*)
yann@1311
   110
                eval ${item%%=*}="'${item#*=}'"
yann@1311
   111
                ;;
yann@1311
   112
            *)  do_error "has_or_abort: incorrect parameters: '$@'";;
yann@1311
   113
        esac
yann@1311
   114
    done
yann@1311
   115
yann@1311
   116
    case "${prog}:${inc}:${lib}" in
yann@1311
   117
        ?*::)
yann@1311
   118
            for item in ${prog}; do
yann@1311
   119
                printf "Checking for '${item}'... "
yann@1311
   120
                if [ -n "${var}" ]; then
yann@1311
   121
                    eval val="\${${var}}"
yann@1311
   122
                    if [ -n "${val}" ]; then
yann@1311
   123
                        printf "${val} (cached)\n"
yann@1311
   124
                        return 0
yann@1311
   125
                    fi
yann@1311
   126
                fi
yann@1311
   127
                where="$( which "${item}" 2>/dev/null )"
yann@1311
   128
                if [ -z "${where}" ]; then
yann@1312
   129
                    printf "no\n"
yann@1311
   130
                    continue
yann@1311
   131
                elif [ -n "${ver}" ]; then
yann@1333
   132
                    str=$( "${where}" --version 2>&1 |grep -E "${ver}" |head -n 1 )
yann@1311
   133
                    if [ -z "${str}" ]; then
yann@1312
   134
                        printf "no\n"
yann@1311
   135
                        unset where
yann@1311
   136
                        continue
yann@1311
   137
                    fi
yann@1311
   138
                fi
yann@1312
   139
                status="${where}"
yann@1311
   140
                break
yann@1311
   141
            done
yann@1311
   142
            ;;
yann@1311
   143
        :?*:)
yann@1312
   144
            for item in ${inc}; do
yann@1312
   145
                printf "Checking for '${item}'... "
yann@1312
   146
                if printf "#include \"${item}\"" |gcc -x c -c - -o /dev/null >/dev/null 2>&1; then
yann@1312
   147
                    where="${item}"
yann@1312
   148
                    status=yes
yann@1312
   149
                    break;
yann@1312
   150
                fi
yann@1312
   151
                printf "no\n"
yann@1312
   152
            done
yann@1311
   153
            ;;
yann@1311
   154
        ::?*)
yann@1313
   155
            for item in ${lib}; do
yann@1313
   156
                printf "Checking for '${item}'... "
yann@1313
   157
                where="$( gcc -print-file-name="${item}" )"
yann@1313
   158
                if [ "${where}" != "${item}" ]; then
yann@1313
   159
                    where="$( readlink -e "${where}" )"
yann@1313
   160
                    status=yes
yann@1313
   161
                    break;
yann@1313
   162
                fi
yann@1313
   163
                printf "no\n"
yann@1313
   164
            done
yann@1311
   165
            ;;
yann@1311
   166
    esac
yann@1560
   167
yann@1312
   168
    if [ -z "${status}" ]; then
yann@1312
   169
        printf "\n${err:-${prog}${inc}${lib}: none found}\n\n"
yann@1312
   170
        printf "Either you are missing entirely the needed tool,\n"
yann@1312
   171
        printf "or the version you have is too old.\n"
yann@1312
   172
        if [ -n "${var}" ]; then
yann@1312
   173
            printf "You can give the path to this tool using: --with-${var}=PATH\n"
yann@1312
   174
        fi
yann@1560
   175
        printf "\n"
yann@1560
   176
        return 1
yann@1560
   177
    fi
yann@1560
   178
yann@1560
   179
    printf "${status}"
yann@1560
   180
    if [ -n "${var}" ]; then
yann@1560
   181
        eval ${var}='"'"${where}"'"'
yann@1560
   182
        add_to_var_list "${var}"
yann@1560
   183
    fi
yann@1560
   184
    printf "\n"
yann@1560
   185
}
yann@1560
   186
yann@1560
   187
# This function checks for a tool, and aborts if not found
yann@1560
   188
# See check_for(), above, for how to call has_or_abort
yann@1560
   189
has_or_abort() {
yann@1560
   190
    if ! check_for "$@"; then
yann@1312
   191
        # FORCE can be set in the environment
yann@1312
   192
        [ -z "${FORCE}" ] && do_error "Bailing out..."
yann@1312
   193
        printf "\n"
yann@1312
   194
        printf "<*                                          *>\n"
yann@1312
   195
        printf "<*            FORCE in action:              *>\n"
yann@1312
   196
        printf "<* Continuing despite missing pre-requisite *>\n"
yann@1312
   197
        printf "<*          Prepare for breakage            *>\n"
yann@1312
   198
        printf "<*                                          *>\n"
yann@1312
   199
        printf "\n"
yann@1312
   200
    fi
yann@1311
   201
}
yann@1311
   202
yann@183
   203
do_help() {
yann@183
   204
    cat <<__EOF__
yann@1140
   205
\`configure' configures crosstool-NG-${VERSION} to adapt to many kind of systems.
yann@183
   206
yann@183
   207
USAGE: ./configure [OPTION]...
yann@183
   208
yann@183
   209
Defaults for the options are specified in brackets.
yann@183
   210
yann@183
   211
Configuration:
yann@1105
   212
  -h, --help              display this help and exit
yann@1311
   213
      --force             force configure to continue, even in case
yann@1311
   214
                          some pre-requisites are missing
yann@615
   215
yann@615
   216
Installation directories:
yann@185
   217
  --prefix=PREFIX         install files in PREFIX [${PREFIX_DEFAULT}]
yann@285
   218
  --local                 don't install, and use current directory
yann@183
   219
yann@183
   220
By default, \`make install' will install all the files in
yann@183
   221
\`${PREFIX_DEFAULT}/bin', \`${PREFIX_DEFAULT}/lib' etc.  You can specify
yann@183
   222
an installation prefix other than \`${PREFIX_DEFAULT}' using \`--prefix',
yann@183
   223
for instance \`--prefix=\${HOME}'.
yann@183
   224
yann@183
   225
For better control, use the options below.
yann@183
   226
yann@183
   227
Fine tuning of the installation directories:
yann@683
   228
  --bindir=DIR            user executables [PREFIX/bin]
yann@683
   229
  --libdir=DIR            object code libraries [PREFIX/lib]
yann@683
   230
  --docdir=DIR            info documentation [PREFIX/share/doc]
yann@683
   231
  --mandir=DIR            man documentation [PREFIX/share/man]
yann@614
   232
yann@614
   233
Optional Features:
yann@1140
   234
  --with-install=PATH     Specify the full PATH to GNU install
yann@1311
   235
  --with-make=PATH        Specify the full PATH to GNU make >= 3.80
yann@1140
   236
  --with-grep=PATH        Specify the full PATH to GNU grep
yann@1140
   237
  --with-sed=PATH         Specify the full PATH to GNU sed
yann@1140
   238
  --with-bash=PATH        Specify the full PATH to bash >= 3.0
yann@183
   239
__EOF__
yann@183
   240
}
yann@183
   241
yann@376
   242
#---------------------------------------------------------------------
yann@376
   243
# Set user's options
yann@376
   244
yann@182
   245
while [ $# -ne 0 ]; do
yann@182
   246
    case "$1" in
yann@1297
   247
        --local)    LOCAL_set="y"; shift;;
yann@182
   248
        --prefix*)  set_prefix "$1" "$2" && shift || shift 2;;
yann@182
   249
        --bindir*)  set_bindir "$1" "$2" && shift || shift 2;;
yann@182
   250
        --libdir*)  set_libdir "$1" "$2" && shift || shift 2;;
yann@182
   251
        --docdir*)  set_docdir "$1" "$2" && shift || shift 2;;
yann@182
   252
        --mandir*)  set_mandir "$1" "$2" && shift || shift 2;;
yann@1140
   253
        --with-*)   set_tool   "$1" "$2" && shift || shift 2;;
yann@1311
   254
        --force)    FORCE=1; shift;;
yann@183
   255
        --help|-h)  do_help; exit 0;;
yann@1333
   256
        *)          printf "Unrecognised option: '${1}'\n"; do_help; exit 1;;
yann@182
   257
    esac
yann@182
   258
done
yann@182
   259
yann@641
   260
# Use defaults
yann@185
   261
[ -z "${PREFIX}" ] && set_prefix "" "${PREFIX_DEFAULT}"
yann@641
   262
yann@641
   263
# Special case when installing locally
yann@1297
   264
if [ "${LOCAL_set}" = "y" ]; then
yann@1105
   265
    set_prefix "" "$( pwd )"
yann@1105
   266
    set_bindir "" "$( pwd )"
yann@1105
   267
    set_libdir "" "$( pwd )"
yann@1105
   268
    set_docdir "" "$( pwd )/docs"
yann@1105
   269
    set_mandir "" "$( pwd )/docs"
yann@285
   270
fi
yann@183
   271
yann@376
   272
#---------------------------------------------------------------------
yann@1140
   273
# Some sanity checks, now
yann@1106
   274
yann@1560
   275
# We check for grep and sed manually, because they are used in check_for()
yann@1140
   276
printf "Checking for 'grep'... "
yann@1140
   277
if [ -n "${grep}" ]; then
yann@1333
   278
    printf "${grep} (cached)\n"
yann@1140
   279
else
yann@1140
   280
    grep="$( which grep 2>/dev/null )"
yann@1187
   281
    if [ -z "${grep}" ]; then
yann@1333
   282
        printf "not found\n"
yann@1187
   283
    else
yann@1333
   284
        printf "${grep}\n"
yann@1187
   285
        printf "Checking whether '${grep}' supports -E... "
yann@1187
   286
        if echo 'foo' |"${grep}" -E 'foo' >/dev/null 2>&1; then
yann@1333
   287
            printf "yes\n"
yann@1187
   288
        else
yann@1333
   289
            printf "no\n"
yann@1187
   290
            grep=
yann@1187
   291
        fi
yann@1187
   292
    fi
yann@1106
   293
fi
yann@1187
   294
if [ -z "${grep}" ]; then
yann@1333
   295
    printf "Either you are missing entirely the needed tool,\n"
yann@1333
   296
    printf "or the version you have is too old.\n"
yann@1333
   297
    printf "You can give the path to this tool using: --with-grep=PATH\n"
yann@1187
   298
    do_error "Bailing out..."
yann@1140
   299
fi
yann@1311
   300
add_to_var_list grep
yann@1140
   301
yann@1140
   302
printf "Checking for 'sed'... "
yann@1140
   303
if [ -n "${sed}" ]; then
yann@1333
   304
    printf "${sed} (cached)\n"
yann@1140
   305
else
yann@1140
   306
    sed="$( which sed 2>/dev/null )"
yann@1187
   307
    if [ -z "${sed}" ]; then
yann@1333
   308
        printf "not found\n"
yann@1187
   309
    else
yann@1333
   310
        printf "${sed}\n"
rpjday@1288
   311
        printf "Checking whether '${sed}' supports -i and -e... "
yann@1187
   312
        touch .ct-ng.sed.test
yann@1187
   313
        if "${sed}" -r -i -e 's/foo/bar/' .ct-ng.sed.test >/dev/null 2>&1; then
yann@1333
   314
            printf "yes\n"
yann@1187
   315
        else
yann@1333
   316
            printf "no\n"
yann@1187
   317
            sed=
yann@1187
   318
        fi
yann@1187
   319
        rm -f .ct-ng.sed.test
yann@1187
   320
    fi
yann@1140
   321
fi
yann@1187
   322
if [ -z "${sed}" ]; then
yann@1333
   323
    printf "Either you are missing entirely the needed tool,\n"
yann@1333
   324
    printf "or the version you have is too old.\n"
yann@1333
   325
    printf "You can give the path to this tool using: --with-sed=PATH\n"
yann@1187
   326
    do_error "Bailing out..."
yann@1140
   327
fi
yann@1311
   328
add_to_var_list sed
yann@1140
   329
yann@1311
   330
# The regular list of tools we can now easily check for
yann@1311
   331
has_or_abort prog=bash                              \
yann@1311
   332
             var=bash                               \
yann@1477
   333
             ver='^GNU bash, version (3\.[1-9]|4)'  \
yann@1477
   334
             err="'bash' 3.1 or above was not found"
yann@1311
   335
has_or_abort prog=cut
yann@1311
   336
has_or_abort prog=install var=install
yann@1311
   337
has_or_abort prog=make                                  \
yann@1311
   338
             var=make                                   \
yann@1311
   339
             ver='^GNU Make (3.[89][[:digit:]]|[4-9])'  \
yann@1311
   340
             err="GNU 'make' 3.80 or above was not found"
yann@1311
   341
has_or_abort prog=gcc
yann@1431
   342
has_or_abort prog="awk gawk" ver='^GNU Awk' err="GNU 'awk' was not found"
yann@1311
   343
has_or_abort prog=bison
yann@1311
   344
has_or_abort prog=flex
yann@1311
   345
has_or_abort prog=makeinfo
yann@1311
   346
has_or_abort prog=automake                                                      \
oron@1432
   347
             ver='\(GNU automake\) (1\.[[:digit:]]{2,}|[2-9][[:digit:]]*\.)'    \
yann@1311
   348
             err="'automake' 1.10 or above was not found"
yann@1311
   349
has_or_abort prog=libtool                                                                           \
yann@1311
   350
             ver='\(GNU libtool.*\) (2[[:digit:]]*\.|1\.6[[:digit:]]*\.|1\.5\.[2-9][[:digit:]]+)'   \
yann@1311
   351
             err="'libtool' 1.5.26 or above was not found"
yann@1402
   352
has_or_abort prog=stat ver='GNU coreutils'
yann@1311
   353
has_or_abort prog="curl wget"
yann@1347
   354
has_or_abort prog=cvs
yann@1311
   355
has_or_abort prog=patch
yann@1311
   356
has_or_abort prog=tar
yann@1311
   357
has_or_abort prog=gzip
yann@1311
   358
has_or_abort prog=bzip2
yann@1311
   359
has_or_abort prog=lzma
yann@1313
   360
has_or_abort prog=readlink
yann@1311
   361
yann@1312
   362
has_or_abort inc="ncurses/ncurses.h ncurses/curses.h ncurses.h curses.h"    \
yann@1312
   363
             err="'ncurses' headers files were not found"
yann@1313
   364
yann@1332
   365
ncurses_libs="$( for l in ncursesw ncurses curses; do   \
yann@1332
   366
                     for x in so a dylib; do            \
yann@1332
   367
                         printf "lib$l.$x ";            \
yann@1332
   368
                     done;                              \
yann@1332
   369
                 done                                   \
yann@1332
   370
               )"
yann@1332
   371
has_or_abort lib="${ncurses_libs}"                  \
yann@1313
   372
             err="'ncurses' library was not found"
yann@1106
   373
yann@1106
   374
#---------------------------------------------------------------------
yann@1106
   375
# Compute the version string
yann@376
   376
yann@435
   377
# If this version is a svn snapshot, try to get the revision number
yann@435
   378
# If we can't get the revision number, use date
yann@435
   379
case "${VERSION}" in
Yann@1409
   380
    *+hg|hg)
Yann@1409
   381
        has_or_abort prog=hg
yann@1311
   382
        printf "Computing version string... "
Yann@1409
   383
        REVISION="$( hg id -n 2>/dev/null )"
yann@1105
   384
        case "${REVISION}" in
Yann@1409
   385
            "")
yann@1105
   386
                VERSION="${VERSION}_unknown@$( date +%Y%m%d.%H%M%S )";;
yann@1105
   387
            *)
yann@1430
   388
                VERSION="${VERSION}_$( hg id -b )@${REVISION%%+}_$( hg id -i )"
yann@1105
   389
                ;;
yann@1105
   390
        esac
yann@1105
   391
        # Arrange to have no / in the directory name, no need to create an
yann@1105
   392
        # arbitrarily deep directory structure
yann@1333
   393
        VERSION="$( printf "${VERSION}\n" |"${sed}" -r -e 's|/+|_|g;' )"
yann@444
   394
        ;;
yann@435
   395
esac
yann@553
   396
echo "${VERSION}"
yann@435
   397
yann@1106
   398
#---------------------------------------------------------------------
yann@1106
   399
# Compute and check install paths
yann@1106
   400
yann@614
   401
# Now we have the version string, we can build up the paths
yann@554
   402
[ -z "${BINDIR_set}" ] && BINDIR="${PREFIX}/bin"
yann@554
   403
[ -z "${LIBDIR_set}" ] && LIBDIR="${PREFIX}/lib/ct-ng-${VERSION}"
yann@554
   404
[ -z "${DOCDIR_set}" ] && DOCDIR="${PREFIX}/share/doc/ct-ng-${VERSION}"
yann@554
   405
[ -z "${MANDIR_set}" ] && MANDIR="${PREFIX}/share/man/man1"
yann@554
   406
yann@1047
   407
# Check that install PATHs are absolute
yann@1047
   408
for p in BIN LIB DOC MAN; do
yann@1105
   409
    var="${p}DIR"
yann@1105
   410
    eval v='"${'"${var}"'}"'
yann@1105
   411
    case "${v}" in
yann@1105
   412
        /*) ;;
yann@1105
   413
        *)  do_error "'${var}' is not an absolute path: '${v}'"
yann@1105
   414
    esac
yann@1047
   415
done
yann@1047
   416
yann@1106
   417
#---------------------------------------------------------------------
yann@1106
   418
# That's all, folks!
yann@614
   419
yann@641
   420
printf "Building up Makefile... "
yann@1140
   421
var_sed="$( for var_name in ${var_list}; do
yann@1140
   422
                eval echo 's,@@${var_name}@@,${'"${var_name}"'},g'
yann@1140
   423
            done 
yann@1140
   424
          )"
yann@1106
   425
"${sed}" -r -e "s,@@BINDIR@@,${BINDIR},g
yann@1106
   426
                s,@@LIBDIR@@,${LIBDIR},g
yann@1106
   427
                s,@@DOCDIR@@,${DOCDIR},g
yann@1106
   428
                s,@@MANDIR@@,${MANDIR},g
yann@1106
   429
                s,@@VERSION@@,${VERSION},g
yann@1106
   430
                s,@@DATE@@,${DATE},g
yann@1140
   431
                ${var_sed}
yann@1106
   432
                s,@@LOCAL@@,${LOCAL_set},g"  Makefile.in >Makefile
yann@673
   433
echo "done"
yann@185
   434
yann@185
   435
cat <<__EOF__
yann@673
   436
yann@197
   437
crosstool-NG configured as follows:
yann@554
   438
  PREFIX='${PREFIX}'
yann@554
   439
  BINDIR='${BINDIR}'
yann@554
   440
  LIBDIR='${LIBDIR}'
yann@554
   441
  DOCDIR='${DOCDIR}'
yann@554
   442
  MANDIR='${MANDIR}'
yann@1106
   443
yann@1106
   444
Now run:
yann@1106
   445
  make
yann@185
   446
__EOF__
yann@1298
   447
if [ "${LOCAL_set}" != "y" ]; then
yann@1297
   448
    printf "  make install\n"
yann@1297
   449
fi