# Makefile for Netpbm

# Configuration should normally be done in the included file Makefile.config.

# Targets in this file:
#
#   all: (default).  Build everything, in the source directory.
#   install:  Install everything into the live system
#   install.bin:  Install executables into the live system
#   install.lib:  Install Netpbm (and maybe tiff) shared libraries
#   install.man:  Install man pages
#   install-dev:  Install development stuff: headers and static libraries
#   merge:  Build everything, but in the "merge" style.  See below.
#   install-merge: Install everything, built in the "merge" style.

# About the "merge" target: Normally the Makefiles build and install
# separate executables 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 programs together into one huge
# executable, along with a tiny dispatch program that runs one of the
# programs based on the command name with which it was invoked.  You
# install the merged executable with a file system link for the name
# of each program it includes.  On a Sun3 under SunOS 3.5 the space
# for executables went from 2970K to 370K in an older Netpbm.
# On a Linux x86 system with Netpbm 8.4, it saved 615K.
#
# To build a "merge" system, do a "make merge" instead of "make [all]"
# and "make install-merge" instead of "make install".  And as long as
# you're building a merge system, you aren't gaining a lot from the 
# shared libraries, so you might want to avoid the headache and add
# a STATICLIB=Y to both those Make commands.

# The CURDIR variable presents a problem because it was introduced in
# GNU Make 3.77.  We need the CURDIR variable in order for our 'make
# -C xxx -f xxx' commands to work.  If we used the obvious alternative
# ".", that wouldn't work because it would refer to the directory
# named in -C, not the directory the make file you are reading is
# running in.  The -f option is necessary in order to have separate
# source and object directories in the future.

ifeq ($(CURDIR)x,x)
all install install.bin install.lib install.man install-dev \
merge install-merge:
	@echo "YOU NEED AT LEAST VERSION 3.77 OF GNU MAKE TO BUILD NETPBM."
	@echo "Netpbm's makefiles need the CURDIR variable that was "
	@echo "introduced in 3.77.  Your version does not have CURDIR."
	@echo
	@echo "You can get a current GNU Make via http://www.gnu.org/software"
else


ifeq ($(SRCDIR)x,x)
  SRCDIR := $(CURDIR)
endif
BUILDDIR = .
# Some day, we'll figure out how to make BUILDDIR != SRCDIR

include Makefile.config

SUBDIRS = pbm pgm ppm pnm

# 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: $(SUBDIRS)

.PHONY: $(SUBDIRS) libtiff
$(SUBDIRS) libtiff:
	$(MAKE) -C $@ -f $(SRCDIR)/$@/Makefile all

#libopt is a utility program used by the make files
libopt: libopt.o
	$(LD) $(LDFLAGS) -o $@ $< $(CDEBUG)
libopt.o: $(SRCDIR)/libopt.c
	$(CC) -c $(CFLAGS) -o $@ $<

.PHONY: config
config: Makefile.config

Makefile.config: $(SRCDIR)/Makefile.config.in
	$(SRCDIR)/configure $<
	@echo
	@echo

ifeq ($(STATICLIB),Y)
    SHAREDLIB_INSTALL_DEP = 
else
    SHAREDLIB_INSTALL_DEP = install.lib
endif

.PHONY: install
install: install.bin $(SHAREDLIB_INSTALL_DEP) install.man 
.PHONY: install-merge
install-merge: install.merge $(SHAREDLIB_INSTALL_DEP) install.man

MERGELIST = $(patsubst %,%.merge,$(SUBDIRS))

.PHONY: $(MERGELIST)
$(MERGELIST):
	$(MAKE) -C $(patsubst %.merge,%,$@) \
	  -f $(SRCDIR)/$(patsubst %.merge,%,$@)/Makefile   merge
# e.g.  make -C pbm -f /usr/src/netpbm/pbm/Makefile merge

.PHONY: merge
merge:  $(MERGELIST)

.PHONY: install.bin
install.bin:
	$(SRCDIR)/mkinstalldirs $(INSTALLBINARIES)
	for i in $(SUBDIRS) ; do \
	    $(MAKE) -C $$i -f $(SRCDIR)/$$i/Makefile install.bin ; \
	done

.PHONY: install-merge
install.merge:
	$(SRCDIR)/mkinstalldirs $(INSTALLBINARIES)
	for i in $(SUBDIRS) ; do \
	    $(MAKE) -C $$i -f $(SRCDIR)/$$i/Makefile install.merge ; \
	done

.PHONY: install.lib
install.lib:
	$(SRCDIR)/mkinstalldirs $(INSTALLLIBS)
	for i in $(SUBDIRS) ; do \
	    $(MAKE) -C $$i -f $(SRCDIR)/$$i/Makefile install.lib ; \
	done
ifeq ($(TIFFLIB_DIR), $(SRCDIR)/libtiff)
# We built using our own TIFF library, so better install it too.
	$(MAKE) -C libtiff -f $(SRCDIR)/libtiff/Makefile install.lib
endif

.PHONY: install.man
install.man:
	$(SRCDIR)/mkinstalldirs \
	    $(INSTALLMANUALS1) $(INSTALLMANUALS3) $(INSTALLMANUALS5)
	for i in $(SUBDIRS) ; do \
	    $(MAKE) -C $$i -f $(SRCDIR)/$$i/Makefile install.man ; \
	done

.PHONY: install-dev
install-dev: install.hdr install.staticlib

.PHONY: install.hdr
install.hdr:
	$(SRCDIR)/mkinstalldirs $(INSTALLHDRS)
	$(INSTALL) -c -m$(INSTALL_PERM_HDR) \
	    $(SRCDIR)/pbmplus.h $(INSTALLHDRS)
	$(INSTALL) -c -m$(INSTALL_PERM_HDR) \
	    $(SRCDIR)/shhopt/shhopt.h $(INSTALLHDRS)
	for i in $(SUBDIRS) ; do \
	    $(MAKE) -C $$i -f $(SRCDIR)/$$i/Makefile install.hdr ; \
	done
ifeq ($(TIFFLIB_DIR), $(SRCDIR)/libtiff)
# We built using our own TIFF library, so better install it too.
	$(MAKE) -C libtiff -f $(SRCDIR)/libtiff/Makefile install.hdr
endif

.PHONY: install.staticlib
install.staticlib:
	$(SRCDIR)/mkinstalldirs $(INSTALLLIBS)
	for i in $(SUBDIRS) ; do \
	    $(MAKE) -C $$i -f $(SRCDIR)/$$i/Makefile install.staticlib ; \
	done
ifeq ($(TIFFLIB_DIR), $(SRCDIR)/libtiff)
# We built using our own TIFF library, so better install it too.
	$(MAKE) -C libtiff -f $(SRCDIR)/libtiff/Makefile install.staticlib
endif

.PHONY: clean
clean:
	rm -f libopt libopt.o
	for i in $(SUBDIRS) libtiff urt shhopt ; do \
	    $(MAKE) -C $$i -f $(SRCDIR)/$$i/Makefile clean ; \
	done

.PHONY: distclean
distclean: clean
	rm -f Makefile.config
	$(SRCDIR)/empty_depend

.PHONY: dep
dep:
	for i in $(SUBDIRS) libtiff ; do \
	    $(MAKE) -C $$i -f $(SRCDIR)/$$i/Makefile dep ; \
	done
# The following endif is for the else block that contains virtually the
# whole file, for the test of the existence of CURDIR.
endif