configure
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Mon Oct 27 18:42:26 2008 +0000 (2008-10-27)
changeset 1001 c8ac48ba1411
parent 886 defdd986c14d
child 1017 34267fb0912e
permissions -rwxr-xr-x
Enhance the make fragments:
- comment the different parts
- re-order the code so that it is homogeneous amogst fragments
- eye-candy in some existing comments

/trunk/tools/tools.mk | 17 15 2 0 +++++++++++++++--
/trunk/steps.mk | 38 26 12 0 ++++++++++++++++++++++++++------------
/trunk/samples/samples.mk | 41 28 13 0 ++++++++++++++++++++++++++++-------------
/trunk/config/config.mk | 2 1 1 0 +-
4 files changed, 70 insertions(+), 28 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 awk/^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                   str=$(${tool} --version 2>&1 |grep -E "${regexp}" |head -n 1)
    87                   if [ -z "${str}" ]; then
    88                       echo "wrong version string"
    89                       where=""
    90                       continue
    91                   fi
    92               fi
    93               echo "${where}"
    94               return 0
    95           fi
    96       done;
    97     }
    98     [ ${FORCE} -eq 0 ] && do_error "Bailing out..."
    99     return 0
   100 }
   101 
   102 # Given an option string and the following argument,
   103 # echoes the value of the option.
   104 # If --var=val => echoes val and returns 0, meaning second arg was not consumed
   105 # If --var val => echoes val and returns non null, meaning second arg was used
   106 get_optval(){
   107     local ret
   108     case "$1" in
   109         --*=?*)
   110             echo "${1}" |cut -d '=' -f 2-
   111             ret=0
   112             ;;
   113         *)
   114             echo "${2}"
   115             ret=1
   116             ;;
   117     esac
   118     return ${ret}
   119 }
   120 
   121 # The set_xxx functions will set the corresponding configuration variable
   122 # They return 0 if second arg was not consumed, and non-zero if it was consumed.
   123 set_prefix() {
   124     PREFIX=$(get_optval "$1" "$2")
   125     return $?
   126 }
   127 set_bindir() {
   128     BINDIR_set=1
   129     BINDIR=$(get_optval "$1" "$2")
   130     return $?
   131 }
   132 set_libdir() {
   133     LIBDIR_set=1
   134     LIBDIR=$(get_optval "$1" "$2")
   135     return $?
   136 }
   137 set_docdir() {
   138     DOCDIR_set=1
   139     DOCDIR=$(get_optval "$1" "$2")
   140     return $?
   141 }
   142 set_mandir() {
   143     MANDIR_set=1
   144     MANDIR=$(get_optval "$1" "$2")
   145     return $?
   146 }
   147 
   148 # The set_contrib function is different in that it will work like the others,
   149 # except in two cases:
   150 # If all => replaces all with the list of all available contribs
   151 # If list => just echoes the list of all available contribs, and instructs
   152 # caller to quit immediately by setting do_quit to non null.
   153 # (can't use the return code, see above).
   154 set_contrib() {
   155     opt_val=$(get_optval "$1" "$2")
   156     local ret=$?
   157     case "${opt_val}" in
   158         all)
   159             CONTRIB_list=$(LC_ALL=C ls -1 contrib/*.patch.lzma  \
   160                            |xargs -I {} basename {} .patch.lzma \
   161                            |sed -r -e ':a; /$/N; s/\n/,/; ta;'
   162                           )
   163             ;;
   164         list)
   165             do_quit=1
   166             echo "Available contributions:"
   167             LC_ALL=C ls -1 contrib/*.patch.lzma     \
   168             |xargs -I {} basename {} .patch.lzma    \
   169             |sed -r -e 's/^/  /;'
   170             ;;
   171         *)  CONTRIB_list="${CONTRIB_list},${opt_val}";;
   172     esac
   173     return $ret
   174 }
   175 
   176 do_help() {
   177     cat <<__EOF__
   178 \`configure' configures crosstool-NG ${VERSION} to adapt to many kind of systems.
   179 
   180 USAGE: ./configure [OPTION]...
   181 
   182 Defaults for the options are specified in brackets.
   183 
   184 Configuration:
   185   -h, --help              display this help and exit                                                                                                                  
   186       --force             force ./configure to complete, even if one or more
   187                           tools were not found. Use at your own risk, only if
   188                           you know what you are doing!
   189 
   190 Installation directories:
   191   --prefix=PREFIX         install files in PREFIX [${PREFIX_DEFAULT}]
   192   --local                 don't install, and use current directory
   193 
   194 By default, \`make install' will install all the files in
   195 \`${PREFIX_DEFAULT}/bin', \`${PREFIX_DEFAULT}/lib' etc.  You can specify
   196 an installation prefix other than \`${PREFIX_DEFAULT}' using \`--prefix',
   197 for instance \`--prefix=\${HOME}'.
   198 
   199 For better control, use the options below.
   200 
   201 Fine tuning of the installation directories:
   202   --bindir=DIR            user executables [PREFIX/bin]
   203   --libdir=DIR            object code libraries [PREFIX/lib]
   204   --docdir=DIR            info documentation [PREFIX/share/doc]
   205   --mandir=DIR            man documentation [PREFIX/share/man]
   206 
   207 Optional Features:
   208   --with-contrib=XXX      Include externally contributed features found in the
   209                           contrib/ sub-directory. Set to a comma-separated list
   210                           of features. Use 'all' to use all contributions, and
   211                           'list' to see which are available.
   212 __EOF__
   213 }
   214 
   215 #---------------------------------------------------------------------
   216 # Set user's options
   217 
   218 while [ $# -ne 0 ]; do
   219     case "$1" in
   220         --prefix*)  set_prefix "$1" "$2" && shift || shift 2;;
   221         --bindir*)  set_bindir "$1" "$2" && shift || shift 2;;
   222         --libdir*)  set_libdir "$1" "$2" && shift || shift 2;;
   223         --docdir*)  set_docdir "$1" "$2" && shift || shift 2;;
   224         --mandir*)  set_mandir "$1" "$2" && shift || shift 2;;
   225         --local)    LOCAL_set=1; shift;;
   226         --force)    FORCE=1; shift;;
   227         --with-contrib*)
   228                     set_contrib "$1" "$2" && shift || shift 2
   229                     [ "${do_quit}" = "1" ] && exit 0
   230                     ;;
   231         --help|-h)  do_help; exit 0;;
   232         *)          do_help; exit 1;;
   233     esac
   234 done
   235 
   236 # Use defaults
   237 [ -z "${PREFIX}" ] && set_prefix "" "${PREFIX_DEFAULT}"
   238 
   239 # Special case when installing locally
   240 if [ "${LOCAL_set}" = "1" ]; then
   241     set_prefix "" $(pwd)
   242     set_bindir "" $(pwd)
   243     set_libdir "" $(pwd)
   244     set_docdir "" $(pwd)/docs
   245     set_mandir "" $(pwd)/docs
   246 fi
   247 
   248 #---------------------------------------------------------------------
   249 # Some sanity checks, now
   250 
   251 # If this version is a svn snapshot, try to get the revision number
   252 # If we can't get the revision number, use date
   253 printf "Computing version string... "
   254 case "${VERSION}" in
   255   *+svn)
   256     REVISION=$(LC_ALL=C svnversion)
   257     case "${REVISION}" in
   258       exported)
   259         VERSION="${VERSION}unknown@$(date +%Y%m%d.%H%M%S)";;
   260       *)
   261         URL=$(LC_ALL=C svn info 2>/dev/null |egrep 'URL: ' |cut -d ' ' -f 2-)
   262         ROOT=$(LC_ALL=C svn info 2>/dev/null |egrep 'Repository Root: ' |cut -d ' ' -f 3-)
   263         VERSION="${VERSION}${URL#${ROOT}}@${REVISION}"
   264         ;;
   265     esac
   266     # Arrange to have no / in the directory name, no need to create an
   267     # arbitrarily deep directory structure
   268     VERSION=$(echo "${VERSION}" |sed -r -e 's|/+|_|g;')
   269     ;;
   270 esac
   271 echo "${VERSION}"
   272 
   273 # Now we have the version string, we can build up the paths
   274 [ -z "${BINDIR_set}" ] && BINDIR="${PREFIX}/bin"
   275 [ -z "${LIBDIR_set}" ] && LIBDIR="${PREFIX}/lib/ct-ng-${VERSION}"
   276 [ -z "${DOCDIR_set}" ] && DOCDIR="${PREFIX}/share/doc/ct-ng-${VERSION}"
   277 [ -z "${MANDIR_set}" ] && MANDIR="${PREFIX}/share/man/man1"
   278 
   279 # Check the existence of absolutely required tools
   280 { IFS='
   281 ';
   282   for tool in ${TOOLS_TO_CHECK}; do
   283       has_or_abort "${tool}"
   284   done;
   285 }
   286 
   287 # It's safer to change all ',' to spaces rather than setting IFS
   288 CONTRIB_list=$(echo "${CONTRIB_list}" |sed -r -e 's/,+/ /g; s/ +/ /g; s/^ //g; s/ $//g;')
   289 if [ -n "${CONTRIB_list}" ]; then
   290     has_or_abort 'lzcat/'
   291     printf "Applying contributed code: "
   292     for c in ${CONTRIB_list}; do
   293         printf "${c}, "
   294         if [ ! -f "contrib/${c}.patch.lzma" ]; then
   295             do_error "Contribution '${c}' does not exist"
   296         fi
   297         lzcat "contrib/${c}.patch.lzma" |patch -p1 >/dev/null 2>&1
   298     done
   299     echo "done"
   300 fi
   301 
   302 printf "Building up Makefile... "
   303 sed -r -e "s,@@BINDIR@@,${BINDIR},g;"   \
   304        -e "s,@@LIBDIR@@,${LIBDIR},g;"   \
   305        -e "s,@@DOCDIR@@,${DOCDIR},g;"   \
   306        -e "s,@@MANDIR@@,${MANDIR},g;"   \
   307        -e "s,@@VERSION@@,${VERSION},g;" \
   308        -e "s,@@DATE@@,${DATE},g;"       \
   309        -e "s,@@LOCAL@@,${LOCAL_set},g;" \
   310        Makefile.in >Makefile
   311 echo "done"
   312 
   313 cat <<__EOF__
   314 
   315 crosstool-NG configured as follows:
   316   PREFIX='${PREFIX}'
   317   BINDIR='${BINDIR}'
   318   LIBDIR='${LIBDIR}'
   319   DOCDIR='${DOCDIR}'
   320   MANDIR='${MANDIR}'
   321   CONTRIB='${CONTRIB_list}'
   322 __EOF__