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

# Targets in this file:
#
#   nonmerge:     Build everything, in the source directory.
#   merge:        Build everything as merged executables, in the source dir
#   install:      install-run + install-dev
#   install-run:  install stuff needed to run Netpbm programs
#   install-dev:  Install stuff needed to develop/build programs that use
#                 Netpbm libraries:  headers and static libraries.
#   install.bin:  Install executables
#   install.lib:  Install Netpbm shared libraries
#   install.man:  Install man pages
#   install.staticlib:  Install Netpbm static libraries
#   install.sharedlibstub:  Install the ".so" file - the file used at link
#                 time to prepare a program for runtime linking of a library
#   install.hdr:  Install Netpbm library interface headers

#   The default target is either "merge" or "nonmerge", as determined by
#   the DEFAULT_TARGET variable set by Makefile.config.

# 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, just set DEFAULT_TARGET to "merge" instead
# of "nomerge" in Makefile.config.  In that case, you should probably also
# set NETPBMLIBTYPE to "unixstatic", since shared libraries don't do you 
# much good.

# 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-run install-dev:
	@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"
	@echo 
	@echo "If upgrading is impossible, try modifying GNUMakefile and "
	@echo "Makefile.common to replace \$(CURDIR) with \$(shell /bin/pwd) "
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

default: $(DEFAULT_TARGET)

all: nonmerge

.PHONY: nonmerge
nonmerge: $(SUBDIRS)

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

.PHONY: config
config: Makefile.config

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

.PHONY: install
install: install-run install-dev

.PHONY: install-run
ifeq ($(DEFAULT_TARGET),merge)
install-run: install-merge
else
install-run: install-nonmerge 
endif

install-merge: install.merge install.lib install.man

install-nonmerge: install.bin install.lib 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)

# The following generates a warning message if two of these variables are
# set to the same directory.  That's OK.
$(INSTALLMANUALS1) $(INSTALLMANUALS3) $(INSTALLMANUALS5) $(INSTALLHDRS) \
    $(INSTALLSTATICLIBS) $(INSTALLBINARIES) $(INSTALLLIBS):
	$(SRCDIR)/buildtools/mkinstalldirs $@

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

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

.PHONY: install.lib
ifeq ($(NETPBMLIBTYPE),unixstatic)
install.lib:
else
install.lib: $(INSTALLLIBS)
	for i in $(SUBDIRS) ; do \
	    $(MAKE) -C $$i -f $(SRCDIR)/$$i/Makefile install.lib ; \
	done
	$(SRCDIR)/buildtools/try_ldconfig
endif


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

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

.PHONY: install.hdr
install.hdr: $(INSTALLHDRS) $(SRCDIR)/pm_config.h
# See notes in Makefile.common about how $(INSTALL) varies from one 
# platform to another.
	$(INSTALL) -c -m $(INSTALL_PERM_HDR) \
	    $(SRCDIR)/pm_config.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 ($(STATICLIB_TOO),y)
BUILD_STATIC = y
else
  ifeq ($(NETPBMLIBTYPE),unixstatic)
    BUILD_STATIC = y
  else
    BUILD_STATIC = n
  endif
endif

.PHONY: install.staticlib
ifeq ($(BUILD_STATIC),y)
install.staticlib: $(INSTALLSTATICLIBS)
	for i in $(SUBDIRS) ; do \
	    $(MAKE) -C $$i -f $(SRCDIR)/$$i/Makefile install.staticlib ; \
	done
else
install.staticlib: 
endif

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

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

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

.PHONY: dep
dep:
	for i in $(SUBDIRS); 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
