#############################################################################
# Copyright 2004 NVIDIA Corporation.  All Rights Reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# (This is the Modified BSD License)
#############################################################################
#
# $Revision: $    $Date:  $
#
# "RIB" is a trademark of Pixar, Inc.
#

uname := ${shell uname | sed 's/_NT-.*//' | tr '[:upper:]' '[:lower:]'}
hw_arch := ${shell uname -m}


ifeq (cygwin,${uname})
  OEXT := .obj
  SOEXT := .dll
  INCLUDES = /I"${GELATOHOME}/include"

  CFLAGS = /nologo /EHsc /D_CRT_SECURE_NO_DEPRECATE ${RUNTIME_LIBC} \
        ${INCLUDES} ${OPTIMIZE}

  ifdef DEBUG
    OPTIMIZE := /RTCsu /Zi /Od /DDEBUG /D_DEBUG
    RUNTIME_LIBC := /MDd
  else
    ifeq (x86_64,${hw_arch})
      OPTIMIZE := /Ox /Zi /DNDEBUG
    else
      OPTIMIZE := /Ox /arch:SSE2 /Zi /DNDEBUG
    endif
    RUNTIME_LIBC := /MD
  endif

  ifdef MSDEV
    VSDIR := ${subst \,/,${MSDEV}}
  else
    VSDIR := C:/Program Files/Microsoft Visual Studio 8/VC
    #VSDIR := D:/Program Files/Microsoft Visual Studio 8/VC
  endif

  LD := "${VSDIR}/bin/link" /nologo /DEBUG
  CXX := "${VSDIR}/bin/cl" /nologo 
  LIBS := /LIBPATH:"${GELATOHOME}/lib" libgelato.lib
  EXES := rib.generator.dll rib.generator.dll.manifest \
          rib.generator.ilk rib.generator.pdb vc80.pdb

else # ! cygwin
  CXX = g++
  OEXT := .o
  SOEXT := .so
  INCLUDES = -I"${GELATOHOME}/include"
  CFLAGS = ${INCLUDES} -O2 -fPIC 
endif # ! cygwin

HEADERS = RIBParser.h Ribelato.h
SRCS = RIBParser.cpp Ribelato.cpp rib.generator.cpp
OBJS = ${patsubst %.cpp, %${OEXT}, ${SRCS}}
TXTS = CHANGES INSTALL LICENSE README

%${OEXT}: %.cpp
ifeq (cygwin,${uname})
	$(CXX) ${CFLAGS} -c $< /Fo$*${OEXT}
else
	$(CXX) ${CFLAGS} -c $< -o $*${OEXT}
endif


rib.generator${SOEXT}: ${OBJS}
ifeq (cygwin,${uname})
	${LD} /nologo /DLL ${OBJS} ${LIBS} /out:$@
	/bin/rm -f rib.generator.exp rib.generator.lib
else
	${AR} cr rib.generator.a ${OBJS}
	$(CXX) -Bdynamic --export-dynamic -shared -Wl,-whole-archive \
		rib.generator.a \
		-Wl,-no-whole-archive -o rib.generator${SOEXT}
	/bin/rm -f rib.generator.a
endif

#test${OEXT}: test.cpp Ribelato.h
#	$(CXX) ${CFLAGS} -c test.cpp -o test${OEXT}

rib.generator${OEXT}: rib.generator.cpp ${HEADERS}

Ribelato${OEXT}: ${HEADERS}

tar: rib.generator.so
	tar czf Ribelato-linux.tgz $(TXTS) rib.generator.so \
		Makefile $(HEADERS) $(SRCS)

zipWin: rib.generator.dll
	zip Ribelato-win.zip $(TXTS) rib.generator.dll \
		Makefile $(HEADERS) $(SRCS) \
		ribbit.sln ribbit.vcproj

tarAll: rib.generator.so rib.generator.dll
	tar czf Ribelato-all.tgz $(TXTS) rib.generator.so rib.generator.dll \
		Makefile $(HEADERS) $(SRCS) \
		ribbit.sln ribbit.vcproj

all:	rib.generator${SOEXT}

clean:
	/bin/rm -f $(OBJS) $(EXES)
