# 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.

SRCDIR = .
include $(SRCDIR)/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: compile.h $(SUBDIRS)

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

.PHONY: $(SUBDIRS)
$(SUBDIRS):
	$(MAKE) -C $@ 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:
	$(SRCDIR)/stamp-date

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


.PHONY: install
install: install.bin install.lib install.man 
install-merge: install.merge install.lib install.man

.PHONY: merge
merge:  compile.h
	for i in $(SUBDIRS) ; do \
	    ( echo merge.$$i ; cd $$i ; $(MAKE) merge );\
	done

.PHONY: install.bin
install.bin:
	$(INSTALL) -d -mu=rwx,go=rx $(INSTALLBINARIES)
	for i in $(SUBDIRS) ; do \
	    ( echo install.bin.$$i ; cd $$i ; $(MAKE) install.bin ); \
	done

.PHONY: install-merge
install.merge:
	$(INSTALL) -d -mu=rwx,go=rx $(INSTALLBINARIES)
	for i in $(SUBDIRS) ; do \
	    ( echo install.merge.$$i ; cd $$i ; $(MAKE) install.merge ); \
	done

.PHONY: install.lib
install.lib:
	$(INSTALL) -d -mu=rwx,go=rx $(INSTALLLIBS)
	for i in $(SUBDIRS) ; do \
	    ( echo install.lib.$$i ; cd $$i ; $(MAKE) install.lib ); \
	done
ifeq ($(TIFFLIB_DIR), $(SRCDIR)/libtiff)
# We built using our own TIFF library, so better install it too.
	cd libtiff; $(MAKE) install
endif

.PHONY: install.man
install.man:
	$(INSTALL) -d -mu=rwx,go=rx \
	    $(INSTALLMANUALS1) $(INSTALLMANUALS3) $(INSTALLMANUALS5)
	for i in $(SUBDIRS) ; do \
	    ( echo install.man.$$i ; cd $$i ; $(MAKE) install.man ); \
	done

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

.PHONY: install.hdr
install.hdr:
	$(INSTALL) -d -mu=rwx,go=rx $(INSTALLHDRS)
	$(INSTALL) -m$(INSTALL_PERM_HDR) pbmplus.h $(INSTALLHDRS)
	for i in $(SUBDIRS) ; do \
	    ( echo install.hdr.$$i ; cd $$i ; $(MAKE) install.hdr ); \
	done
ifeq ($(TIFFLIB_DIR), $(SRCDIR)/libtiff)
# We built using our own TIFF library, so better install it too.
	cd libtiff; $(MAKE) install.hdr
endif

.PHONY: install.staticlib
install.staticlib:
	$(INSTALL) -d -mu=rwx,go=rx $(INSTALLLIBS)
	for i in $(SUBDIRS) ; do \
	    (echo install.staticlib.$$i; cd $$i; $(MAKE) install.staticlib ); \
	done
ifeq ($(TIFFLIB_DIR), $(SRCDIR)/libtiff)
# We built using our own TIFF library, so better install it too.
	cd libtiff; $(MAKE) install.staticlib
endif

.PHONY: clean
clean:
	rm -f compile.h libopt libopt.o
	for i in $(SUBDIRS) libtiff shhopt ; do \
	    ( echo clean.$$i ; cd $$i ; $(MAKE) clean ); \
	done

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