configure
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Wed Jul 16 21:59:49 2008 +0000 (2008-07-16)
changeset 673 a78a4bcc62d6
parent 641 c20c96f8e999
child 674 ab1fa87c138d
permissions -rwxr-xr-x
Introduce a list of tools to check for at ./configure time.
Move some functions around (no code change).
Only tell about applying contribs when there are contribs to apply.
Some eye-candy here and there.

/trunk/configure | 94 75 19 0 +++++++++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 75 insertions(+), 19 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 TOOLS_TO_CHECK='
    13 make/^GNU
    14 gcc
    15 awk/^GNU
    16 sed
    17 bison
    18 flex
    19 makeinfo
    20 patch
    21 tar
    22 gzip
    23 bzip2
    24 foo/^GNU
    25 '
    26 
    27 PREFIX_DEFAULT=/usr/local
    28 
    29 BINDIR_set=
    30 LIBDIR_set=
    31 DOCDIR_set=
    32 MANDIR_set=
    33 LOCAL_set=
    34 
    35 do_quit=
    36 CONTRIB_list=
    37 
    38 # Simply print the error message, and exit. Obvious, he?
    39 do_error() {
    40     echo "${@}"
    41     exit 1
    42 }
    43 
    44 # A small function to test for existence of various tools
    45 # Usage: has_or_abort foobar
    46 #          -> foobar must exist in PATH
    47 # Usage: has_or_abort foobar/string
    48 #          -> foobar must exist in PATH, and $(foobar --version) must contain 'string'
    49 has_or_abort() {
    50     tool=$(echo "${1}/" |cut -d / -f 1)
    51     regexp=$(echo "${1}/" |cut -d / -f 2)
    52     printf "Checking for '${tool}'... "
    53     where=$(which "${tool}" 2>/dev/null || true)
    54     if [ -z "${where}" ]; then
    55         do_error "not found!"
    56     else
    57         printf "${where}"
    58         if [ -n "${regexp}" ]; then
    59             str=$(${tool} --version 2>&1 |egrep "${regexp}" |head -n 1)
    60             if [ -z "${str}" ]; then
    61                 do_error " failed: '${tool} --version' does not match regexp '${regexp}'"
    62             fi
    63         fi
    64         echo
    65     fi
    66     return 0
    67 }
    68 
    69 # Given an option string and the following argument,
    70 # echoes the value of the option.
    71 # If --var=val => echoes val and returns 0, meaning second arg was not consumed
    72 # If --var val => echoes val and returns non null, meaning second arg was used
    73 get_optval(){
    74     local ret
    75     case "$1" in
    76         --*=?*)
    77             echo "${1}" |cut -d '=' -f 2-
    78             ret=0
    79             ;;
    80         *)
    81             echo "${2}"
    82             ret=1
    83             ;;
    84     esac
    85     return ${ret}
    86 }
    87 
    88 # The set_xxx functions will set the corresponding configuration variable
    89 # They return 0 if second arg was not consumed, and non-zero if it was consumed.
    90 set_prefix() {
    91     PREFIX=$(get_optval "$1" "$2")
    92     return $?
    93 }
    94 set_bindir() {
    95     BINDIR_set=1
    96     BINDIR=$(get_optval "$1" "$2")
    97     return $?
    98 }
    99 set_libdir() {
   100     LIBDIR_set=1
   101     LIBDIR=$(get_optval "$1" "$2")
   102     return $?
   103 }
   104 set_docdir() {
   105     DOCDIR_set=1
   106     DOCDIR=$(get_optval "$1" "$2")
   107     return $?
   108 }
   109 set_mandir() {
   110     MANDIR_set=1
   111     MANDIR=$(get_optval "$1" "$2")
   112     return $?
   113 }
   114 
   115 # The set_contrib function is different in that it will work like the others,
   116 # except in two cases:
   117 # If all => replaces all with the list of all available contribs
   118 # If list => just echoes the list of all available contribs, and instructs
   119 # caller to quit immediately by setting do_quit to non null.
   120 # (can't use the return code, see above).
   121 set_contrib() {
   122     opt_val=$(get_optval "$1" "$2")
   123     local ret=$?
   124     case "${opt_val}" in
   125         all)
   126             CONTRIB_list=$(LC_ALL=C ls -1 contrib/*.patch.lzma  \
   127                            |xargs -I {} basename {} .patch.lzma \
   128                            |sed -r -e ':a; /$/N; s/\n/,/; ta;'
   129                           )
   130             ;;
   131         list)
   132             do_quit=1
   133             echo "Available contributions:"
   134             LC_ALL=C ls -1 contrib/*.patch.lzma     \
   135             |xargs -I {} basename {} .patch.lzma    \
   136             |sed -r -e 's/^/  /;'
   137             ;;
   138         *)  CONTRIB_list="${CONTRIB_list},${opt_val}";;
   139     esac
   140     return $ret
   141 }
   142 
   143 do_help() {
   144     cat <<__EOF__
   145 \`configure' configures crosstool-NG ${VERSION} to adapt to many kind of systems.
   146 
   147 USAGE: ./configure [OPTION]...
   148 
   149 Defaults for the options are specified in brackets.
   150 
   151 Configuration:
   152   -h, --help              display this help and exit                                                                                                                  
   153 
   154 Installation directories:
   155   --prefix=PREFIX         install files in PREFIX [${PREFIX_DEFAULT}]
   156   --local                 don't install, and use current directory
   157 
   158 By default, \`make install' will install all the files in
   159 \`${PREFIX_DEFAULT}/bin', \`${PREFIX_DEFAULT}/lib' etc.  You can specify
   160 an installation prefix other than \`${PREFIX_DEFAULT}' using \`--prefix',
   161 for instance \`--prefix=\${HOME}'.
   162 
   163 For better control, use the options below.
   164 
   165 Fine tuning of the installation directories:
   166   --bindir=DIR           user executables [PREFIX/bin]
   167   --libdir=DIR           object code libraries [PREFIX/lib]
   168   --docdir=DIR           info documentation [PREFIX/share/doc]
   169   --mandir=DIR           man documentation [PREFIX/share/man]
   170 
   171 Optional Features:
   172   --with-contrib=XXX     Include externally contributed features found in the
   173                          contrib/ sub-directory. Set to a comma-separated list
   174                          of features. Use 'all' to use all contributions, and
   175                          'list' to see which are available.
   176 __EOF__
   177 }
   178 
   179 #---------------------------------------------------------------------
   180 # Set user's options
   181 
   182 while [ $# -ne 0 ]; do
   183     case "$1" in
   184         --prefix*)  set_prefix "$1" "$2" && shift || shift 2;;
   185         --bindir*)  set_bindir "$1" "$2" && shift || shift 2;;
   186         --libdir*)  set_libdir "$1" "$2" && shift || shift 2;;
   187         --docdir*)  set_docdir "$1" "$2" && shift || shift 2;;
   188         --mandir*)  set_mandir "$1" "$2" && shift || shift 2;;
   189         --local)    LOCAL_set=1; shift;;
   190         --with-contrib*)
   191                     set_contrib "$1" "$2" && shift || shift 2
   192                     [ "${do_quit}" = "1" ] && exit 0
   193                     ;;
   194         --help|-h)  do_help; exit 0;;
   195         *)          do_help; exit 1;;
   196     esac
   197 done
   198 
   199 # Use defaults
   200 [ -z "${PREFIX}" ] && set_prefix "" "${PREFIX_DEFAULT}"
   201 
   202 # Special case when installing locally
   203 if [ "${LOCAL_set}" = "1" ]; then
   204     set_prefix "" $(pwd)
   205     set_bindir "" $(pwd)
   206     set_libdir "" $(pwd)
   207     set_docdir "" $(pwd)/docs
   208     set_mandir "" $(pwd)/docs
   209 fi
   210 
   211 #---------------------------------------------------------------------
   212 # Some sanity checks, now
   213 
   214 # If this version is a svn snapshot, try to get the revision number
   215 # If we can't get the revision number, use date
   216 printf "Computing version string... "
   217 case "${VERSION}" in
   218   *+svn)
   219     REVISION=$(LC_ALL=C svnversion)
   220     case "${REVISION}" in
   221       exported)
   222         VERSION="${VERSION}unknown@$(date +%Y%m%d.%H%M%S)";;
   223       *)
   224         URL=$(LC_ALL=C svn info 2>/dev/null |egrep 'URL: ' |cut -d ' ' -f 2-)
   225         ROOT=$(LC_ALL=C svn info 2>/dev/null |egrep 'Repository Root: ' |cut -d ' ' -f 3-)
   226         VERSION="${VERSION}${URL#${ROOT}}@${REVISION}"
   227         ;;
   228     esac
   229     # Arrange to have no / in the directory name, no need to create an
   230     # arbitrarily deep directory structure
   231     VERSION=$(echo "${VERSION}" |sed -r -e 's|/+|_|g;')
   232     ;;
   233 esac
   234 echo "${VERSION}"
   235 
   236 # Now we have the version string, we can build up the paths
   237 [ -z "${BINDIR_set}" ] && BINDIR="${PREFIX}/bin"
   238 [ -z "${LIBDIR_set}" ] && LIBDIR="${PREFIX}/lib/ct-ng-${VERSION}"
   239 [ -z "${DOCDIR_set}" ] && DOCDIR="${PREFIX}/share/doc/ct-ng-${VERSION}"
   240 [ -z "${MANDIR_set}" ] && MANDIR="${PREFIX}/share/man/man1"
   241 
   242 # Check bash is present, and at least version 3.0
   243 printf "Checking '/bin/bash' is at least bash-3.0... "
   244 [ -x /bin/bash ] || do_error "bash 3.0 or above was not found in '/bin/bash'"
   245 bash_version=$(/bin/bash -c 'echo ${BASH_VERSION}')
   246 bash_major=$(/bin/bash -c 'echo ${BASH_VERSINFO[0]}')
   247 [ ${bash_major} -ge 3 ] || do_error "bash 3.0 or above is needed ('/bin/bash' is '${bash_version}')"
   248 echo "${bash_version}"
   249 
   250 # Check the existence of absolutely required tools
   251 for tool in ${TOOLS_TO_CHECK}; do
   252     has_or_abort "${tool}"
   253 done
   254 
   255 # It's safer to change all ',' to spaces rather than setting IFS
   256 CONTRIB_list=$(echo "${CONTRIB_list}" |sed -r -e 's/,+/ /g;')
   257 if [ -n "${CONTRIB_list}" ]; then
   258     has_or_abort lzcat
   259     printf "Applying contributed code: "
   260     for c in ${CONTRIB_list}; do
   261         printf "${c}, "
   262         if [ ! -f "contrib/${c}.patch.lzma" ]; then
   263             do_error "Contribution '${c}' does not exist"
   264         fi
   265         lzcat "contrib/${c}.patch.lzma" |patch -p1 >/dev/null 2>&1
   266     done
   267     echo "done"
   268 fi
   269 
   270 printf "Building up Makefile... "
   271 sed -r -e "s,@@BINDIR@@,${BINDIR},g;"   \
   272        -e "s,@@LIBDIR@@,${LIBDIR},g;"   \
   273        -e "s,@@DOCDIR@@,${DOCDIR},g;"   \
   274        -e "s,@@MANDIR@@,${MANDIR},g;"   \
   275        -e "s,@@VERSION@@,${VERSION},g;" \
   276        -e "s,@@DATE@@,${DATE},g;"       \
   277        -e "s,@@LOCAL@@,${LOCAL_set},g;" \
   278        Makefile.in >Makefile
   279 echo "done"
   280 
   281 cat <<__EOF__
   282 
   283 crosstool-NG configured as follows:
   284   PREFIX='${PREFIX}'
   285   BINDIR='${BINDIR}'
   286   LIBDIR='${LIBDIR}'
   287   DOCDIR='${DOCDIR}'
   288   MANDIR='${MANDIR}'
   289   CONTRIB='${CONTRIB_list}'
   290 __EOF__