configure
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Sun Feb 01 18:40:16 2009 +0000 (2009-02-01)
changeset 1187 3a76b3242ed8
parent 1151 06893705782f
child 1213 61e11b37185a
permissions -rwxr-xr-x
Checking for grep and sed has been rationalised, and now emit the same messages as for the other tools.

/trunk/configure | 53 36 17 0 ++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 36 insertions(+), 17 deletions(-)
     1 #!/bin/sh
     2 
     3 VERSION=$( cat .version )
     4 DATE=$( date +%Y%m%d )
     5 
     6 # All absolutely required tools, one per line to ease diff.
     7 # See function 'has_or_abort, below, for syntax
     8 #  - Hopefully, if gcc is present, then all associated tools will be
     9 #  - awk must be GNU awk
    10 #  - makeinfo for building docs, even if discarded later on
    11 #  - others obvious... :-/
    12 #
    13 # Format of a pattern to check for, one per line:
    14 #   pattern := var_name : tool_pattern  OR  tool_pattern
    15 #   tool_pattern := tool_test  OR  tool_pattern || tool_test
    16 #   tool_test := tool=regexp OR tool
    17 #   tool := basename of the tool  OR  absolute pathname to the tool
    18 #   regexp := valid grep(1) extended regular expression, $( tool --version)
    19 #             will be matched against this regexp.
    20 #
    21 # In case a pattern list is given (eg foo || bar || buz), then tests are performed
    22 # from left to right, stopping at the first matching test (like the shell
    23 # would parse 'foo || bar || buz' ).
    24 #
    25 # Examples:
    26 #    bash:bash=^GNU bash, version 3\.
    27 #     - if ${bash} is set and non-null, does nothing
    28 #     - else ensures that bash exists in the PATH, and that
    29 #         "$( "$( which bash )" --version |head -n 1 )"
    30 #       matches the regexp '^GNU bash, version 3\.'
    31 #       - if so, then sets bash="$( which bash )"
    32 #    autoconf=(GNU Autoconf) || autoconf2.50
    33 #     - does not look at an existing variable
    34 #     - ensures that:
    35 #         - 'autoconf' is to be found in the PATH, and that $( autoconf --version |head -n 1 )
    36 #           matches the regexp '(GNU Autoconf)' (which btw is the signature of
    37 #           autoconf >= 2.50),
    38 #       OR that:
    39 #         - 'autoconf2.50' is to be found in the PATH
    40 #
    41 TOOLS_TO_CHECK='
    42 bash:bash=^GNU bash, version 3\.
    43 cut
    44 xargs
    45 install:install=GNU coreutils
    46 make:make=^GNU Make
    47 gcc
    48 awk:awk=^GNU Awk || gawk=^GNU Awk
    49 bison
    50 flex
    51 makeinfo
    52 automake=\(GNU automake\) (1\.[[:digit:]]{2,}\.|[2-9][[:digit:]]*\.)
    53 libtool=\(GNU libtool\) (2[[:digit:]]*\.|1\.6[[:digit:]]*\.|1\.5\.[2-9][[:digit:]]+)
    54 curl || wget
    55 patch
    56 tar
    57 gzip
    58 bzip2
    59 '
    60 
    61 PREFIX_DEFAULT=/usr/local
    62 
    63 BINDIR_set=
    64 LIBDIR_set=
    65 DOCDIR_set=
    66 MANDIR_set=
    67 LOCAL_set=
    68 
    69 do_quit=
    70 
    71 # Simply print the error message, and exit. Obvious, he?
    72 do_error() {
    73     echo "${@}"
    74     exit 1
    75 }
    76 
    77 # A small function to test for existence of various tools
    78 # Usage: has_or_abort test_pattern (see top of file, TOOLS_TO_CHECK, for
    79 #                                   complete pattern format)
    80 has_or_abort() {
    81     local save_IFS
    82     local var_name
    83     local var_value
    84     local tool_pattern
    85     local field
    86 
    87     var_name="$( echo "${1}" |"${sed}" -r -e 's/^(([^=:]+):.+|[^:=]+=.+|[^:=]+)$/\2/;' )"
    88     field="${var_name:+2}"
    89     field="${field:-1}"
    90     tool_pattern="$( echo "${1}" |cut -d : -f ${field}- |"${sed}" -r -e 's/ *\|\| */\n/g;' )"
    91 
    92     save_IFS="${IFS}"
    93     # Set IFS to \n only
    94     IFS='
    95 '
    96     for item in ${tool_pattern}; do
    97         case "${item}" in
    98             *=*)
    99                 tool="${item%%=*}"
   100                 regexp="${item#*=}"
   101                 ;;
   102             *)  tool="${item}"
   103                 regexp=
   104                 ;;
   105         esac
   106 
   107         printf "Checking for '${tool}'... "
   108         if [ -n "${var_name}" ]; then
   109             eval var_value='"${'"${var_name}"'}"'
   110             if [ -n "${var_value}" ]; then
   111                 echo "${var_value} (cached)"
   112                 return 0
   113             fi
   114         fi
   115         where=$( which "${tool}" 2>/dev/null )
   116         if [ -z "${where}" ]; then
   117             echo "not found"
   118             where=
   119             continue
   120         elif [ -n "${regexp}" ]; then
   121             tool_version=$( ${tool} --version 2>&1 )
   122             str=$( echo "${tool_version}" |"${grep}" -E "${regexp}" |head -n 1 )
   123             if [ -z "${str}" ]; then
   124                 echo "not found"
   125                 where=""
   126                 continue
   127             fi
   128         fi
   129         break
   130     done
   131     if [ -z "${where}" ]; then
   132         for item in ${tool_pattern}; do
   133             case "${item}" in
   134                 *=*)
   135                     tool="${item%%=*}"
   136                     regexp="${item#*=}"
   137                     ;;
   138                 *)  tool="${item}"
   139                     regexp=
   140                     ;;
   141             esac
   142             printf "  could not find '${tool}'"
   143             [ -n "${regexp}" ] && printf " matching regexp '${regexp}'"
   144             echo
   145         done
   146         echo "Either you are missing entirely the needed tool,"
   147         echo "or the version you have is tool old."
   148         if [ -n "${var_name}" ]; then
   149             echo "You can give the path to this tool using: --with-${var_name}=PATH"
   150         fi
   151         # FORCE can be set in the environment
   152         [ -z "${FORCE}" ] && do_error "Bailing out..."
   153     else
   154         echo "${where}"
   155         if [ -n "${var_name}" ]; then
   156             eval ${var_name}='"'"${where}"'"'
   157         fi
   158     fi
   159     IFS="${save_IFS}"
   160     return 0
   161 }
   162 
   163 # Given an option string and the following argument,
   164 # echoes the value of the option.
   165 # If --var=val => echoes val and returns 0, meaning second arg was not consumed
   166 # If --var val => echoes val and returns non null, meaning second arg was used
   167 get_optval(){
   168     case "$1" in
   169         --*=?*)
   170             echo "${1#*=}"
   171             return 0
   172             ;;
   173         *)
   174             echo "${2}"
   175             return 1
   176             ;;
   177     esac
   178 }
   179 
   180 # The set_xxx functions will set the corresponding configuration variable
   181 # They return 0 if second arg was not consumed, and non-zero if it was consumed.
   182 set_prefix() {
   183     PREFIX="$( get_optval "$1" "$2" )"
   184 }
   185 set_bindir() {
   186     BINDIR_set=1
   187     BINDIR="$( get_optval "$1" "$2" )"
   188 }
   189 set_libdir() {
   190     LIBDIR_set=1
   191     LIBDIR="$( get_optval "$1" "$2" )"
   192 }
   193 set_docdir() {
   194     DOCDIR_set=1
   195     DOCDIR="$( get_optval "$1" "$2" )"
   196 }
   197 set_mandir() {
   198     MANDIR_set=1
   199     MANDIR="$( get_optval "$1" "$2" )"
   200 }
   201 set_tool() {
   202     local var_name="${1%%=*}"
   203     var_name="${var_name#--with-}"
   204     eval ${var_name}="\$( get_optval "$1" "$2" )"
   205 }
   206 
   207 do_help() {
   208     cat <<__EOF__
   209 \`configure' configures crosstool-NG-${VERSION} to adapt to many kind of systems.
   210 
   211 USAGE: ./configure [OPTION]...
   212 
   213 Defaults for the options are specified in brackets.
   214 
   215 Configuration:
   216   -h, --help              display this help and exit
   217 
   218 Installation directories:
   219   --prefix=PREFIX         install files in PREFIX [${PREFIX_DEFAULT}]
   220   --local                 don't install, and use current directory
   221 
   222 By default, \`make install' will install all the files in
   223 \`${PREFIX_DEFAULT}/bin', \`${PREFIX_DEFAULT}/lib' etc.  You can specify
   224 an installation prefix other than \`${PREFIX_DEFAULT}' using \`--prefix',
   225 for instance \`--prefix=\${HOME}'.
   226 
   227 For better control, use the options below.
   228 
   229 Fine tuning of the installation directories:
   230   --bindir=DIR            user executables [PREFIX/bin]
   231   --libdir=DIR            object code libraries [PREFIX/lib]
   232   --docdir=DIR            info documentation [PREFIX/share/doc]
   233   --mandir=DIR            man documentation [PREFIX/share/man]
   234 
   235 Optional Features:
   236   --with-install=PATH     Specify the full PATH to GNU install
   237   --with-make=PATH        Specify the full PATH to GNU make
   238   --with-grep=PATH        Specify the full PATH to GNU grep
   239   --with-sed=PATH         Specify the full PATH to GNU sed
   240   --with-awk=PATH         Specify the full PATH to GNU awk
   241   --with-bash=PATH        Specify the full PATH to bash >= 3.0
   242 __EOF__
   243 }
   244 
   245 #---------------------------------------------------------------------
   246 # Set user's options
   247 
   248 while [ $# -ne 0 ]; do
   249     case "$1" in
   250         --local)    LOCAL_set=1; shift;;
   251         --prefix*)  set_prefix "$1" "$2" && shift || shift 2;;
   252         --bindir*)  set_bindir "$1" "$2" && shift || shift 2;;
   253         --libdir*)  set_libdir "$1" "$2" && shift || shift 2;;
   254         --docdir*)  set_docdir "$1" "$2" && shift || shift 2;;
   255         --mandir*)  set_mandir "$1" "$2" && shift || shift 2;;
   256         --with-*)   set_tool   "$1" "$2" && shift || shift 2;;
   257         --help|-h)  do_help; exit 0;;
   258         *)          echo "Unrecognised option: '${1}'"; do_help; exit 1;;
   259     esac
   260 done
   261 
   262 # Use defaults
   263 [ -z "${PREFIX}" ] && set_prefix "" "${PREFIX_DEFAULT}"
   264 
   265 # Special case when installing locally
   266 if [ "${LOCAL_set}" = "1" ]; then
   267     set_prefix "" "$( pwd )"
   268     set_bindir "" "$( pwd )"
   269     set_libdir "" "$( pwd )"
   270     set_docdir "" "$( pwd )/docs"
   271     set_mandir "" "$( pwd )/docs"
   272 fi
   273 
   274 #---------------------------------------------------------------------
   275 # Some sanity checks, now
   276 
   277 # We check for grep and sed manually, because they are used in has_or_abort
   278 printf "Checking for 'grep'... "
   279 if [ -n "${grep}" ]; then
   280     echo "${grep} (cached)"
   281 else
   282     grep="$( which grep 2>/dev/null )"
   283     if [ -z "${grep}" ]; then
   284         echo "not found"
   285     else
   286         echo "${grep}"
   287         printf "Checking whether '${grep}' supports -E... "
   288         if echo 'foo' |"${grep}" -E 'foo' >/dev/null 2>&1; then
   289             echo "yes"
   290         else
   291             echo "no"
   292             grep=
   293         fi
   294     fi
   295 fi
   296 if [ -z "${grep}" ]; then
   297     echo "Either you are missing entirely the needed tool,"
   298     echo "or the version you have is tool old."
   299     echo "You can give the path to this tool using: --with-grep=PATH"
   300     do_error "Bailing out..."
   301 fi
   302 
   303 printf "Checking for 'sed'... "
   304 if [ -n "${sed}" ]; then
   305     echo "${sed} (cached)"
   306 else
   307     sed="$( which sed 2>/dev/null )"
   308     if [ -z "${sed}" ]; then
   309         echo "not found"
   310     else
   311         echo "${sed}"
   312         printf "Checking wether '${sed}' supports -i and -e... "
   313         touch .ct-ng.sed.test
   314         if "${sed}" -r -i -e 's/foo/bar/' .ct-ng.sed.test >/dev/null 2>&1; then
   315             echo "yes"
   316         else
   317             echo "no"
   318             sed=
   319         fi
   320         rm -f .ct-ng.sed.test
   321     fi
   322 fi
   323 if [ -z "${sed}" ]; then
   324     echo "Either you are missing entirely the needed tool,"
   325     echo "or the version you have is tool old."
   326     echo "You can give the path to this tool using: --with-sed=PATH"
   327     do_error "Bailing out..."
   328 fi
   329 
   330 # Check the existence of absolutely required tools
   331 save_IFS="${IFS}"
   332 IFS='
   333 '
   334 for tool in ${TOOLS_TO_CHECK}; do
   335     has_or_abort "${tool}"
   336 done
   337 IFS="${save_IFS}"
   338 
   339 #---------------------------------------------------------------------
   340 # Compute the version string
   341 
   342 # If this version is a svn snapshot, try to get the revision number
   343 # If we can't get the revision number, use date
   344 printf "Computing version string... "
   345 case "${VERSION}" in
   346     *+svn|svn)
   347         REVISION="$( LC_ALL=C svnversion )"
   348         case "${REVISION}" in
   349             exported)
   350                 VERSION="${VERSION}_unknown@$( date +%Y%m%d.%H%M%S )";;
   351             *)
   352                 URL="$( LC_ALL=C svn info 2>/dev/null   \
   353                                  |egrep 'URL: '         \
   354                                  |cut -d ' ' -f 2-      \
   355                       )"
   356                 ROOT="$( LC_ALL=C svn info 2>/dev/null      \
   357                          |"${grep}" '^Repository Root: '    \
   358                          |cut -d ' ' -f 3-                  \
   359                        )"
   360                 VERSION="${VERSION}${URL#${ROOT}}@${REVISION}"
   361                 ;;
   362         esac
   363         # Arrange to have no / in the directory name, no need to create an
   364         # arbitrarily deep directory structure
   365         VERSION="$( echo "${VERSION}" |"${sed}" -r -e 's|/+|_|g;' )"
   366         ;;
   367 esac
   368 echo "${VERSION}"
   369 
   370 #---------------------------------------------------------------------
   371 # Compute and check install paths
   372 
   373 # Now we have the version string, we can build up the paths
   374 [ -z "${BINDIR_set}" ] && BINDIR="${PREFIX}/bin"
   375 [ -z "${LIBDIR_set}" ] && LIBDIR="${PREFIX}/lib/ct-ng-${VERSION}"
   376 [ -z "${DOCDIR_set}" ] && DOCDIR="${PREFIX}/share/doc/ct-ng-${VERSION}"
   377 [ -z "${MANDIR_set}" ] && MANDIR="${PREFIX}/share/man/man1"
   378 
   379 # Check that install PATHs are absolute
   380 for p in BIN LIB DOC MAN; do
   381     var="${p}DIR"
   382     eval v='"${'"${var}"'}"'
   383     case "${v}" in
   384         /*) ;;
   385         *)  do_error "'${var}' is not an absolute path: '${v}'"
   386     esac
   387 done
   388 
   389 #---------------------------------------------------------------------
   390 # That's all, folks!
   391 
   392 printf "Building up Makefile... "
   393 var_list="grep
   394           sed
   395           $( printf "${TOOLS_TO_CHECK}"                                 \
   396              |"${sed}" -r -e 's/^(([^=:]+):.+|[^:=]+=.+|[^:=]+)$/\2/;'
   397            )"
   398 var_sed="$( for var_name in ${var_list}; do
   399                 eval echo 's,@@${var_name}@@,${'"${var_name}"'},g'
   400             done 
   401           )"
   402 "${sed}" -r -e "s,@@BINDIR@@,${BINDIR},g
   403                 s,@@LIBDIR@@,${LIBDIR},g
   404                 s,@@DOCDIR@@,${DOCDIR},g
   405                 s,@@MANDIR@@,${MANDIR},g
   406                 s,@@VERSION@@,${VERSION},g
   407                 s,@@DATE@@,${DATE},g
   408                 ${var_sed}
   409                 s,@@LOCAL@@,${LOCAL_set},g"  Makefile.in >Makefile
   410 echo "done"
   411 
   412 cat <<__EOF__
   413 
   414 crosstool-NG configured as follows:
   415   PREFIX='${PREFIX}'
   416   BINDIR='${BINDIR}'
   417   LIBDIR='${LIBDIR}'
   418   DOCDIR='${DOCDIR}'
   419   MANDIR='${MANDIR}'
   420 
   421 Now run:
   422   make
   423   make install
   424 __EOF__