#!/bin/sh
#
# ${R_HOME}/bin/SHLIB

# @configure_input@

revision='$Rev$'
version=`set - ${revision}; echo ${2}`
version="R shared library builder ${version}

Copyright (C) 2000-2007 The R Core Development Team.
This is free software; see the GNU General Public License version 2
or later for copying conditions.  There is NO warranty."

usage="Usage: R CMD SHLIB [options] files|linker options

Build a shared library for dynamic loading from the specified source or
object files (which are automagically made from their sources) or 
linker options.  If not given via '--output', the name for the shared
library is determined from the first source or object file.

Options:
  -h, --help		print short help message and exit
  -v, --version		print SHLIB version info and exit
  -o, --output=LIB	use LIB as (full) name for the built library
  -c, --clean		remove files created during compilation
  --preclean		remove files created during a previous run

Report bugs to <r-bugs@r-project.org>."

R_HOME=`echo ${R_HOME} | sed 's/ /\\\\ /g'`

objs=
shlib=
makefiles="-f ${R_SHARE_DIR}/make/shlib.mk"
shlib_libadd='@SHLIB_LIBADD@'
with_cxx=false
with_f77=false
with_f9x=false
with_objc=false
with_objcxx=false
pkg_libs=
clean=false
preclean=false

while test -n "${1}"; do
  case ${1} in
    -h|--help)
       echo "${usage}"; exit 0 ;;
    -v|--version)
       echo "${version}"; exit 0 ;;
    -c|--clean)
      clean=true ;;
    --preclean)
      preclean=true ;;
    -o)
      shlib=${2}; shift ;;
    --output=*)
      shlib=`echo "${1}" | sed -e 's/[^=]*=//'` ;;
    *.cc|*.cpp|*.C)
      with_cxx=true
      if test -z "${shlib}"; then
	shlib="`echo ${1} | sed 's/\.[^\.][^\.]*$/@SHLIB_EXT@/'`"
      fi
      objs="${objs} `echo ${1} | sed 's/\.[^\.][^\.]*$/.o/'`"
      ;;
    *.m)
      with_objc=true
      if test -z "${shlib}"; then
	shlib="`echo ${1} | sed 's/\.[^\.][^\.]*$/@SHLIB_EXT@/'`"
      fi
      objs="${objs} `echo ${1} | sed 's/\.[^\.][^\.]*$/.o/'`"
      ;;
    *.mm|*.M)
      with_objcxx=true
      # ObjC++ implies ObjC because we need ObjC runtime
      with_objc=true
      # ObjC++ implies C++ because we use C++ linker
      with_cxx=true
      if test -z "${shlib}"; then
	shlib="`echo ${1} | sed 's/\.[^\.][^\.]*$/@SHLIB_EXT@/'`"
      fi
      objs="${objs} `echo ${1} | sed 's/\.[^\.][^\.]*$/.o/'`"
      ;;
    *.f)
      with_f77=true
      if test -z "${shlib}"; then
	shlib="`echo ${1} | sed 's/\.[^\.][^\.]*$/@SHLIB_EXT@/'`"
      fi
      objs="${objs} `echo ${1} | sed 's/\.[^\.][^\.]*$/.o/'`"
      ;;
    *.f90|*.f95)
      with_f9x=true
      if test -z "${shlib}"; then
	shlib="`echo ${1} | sed 's/\.[^\.][^\.]*$/@SHLIB_EXT@/'`"
      fi
      objs="${objs} `echo ${1} | sed 's/\.[^\.][^\.]*$/.o/'`"
      ;;
    *.[co])
      if test -z "${shlib}"; then
	shlib="`echo ${1} | sed 's/\.[^\.][^\.]*$/@SHLIB_EXT@/'`"
      fi
      objs="${objs} `echo ${1} | sed 's/\.[^\.][^\.]*$/.o/'`"
      ;;
    *)
      pkg_libs="${pkg_libs} ${1}"
    ;;
  esac
  shift
done

if test -r "${HOME}/.R/Makevars-${R_PLATFORM}"; then
  makefiles="${makefiles} -f \"${HOME}/.R/Makevars-${R_PLATFORM}\""
elif test -r "${HOME}/.R/Makevars"; then
  makefiles="${makefiles} -f \"${HOME}/.R/Makevars\""
fi

makeobjs="OBJECTS=\"${objs}\""
if test -r Makevars; then
  makefiles="-f Makevars ${makefiles}"
  # use of OBJS was replaced by OBJECTS in Aug 2001
  #if grep '^ *OBJS *=' Makevars >/dev/null; then
  #  makeobjs="OBJECTS='\$(OBJS)'"
  #fi
  if grep '^ *OBJECTS *=' Makevars >/dev/null; then
    makeobjs=
  fi
fi

makeargs="SHLIB=\"${shlib}\""
if ${with_f9x}; then
    makeargs="SHLIB_LDFLAGS='\$(SHLIB_FCLDFLAGS)' ${makeargs}"
    makeargs="SHLIB_LD='\$(SHLIB_FCLD)' ${makeargs}"
else
  if ${with_cxx}; then
    makeargs="SHLIB_LDFLAGS='\$(SHLIB_CXXLDFLAGS)' ${makeargs}"
    makeargs="SHLIB_LD='\$(SHLIB_CXXLD)' ${makeargs}"
  fi
  if ${with_f77}; then
    if ${with_objc}; then
      shlib_libadd="\$(OBJC_LIBS) ${shlib_libadd}"
    fi
    if test -z "${shlib_libadd}"; then
      makeargs="${makeargs} SHLIB_LIBADD='\$(FLIBS)'"
    else
      makeargs="${makeargs} SHLIB_LIBADD='\$(FLIBS) ${shlib_libadd}'"
    fi
  else
    if ${with_objc}; then
      makeargs="${makeargs} SHLIB_LIBADD='\$(OBJC_LIBS)'"
    fi
  fi
fi
if test -n "${pkg_libs}"; then
  makeargs="${makeargs} PKG_LIBS='${pkg_libs}'"
fi

if ${preclean}; then
   eval ${MAKE} ${makefiles} ${makeargs} ${makeobjs} shlib-clean
fi
eval ${MAKE} ${makefiles} ${makeargs} ${makeobjs}
exitstatus=$?
if ${clean}; then
   eval ${MAKE} ${makefiles} ${makeargs} ${makeobjs} shlib-clean
fi
exit ${exitstatus}

### Local Variables: ***
### mode: sh ***
### sh-indentation: 2 ***
### End: ***