# SPI-specific settings
$(info Including spi/Makefile-bits-spcomp2)

# READ THIS FIRST
#
# If you are building for installation as an SpComp2, make like this:
# 1) On Linux, you must be setshot, for patchelf to be found; on
#    Mac OS X, no need to be setshot, just make sure SP_OS=lion and
#    COMPILER=clang.
# 2) make spcomp2_install_local
# or 2) make spcomp2_install
#
# Other notes about the SpComp2:
# - it also tries to install OIIO's binaries alongside the libs. It would
#   be really nice if somebody would either appwrap those binaries or
#   symlink them to an spi show path on each install. For the moment
#   though, they are just there for people who know.
# - it would also be nice if we had a convention for tagging SpComp2
#   release versions on the svn server.
#

OPENIMAGEIO_SPCOMP2_VERSION=32

# Default namespace
NAMESPACE ?= 'OpenImageIO_SPI'
SOVERSION ?= ${OPENIMAGEIO_SPCOMP2_VERSION}
SPCOMP2_SHOTTREE_LOCATION = /shots/spi/home/lib/SpComp2
INSTALL_SPCOMP2_LOCATION = /shots/spi/home/lib/SpComp2

PYLIB_LIB_PREFIX ?= 1
PYLIB_INCLUDE_SONAME ?= 1

OCIO_VERSION ?= 2
OCIO_PATH ?= ${SPCOMP2_SHOTTREE_LOCATION}/OpenColorIO/${SP_OS}-${COMPILER}/v${OCIO_VERSION}
OIIO_PATH ?= ${SPCOMP2_SHOTTREE_LOCATION}/OpenImageIO/${SP_OS}-${COMPILER}/v${OPENIMAGEIO_SPCOMP2_VERSION}

SPCOMP2_RPATH_OPT ?= ${OCIO_PATH}/lib
SPCOMP2_RPATH_DEBUG ?= ${OCIO_PATH}/lib/debug

PYSPCOMP2_RPATH_OPT ?= ${OCIO_PATH}/lib:${OIIO_PATH}/lib
PYSPCOMP2_RPATH_DEBUG ?= ${OCIO_PATH}/lib/debug:${OIIO_PATH}/lib/debug

## Spinux (current)
ifeq ($(SP_OS), spinux1)
    COMPILER=gcc44m64
    PYVER=2.6
    PYVERNOSPACE=26
    # At SPI, we have two "flavors" of spinux.  One is based on Foresight, which
    # uses a special libGL (below).  The other is based on Fedora which uses
    # the standard libGL.  This attempts to detect which libGL to use.
    SPINUX_GL_LIB = /usr/lib64/xorg.nvidia.3d/libGL.so
    MY_CMAKE_FLAGS += $(if $(wildcard ${SPINUX_GL_LIB}), -DOPENGL_gl_LIBRARY=${SPINUX_GL_LIB})
    ifneq (,$(wildcard /usr/include/OpenEXR2))
        MY_CMAKE_FLAGS += \
	  -DILMBASE_CUSTOM=1 \
	  -DILMBASE_CUSTOM_INCLUDE_DIR=/usr/include/OpenEXR2 \
          -DILMBASE_CUSTOM_LIB_DIR=/usr/lib64/OpenEXR2 \
	  -DILMBASE_CUSTOM_LIBRARIES="Imath Half IlmThread Iex" \
	  -DOPENEXR_CUSTOM=1 \
	  -DOPENEXR_CUSTOM_INCLUDE_DIR=/usr/include/OpenEXR2 \
          -DOPENEXR_CUSTOM_LIB_DIR=/usr/lib64/OpenEXR2 \
	  -DOPENEXR_CUSTOM_LIBRARY="IlmImf"
    else
        MY_CMAKE_FLAGS += \
	  -DILMBASE_CUSTOM=1 \
          -DILMBASE_CUSTOM_LIB_DIR=/usr/lib64 \
	  -DILMBASE_CUSTOM_LIBRARIES="SpiImath SpiHalf SpiIlmThread SpiIex" \
	  -DOPENEXR_CUSTOM=1 \
          -DOPENEXR_CUSTOM_LIB_DIR=/usr/lib64 \
	  -DOPENEXR_CUSTOM_LIBRARY="SpiIlmImf"
    endif
    MY_CMAKE_FLAGS += -DOCIO_PATH="${OCIO_PATH}"
else ifeq ($(SP_OS), lion)
    COMPILER=clang
    PYVER=2.7
    PYVERNOSPACE=27
    MACPORTS_PREFIX=/opt/local-10.7-2012-08

    # CMAKE_CXX_COMPILER:
    #   otherwise g++ is used and SPI standardized on clang.
    # CMAKE_INSTALL_NAME_DIR:
    #   without libraries and applications linking against this
    #   library will not be able to find it at run time; prepends
    #   "@rpath/" to the internal ID
    # OPENJPEG_HOME:
    #   Setting THIRD_PARTY_TOOLS_HOME isn't sufficient
    # OVERRIDE_SHARED_LIBRARY_SUFFIX:
    #   Set to .so so that shared libraries end in .so to be consistent
    #   with Linux SpComp2 shared libraries
    # PYTHON_EXECUTABLE:
    # PYTHON_INCLUDE_DIR:
    # PYTHON_LIBRARY:
    #   Use MacPorts' Python
    # THIRD_PARTY_TOOLS_HOME:
    #   Find everything in SPI's MacPorts.
    # ZLIB_ROOT:
    #   Needed to use SPI's MacPorts' zlib.
    MY_CMAKE_FLAGS += \
      -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \
      -DCMAKE_INSTALL_NAME_DIR=@rpath \
      -DOCIO_PATH="${OCIO_PATH}" \
      -DOPENJPEG_HOME=${MACPORTS_PREFIX} \
      -DOVERRIDE_SHARED_LIBRARY_SUFFIX=.so \
      -DPYTHON_EXECUTABLE=${MACPORTS_PREFIX}/bin/python${PYVER} \
      -DPYTHON_INCLUDE_DIR=${MACPORTS_PREFIX}/Library/Frameworks/Python.framework/Versions/2.7/Headers \
      -DPYTHON_LIBRARY=${MACPORTS_PREFIX}/Library/Frameworks/Python.framework/Versions/2.7/Python \
      -DTHIRD_PARTY_TOOLS_HOME=${MACPORTS_PREFIX} \
      -DZLIB_ROOT=${MACPORTS_PREFIX}

    ifeq (${SPCOMP2_USE_BOOSTVERS}, 1)
    BOOSTVERS_UNDERSCORE = ${shell echo ${BOOSTVERS} | sed "s/\\./_/"}
    MY_CMAKE_FLAGS += \
        -DBOOST_CUSTOM=1 \
        -DBoost_INCLUDE_DIRS=/usr/include/boost_${BOOSTVERS} \
        -DBoost_LIBRARY_DIRS=/usr/lib64/boost_${BOOSTVERS} \
        -DBoost_LIBRARIES=optimized;/usr/lib64/boost_${BOOSTVERS}/libboost_filesystem-gcc44-mt-${BOOSTVERS_UNDERSCORE}.so;optimized;/usr/lib64/boost_${BOOSTVERS}/libboost_filesystem-gcc44-mt-${BOOSTVERS_UNDERSCORE}.so;optimized;/usr/lib64/boost_${BOOSTVERS}/libboost_regex-gcc44-mt-${BOOSTVERS_UNDERSCORE}.so;optimized;/usr/lib64/boost_${BOOSTVERS}/libboost_system-gcc44-mt-${BOOSTVERS_UNDERSCORE}.so;optimized;/usr/lib64/boost_${BOOSTVERS}/libboost_thread-gcc44-mt-${BOOSTVERS_UNDERSCORE}.so
    endif

else
# Not sure how to obtain the correct GCC version but this returns the
# version of GCC in the path.
#GCC_VERSION=`gcc --version | grep 'gcc' | awk '{print $3}' | sed 's/\.//g'`
$(error Unknown SP_OS)
fi
endif  # endif $(SP_OS)


SPARCH=$(SP_OS)-$(COMPILER)
SPPYARCH=$(SPARCH)-py$(PYVERNOSPACE)

all: dist

.PHONY: spcomp2_install spcomp2_install_local clean all

comma:= ,
empty:=
space:= $(empty) $(empty)

INSTALL_BIN_LOCATION = /shots/spi/home/bin/$(SP_OS)
INSTALL_SPCOMP2_CURRENT = $(INSTALL_SPCOMP2_LOCATION)/OpenImageIO/$(SPARCH)/v$(OPENIMAGEIO_SPCOMP2_VERSION)

spcomp2_install_local: INSTALL_SPCOMP2_LOCATION = $(SPCOMP2_LOCAL_PATH)
spcomp2_install_local: INSTALL_BIN_LOCATION = $(INSTALL_SPCOMP2_CURRENT)/bin
spcomp2_install_local: spcomp2_install

local: dist

spcomp2: MY_CMAKE_FLAGS += \
		-DCMAKE_SKIP_BUILD_RPATH:BOOL=ON \
		-DCMAKE_INSTALL_RPATH=$(INSTALL_SPCOMP2_LOCATION)/OpenImageIO/$(SPARCH)/v$(OPENIMAGEIO_SPCOMP2_VERSION)/lib \
		-DCMAKE_INSTALL_RPATH_USE_LINK_PATH:BOOL=ON \
		-DPYLIB_INCLUDE_SONAME:BOOL=ON \
		-DPYLIB_LIB_PREFIX:BOOL=ON

spcomp2: dist

spcomp2_debug: MY_CMAKE_FLAGS += \
		-DCMAKE_SKIP_BUILD_RPATH:BOOL=ON \
		-DCMAKE_INSTALL_RPATH=$(INSTALL_SPCOMP2_LOCATION)/OpenImageIO/$(SPARCH)/v$(OPENIMAGEIO_SPCOMP2_VERSION)/lib \
		-DCMAKE_INSTALL_RPATH_USE_LINK_PATH:BOOL=ON \
		-DPYLIB_INCLUDE_SONAME:BOOL=ON \
		-DPYLIB_LIB_PREFIX:BOOL=ON

spcomp2_debug: debug

spcomp2_install_fixup_lion: spcomp2 spcomp2_debug
	echo $(INSTALL_SPCOMP2_LOCATION)

	install_name_tool \
	-add_rpath ${OCIO_PATH}/lib \
	${dist_dir}/python/libPyOpenImageIO.so

	install_name_tool \
	-add_rpath ${OIIO_PATH}/lib \
	${dist_dir}/python/libPyOpenImageIO.so

	install_name_tool \
	-add_rpath ${OCIO_PATH}/lib/debug \
	${dist_dir}.debug/python/libPyOpenImageIO.so

	install_name_tool \
	-add_rpath ${OIIO_PATH}/lib/debug \
	${dist_dir}.debug/python/libPyOpenImageIO.so
	
spcomp2_install_fixup_spinux1: spcomp2 spcomp2_debug
	echo $(INSTALL_SPCOMP2_LOCATION)

	patchelf \
	--set-rpath $(SPCOMP2_RPATH_OPT) \
	${dist_dir}/lib/libOpenImageIO.so
	
	patchelf \
	--set-rpath $(SPCOMP2_RPATH_DEBUG) \
	${dist_dir}.debug/lib/libOpenImageIO.so
	
	patchelf \
	--set-rpath $(PYSPCOMP2_RPATH_OPT) \
	${dist_dir}/python/libPyOpenImageIO.so
	
	patchelf \
	--set-rpath $(PYSPCOMP2_RPATH_DEBUG) \
	${dist_dir}.debug/python/libPyOpenImageIO.so
	
# This goal can't start with 'install' because something elsewhere picks
# it up and starts doing bad things
spcomp2_install: spcomp2_install_fixup_$(SP_OS)
	perl -I/usr/local/spi/lib/make /usr/local/spi/bin/spcomp_install.pl -m installhost \
	--project=OpenImageIO \
	--version=$(OPENIMAGEIO_SPCOMP2_VERSION) \
	--root=$(INSTALL_SPCOMP2_LOCATION) \
	--arch=$(SPARCH) \
	--headers=$(subst $(space),$(comma),$(wildcard ${dist_dir}/include/OpenImageIO/*)) \
	--cflags= --static_lflags="$(LINK_BOOST) $(LINK_OPENEXR) -lpthread" \
	--namespace=${NAMESPACE} \
	--srcdir=${dist_dir}/lib \
	--builddir_o=${dist_dir}/lib \
	--builddir_d=${dist_dir}.debug/lib
	
	perl -I/usr/local/spi/lib/make /usr/local/spi/bin/spcomp_install.pl -m installhost \
	--project=PyOpenImageIO \
	--version=$(OPENIMAGEIO_SPCOMP2_VERSION) \
    --root=$(INSTALL_SPCOMP2_LOCATION) \
    --arch=$(SPPYARCH) \
	--cflags= --static_lflags="$(LINK_BOOST) $(LINK_OPENEXR) -lpthread"\
	--namespace=${NAMESPACE} \
	--srcdir=${dist_dir}/python \
	--builddir_o=${dist_dir}/python \
	--builddir_d=${dist_dir}.debug/python

