# -*- makefile -*-
# build mpl into a local install dir with
# make -f make.osx PREFIX=/Users/jdhunter/dev PYVERSION=2.6 fetch deps mpl_install
# (see README.osx for more details)

MPLVERSION=1.1.0
PYVERSION=2.6
PYTHON=python${PYVERSION}
MACOSX_DEPLOYMENT_TARGET=10.6
OSX_SDK_VER=10.6
ARCH_FLAGS?=-arch i386 -arch x86_64

# Dependency versions and URLs
#
# Adjusting these version numbers should be enough most of the time,
# but the download URLs are subject to change.

ZLIBVERSION=1.2.5
PNGVERSION=1.5.4
FREETYPEVERSION=2.4.6

ZLIBFILE=zlib-${ZLIBVERSION}.tar.gz
ZLIBDIR=$(basename $(basename ${ZLIBFILE}))
ZLIBURL=http://sourceforge.net/projects/libpng/files/zlib/${ZLIBVERSION}/${ZLIBFILE}/download

PNGFILE=libpng-${PNGVERSION}.tar.gz
PNGDIR=$(basename $(basename ${PNGFILE}))
PNGURL=http://sourceforge.net/projects/libpng/files/libpng15/older-releases/${PNGVERSION}/${PNGFILE}/download

FREETYPEFILE=freetype-${FREETYPEVERSION}.tar.bz2
FREETYPEDIR=$(basename $(basename ${FREETYPEFILE}))
FREETYPEURL=http://download.savannah.gnu.org/releases/freetype/${FREETYPEFILE}

## You shouldn't need to configure past this point

export PKG_CONFIG_PATH=${PREFIX}/lib/pkgconfig
export CFLAGS=${ARCH_FLAGS} -I${PREFIX}/include -I${PREFIX}/include/freetype2 -isysroot /Developer/SDKs/MacOSX${OSX_SDK_VER}.sdk
export LDFLAGS=${ARCH_FLAGS} -L${PREFIX}/lib -syslibroot,/Developer/SDKs/MacOSX${OSX_SDK_VER}.sdk
export MACOSX_DEPLOYMENT_TARGET

help:
	@echo "Interesting targets: clean, fetch, deps, mpl_build, mpl_install_std"
	@echo "See README.osx for details"

check-prefix:
	@if [ ! -d "$(PREFIX)" ]; then echo Set PREFIX to a directory - see README.osx; exit 1; fi

clean:
	rm -rf ${ZLIBFILE} ${PNGFILE} ${FREETYPEFILE} \
	bdist_mpkg-${BDISTMPKGVERSION}.tar.gz \
	bdist_mpkg-${BDISTMPKGVERSION} setupext.pyc \
	lib/matplotlib/mpl-data/matplotlib.conf lib/matplotlib/mpl-data/matplotlibrc \
	zlib-${ZLIBVERSION} libpng-${PNGVERSION} freetype-${FREETYPEVERSION} \
	build



fetch:
	${PYTHON} -c 'import urllib; urllib.urlretrieve("${ZLIBURL}", "${ZLIBFILE}")' &&\
	${PYTHON} -c 'import urllib; urllib.urlretrieve("${PNGURL}", "${PNGFILE}")' &&\
	${PYTHON} -c 'import urllib; urllib.urlretrieve("${FREETYPEURL}", "${FREETYPEFILE}")'

zlib: check-prefix
	rm -rf ${ZLIBDIR} &&\
	tar xvfz ${ZLIBFILE} &&\
	cd ${ZLIBDIR} &&\
	./configure --prefix=${PREFIX} &&\
	${MAKE} -j3 install

png: check-prefix zlib
	rm -rf ${PNGDIR} &&\
	tar xvfz ${PNGFILE} && \
	cd ${PNGDIR} &&\
	./configure  --disable-dependency-tracking  --prefix=${PREFIX} &&\
	${MAKE} -j3 install

freetype: check-prefix zlib
	rm -rf ${FREETYPEDIR} &&\
	tar xvfj ${FREETYPEFILE} &&\
	cd ${FREETYPEDIR} &&\
	./configure  --prefix=${PREFIX} &&\
	${MAKE} -j3 install &&\
	cp objs/.libs/libfreetype.a .

deps: zlib png freetype
	echo 'all done'

# Various build and install targets
#
# The values of PKG_CONFIG_PATH, MACOSX_DEPLOYMENT_TARGET, CFLAGS,
# LDFLAGS affect the compilation; note that variables are
# automatically put into the environment by make.

mpl_build: check-prefix
	${PYTHON} setup.py build

mpl_install: check-prefix
	${PYTHON} setup.py install --prefix=${PREFIX}

mpl_install_std: check-prefix
	${PYTHON} setup.py install

mpl_install_egg: check-prefix
	${PYTHON} setupegg.py install

mpl_install_develop: check-prefix
	${PYTHON} setupegg.py develop


binaries: check-prefix
	@if [ -z "$(PYVERSION)" ]; then echo Must set PYVERSION; exit 1; fi
	unset PKG_CONFIG_PATH &&\
	cp release/osx/data/setup.cfg release/osx/data/ReadMe.txt . &&\
	rm -f ${PREFIX}/lib/*.dylib &&\
	VERSIONER_PYTHON_PREFER_32_BIT=yes bdist_mpkg --readme=ReadMe.txt &&\
	hdiutil create -srcdir dist/matplotlib-${MPLVERSION}-py${PYVERSION}-macosx${OSX_SDK_VER}.mpkg  dist/matplotlib-${MPLVERSION}-py${PYVERSION}-macosx${OSX_SDK_VER}.dmg &&\
	VERSIONER_PYTHON_PREFER_32_BIT=yes ${PYTHON} setupegg.py bdist_egg
