# Makefile for pbmplus tools.
#
# Copyright (C) 1989, 1991 by Jef Poskanzer.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation.  This software is provided "as is" without express or
# implied warranty.

# CONFIGURE: Normally the Makefiles build and install separate binaries for
# each program.  However, on some systems (especially those without shared
# libraries) this can mean a lot of space.  In this case you might try
# building a "merge" instead.  The idea here is to link all the binaries
# together into one huge executable, with a tiny dispatch program as the
# main.  Then the merged binary is installed with file-system links for
# each program it includes.  The dispatch routine can tell which program
# to run by looking at argv[0].  On a Sun3 under SunOS 3.5 the space for
# executables went from 2.9 meg to .36 meg.
#
# To build a "merge" system, do a "make merge" instead of "make [all]"
# and "make install-merge" instead of "make install".

#CC =		cc
#CC =		gcc
#CC =		gcc -fpcc-struct-return
CC =		gcc -pedantic -fpcc-struct-return

# CONFIGURE: cc flags go here.
#CDEBUG is for setting on the command line to temporarily add an additional
#compiler option.  E.g. make all CDEBUG=-g  or make CDEBUG=-Werror

CFLAGS =	-O2 -Wall -Wno-uninitialized $(TIFFDEF) $(CDEBUG)

# CONFIGURE: ld flags go here.  Eunice users may want to use -noshare so that
# the binaries can run standalone.
LDFLAGS =	-s
#LDFLAGS =	
#LDFLAGS =	-noshare

# CONFIGURE: The netpbm package contains a version of the Tiff library,
# libtiff.  If you want to use your own, fill in the appropriate paths
# below for the library and its interface headers

# These paths are relative to one of our subdirectories.  These variables are
# used only in subdirectory make files.
TIFFINC =	-I../libtiff
TIFFLIB =	../libtiff/libtiff.a
TIFFDEF =	-DLIBTIFF

# CONFIGURE: If you want to build the jpeg converters or tiff
# converters that use jpeg compression in tiff files, you must have
# the jpeg library already installed.  Put the locations of the link
# library and the interface header files here.  If you do not have the
# JPEG library, and still want to successully build everything else,
# including non-JPEG versions of the tiff converters, put NONE for
# these.

# Special note for people using an already-installed Tiff library:  If
# your library was built with the JPEG stuff statically linked in, you
# don't need the JPEG library and the tiff converters will still do jpeg.
# If your library references a dynamic JPEG library, you must specify
# at least JPEGLIB_DIR here, or the tiff converters will not build at
# all.

JPEGLIB_DIR = /usr/lib/jpeg
JPEGHDR_DIR = /usr/include/jpeg

# CONFIGURE: Specify the directory where you want the executables.
# If you need scripts and binaries to be in different directories, you
# can set that up too.
INSTALLBINARIES = /usr/local/netpbm
INSTALLSCRIPTS = $(INSTALLBINARIES)

# CONFIGURE: Specify the directory where you want the shared libraries
# installed.  For this to be effective, you must have set up this location
# to be searched by your system's program executor for runtime libraries.
INSTALLLIBS = $(INSTALLBINARIES)

# CONFIGURE: Specify the directories that you want the manual sources copied to,
# plus the suffix you want them to have.
INSTALLMANUALS1 =	/usr/local/man/man1
SUFFIXMANUALS1 =	1
INSTALLMANUALS3 =	/usr/local/man/man3
SUFFIXMANUALS3 =	3
INSTALLMANUALS5 =	/usr/local/man/man5
SUFFIXMANUALS5 =	5

# CONFIGURE: Normally the man pages are installed using "cp".  By changing
# this define you can use something else, for example a script that calls
# compress or pack. The ../mantocat is used on systems which use man pages
# in the "cat" format.
MANCP =			cp
#MANCP =		../mantocat


# End of configurable definitions.

SHELL =		/bin/sh
MAKE =		make
SUBDIRS =	pbm pgm ppm pnm

MAKE_OVERRIDES = 'CC=$(CC)' 'CFLAGS=$(CFLAGS)' 'TIFFDEF=$(TIFFDEF)' 'TIFFINC=$(TIFFINC)' 'TIFFLIB=$(TIFFLIB)' 'LDFLAGS=$(LDFLAGS)'

# Note that the Tiff library, from the libtiff subdirectory only gets built
# if you specify the homegrown libtiff as $(TIFFLIB), in which case it is
# a dependency of stuff in the pnm subdirectory.  Or you can cd to the 
# libtiff subdirectory and do a "make all" there.
.PHONY: all
all: compile.h $(SUBDIRS)

.PHONY: libtiff
libtiff:
	$(MAKE) -C $@ all

.PHONY: $(SUBDIRS)
$(SUBDIRS):
	$(MAKE) -C $@ $(MAKE_OVERRIDES) all

# Note that we create a new compile.h only for the first make after a 
# make clean.  This is good enough.  We used to do stamp-date for every
# build of "all" from this make file, but nowhere else, so it was really
# sloppy.

compile.h:
	./stamp-date

.PHONY: install
install: install.bin install.lib install.man 

.PHONY: merge
merge:
	./stamp-date
	( echo "libtiff" ; cd libtiff ; make )
	for i in $(SUBDIRS) ; do \
	    ( echo $$i ; cd $$i ; $(MAKE) $(MFLAGS) $(MAKE_OVERRIDES) merge );\
	done

.PHONY: install.bin
install.bin:
	-mkdir $(INSTALLBINARIES)
	for i in $(SUBDIRS) ; do \
	    ( echo $$i ; cd $$i ; $(MAKE) $(MFLAGS) $(MAKE_OVERRIDES) \
        install.bin ); \
	done

.PHONY: install-merge
install.merge:
	-mkdir $(INSTALLBINARIES)
	for i in $(SUBDIRS) ; do \
	    ( echo $$i ; cd $$i ; $(MAKE) $(MFLAGS) $(MAKE_OVERRIDES) \
         install.merge ); \
	done

.PHONY: install.lib
install.lib:
	for i in $(SUBDIRS) ; do \
	    ( echo $$i ; cd $$i ; $(MAKE) $(MFLAGS) $(MAKE_OVERRIDES) \
        install.lib ); \
	done
	cd libtiff; $(MAKE) install

.PHONY: install.man
install.man:
	for i in $(SUBDIRS) ; do \
	    ( echo $$i ; cd $$i ; $(MAKE) $(MFLAGS) $(MAKE_OVERRIDES) \
        install.man ); \
	done

.PHONY: clean
clean:
	rm -f *.shar *.shar? art.* *~
	rm -f compile.h
	for i in $(SUBDIRS) libtiff shhopt ; do \
	    ( echo $$i ; cd $$i ; $(MAKE) $(MFLAGS) $(MAKE_OVERRIDES) clean ); \
	done

.PHONY: dep
dep:
	for i in $(SUBDIRS) libtiff ; do \
	    ( echo $$i ; cd $$i ; $(MAKE) $(MFLAGS) $(MAKE_OVERRIDES) dep ); \
	done
