#
# Makefile
#
# Copyright (C) 2004 Koji Nakamaru
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

TARGET=fv
SRCS=\
	CApp.c \
	common.c \
	error.c \
	CAppFV.c \
	CFile.c \
	CReader.c \
	CReaderHDR.c \
	CReaderPFM.c \
	CImage.c \

OBJS=$(subst .c,.o,$(SRCS))
DEPS=$(subst .c,.d,$(SRCS))

OPTIMIZER=release

ifeq ("$(OPTIMIZER)", "release")
OPT=\
-Wall -O3 -march=i686 \
-fomit-frame-pointer -finline-functions -fstrength-reduce \
-falign-loops=2 -falign-jumps=2 -falign-functions=2 \
-DNDEBUG
else
OPT=\
-Wall -g
endif

ifeq ("$(shell uname -o)", "Cygwin")
CXX=g++-3
CXXFLAGS=\
$(OPT) -mno-cygwin -mwindows -Inocygwin/inc -DFREEGLUT_STATIC
LDFLAGS=\
-Lnocygwin/lib -lpng15 -lfreeglut_static -lz -lbz2 -llzma -lpthreadGC2 \
-lglu32 -lopengl32 -lwinmm
else
ifeq ("$(shell uname -o)", "Darwin")
CXXFLAGS=\
$(OPT) -I/opt/local/include
LDFLAGS=\
-framework OpenGL -framework GLUT -framework Foundation \
-L/opt/local/lib -lpng15 -lz -lbz2 -llzma -lpthread \
-lm
else
CXXXFLAGS=\
$(OPT)
LDFLAGS=\
-lpng -lz -lbz2 -llzma -lpthread \
-lfreeglut -lGLU -lGL -L/usr/X11R6/lib -lXi -lXmu -lX11 -lXext -lSM \
-lm 
endif
endif

ifeq ("$(shell uname -o)", "Cygwin")
$(TARGET): $(OBJS)
	windres -i resource.rc -o resource.o
	$(CXX) $(CXXFLAGS) -o $@ $(OBJS) resource.o $(LDFLAGS)
else
$(TARGET): $(OBJS)
	$(CXX) $(CXXFLAGS) -o $@ $(OBJS) $(LDFLAGS)
endif

allclean: clean
	rm -f $(TARGET) $(TARGET).exe

clean:
	rm -f $(OBJS) $(DEPS) *~

ifneq "$(MAKECMDGOALS)" "clean"
  -include $(DEPS)
endif
define make-depend
  $(CXX) -MM -MF $3 -MP -MT $2 $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) $1
endef

%.o: %.c
	@$(call make-depend,$<,$@,$(subst .o,.d,$@))
	$(COMPILE.C) $(OUTPUT_OPTION) $<

archive: allclean $(TARGET)
	version=`grep '#define k_version' CAppFV.c | sed -e 's/#define k_version	*"//' -e 's/"//'`; \
	name=fv-src-$$version; \
	mkdir $$name; \
	cp AUTHORS COPYING ChangeLog INSTALL NEWS README $$name; \
	cp -r Makefile *.[ch] $$name; \
	cp resource.ico resource.rc resource.xpm $$name; \
	rm -f $$name.zip; \
	zip -qr9 $$name.zip $$name; \
	rm -rf $$name; \
	name=$$name-win32; \
	mkdir $$name; \
	cp AUTHORS COPYING ChangeLog INSTALL NEWS README $$name; \
	cp -r Makefile *.[ch] $$name; \
	cp resource.ico resource.rc resource.xpm $$name; \
	mkdir $$name/nocygwin; \
	cp -r nocygwin/inc nocygwin/lib $$name/nocygwin; \
	rm -f $$name.zip; \
	zip -qr9 $$name.zip $$name; \
	rm -rf $$name; \
	name=fv-bin-$$version-win32; \
	mkdir $$name ; \
	cp fv.exe pthreadGC2.dll README $$name; \
	rm -f $$name.zip; \
	zip -qr9 $$name.zip $$name; \
	rm -rf $$name
