#
#  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
#---------------------------------
CC = g++
BINDIR=/usr/bin
PLUGINDIR=`gimptool-2.0 --gimpplugindir`/plug-ins
GMIC_CFLAGS =
GMIC_LDFLAGS = -lpthread
GMIC4GIMP_CFLAGS = -Dcimg_display=0
GMIC4GIMP_LDFLAGS = -lpthread

ifeq ($(CC),g++)
GMIC_CFLAGS += -Wall -W
GMIC_LDFLAGS += -lm
GMIC4GIMP_CFLAGS += -Wall -W
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
GMIC4GIMP_CFLAGS+= -O3 -ipo -no-prec-div
else
GMIC_CFLAGS+= -O3 -ffast-math -fno-tree-pre
GMIC4GIMP_CFLAGS+= -O3 -ffast-math -fno-tree-pre
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 # -lswscale

# 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
GMIC_LDFLAGS+= -lMagick++

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

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

# By default, compilation of 'gmic' and 'gmic4gimp' is done for 'linux' architectures.
all: linux_swscale gmic4gimp linux

distclean: clean

clean:
	rm -rf gmic*.o gmic gmic4gimp *~

# Update the default 'gmic4gimp' macro file ('gmic' must be already installed !)
gmic4gimp_def: gmic4gimp_def.raw
	gmic -t char gmic4gimp_def.raw:`du -Db gmic4gimp_def.raw | awk '{print $$1}'` -o -.h | sed 's/unnamed/gmic4gimp_def/' | sed 's/char/const char/' > ../../CImg/examples/gmic4gimp_def.h
	gmic -t uchar ../html/img/logo_mini.ppm -permute vxyz -o -.h | sed 's/unnamed/logo/' | sed 's/char/const char/' >> ../../CImg/examples/gmic4gimp_def.h

# Update the default 'gmic' macro file ('gmic' must be already installed !)
gmic_def: gmic_def.raw
	gmic -t char gmic_def.raw:`du -Db gmic_def.raw | awk '{print $$1}'` -o -.h | sed 's/unnamed/def/' | sed 's/char/const char/' > ../../CImg/examples/gmic_def.h

# Compile the command line version 'gmic' of G'MIC.
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.cpp 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
	$(CC) -o gmic gmic.cpp -Dgmic_separate_compilation -Dgmic_main $(CFLAGS) $(GMIC_CFLAGS) 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 $(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

# Compile the plug-in version 'gmic4gimp' of G'MIC for Gimp.
# There is only one 'gimc4gimp' entry for all targets.
gmic4gimp.o: gmic.cpp
	$(CC) -o gmic4gimp.o -c gmic.cpp -Dgmic_minimal $(GMIC4GIMP_CFLAGS)

gmic4gimp: gmic4gimp.o gmic4gimp.cpp
	$(CC) -o gmic4gimp gmic4gimp.cpp gmic4gimp.o -I.. -I./plugins `gimptool-2.0 --cflags` `gimptool-2.0 --libs` $(GMIC4GIMP_CFLAGS) $(GMIC4GIMP_LDFLAGS)

# Compile the command line tool version 'gmic' of G'MIC.
# There is different entries here for various targets.
linux:
	make "CFLAGS+=-Dcimg_display=1 -I/usr/X11R6/include" "LDFLAGS+=-L/usr/X11R6/lib -lX11 -lpthread" "STRIP_EXE=1" gmic

linux_swscale:
	make "CFLAGS+=-Dcimg_display=1 -I/usr/X11R6/include" "LDFLAGS+=-L/usr/X11R6/lib -lX11 -lpthread -lswscale" "STRIP_EXE=1" gmic

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:
	make "CFLAGS+=-Dcimg_display=3 -framework Carbon" "LDFLAGS+=-framework Carbon" "MACOSX_APP=1" gmic

minimal:
	$(CC) -o gmic gmic.cpp -Dgmic_minimal -Dgmic_main -Dcimg_display=1 -I/usr/X11R6/include -g -W -Wall -ansi -pedantic -L/usr/X11R6/lib -lX11 -lpthread

def:
	make gmic_def
	make gmic4gimp_def

# Install - Uninstall
install:
	mkdir -p $(DESTDIR)$(PLUGINDIR)/
	cp -f gmic4gimp $(DESTDIR)$(PLUGINDIR)/
	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

# End of Makefile
