#!/bin/bash
#---------------------------------------------------------------------
##
## @Synopsis Set of functions for dealing with the problem of having to
## @Synopsis use different version of the gcc compilers.
##
## The basic usage is as follows:
## <pre>
## - If a spell works with the latest version of gcc, do nothing.
## - To change the compiler version for a spell, add a GCC_VERSION field
##   to the spells DETAILS specifying major.minor version of gcc it needs,
##   e.g. GCC_VERSION=3.4
## - add a 'depends  gccXX' to the spell where XX == majorminor,
##   e.g. 'depends  gcc34'
## - add 'invoke_gcc' to the top of PRE_BUILD if the spell has a
##   custom PRE_BUILD file
## </pre>
##
## @Implementation These functions were added to allow the use of
## @Implementation several versions of gcc in parallel.
##
##
##  @Copyright Copyright 2005 by the Source Mage Team
##
##
#---------------------------------------------------------------------

#---------------------------------------------------------------------
## @param gcc version (digits and dots, followed by an optional +)
## @return 0 if the version of the installed gcc that will be used is equal
##             to the passed version
## @return 0 if the parameter is of the form "version+" and the version of the
##             installed gcc is at least "version"
## @return 1 otherwise
#---------------------------------------------------------------------
real_using_gcc()  {
  : ${CC:=gcc}
  # Check if it's GCC we're using
  case "$CC" in *gcc);; *) return 1;; esac

  local needed_version=${1%+}
  local installed_version=$("$CC" -dumpversion)

  if [[ "$needed_version" == "$1" ]]; then
    grep -qE "^$(esc_str $needed_version)(\.0)*$" <<< "$installed_version"
  else
    local i=0 needed_version2 installed_version2
    explode "$needed_version" . needed_version2
    explode "$installed_version" . installed_version2

    while [[ ${needed_version2[$i]} ]] || [[ ${installed_version2[$i]} ]]; do
      if [[ ${needed_version2[$i]:-0} == ${installed_version2[$i]:-0} ]]; then
        let i++
        continue
      fi
      # capturing the return value of arithmetic comparison inverts it
      return $(( ${needed_version2[$i]:-0} > ${installed_version2[$i]:-0} ))
    done
  fi
}

#---------------------------------------------------------------------
## @param path
##
## Alters the environment so that the gcc in the specified directory
## will be used for compiling
##
#---------------------------------------------------------------------
gcc_add_paths()  {
  export PATH=$(envar_prepend_path $1/bin $PATH)
  export LD_LIBRARY_PATH=$(envar_prepend_path $1/lib $LD_LIBRARY_PATH)
  export LD_RUN_PATH=$(envar_prepend_path $1/lib $LD_RUN_PATH)
  export INFOPATH=$(envar_prepend_path $1/info $INFOPATH)
  export CPPFLAGS="-I $1/include  $CPPFLAGS"
}

#---------------------------------------------------------------------
##
## Determines if the spell specifies a gcc version to use. If so, it
## alters the environment so that that gcc version is used for compilation.
##
#---------------------------------------------------------------------
invoke_gcc()  {
  # $GCC_VERSION contains only two of the version fields, but any patchlevel
  # will do, so we pass a regex to the otherwise stricter real_using_gcc
  if [[ $GCC_VERSION ]] && ! real_using_gcc "$GCC_VERSION(\.[0-9])?"; then
    gcc_add_paths /opt/gcc${GCC_VERSION//.}
  elif [[ -f $SCRIPT_DIRECTORY/USEGCC2 ]]; then
    gcc_add_paths /opt/gcc2
  fi
  echo  "Using gcc version: $(gcc -dumpversion)"
}

# backward compatibility functions; currently still used in archspecs,
# since they were part of sorcery before
use_gcc() {
  real_using_gcc "$@"
}
use_gcc2() {
  real_using_gcc 2.95
}

#---------------------------------------------------------------------
##
## This software is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This software is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this software; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##
#---------------------------------------------------------------------
