# 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
#   package:      Issue instructions on how to install.
#   
#   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 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.  This is much more important when you're statically
# linking than when you're using shared libraries.  On a Sun3 under
# SunOS 3.5, where shared libraries are not available, the space for
# executables went from 2970K to 370K in an older Netpbm.  On a
# GNU/Linux IA32 system with shared libraries in 2002, it went from
# 2949K to 1663K.

# 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 a shared library doesn'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 package install:
	@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


include Makefile.srcdir
BUILDDIR = $(CURDIR)
SUBDIR = 
VPATH=.:$(SRCDIR)

include $(BUILDDIR)/Makefile.config

PRODUCT_SUBDIRS = lib converter analyzer editor generator other
SUPPORT_SUBDIRS = urt buildtools

SUBDIRS = $(PRODUCT_SUBDIRS) $(SUPPORT_SUBDIRS)

SCRIPTS = manweb
MANUALS1 = netpbm
NOMERGEBINARIES = netpbm

HEADERLINKS = pam.h pnm.h ppm.h pgm.h pbm.h pm.h
OBJECTS = netpbm.o

default: $(DEFAULT_TARGET)
	@echo ""
	@echo "Netpbm is built.  The next step is normally to package it "
	@echo "for installation by running "
	@echo ""
	@echo "    make package pkgdir=DIR"
	@echo ""
	@echo "to copy all the Netpbm files you need to install into the "
	@echo "directory DIR.  Then you can proceed to install."

all: nonmerge

.PHONY: nonmerge
nonmerge: $(PRODUCT_SUBDIRS:%=%/all)

OMIT_CONFIG_RULE = 1
include $(SRCDIR)/Makefile.common

$(BUILDDIR)/Makefile.config: $(SRCDIR)/Makefile.config.in
	configure Makefile.config.in

.PHONY: install
install:
	@echo "After doing a 'make', do "
	@echo ""
	@echo "  make package pkgdir=DIR"
	@echo ""
	@echo "to copy all the Netpbm files you need to install into the "
	@echo "directory DIR."
	@echo ""
	@echo "Then, do "
	@echo ""
	@echo "  ./installnetpbm"
	@echo
	@echo "to install from there to your system via an interactive.  "
	@echo "dialog.  Or do it manually using simple copy commands and "
	@echo "following instructions in the file DIR/README"

.PHONY: package init_package advise_installnetpbm
package: init_package install-run install-dev advise_installnetpbm

init_package:
	@if [ -d $(PKGDIR) ]; then \
	  echo "Directory $(PKGDIR) already exists.  Please specify a "; \
	  echo "directory that can be created fresh, like this: "; \
	  echo "  make package PKGDIR=/tmp/newnetpbm "; \
	  false; \
	  fi
	mkdir $(PKGDIR)
	echo "Netpbm install package made by 'make package'" \
	    >$(PKGDIR)/pkginfo
	date >>$(PKGDIR)/pkginfo
	@echo "Manual installation instructions are under construction." \
	    >>$(PKGDIR)/README
	@echo "However, it should be pretty obvious how to install from " \
	    >>$(PKGDIR)/README
	@echo "this directory if you know how your system is put together. " \
	    >>$(PKGDIR)/README
	@echo "There's nothing fancy about it." \
	    >>$(PKGDIR)/README

advise_installnetpbm:
	@echo
	@echo "Netpbm has been successfully packaged under directory"
	@echo "$(PKGDIR).  Run 'installnetpbm' to install it on your system."

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

.PHONY: install-merge install-nonmerge
install-merge: install.merge install.lib install.data \
	install.manweb install.man

install-nonmerge: install.bin install.lib install.data \
	install.manweb install.man

.PHONY: merge
merge: netpbm

MERGELIBS = 
ifneq ($(PNGLIB),NONE)
  MERGELIBS += $(PNGLIB)
endif
ifneq ($(ZLIB),NONE)
  MERGELIBS += $(ZLIB)
endif
ifneq ($(JPEGLIB),NONE)
  MERGELIBS += $(JPEGLIB)
endif
ifneq ($(TIFFLIB),NONE)
  MERGELIBS += $(TIFFLIB)
endif
ifneq ($(URTLIB),NONE)
  MERGELIBS += $(URTLIB)
endif
ifneq ($(LINUXSVGALIB),NONE)
  MERGELIBS += $(LINUXSVGALIB)
endif

# If URTLIB is BUNDLED_URTLIB, then we're responsible for building it, which
# means it needs to be a dependency:
ifeq ($(URTLIB),$(BUNDLED_URTLIB))
  URTLIBDEP = $(URTLIB)
endif

# We do the merge build a weird way.  The natural way would be to have a 
# merge build in Directory D do a merge build in each of its subdirectories S,
# resulting a single S/merge.o2 file in each.  Then it would combine all the
# S/merge.o2 with .o2 files in D to create a single merge.o2.  Then, at the
# top level, you would just link merge.o2 with netpbm.o and the libraries and
# you'd have it.  This way, the dependencies would all work out and it's 
# conceptually simple.

# We don't do it that way because we don't know how to merge two .o files into
# a single .o file on all the platforms (With GNU Ld, it's the --relocateable
# option).  So instead, we make the subdirectory builds just generate a load
# list of .o files (.o2, actually) and then do one huge link of them now.

netpbm:%:%.o mergelist $(NETPBMLIB) $(URTLIBDEP) $(LIBOPT)
# Note that LDFLAGS might contain -L options, so order is important.
	$(LD) -o $@ $< `cat mergelist` \
          $(LDFLAGS) `$(LIBOPT) $(NETPBMLIB) $(MERGELIBS)` \
	  $(MATHLIB) $(NETWORKLD) $(LADD)

netpbm.o: mergetrylist

install.merge: local.install.merge
.PHONY: local.install.merge
local.install.merge:
	cd $(PKGDIR)/bin; $(SYMLINKEXE) netpbm pnmnoraw
	cd $(PKGDIR)/bin; $(SYMLINKEXE) netpbm gemtopbm
	cd $(PKGDIR)/bin; $(SYMLINKEXE) netpbm pnminterp
	cd $(PKGDIR)/bin; $(SYMLINKEXE) netpbm pgmoil
	cd $(PKGDIR)/bin; $(SYMLINKEXE) netpbm ppmtojpeg
	cd $(PKGDIR)/bin; $(SYMLINKEXE) netpbm bmptoppm
	cd $(PKGDIR)/bin; $(SYMLINKEXE) netpbm pgmnorm
	cd $(PKGDIR)/bin; $(SYMLINKEXE) netpbm pnmfile

ifneq ($(NETPBMLIBTYPE),unixstatic)
install.lib: lib/install.lib
else
install.lib:
endif

.PHONY: install.manweb
install.manweb: $(PKGDIR)/man/web/netpbm.url $(PKGDIR)/bin/doc.url

$(PKGDIR)/man/web/netpbm.url: $(PKGDIR)/man/web
	echo "$(NETPBM_DOCURL)" > $@
	chmod $(INSTALL_PERM_MAN) $@

$(PKGDIR)/bin/doc.url: $(PKGDIR)/bin
	echo "$(NETPBM_DOCURL)" > $@
	chmod $(INSTALL_PERM_MAN) $@

.PHONY: install-dev
# Note that you might install the development package and NOT the runtime
# package.  If you have a special system for building stuff, maybe for 
# multiple platforms, that's what you'd do.  Ergo, install.lib is here even
# though it is also part of the runtime install.
install-dev: install.hdr install.staticlib install.lib install.sharedlibstub

.PHONY: install.hdr
install.hdr: $(PKGDIR)/include
	$(MAKE) -C lib -f $(SRCDIR)/lib/Makefile \
	    SRCDIR=$(SRCDIR) BUILDDIR=$(BUILDDIR) install.hdr
	$(INSTALL) -c -m $(INSTALL_PERM_HDR) \
	    $(SRCDIR)/pm_config.h $(PKGDIR)/include

ifeq ($(STATICLIB_TOO),y)
BUILD_STATIC = y
else
  ifeq ($(NETPBMLIBTYPE),unixstatic)
    BUILD_STATIC = y
  else
    BUILD_STATIC = n
  endif
endif

.PHONY: install.staticlib
install.staticlib: 
ifeq ($(BUILD_STATIC),y)
	$(MAKE) -C lib -f $(SRCDIR)/lib/Makefile \
	SRCDIR=$(SRCDIR) BUILDDIR=$(BUILDDIR) install.staticlib 
endif

.PHONY: install.sharedlibstub
install.sharedlibstub:
	$(MAKE) -C lib -f $(SRCDIR)/lib/Makefile \
	    SRCDIR=$(SRCDIR) BUILDDIR=$(BUILDDIR) install.sharedlibstub 

clean: localclean

.PHONY: localclean
localclean:
	rm -f netpbm

# Note that removing Makefile.config must be the last thing we do,
# because no other makes will work after that is done.
distclean: clean localdistclean
.PHONY: localdistclean
localdistclean:
	-rm -f `find -type l`
	-rm -f Makefile.config

# The following endif is for the else block that contains virtually the
# whole file, for the test of the existence of CURDIR.
endif
