configure
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Sun Jan 18 15:08:28 2009 +0000 (2009-01-18)
changeset 1140 6bdb31785d6c
parent 1132 c232833120c1
child 1151 06893705782f
permissions -rwxr-xr-x
./configure: enable user to specify path to some tools

/trunk/configure | 203 107 96 0 +++++++++++++++++++++++++++++++++-----------------------------
1 file changed, 107 insertions(+), 96 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/^((([^=:]+):.+|[^:=]+)=.+|[^:=]+)$/\3/;' )"
    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             echo "  could not find '${tool}' matching regexp '${regexp}'"
   143         done
   144         echo "Either you are missing entirely the needed tool,"
   145         echo "or the version you have is tool old."
   146         # FORCE can be set in the environment
   147         [ -z "${FORCE}" ] && do_error "Bailing out..."
   148     else
   149         echo "${where}"
   150         if [ -n "${var_name}" ]; then
   151             eval ${var_name}='"'"${where}"'"'
   152         fi
   153     fi
   154     IFS="${save_IFS}"
   155     return 0
   156 }
   157 
   158 # Given an option string and the following argument,
   159 # echoes the value of the option.
   160 # If --var=val => echoes val and returns 0, meaning second arg was not consumed
   161 # If --var val => echoes val and returns non null, meaning second arg was used
   162 get_optval(){
   163     case "$1" in
   164         --*=?*)
   165             echo "${1#*=}"
   166             return 0
   167             ;;
   168         *)
   169             echo "${2}"
   170             return 1
   171             ;;
   172     esac
   173 }
   174 
   175 # The set_xxx functions will set the corresponding configuration variable
   176 # They return 0 if second arg was not consumed, and non-zero if it was consumed.
   177 set_prefix() {
   178     PREFIX="$( get_optval "$1" "$2" )"
   179 }
   180 set_bindir() {
   181     BINDIR_set=1
   182     BINDIR="$( get_optval "$1" "$2" )"
   183 }
   184 set_libdir() {
   185     LIBDIR_set=1
   186     LIBDIR="$( get_optval "$1" "$2" )"
   187 }
   188 set_docdir() {
   189     DOCDIR_set=1
   190     DOCDIR="$( get_optval "$1" "$2" )"
   191 }
   192 set_mandir() {
   193     MANDIR_set=1
   194     MANDIR="$( get_optval "$1" "$2" )"
   195 }
   196 set_tool() {
   197     local var_name="${1%%=*}"
   198     var_name="${var_name#--with-}"
   199     eval ${var_name}="\$( get_optval "$1" "$2" )"
   200 }
   201 
   202 do_help() {
   203     cat <<__EOF__
   204 \`configure' configures crosstool-NG-${VERSION} to adapt to many kind of systems.
   205 
   206 USAGE: ./configure [OPTION]...
   207 
   208 Defaults for the options are specified in brackets.
   209 
   210 Configuration:
   211   -h, --help              display this help and exit
   212 
   213 Installation directories:
   214   --prefix=PREFIX         install files in PREFIX [${PREFIX_DEFAULT}]
   215   --local                 don't install, and use current directory
   216 
   217 By default, \`make install' will install all the files in
   218 \`${PREFIX_DEFAULT}/bin', \`${PREFIX_DEFAULT}/lib' etc.  You can specify
   219 an installation prefix other than \`${PREFIX_DEFAULT}' using \`--prefix',
   220 for instance \`--prefix=\${HOME}'.
   221 
   222 For better control, use the options below.
   223 
   224 Fine tuning of the installation directories:
   225   --bindir=DIR            user executables [PREFIX/bin]
   226   --libdir=DIR            object code libraries [PREFIX/lib]
   227   --docdir=DIR            info documentation [PREFIX/share/doc]
   228   --mandir=DIR            man documentation [PREFIX/share/man]
   229 
   230 Optional Features:
   231   --with-install=PATH     Specify the full PATH to GNU install
   232   --with-make=PATH        Specify the full PATH to GNU make
   233   --with-grep=PATH        Specify the full PATH to GNU grep
   234   --with-sed=PATH         Specify the full PATH to GNU sed
   235   --with-awk=PATH         Specify the full PATH to GNU awk
   236   --with-bash=PATH        Specify the full PATH to bash >= 3.0
   237 __EOF__
   238 }
   239 
   240 #---------------------------------------------------------------------
   241 # Set user's options
   242 
   243 while [ $# -ne 0 ]; do
   244     case "$1" in
   245         --local)    LOCAL_set=1; shift;;
   246         --prefix*)  set_prefix "$1" "$2" && shift || shift 2;;
   247         --bindir*)  set_bindir "$1" "$2" && shift || shift 2;;
   248         --libdir*)  set_libdir "$1" "$2" && shift || shift 2;;
   249         --docdir*)  set_docdir "$1" "$2" && shift || shift 2;;
   250         --mandir*)  set_mandir "$1" "$2" && shift || shift 2;;
   251         --with-*)   set_tool   "$1" "$2" && shift || shift 2;;
   252         --help|-h)  do_help; exit 0;;
   253         *)          echo "Unrecognised option: '${1}'"; do_help; exit 1;;
   254     esac
   255 done
   256 
   257 # Use defaults
   258 [ -z "${PREFIX}" ] && set_prefix "" "${PREFIX_DEFAULT}"
   259 
   260 # Special case when installing locally
   261 if [ "${LOCAL_set}" = "1" ]; then
   262     set_prefix "" "$( pwd )"
   263     set_bindir "" "$( pwd )"
   264     set_libdir "" "$( pwd )"
   265     set_docdir "" "$( pwd )/docs"
   266     set_mandir "" "$( pwd )/docs"
   267 fi
   268 
   269 #---------------------------------------------------------------------
   270 # Some sanity checks, now
   271 
   272 # We check for grep and sed manually, because they are used in has_or_abort
   273 printf "Checking for 'grep'... "
   274 if [ -n "${grep}" ]; then
   275     echo "${grep} (cached)"
   276 else
   277     grep="$( which grep 2>/dev/null )"
   278     [ -z "${grep}" ] && do_error "not found"
   279     echo "${grep}"
   280 fi
   281 printf "Checking whether '${grep}' supports -E... "
   282 if echo 'foo' |"${grep}" -E 'foo' >/dev/null 2>&1; then
   283     echo "yes"
   284 else
   285     do_error "no"
   286 fi
   287 
   288 printf "Checking for 'sed'... "
   289 if [ -n "${sed}" ]; then
   290     echo "${sed} (cached)"
   291 else
   292     sed="$( which sed 2>/dev/null )"
   293     [ -z "${sed}" ] && do_error "not found"
   294     echo "${sed}"
   295 fi
   296 printf "Checking wether '${sed}' supports -i and -e... "
   297 touch .ct-ng.sed.test
   298 if "${sed}" -r -i -e 's/foo/bar/' .ct-ng.sed.test >/dev/null 2>&1; then
   299     rm -f .ct-ng.sed.test
   300     echo "yes"
   301 else
   302     rm -f .ct-ng.sed.test
   303     do_error "no"
   304 fi
   305 
   306 # Check the existence of absolutely required tools
   307 save_IFS="${IFS}"
   308 IFS='
   309 '
   310 for tool in ${TOOLS_TO_CHECK}; do
   311     has_or_abort "${tool}"
   312 done
   313 IFS="${save_IFS}"
   314 
   315 #---------------------------------------------------------------------
   316 # Compute the version string
   317 
   318 # If this version is a svn snapshot, try to get the revision number
   319 # If we can't get the revision number, use date
   320 printf "Computing version string... "
   321 case "${VERSION}" in
   322     *+svn|svn)
   323         REVISION="$( LC_ALL=C svnversion )"
   324         case "${REVISION}" in
   325             exported)
   326                 VERSION="${VERSION}_unknown@$( date +%Y%m%d.%H%M%S )";;
   327             *)
   328                 URL="$( LC_ALL=C svn info 2>/dev/null   \
   329                                  |egrep 'URL: '         \
   330                                  |cut -d ' ' -f 2-      \
   331                       )"
   332                 ROOT="$( LC_ALL=C svn info 2>/dev/null      \
   333                          |"${grep}" '^Repository Root: '    \
   334                          |cut -d ' ' -f 3-                  \
   335                        )"
   336                 VERSION="${VERSION}${URL#${ROOT}}@${REVISION}"
   337                 ;;
   338         esac
   339         # Arrange to have no / in the directory name, no need to create an
   340         # arbitrarily deep directory structure
   341         VERSION="$( echo "${VERSION}" |"${sed}" -r -e 's|/+|_|g;' )"
   342         ;;
   343 esac
   344 echo "${VERSION}"
   345 
   346 #---------------------------------------------------------------------
   347 # Compute and check install paths
   348 
   349 # Now we have the version string, we can build up the paths
   350 [ -z "${BINDIR_set}" ] && BINDIR="${PREFIX}/bin"
   351 [ -z "${LIBDIR_set}" ] && LIBDIR="${PREFIX}/lib/ct-ng-${VERSION}"
   352 [ -z "${DOCDIR_set}" ] && DOCDIR="${PREFIX}/share/doc/ct-ng-${VERSION}"
   353 [ -z "${MANDIR_set}" ] && MANDIR="${PREFIX}/share/man/man1"
   354 
   355 # Check that install PATHs are absolute
   356 for p in BIN LIB DOC MAN; do
   357     var="${p}DIR"
   358     eval v='"${'"${var}"'}"'
   359     case "${v}" in
   360         /*) ;;
   361         *)  do_error "'${var}' is not an absolute path: '${v}'"
   362     esac
   363 done
   364 
   365 #---------------------------------------------------------------------
   366 # That's all, folks!
   367 
   368 printf "Building up Makefile... "
   369 var_list="grep
   370           sed
   371           $( printf "${TOOLS_TO_CHECK}"                                 \
   372              |"${sed}" -r -e 's/^((([^=:]+):.+|[^:=]+)=.+|[^:=]+)$/\3/;'
   373            )"
   374 var_sed="$( for var_name in ${var_list}; do
   375                 eval echo 's,@@${var_name}@@,${'"${var_name}"'},g'
   376             done 
   377           )"
   378 "${sed}" -r -e "s,@@BINDIR@@,${BINDIR},g
   379                 s,@@LIBDIR@@,${LIBDIR},g
   380                 s,@@DOCDIR@@,${DOCDIR},g
   381                 s,@@MANDIR@@,${MANDIR},g
   382                 s,@@VERSION@@,${VERSION},g
   383                 s,@@DATE@@,${DATE},g
   384                 ${var_sed}
   385                 s,@@LOCAL@@,${LOCAL_set},g"  Makefile.in >Makefile
   386 echo "done"
   387 
   388 cat <<__EOF__
   389 
   390 crosstool-NG configured as follows:
   391   PREFIX='${PREFIX}'
   392   BINDIR='${BINDIR}'
   393   LIBDIR='${LIBDIR}'
   394   DOCDIR='${DOCDIR}'
   395   MANDIR='${MANDIR}'
   396 
   397 Now run:
   398   make
   399   make install
   400 __EOF__