# This is a make file inclusion, to be included in all the
# Netpbm make files.

# This file is meant to contain rules that are substantially the same
# in each of the pbm, pgm, ppm, and pnm subdirectory makes, to avoid
# duplication of effort.

# The following variables must be set in any make file that uses these
# rules:
#
# OBJECTS: .o files to be built from .c files with the standard rule.
# PORTBINARIES: list of conventional executables to be built with the standard
#   rule
# MATHBINARIES: list of conventional executables to be built with the standard
#   rule, but that need math functions too.
# LIBROOT: pbm, pgm, ppm, or pnm, whichever is the root of library names
# for this build.
# MAJ: major number of shared libraries
# MIN: minor number of shared libraries
#   The minor number in the filename corresponds to the point release of
#   Netpbm at which the library last changed.  Do not change the major 
#   number (and soname) unless the new library is not backward compatible.
# LIBOBJECTS: list of the object files that make up the library, all of which
#   are to be built with the standard rule for library objects.
# LIBOBJECTS_X: same as LIBOBJECTS, but not built with standard rule
# BINARIES: list of all the executables that need to be installed.
# MERGEBINARIES: subset of BINARIES that can be included in a merge build
# NOMERGEBINARIES: subset of BINARIES that cannot be included in a merge build
# INSTALLBINARIES: directory where they need to be installed.
# SCRIPTS: list of all the scripts that need to be installed
# INSTALLSCRIPTS: directory where they need to be installed.
# MANUALS1: list of all the topic names of Section 1 man pages that need
# to be installed (e.g. "pnmtotiff" for the Pnmtotiff man page).
# SUFFIXMANUALS1: suffix for Section 1 man pages (typically "1").
# INSTALLMANUALS1: directory where Section 1 man pages need to go.
# (Analogous variables for Sections 3 and 5)
# INTERFACE_HEADERS: list of interface header (.h) files that need to be
# installed.
# INSTALLLIBS: directory where they need to be installed.
# MANCP: command to use to copy man pages where they belong.  Typically "cp".
# INSTALL: command to use to copy files to where they belong
# INSTALL_PERM_BIN: file permissions for installed binaries
# INSTALL_PERM_LIB: ...same for libraries
# INSTALL_PERM_HDR: ...same for headers
# INSTALL_PERM_MAN: ...same for man pages
# MERGENAME: name of the merged executable (e.g. pbmmerge).
# MERGE_OBJECTS: list of object files that make up the merged executable,
#   not counting the master object (e.g. pbmmerge.o).  All of these are to
#   be built with the standard rule for merged objects.
# NETPBMLIBS: list of Netpbm libraries needed by the merged executable
#   being built
# LIBLIBS: list of Netpbm libraries needed by the Netpbm library being built.
# CC: C compiler command for the merge main object
# CFLAGS: C compiler general options for the merge main object
# CFLAGS_SHLIB: Extra C compiler options for compiling a .o file that will
#   be in a shared library.  (usually null or -fpic).
# LD: linker command
# LDFLAGS: linker options 
# INCLUDE: Compiler option string to establish the search path for include
#   files when compiling thing or computing dependencies (make dep).

# libopt is a utility program used in the make file below.
$(SRCDIR)/libopt:
	$(MAKE) -C $(SRCDIR) libopt

# Rule for objects.
$(OBJECTS): %.o: %.c
# Note that the user may have configured -I options into CFLAGS.
	$(CC) -c $(INCLUDE) $(CFLAGS) -o $@ $<

LIBOPT = $(SRCDIR)/libopt

# Rules for conventional single-object file executables
$(PORTBINARIES): %: %.o $(NETPBMLIBS) $(LIBOPT)
# Note that LDFLAGS might contain -L options, so order is important.
	$(LD) -o $@ $@.o `$(LIBOPT) $(NETPBMLIBS)` $(LDFLAGS) $(CDEBUG)

# Same as above, but needing math functions
$(MATHBINARIES): %: %.o $(NETPBMLIBS) $(LIBOPT)
# Note that LDFLAGS might contain -L options, so order is important.
	$(LD) -o $@ $@.o -lm `$(LIBOPT) $(NETPBMLIBS)` $(LDFLAGS) $(CDEBUG)


# MERGE STUFF

$(MERGE_OBJECTS): %.o2: %.c
# Note that the user may have configured -I options into CFLAGS.
	$(CC) -c $(INCLUDE) $(CFLAGS) "-Dmain=$*_main" -o $@ $<

merge.h: 
	$(SRCDIR)/make_merge.sh $(MERGEBINARIES)

$(MERGENAME).o: $(MERGENAME).c merge.h
	$(CC) $(CFLAGS) -Wno-implicit -c -o $@ $<

$(MERGENAME): $(MERGENAME).o $(MERGE_OBJECTS) $(NETPBMLIBS) $(LIBOPT)
# Note that LDFLAGS might contain -L options, so order is important.
	$(LD) -o $@ $< $(MERGE_OBJECTS) \
          `$(LIBOPT) $(NETPBMLIBS)` -lm $(LDFLAGS) $(CDEBUG)


# BUILDING NETPBM LIBRARIES

$(LIBOBJECTS): %.o: %.c
# Note that the user may have configured -I options into CFLAGS.
	$(CC) -c $(INCLUDE) $(CFLAGS) $(CFLAGS_SHLIB) -o $@ $<

SONAME = lib$(LIBROOT).so.$(MAJ)

# The libxxx.so link is needed to link the executables.
lib$(LIBROOT).so: $(SONAME)
	rm -f $@
	ln -s $< $@
# The $(SONAME) link is only needed to test the programs without
# installing the libraries (in that case, you also need to direct the 
# dynamic linker to the source directories, e.g. set LD_LIBRARY_PATH).
$(SONAME): lib$(LIBROOT).so.$(MAJ).$(MIN)
	rm -f $@
	ln -s $< $@
lib$(LIBROOT).so.$(MAJ).$(MIN): $(LIBOBJECTS) $(LIBOBJECTS_X) $(LIBLIBS) \
                                $(LIBOPT)
	$(LD) $(LDSHLIB) -o $@ $(LIBOBJECTS) $(LIBOBJECTS_X) \
          `$(LIBOPT) $(LIBLIBS)` -lc

# Static library.  Unused by default, but with a small change to make files...
lib$(LIBROOT).a: $(LIBOBJECTS) $(LIBOBJECTS_X)
	-rm -f $@
ifeq ($(CPU),PPC)
	ppc-amigaos-ar rc $@ $(LIBOBJECTS) $(LIBOBJECTS_X)
	-ppc-amigaos-ranlib $@
else
	ar rc $@ $(LIBOBJECTS) $(LIBOBJECTS_X)
	-ranlib $@
endif

# Some maintenance notes about $(INSTALL): Some install programs can
# install multiple files in one shot; others can take only one file at
# a time.  Some have a -c option; others ignore -c.  Some can take
# permissions in mnemonic form (u=rwx,go=rx); others can't, but all
# take the encoded form (755).  Some have a -d option to install
# directories and never install them implicitly.  Others create
# directories only implicitly.  Installbsd and OSF1 Install need a
# space in "-m 755".  Others don't care.  2000.05.17.  OSF1 Install
# takes only one parameter: the source file.  It picks a destination
# directory by default, or you can specify it with a -f option.
# 2000.06.15


# Before Netpbm 8.4, we did hard links instead of symbolic.  A user
# requested symbolic, and I couldn't see a reason for hard (original reason
# may have been lack of symbolic capability), so I changed it.  -Bryan
.PHONY: install.merge.common
install.merge.common: $(MERGENAME) $(NOMERGEBINARIES) install.script
	cd $(INSTALLBINARIES) ; rm -f $(BINARIES) $(MERGE_ALIASES)
ifneq ($(MERGENAME)x,x)
	$(INSTALL) -c -s -m $(INSTALL_PERM_BIN) $(MERGENAME) $(INSTALLBINARIES)
	cd $(INSTALLBINARIES) ; \
	   for i in $(MERGEBINARIES) $(MERGE_ALIASES) ; \
             do ln -s $(MERGENAME)$(EXE) $$i ; \
             done
endif
ifneq ($(NOMERGEBINARIES)x,x)
	for x in $(NOMERGEBINARIES); \
        do \
           $(INSTALL) -c -s -m $(INSTALL_PERM_BIN) $$x $(INSTALLBINARIES); \
	done
endif

# INSTALLING

.PHONY: install.bin.common
install.bin.common: $(BINARIES) install.script
# Note that on Cygwin, the executables are actually pbmmake.exe, etc.
# Make and Install know that pbmmake.exe counts as pbmmake.
	for x in $(BINARIES); \
	do \
	   $(INSTALL) -c -m $(INSTALL_PERM_BIN) $$x $(INSTALLBINARIES); \
	done

.PHONY: install.script
install.script: $(SCRIPTS)
ifneq ($(SCRIPTS)x,x)
	for x in $(SCRIPTS); \
	do \
	   $(INSTALL) -c -m $(INSTALL_PERM_BIN) $$x $(INSTALLSCRIPTS); \
	done
endif

.PHONY: install.man.common
install.man.common:
ifneq ($(MANUALS1)x,x)
	for i in $(MANUALS1) ; do \
	    rm -f $(INSTALLMANUALS1)/$$i.$(SUFFIXMANUALS1) ; \
	    $(MANCP) $$i.1 $(INSTALLMANUALS1)/$$i.$(SUFFIXMANUALS1) ; \
	done
endif
ifneq ($(MANUALS3)x,x)
	for i in $(MANUALS3) ; do \
	    rm -f $(INSTALLMANUALS3)/$$i.$(SUFFIXMANUALS3) ; \
	    $(MANCP) $$i.3 $(INSTALLMANUALS3)/$$i.$(SUFFIXMANUALS3) ; \
	done
endif
ifneq ($(MANUALS5)x,x)
	for i in $(MANUALS5) ; do \
	    rm -f $(INSTALLMANUALS5)/$$i.$(SUFFIXMANUALS5) ; \
	    $(MANCP) $$i.5 $(INSTALLMANUALS5)/$$i.$(SUFFIXMANUALS5) ; \
	done
endif

.PHONY: install.hdr
install.hdr:
# You only need to install the interface header files if you are going to
# compile programs that use the Netpbm libraries.  Alternatively, you may
# prefer not to "install" them, but just to access the Netpbm source
# directory when you compile your programs.
	for x in $(INTERFACE_HEADERS); \
	do \
	   $(INSTALL) -c -m $(INSTALL_PERM_HDR) $$x $(INSTALLHDRS); \
	done

# Install a static library 
#
# Note that this installs static libraries even if STATICLIB=N,
# because STATICLIB just tells how the Netpbm programs are to be
# built.  If a previous make STATICLIB=N didn't build the static
# library, the dependency here will do that.
.PHONY: install.staticlib
install.staticlib: lib$(LIBROOT).a
	$(INSTALL) -c -m $(INSTALL_PERM_LIBS) $< $(INSTALLLIBS)/$<

.PHONY: install.lib.common
ifeq ($(NETPBMLIBSUFFIX),so)
# install a shared library
install.lib.common: lib$(LIBROOT).so.$(MAJ).$(MIN) 
	cd $(INSTALLLIBS) ; rm -f lib$(LIBROOT).so.$(MAJ).*
	$(INSTALL) -c -m $(INSTALL_PERM_LIBD) $< $(INSTALLLIBS)
	cd $(INSTALLLIBS) ; rm -f lib$(LIBROOT).so; ln -s $< lib$(LIBROOT).so
	cd $(INSTALLLIBS) ; rm -f $(SONAME); ln -s $< $(SONAME)
endif

.PHONY: clean.common
clean.common:
	-rm -f *.o *.o2 *.a *.so* *.cat *~ core *.core *.i *.s merge.h \
          $(BINARIES) $(MERGENAME)

.PHONY: dep
dep Makefile.depend:
# We use -MG here because of ../compile.h.  It need not exist
# before the first make after a clean.
	$(CC) -MM -MG $(INCLUDE) *.c >Makefile.depend

include Makefile.depend
