configure
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Wed Oct 29 22:27:30 2008 +0000 (2008-10-29)
changeset 1017 34267fb0912e
parent 937 12e98d88cf09
child 1047 0c450efc5e3f
permissions -rwxr-xr-x
Use 'gawk', not plain 'awk'.
We need GNU Awk? Then check for, and use 'gawk', not plain 'awk'.
Be a little mre verbose if a tool was not found.

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