 #
 #  File        : Makefile
 #                ( Makefile for GNU 'make' utility )
 #
 #  Description : Makefile for compiling G'MIC
 #                ( http://gmic.sourceforge.net )
 #
 #  Copyright   : David Tschumperle
 #                ( http://www.greyc.ensicaen.fr/~dtschump/ )
 #
 #  License     : CeCILL v2.0
 #                ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
 #
 #  This software is governed by the CeCILL  license under French law and
 #  abiding by the rules of distribution of free software.  You can  use,
 #  modify and/ or redistribute the software under the terms of the CeCILL
 #  license as circulated by CEA, CNRS and INRIA at the following URL
 #  "http://www.cecill.info".
 #
 #  As a counterpart to the access to the source code and  rights to copy,
 #  modify and redistribute granted by the license, users are provided only
 #  with a limited warranty  and the software's author,  the holder of the
 #  economic rights,  and the successive licensors  have only  limited
 #  liability.
 #
 #  In this respect, the user's attention is drawn to the risks associated
 #  with loading,  using,  modifying and/or developing or reproducing the
 #  software by the user in light of its specific status of free software,
 #  that may mean  that it is complicated to manipulate,  and  that  also
 #  therefore means  that it is reserved for developers  and  experienced
 #  professionals having in-depth computer knowledge. Users are therefore
 #  encouraged to load and test the software's suitability as regards their
 #  requirements in conditions enabling the security of their systems and/or
 #  data to be ensured and,  more generally, to use and operate it in the
 #  same conditions as regards security.
 #
 #  The fact that you are presently reading this means that you have had
 #  knowledge of the CeCILL license and that you accept its terms.
 #

#---------------------------------
# Set correct variables and paths
#---------------------------------
GMIC_VERSION = 0.8
CC = g++ -Ihtml

ifeq ($(CC),icc)
GMIC_CFLAGS = -Ihtml
GMIC_LDFLAGS = -lpthread
else
GMIC_CFLAGS = -Wall -W -Ihtml
GMIC_LDFLAGS = -lm -lpthread
endif

#---------------------------------------------------
# Set compilation flags allowing to customize G'MIC
#---------------------------------------------------

# Flags to enable code optimization by the compiler.
ifeq ($(CC),icc)
GMIC_CFLAGS+= -O3 -ipo -no-prec-div
else
GMIC_CFLAGS+= -O3 -ffast-math
endif

# Flags to enable fast image display, using the XSHM library.
GMIC_CFLAGS+= -Dcimg_use_xshm
GMIC_LDFLAGS+= -lXext

# Flags to enable screen mode switching, using the XRandr library.
GMIC_CFLAGS+= -Dcimg_use_xrandr
GMIC_LDFLAGS+= -lXrandr

# Flags to enable native support for PNG image files, using the PNG library.
GMIC_CFLAGS+= -Dcimg_use_png
GMIC_LDFLAGS+= -lpng -lz

# Flags to enable native support for JPEG image files, using the JPEG library.
GMIC_CFLAGS+= -Dcimg_use_jpeg
GMIC_LDFLAGS+= -ljpeg

# Flags to enable native support for TIFF image files, using the TIFF library.
GMIC_CFLAGS+= -Dcimg_use_tiff
GMIC_LDFLAGS+= -ltiff

# Flags to enable native support for various video files, using the FFMPEG library.
GMIC_CFLAGS+= -I/usr/include/ffmpeg -Dcimg_use_ffmpeg
GMIC_LDFLAGS+= -lavcodec -lavformat

# Flags to enable native support for compressed .cimgz files, using the Zlib library.
GMIC_CFLAGS+= -Dcimg_use_zlib
GMIC_LDFLAGS+= -lz

# Flags to enable native support of most classical image file formats, using the Magick++ library.
GMIC_CFLAGS+= -Dcimg_use_magick `Magick++-config --cppflags` `Magick++-config --cxxflags`
GMIC_LDFLAGS+= `Magick++-config --ldflags` `Magick++-config --libs`

# Flags to enable the use of the FFTW3 library.
GMIC_CFLAGS+= -Dcimg_use_fftw3
GMIC_LDFLAGS+= -lfftw3

BINDIR=/usr/bin

#-------------------------
# Define Makefile entries
#-------------------------

# Compilation is done for 'linux' architectures by default. 'macosx' and 'solaris' are also available options.
all: linux

distclean: clean

clean:
	rm -rf gmic*.o gmic

gmic_bool.o: gmic.cpp
	$(CC) -o gmic_bool.o -c gmic.cpp -Dgmic_separate_compilation -Dgmic_bool $(CFLAGS) $(GMIC_CFLAGS)
gmic_uchar.o: gmic.cpp
	$(CC) -o gmic_uchar.o -c gmic.cpp -Dgmic_separate_compilation -Dgmic_uchar $(CFLAGS) $(GMIC_CFLAGS)
gmic_char.o: gmic.cpp
	$(CC) -o gmic_char.o -c gmic.cpp -Dgmic_separate_compilation -Dgmic_char $(CFLAGS) $(GMIC_CFLAGS)
gmic_ushort.o: gmic.cpp
	$(CC) -o gmic_ushort.o -c gmic.cpp -Dgmic_separate_compilation -Dgmic_ushort $(CFLAGS) $(GMIC_CFLAGS)
gmic_short.o: gmic.cpp
	$(CC) -o gmic_short.o -c gmic.cpp -Dgmic_separate_compilation -Dgmic_short $(CFLAGS) $(GMIC_CFLAGS)
gmic_uint.o: gmic.cpp
	$(CC) -o gmic_uint.o -c gmic.cpp -Dgmic_separate_compilation -Dgmic_uint $(CFLAGS) $(GMIC_CFLAGS)
gmic_int.o: gmic.cpp
	$(CC) -o gmic_int.o -c gmic.cpp -Dgmic_separate_compilation -Dgmic_int $(CFLAGS) $(GMIC_CFLAGS)
gmic_float.o: gmic.cpp
	$(CC) -o gmic_float.o -c gmic.cpp -Dgmic_separate_compilation -Dgmic_float $(CFLAGS) $(GMIC_CFLAGS)
gmic_double.o: gmic.cpp
	$(CC) -o gmic_double.o -c gmic.cpp -Dgmic_separate_compilation -Dgmic_double $(CFLAGS) $(GMIC_CFLAGS)

gmic: gmic_bool.o gmic_uchar.o gmic_char.o gmic_ushort.o gmic_short.o gmic_uint.o gmic_int.o gmic_float.o gmic_double.o gmic.cpp
	$(CC) -o gmic gmic.cpp -Dgmic_separate_compilation -Dgmic_main \
		 gmic_bool.o gmic_uchar.o gmic_char.o gmic_ushort.o gmic_short.o \
		 gmic_uint.o gmic_int.o gmic_float.o gmic_double.o $(CFLAGS) $(GMIC_CFLAGS) $(LDFLAGS) $(GMIC_LDFLAGS)

ifeq ($(MACOSX_APP),1)
	mkdir -p gmic.app/Contents/MacOS
	mv ${*} gmic.app/Contents/MacOS
endif
ifeq ($(STRIP_EXE),1)
	strip gmic
endif

# Linux/BSD/Mac OSX targets
linux:
	make "CFLAGS+=-Dcimg_display=1 -I/usr/X11R6/include" "LDFLAGS+=-L/usr/X11R6/lib -lX11 -lpthread" "STRIP_EXE=1" gmic

# Sun Solaris targets
solaris:
	make "CFLAGS+=-Dcimg_display=1 -I/usr/X11R6/include" "LDFLAGS+=-L/usr/X11R6/lib -R/usr/X11R6/lib -lX11 -lpthread -lrt -lnsl -lsocket" "STRIP_EXE=1" gmic

# MacOsX with Carbon display targets
macosx:
	make "CFLAGS+=-Dcimg_display=3 -framework Carbon" "LDFLAGS+=-framework Carbon" "MACOSX_APP=1" gmic

# Minimal dependancies, for testing purposes (fast compilation).
debug:
	$(CC) -o gmic gmic.cpp -Dgmic_debug -Dcimg_display=1 -I/usr/X11R6/include -g -W -Wall -ansi -pedantic -L/usr/X11R6/lib -lX11 -lpthread

install:
	mkdir -p $(DESTDIR)$(BINDIR)/
	cp -f gmic $(DESTDIR)$(BINDIR)/
	mkdir -p $(DESTDIR)/usr/share/man/man1/
	mkdir -p $(DESTDIR)/usr/share/man/fr/man1/
	cp -f man/gmic.1.gz $(DESTDIR)/usr/share/man/man1/gmic.1.gz
	cp -f man/gmic.1.gz $(DESTDIR)/usr/share/man/fr/man1/gmic.1.gz
	mkdir -p $(DESTDIR)/usr/share/doc/gmic/html/img/
	for pixmap in html/*.*; do \
	  if test -f $$pixmap; then \
	    cp -f  $$pixmap  $(DESTDIR)/usr/share/doc/gmic/html/; \
	  fi \
	done
	for pixmap in html/img/*.*; do \
	  if test -f $$pixmap; then \
	    cp -f  $$pixmap  $(DESTDIR)/usr/share/doc/gmic/html/img/; \
	  fi \
	done

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/gmic
	rm -rf $(DESTDIR)/usr/share/doc/gmic/
	rm -f $(DESTDIR)/usr/share/man/man1/gmic.1.gz
	rm -f $(DESTDIR)/usr/share/man/fr/man1/gmic.1.gz
