#!/usr/bin/env bash
########################################################################
# Set various environment variables
########################################################################
# Assume current directory is SAGE_ROOT/build
SAGE_ROOT=`cd .. && pwd -P`
SAGE_SRC="$SAGE_ROOT/src"
SAGE_LOCAL="$SAGE_ROOT/local"
SAGE_SHARE="$SAGE_LOCAL/share"
SAGE_EXTCODE="$SAGE_SHARE/sage/ext"
SAGE_LOGS="$SAGE_ROOT/logs/pkgs"
SAGE_SPKG_INST="$SAGE_LOCAL/var/lib/sage/installed"
SAGE_VERSION=`cat $SAGE_ROOT/VERSION.txt | sed 's+.*\ \(.*\),.*+\1+'`
PATH="$SAGE_SRC/bin:$SAGE_LOCAL/bin:$PATH"
PYTHONPATH="$SAGE_LOCAL"
export SAGE_ROOT SAGE_SRC SAGE_LOCAL SAGE_EXTCODE SAGE_LOGS SAGE_SPKG_INST SAGE_VERSION PATH PYTHONPATH
# Storing the start time of the build process. The time is stored in
# seconds since 1970-01-01 in a hidden file called
# "SAGE_ROOT/.BUILDSTART". See ticket #6744.
echo `date -u "+%s"` > "$SAGE_ROOT/.BUILDSTART"
########################################################################
# Support for upgrading
########################################################################
# Check that we aren't accidentally upgrading from old (< 6.0)
# versions of Sage. See #14715.
# The file $SAGE_ROOT/spkg/install no longer exists in Sage 6.0,
# but it did exist in all earlier versions.
if [ -f "$SAGE_ROOT/spkg/install" ]; then
cat >&2 <<EOF
************************************************************************
Error: it is impossible to upgrade your version of Sage to version 6.0
or later. It is recommended to install the latest version of Sage from
scratch. If you currently have Sage 4.5 or later and really insist on
upgrading, it should still be possible to upgrade to Sage 5.13 with the
following command:
./sage -upgrade http://boxen.math.washington.edu/home/release/sage-5.13/sage-5.13
************************************************************************
EOF
exit 2
fi
if [ "$SAGE_UPGRADING" = yes ]; then
# We're doing an upgrade. Let build/Makefile call sage-spkg with
# "-f" to force rebuilding dependent packages, too:
export SAGE_SPKG_OPTS="-f"
fi
###############################################################################
# Create basic directories needed for Sage
###############################################################################
mkdir -p "$SAGE_ROOT/tmp"
mkdir -p "$SAGE_LOGS"
mkdir -p "$SAGE_LOCAL/bin"
mkdir -p "$SAGE_LOCAL/etc"
mkdir -p "$SAGE_LOCAL/lib"
mkdir -p "$SAGE_SPKG_INST"
mkdir -p "$SAGE_SHARE"
###############################################################################
# Determine whether to install GCC (gcc, g++, gfortran).
###############################################################################
# Give a deprecation message whenever SAGE_FORTRAN or SAGE_FORTRAN_LIB
# is set to something. See Trac #13349.
if [ -n "$SAGE_FORTRAN" ] || [ -n "$SAGE_FORTRAN_LIB" ]; then
cat >&2 <<EOF
WARNING: the environment variables SAGE_FORTRAN and SAGE_FORTRAN_LIB
are deprecated since sage-5.3.
The GNU Compiler Collection (GCC) is included in Sage and will be
installed if you do not have a Fortran compiler. If you do have a
Fortran compiler installed and you want to specify its path, you can
use the standard environment variable FC (instead of SAGE_FORTRAN) to
point to the Fortran compiler.
As replacement to SAGE_FORTRAN_LIB, you can either add the directory
containing the Fortran library to LIBRARY_PATH and/or LD_LIBRARY_PATH,
or copy/symlink the Fortran library into \$SAGE_ROOT/local/lib. In most
cases, none of this is needed as the compiler/linker should find the
Fortran library by itself.
Support for SAGE_FORTRAN and SAGE_FORTRAN_LIB might be removed from
future versions of Sage. Moreover, these variables are not tested
anymore and might not work as you expect.
EOF
sleep 5
fi
# Determine various compilers. These variables should not be exported,
# they are only used in this build/install script to determine whether to
# install GCC. The "real" $CC, $CXX,... variables for building Sage are
# set in sage-env.
if [ -z "$CC" ]; then
CC=gcc
fi
if [ -z "$CXX" ]; then
if command -v g++ >/dev/null 2>/dev/null; then
CXX=g++
fi
fi
if [ -z "$FC" ]; then
if [ -n "$SAGE_FORTRAN" ]; then
FC="$SAGE_FORTRAN"
elif command -v gfortran >/dev/null 2>/dev/null; then
FC=gfortran
elif command -v g95 >/dev/null 2>/dev/null; then
FC=g95
elif command -v g77 >/dev/null 2>/dev/null; then
FC=g77
fi
fi
if [ -f "$SAGE_LOCAL/bin/gcc" ]; then
# GCC is already installed. Normally we don't need to re-install
# GCC, unless this is an upgrade. To disable unneeded rebuilding
# of GCC, we touch the installed file for GCC, such that it will
# really only be built if one of its dependencies has been upgraded.
if [ "$SAGE_UPGRADING" = yes ]; then
echo >&2 "We are upgrading Sage and GCC was installed before, it will get"
echo >&2 "re-installed if needed."
need_to_install_gcc=yes
for f in "$SAGE_SPKG_INST"/gcc-*; do
if [ -f "$f" ]; then
touch "$f"
fi
done
else
need_to_install_gcc=no
fi
elif [ -n "$SAGE_INSTALL_GCC" ]; then
# Check the value of the environment variable SAGE_INSTALL_GCC
case "$SAGE_INSTALL_GCC" in
yes)
echo >&2 "Installing GCC because SAGE_INSTALL_GCC is set to 'yes'."
need_to_install_gcc=yes;;
no)
need_to_install_gcc=no;;
*)
echo >&2 "Error: SAGE_INSTALL_GCC should be set to 'yes' or 'no'."
echo >&2 "You can also leave it unset to install GCC when needed."
exit 2;;
esac
else
# SAGE_INSTALL_GCC is not set, install GCC when needed.
need_to_install_gcc=no
# Check whether $CC is some version of GCC. If it's a different
# compiler, install GCC.
CCtype=`testcc.sh $CC`
if [ "$CCtype" != GCC ]; then
echo >&2 "Installing GCC because your '$CC' isn't GCC (GNU CC)."
need_to_install_gcc=yes
else
# $CC points to some version of GCC, find out which version.
GCCVERSION=`$CC -dumpversion`
case $GCCVERSION in
[0-3].*|4.[0-3]|4.[0-3].*)
# Install our own GCC if the system-provided one is older than gcc-4.4.
# * gcc-4.2.4 compiles a slow IML:
# https://groups.google.com/forum/?fromgroups#!topic/sage-devel/Ux3t0dW2FSI
# * gcc-4.3 might have trouble building ATLAS:
# https://groups.google.com/forum/?fromgroups#!topic/sage-devel/KCeFqQ_w2FE
echo >&2 "Installing GCC because you have $CC version $GCCVERSION, which is quite old."
need_to_install_gcc=yes;;
4.4*|4.5*)
# GCC 4.4.x and GCC 4.5.x fail to compile PARI/GP on ia64:
# * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46044
if [ x`uname -m` = xia64 ]; then
echo >&2 "Installing GCC because you have $CC version $GCCVERSION on ia64."
echo >&2 "gcc <= 4.5 fails to compile PARI/GP on ia64."
need_to_install_gcc=yes
fi;;
4.6.[01])
# Also install GCC if we have version 4.6.0 or 4.6.1, which is
# known to give trouble within Sage:
# * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48702
# * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48774
echo >&2 "Installing GCC because you have $CC version $GCCVERSION."
echo >&2 "gcc-4.6.0 and gcc-4.6.1 have known bugs affecting Sage."
need_to_install_gcc=yes;;
4.6*)
# GCC 4.6.x doesn't compile ECL on Cygwin:
# * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52061
if uname | grep CYGWIN >/dev/null; then
echo >&2 "Installing GCC because you have $CC version $GCCVERSION on Cygwin."
echo >&2 "gcc-4.6.x fails to compile ECL on Cygwin."
need_to_install_gcc=yes
fi;;
4.7.0)
# GCC 4.7.0 is very broken on ia64, see
# http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48496
# This is fixed in GCC 4.7.1.
if [ x`uname -m` = xia64 ]; then
echo >&2 "Installing GCC because you have $CC version $GCCVERSION on ia64."
echo >&2 "gcc-4.7.0 has a serious bug on ia64."
need_to_install_gcc=yes
fi;;
esac
fi
# Check C++ and Fortran compilers.
if [ -z "$CXX" ]; then
echo >&2 "Installing GCC because a C++ compiler is missing."
need_to_install_gcc=yes
fi
if [ -z "$FC" ]; then
echo >&2 "Installing GCC because a Fortran compiler is missing."
need_to_install_gcc=yes
fi
fi
# If we are not installing GCC: check that the assembler and linker
# used by $CC match $AS and $LD.
# See http://trac.sagemath.org/sage_trac/ticket/14296
if [ $need_to_install_gcc != yes ]; then
if [ "$AS" != "" ]; then
CC_as=`$CC -print-file-name=as 2>/dev/null`
CC_as=`command -v $CC_as 2>/dev/null`
cmd_AS=`command -v $AS`
if [ "$CC_as" != "" -a "$CC_as" != "$cmd_AS" ]; then
echo >&2 "Error: Mismatch of assemblers between"
echo >&2 " * $CC using $CC_as"
echo >&2 " * \$AS equal to $AS"
if [ "$SAGE_PORT" = "" ]; then
echo >&2 "Aborting, either change or unset AS or set SAGE_PORT=yes or set"
echo >&2 "SAGE_INSTALL_GCC=yes (this GCC would use assembler $AS)"
exit 1
else
echo >&2 "Continuing since SAGE_PORT is set."
fi
fi
fi
if [ "$LD" != "" ]; then
CC_ld=`$CC -print-file-name=ld 2>/dev/null`
CC_ld=`command -v $CC_ld 2>/dev/null`
cmd_LD=`command -v $LD`
if [ "$CC_ld" != "" -a "$CC_ld" != "$cmd_LD" ]; then
echo >&2 "Error: Mismatch of linkers between"
echo >&2 " * $CC using $CC_ld"
echo >&2 " * \$LD equal to $LD"
if [ "$SAGE_PORT" = "" ]; then
echo >&2 "Aborting, either change or unset LD or set SAGE_PORT=yes or set"
echo >&2 "SAGE_INSTALL_GCC=yes (this GCC would use linker $LD)"
exit 1
else
echo >&2 "Continuing since SAGE_PORT is set."
fi
fi
fi
fi
###############################################################################
# Create the sage_fortran script.
###############################################################################
# Don't use FC and SAGE_FORTRAN_LIB if we are installing GCC or have
# installed gfortran, as that can cause compatibility problems.
# We want to use the Fortran compiler that we build as part of GCC.
if [ "$need_to_install_gcc" = yes ] || [ -x "$SAGE_LOCAL/bin/gfortran" ]; then
unset FC
unset SAGE_FORTRAN_LIB
fi
# Write sage_fortran script.
cat >"$SAGE_LOCAL/bin/sage_fortran" <<EOF
#!/bin/sh
cat >&2 <<ENDOFWARNING
WARNING: The sage_fortran script is deprecated since Sage 5.11 and
might not be supported in future versions of Sage. You should update
your package to use the standard environment variable \\\$FC instead.
ENDOFWARNING
if [ "x\$FC" = x ]; then
# Default value determined in build/install
myFC='${FC:-gfortran}'
else
myFC="\$FC"
fi
# unset FC to avoid an infinite loop in the case that FC=sage_fortran.
unset FC
if [ "\$SAGE64" = yes ]; then
exec \$myFC -m64 -fPIC "\$@"
else
exec \$myFC -fPIC "\$@"
fi
EOF
chmod +x "$SAGE_LOCAL/bin/sage_fortran"
# Make the fortran library symlink if requested
if [ -f "$SAGE_FORTRAN_LIB" ]; then
( cd "$SAGE_LOCAL/lib" && ln -sf "$SAGE_FORTRAN_LIB" . )
fi
###############################################################################
# Create $SAGE_ROOT/build/Makefile starting from build/deps
###############################################################################
# Trac #15624: use file descriptor 5 since make uses 3 and 4
exec 5>Makefile
cat >&5 <<EOF
#==============================================================================
# This file has been automatically generated by
# $SAGE_ROOT/build/install
# You should not edit it by hand
#==============================================================================
EOF
# Use bash as shell for the Makefile (bash obviously exists, since
# this build/install script runs under bash).
echo >&5 "SHELL = `command -v bash`"
echo >&5
# If the user (or the Makefile) has set SAGE_PARALLEL_SPKG_BUILD=no,
# then turn off parallel building: disable just building multiple
# packages at the same time. Individual packages can still be built
# in parallel by specifying '-j' in $MAKE.
if [ "${SAGE_PARALLEL_SPKG_BUILD:-yes}" = no ]; then
echo ".NOTPARALLEL:" >&5
echo "" >&5
fi
# Usage: newest_version $pkg
# Print version number of latest standard package $pkg
newest_version() {
PKG=$1
if [ -f "$SAGE_ROOT/build/pkgs/$PKG/package-version.txt" ]; then
echo -n $PKG-
cat "$SAGE_ROOT/build/pkgs/$PKG/package-version.txt"
else
echo >&2 "Cannot determine latest version of $PKG."
echo "$PKG"
return 1
fi
}
cat >&5 <<EOF
# Standard packages
ATLAS=`newest_version atlas`
BOEHM_GC=`newest_version boehm_gc`
BOOST_CROPPED=`newest_version boost_cropped`
BZIP2=`newest_version bzip2`
CDDLIB=`newest_version cddlib`
CEPHES=`newest_version cephes`
CLIQUER=`newest_version cliquer`
CONWAY=`newest_version conway_polynomials`
CVXOPT=`newest_version cvxopt`
CYTHON=`newest_version cython`
DOCUTILS=`newest_version docutils`
ECL=`newest_version ecl`
ECLIB=`newest_version eclib`
ECM=`newest_version ecm`
ELLIPTIC_CURVES=`newest_version elliptic_curves`
FFLASFFPACK=`newest_version fflas_ffpack`
FLINT=`newest_version flint`
FLINTQS=`newest_version flintqs`
FPLLL=`newest_version libfplll`
FREETYPE=`newest_version freetype`
GAP=`newest_version gap`
GCC=`newest_version gcc`
GD=`newest_version gd`
GDMODULE=`newest_version gdmodule`
GENUS2REDUCTION=`newest_version genus2reduction`
GFAN=`newest_version gfan`
GF2X=`newest_version gf2x`
GIT=`newest_version git`
GIVARO=`newest_version givaro`
GLPK=`newest_version glpk`
GRAPHS=`newest_version graphs`
GSL=`newest_version gsl`
ICONV=`newest_version iconv`
IML=`newest_version iml`
IPYTHON=`newest_version ipython`
JINJA2=`newest_version jinja2`
JMOL=`newest_version jmol`
LCALC=`newest_version lcalc`
LRCALC=`newest_version lrcalc`
LIBGAP=`newest_version libgap`
LIBPNG=`newest_version libpng`
LINBOX=`newest_version linbox`
M4RI=`newest_version libm4ri`
M4RIE=`newest_version libm4rie`
MATPLOTLIB=`newest_version matplotlib`
MAXIMA=`newest_version maxima`
MPC=`newest_version mpc`
MPFI=`newest_version mpfi`
MPFR=`newest_version mpfr`
MPIR=`newest_version mpir`
MPMATH=`newest_version mpmath`
NETWORKX=`newest_version networkx`
NTL=`newest_version ntl`
NUMPY=`newest_version numpy`
PALP=`newest_version palp`
PARI=`newest_version pari`
PATCH=`newest_version patch`
PEXPECT=`newest_version pexpect`
PILLOW=`newest_version pillow`
POLYBORI=`newest_version polybori`
POLYTOPES_DB=`newest_version polytopes_db`
PPL=`newest_version ppl`
PYCRYPTO=`newest_version pycrypto`
PYGMENTS=`newest_version pygments`
PYNAC=`newest_version pynac`
PYTHON=`newest_version python`
R=`newest_version r`
RPY=`newest_version rpy2`
RATPOINTS=`newest_version ratpoints`
READLINE=`newest_version readline`
RUBIKS=`newest_version rubiks`
SAGENB=`newest_version sagenb`
SAGETEX=`newest_version sagetex`
SCIPY=`newest_version scipy`
SCONS=`newest_version scons`
SETUPTOOLS=`newest_version setuptools`
SINGULAR=`newest_version singular`
SPHINX=`newest_version sphinx`
SQLALCHEMY=`newest_version sqlalchemy`
SQLITE=`newest_version sqlite`
SYMMETRICA=`newest_version symmetrica`
SYMPOW=`newest_version sympow`
SYMPY=`newest_version sympy`
TACHYON=`newest_version tachyon`
NCURSES=`newest_version ncurses`
ZLIB=`newest_version zlib`
ZNPOLY=`newest_version zn_poly`
# Directory to keep track of which packages are installed
INST=`echo "$SAGE_SPKG_INST" | sed 's/ /\\\\ /g'`
EOF
# $(TOOLCHAIN) variable containing prerequisites for the build
echo >&5 -n 'TOOLCHAIN ='
if [ "$SAGE_INSTALL_CCACHE" = yes ]; then
echo >&5 -n ' $(INST)/ccache'
fi
if [ "$need_to_install_gcc" = yes ]; then
echo >&5 -n ' $(INST)/$(GCC)'
# Use this option for the prereq configure script, such that it
# will skip all compiler checks.
export PREREQ_OPTIONS="--disable-compiler-checks $PREREQ_OPTIONS"
fi
echo >&5
echo >&5 'SCRIPT_SOURCES = \'
for file in "$SAGE_SRC/bin/"*; do
echo >&5 " \$(SAGE_SRC)${file#$SAGE_SRC} \\"
done
echo >&5
echo >&5 'SCRIPTS = \'
for file in "$SAGE_SRC/bin/"*; do
echo >&5 " \$(SAGE_LOCAL)${file#$SAGE_SRC} \\"
done
echo >&5
echo >&5 'EXTCODE_SOURCES = \'
for file in `find "$SAGE_SRC"/ext -type f`; do
echo >&5 " \$(SAGE_SRC)${file#$SAGE_SRC} \\"
done
echo >&5
echo >&5 'EXTCODE = \'
for file in `find "$SAGE_SRC"/ext -type f`; do
echo >&5 " \$(SAGE_EXTCODE)${file#$SAGE_SRC/ext} \\"
done
echo >&5
cat >&5 <<EOF
# don't just use \`install\` since we don't want to change permissions
\$(SAGE_LOCAL)/bin/%: \$(SAGE_SRC)/bin/%
cp \$< \$@
# don't just use \`install -D\` since we don't want to change permissions
# cp won't correctly setup the SAGE_EXTCODE directory structure (unlike install)
# so we need a mkdir here to cp can copy into an existing folder
\$(SAGE_EXTCODE)/%: \$(SAGE_SRC)/ext/%
@mkdir -p "\$(@D)"
cp \$< \$@
EOF
# Copy build/deps
cat >&5 <<EOF
#==============================================================================
# What follows now is a copy of
# $SAGE_ROOT/build/deps
#==============================================================================
EOF
cat "$SAGE_ROOT/build/deps" >&5
# Close the Makefile
exec 5>&-
###############################################################################
# Skip the rest if nothing to do (i.e., to [re]build).
###############################################################################
# Set MAKE to "make" if unset
if [ -z "$MAKE" ]; then
export MAKE=make
fi
# If "make" doesn't understand the -q option (although we require
# GNU make, which supports it), it should exit with a non-zero status
# which is not a problem.
if $MAKE -q "$@" >/dev/null 2>/dev/null; then
echo "Nothing to (re)build / all up-to-date."
exit 0
fi
# Dump environment for debugging purposes:
echo "*** ALL ENVIRONMENT VARIABLES BEFORE BUILD: ***"
env | sort
echo "***********************************************"
###############################################################################
# NOW do the actual build:
###############################################################################
time $MAKE "$@"
if [ $? -ne 0 ]; then
cat >&2 <<EOF
***************************************************************
Error building Sage.
The following package(s) may have failed to build:
EOF
for f in "$SAGE_LOGS"/*.log; do
# Look for recent error message in log file.
# Note that "tail -n 20 ..." doesn't work on Solaris.
if tail -20 "$f" | grep "^Error" &>/dev/null; then
base_f=`basename $f .log`
cat >&2 <<EOF
package: $base_f
log file: $f
build directory: ${SAGE_BUILD_DIR:-$SAGE_LOCAL/var/tmp/sage/build}/$base_f
EOF
fi
done
cat >&2 <<EOF
The build directory may contain configuration files and other potentially
helpful information. WARNING: if you now run 'make' again, the build
directory will, by default, be deleted. Set the environment variable
SAGE_KEEP_BUILT_SPKGS to 'yes' to prevent this.
EOF
exit 1
fi
# Build succeeded.
echo "Sage build/upgrade complete!"
if [ "$1" = "all" ]; then
echo
echo "To install small scripts to directly run Sage's versions of GAP,"
echo "the PARI/GP interpreter, Maxima, or Singular etc. (by typing e.g."
echo "just 'gap' or 'gp') into a standard 'bin' directory, start Sage"
echo "by typing 'sage' (or './sage') and enter something like"
echo
echo " install_scripts('/usr/local/bin')"
echo
echo "at the Sage command prompt ('sage:')."
echo
echo "If you issued 'make', 'make all', or a similar command, then the"
echo "HTML version of the documentation will be built right now."
echo "Otherwise, if you want to (re)build the HTML documentation,"
echo "run 'make doc'. To build the PDF version, run 'make doc-pdf'."
echo
fi