Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/bin/sage-bdist
8817 views
#!/usr/bin/env bash

########################################################
# Build Sage *binary* distribution
# This script should be called by the spkg/bin/sage script
#
# Released under the GNU GPL-v2+ -- (c) William Stein
########################################################

set -e

CMD="${0##*/}"

die () {
    echo >&2 -e "$@"
    exit 1
}

usage () {
    echo "usage: $CMD [TMP_DIR]"
}

if [ $# -gt 1 ]; then
    usage
    die
fi

if [ -z "$SAGE_ROOT" ]; then
    die "must be run from within a Sage enviroment, or with SAGE_ROOT provided"
fi

if [ -z "$SAGE_SRC" ]; then
    die "must be run from within a Sage enviroment, or with SAGE_SRC provided"
fi

if [ "$#" -gt 0 ]; then
    TMP_DIR="$1"
else
    TMP_DIR="$SAGE_ROOT/tmp"
fi

source "$SAGE_SRC/bin/sage-version.sh"
echo "Sage version $SAGE_VERSION, release date $SAGE_RELEASE_DATE"

TARGET=sage-"$SAGE_VERSION"-`uname -m`-`uname`
TARGET=`echo $TARGET | sed 's/ //g'`   # Remove spaces

sage-clone-source "$SAGE_ROOT" "$TMP_DIR/$TARGET"

echo "Copying files over to tmp directory"
# We use "tar" to copy files for portability,
# see http://trac.sagemath.org/sage_trac/ticket/14236
tar cf - local | ( cd "$TMP_DIR/$TARGET" && tar xf - )
tar cf - src | ( cd "$TMP_DIR/$TARGET" && tar xf - )

cd "$TMP_DIR"
if [ "$UNAME" = "Darwin" ]; then
    cd "$TARGET"
    # Move everything into a subdirectory sage, but first name it
    # .sage_tmp to avoid it being globbed by *.
    mkdir .sage_tmp
    mv * .git* .sage_tmp
    mv .sage_tmp sage
    cp -p "$SAGE_LOCAL"/bin/sage-README-osx.txt README.txt

    if [ "$SAGE_APP_BUNDLE" = "yes" ]; then

        echo 'Building the Mac Application'

        # Some people don't have the 10.4 sdk installed, but using the default on 10.4 causes problems
        if [ "$MACOSX_DEPLOYMENT_TARGET" = "10.4" -a -e '/Developer/SDKs/MacOSX10.4u.sdk' ]; then
            SET_SDKROOT='SDKROOT=/Developer/SDKs/MacOSX10.4u.sdk'
        else
            SET_SDKROOT=''
        fi

        CONFIGURATION='Debug'
        # Note that we don't have to build this part with the same
        # compiler as everything else, and in fact it causes problems
        # to do so.
        (cd "$SAGE_SRC/mac-app/" && \
            unset CC LD && \
            xcodebuild -target 'Sage' -configuration "$CONFIGURATION" \
            ARCHES="$(uname -m)" \
            $SET_SDKROOT) || 
                die "Failed to build Sage.app.\nIf you don't wish to build Sage.app set SAGE_APP_BUNDLE=no"

        echo 'Copying Sage.app'
        cp -pRL "$SAGE_SRC/mac-app/build/$CONFIGURATION/Sage.app" ./Sage.app
        # Info.plist is a binary plist, so convert it for processing with sed.
        # I would just change it to be an xml plist, but xcode changes it back.
        plutil -convert xml1 ./Sage.app/Contents/Info.plist
        sed -i '' "s/SAGE_VERSION/$SAGE_VERSION/" \
            ./Sage.app/Contents/Info.plist

        mv sage ./Sage.app/Contents/Resources/

        # Rename it with the version number
        mv Sage.app "Sage-$SAGE_VERSION.app"
    else
        echo 'If you wish to create a Mac Application please set'
        echo 'SAGE_APP_BUNDLE=yes'
    fi

    # Go back to the right directory for later copying
    cd "$TMP_DIR"
    if [ "$SAGE_APP_DMG" != "no" ]; then
        echo "Creating $TARGET.dmg"
        echo "(If you don't wish to create a disk image please set SAGE_APP_DMG=no)"
        DYLD_LIBRARY_PATH="$SAGE_ORIG_DYLD_LIBRARY_PATH"; export DYLD_LIBRARY_PATH
        hdiutil create -srcfolder "$TARGET" -format UDBZ "$TARGET".dmg
    else
        echo 'If you wish to create a disk image please set'
        echo 'SAGE_APP_DMG=yes'
        echo '(or unset SAGE_APP_DMG since SAGE_APP_DMG=yes is the default)'
        echo "Creating $TARGET.tar.gz ..."
        chmod -R go=rX "$TARGET"
        tar zcf "$TARGET".tar.gz "$TARGET"
    fi
else
    echo "Creating $TARGET.tar.gz ..."
    chmod -R go=rX "$TARGET"
    tar zcf "$TARGET".tar.gz "$TARGET"
fi

mkdir -p "$SAGE_ROOT"/dist

rm -rf "$SAGE_ROOT/dist/$TARGET"

echo "Moving final distribution file to $SAGE_ROOT/dist"

mv "$TARGET" "$SAGE_ROOT"/dist/
mv "$TARGET".* "$SAGE_ROOT"/dist/