Last sync'd with code: 13 Jul 2004 14:30:35 EDT
get the plain code here
# Modified  makefile for rpng-{win|x} / rpng2-{win|x} / wpng on Cygwin or
# GNU/Linux OS. Using gcc and gmake (Gnu `make').

#      *****  Changes have been made by Soren Andersen  *****
# ------------   cvs internal  ----------------------------------------
#  $Author: somian $  ##    Last modified: 13 Jul 2004 09:57:34
# -----------   /cvs internal  ----------------------------------------

# ---------------------------------------------------------------------
# Based on: Greg Roelofs "makefile.gnu" last modified:  7 March 2002
# ---------------------------------------------------------------------
#       The programs built by this makefile are described in the book,
#       "PNG:  The Definitive Guide," by Greg Roelofs (O'Reilly and
#       Associates, 1999).  Go buy a copy, eh?  Buy some for friends
#       and family, too.  (Not that this is a blatant plug or anything.)
# ---------------------------------------------------------------------
#
#  ALERT: delta situation on "rpng-win.c" and "rpng2-win.c" sources:
#      SOME CHANGES were required to compile the MS Windows (GUI) apps,
#      see a patch "*.diff" file available where this makefile was found.
#      (currently http://home.att.net/~perlspinr/makefileworkshop.html)
#
# *********************************************************************
#  X11 and MS Windows:
# *********************************************************************

#    !!!   README   !!!
# This makefile builds dynamically linked executables (against libpng and
# zlib, that is), but that can be changed by uncommenting the appropriate
# PNGLIB and ZLIB lines.

#  This makefile is "noisy" -- produces a lot of output to document what it
#  is doing. The user may wish to redirect > the output from the console to
#  a log file, for example:

#     $ make [-options and ass't defines] USE_XFREE86=1 all >buildlog.log

# On Cygwin:
#   this makefile builds the "win" flavor (native MS Windows GUI subsystem)
#   and *optionally* the "X" flavor of executables too for each of "rpng" and
#   "rpng2". Offer arguments on the command line to tell make about what you
#   want. You obviously must have "XFree86-Cygwin" properly installed on your
#   system in order to build the "X"-style binaries too.

USE_XFREE86=
USE_MSWIN32=yes
unexport USE_MSWIN32
unexport USE_XFREE86
RAW_un = $(shell uname -s 2>/dev/null | tr [:lower:] [:upper:])
IFIS_gnulin = $(strip $(findstring   LINUX,$(RAW_un)))
IFIS_cygwin = $(strip $(findstring  CYGWIN,$(RAW_un)))
ifneq (,$(IFIS_gnulin))
  USE_XFREE86 = yes
  USE_MSWIN32 =
  X_BUILD     = 3
else
   $(warning Using $(USE_MSWIN32) settings!)
endif

# This makefile supports building in a directory external to the source
# dir. If this is desired, this makefile should be copied to the build dir
# and the variable "SRCDIR" must be defined pointing back towards where
# the sources are, for example:
#
#  $ make -f Makefile.gnu SRCDIR=/build/foo/libpng-1.2.3/contrib/gregbook
#
ifdef SRCDIR
VPATH = $(SRCDIR)
else
SRCDIR = .
endif
export SRCDIR
BLDDIR = $(shell pwd)
LOCALDEP_FILE = localdeps.mk

# macros --------------------------------------------------------------------

# redefine these if desired to finally install in a different location.

PREFIX   = /usr/local
BINPATH := $(PREFIX)/bin
INSTALL  = /usr/bin/install
WHOpng =

# What kind of linking to libpng?
STATICF =
  ifneq (,$(filter static-lpng static_lpng static,$(MAKECMDGOALS)))
STATICF = --static
  endif

# This makefile assumes libpng and zlib devel files have already been
# installed (built from source or downloaded).

# Let `make' figure out PNGINC, PNGLIB, ZINC and ZLIB lines; or give
# "LIBpngD=[something]" explicitly on the `make' command line to override.

ifneq (,$(LIBpngD))
   PNGLIB = $(addprefix -L,$(LIBpngD)$(if $(wildcard $(LIBpngD)/lib/*),/lib,))
   PNGINC = $(addprefix -I,$(LIBpngD)$(if $(wildcard $(LIBpngD)/inc/*),/inc,))
 else
   WHOpng = $(shell libpng-config $(STATICF) --L_opts --I_opts)
   PNGLIB = $(filter -L%,$(WHOpng))
   PNGINC = $(filter -I%,$(WHOpng))
endif

#  ZLIB = -L../zlib -lz
# ZLIB = $(if $(PNGLIB),$(PNGLIB),-L/usr/local/lib)
# ZINC = $(if $(PNGINC),$(if $(findstring \
#  libpng,$(PNGINC),$(PNGINC)/..,$(PNGINC)),-I/usr/local/include))

_raw_Zguess = $(shell libpng-config $(STATICF) --libs)

ZLIB = $(filter -L%,$(_raw_Zguess))
ZINC = $(patsubst -L%,-I%,$(patsubst %/lib,%/include,$(ZLIB)))


# the location of X libraries, headers (if applicable)
XTOP = /usr/X11R6

# the usual culprits... common tools.
CC = gcc
LD = $(CC)
RM = rm -f

# CFLAGS:
# [-ansi, -pedantic and -W can also be used]
override CFLAGS  += -O3 -Wall

# CPP and CC opts ... derive some flags as a fallback default:
CCEXTRA  = $(filter-out -DPNG_%,$(shell libpng-config --cppflags --ccopts))
CPPFLAGS = $(CCEXTRA)


# *******************************************************
#   *** NO USER-SERVICEABLE PARTS BELOW THIS POINT ***{{{1
# *******************************************************

# Just a `make' convenience:
_comma = ,

ifneq (,$(USE_XFREE86)$(IFIS_gnulin))
   ifneq (,$(XINCLUDE))
    X_INCL = $(addprefix -I,$(XINCLUDE))
   else
    X_INCL = $(addprefix -I,$(if $(wildcard $(XTOP)/include/*.h),$(XTOP)/include,))
   endif
    X_LIBD = $(strip $(if $(X_INCL), \
    $(patsubst -I%,-L%,$(patsubst %/include,%/lib,$(X_INCL)))))
endif

## ------- CFLAGS --------
OPT_CFL = $(filter -O%,$(CFLAGS))
ifneq (,$(OPT_CFL))
  LD := $(LD) $(OPT_CFL)
endif

WPNGCFLAGS = $(sort $(filter-out %X11R6/include,$(CFLAGS) \
$(if $(IFIS_cygwin),-mconsole,) $(strip $(PNGINC) $(ZINC))))

# If building with X and MSWin, a second make pass will occur
ifneq "$(USE_XFREE86)" ""
    X_TOO = xbuild
endif

WPNG    = wpng
RPNG    = $(if $(IFIS_cygwin),rpng-win,rpng-x)
RPNG2   = $(if $(IFIS_cygwin),rpng2-win,rpng2-x)

ifneq (,$(IFIS_gnulin))
   override CFLAGS  += $(strip $(PNGINC) $(ZINC) $(X_INCL))
   RLIBS    := $(strip $(X_LIBD) -lX11)
   LDOPTS       = $(shell libpng-config $(STATICF) --ldopts)
   LINKb4LIBS   = $(shell libpng-config $(STATICF) --R_opts)
   LINK_LIBS    = $(shell libpng-config $(STATICF) --L_opts --libs)
else
   ifneq (,$(USE_MSWIN32))
   override CFLAGS += -mwin32 -mwindows $(strip $(PNGINC) $(ZINC))
   LINK_LIBS := $(strip $(PNGLIB) $(ZLIB) -lm)
   LDOPTS     = -mwin32 -mwindows
   endif
   ifeq (1,$(X_BUILD))
   RPNG    = rpng-x
   RPNG2   = rpng2-x
   override CFLAGS += $(strip $(PNGINC) $(ZINC) $(X_INCL))
   RLIBS   := $(strip $(PNGLIB) $(ZLIB) $(X_LIBD) -lX11 -lm)
   LDEXTRA  =
  # prevent make going into infinite recursion loop (later):
   X_BUILD  =
   endif
endif

# If we did not build zlib or libpng in this dir, no sense in doing
#  "-Wl,rpath,.", now is there?
ifeq (,$(filter -L$(BLDDIR)%,$(PNGLIB)))
    LINKb4LIBS := $(strip $(filter-out -Wl$(_comma)-rpath$(_comma).,$(LINKb4LIBS)))
endif

# naming of product programs
Exe = $(if $(IFIS_gnulin),,.exe)

# who what where? when? ... huh? ;-)
ROBJS  := $(RPNG).o readpng.o
ROBJ2  := $(RPNG2).o readpng2.o
WOBJS  := $(WPNG).o writepng.o
EXES   := $(WPNG)$(Exe) $(RPNG)$(Exe) $(RPNG2)$(Exe)

PROGRAMS  := $(RPNG)$(Exe) $(RPNG2)$(Exe) $(WPNG)$(Exe)
ifeq (,$(IFIS_gnulin))
   ifneq "$(USE_XFREE86)" ""
PROGRAMS += $(addsuffix $(Exe),rpng-x rpng2-x)
   endif
endif

# shape the "all" target based on system, args
   ALL_TARGET = drawl report-tell $(EXES) $(X_TOO)
ifeq "$(X_BUILD)" "1"
   ALL_TARGET = drawl report-tell $(EXES)
endif
ifeq "$(X_BUILD)" "3"
   ALL_TARGET = drawl report-tell $(EXES)
endif
# explicit exceptions -------------------------------------------------------

$(WPNG).o writepng.o : CFLAGS := $(WPNGCFLAGS)

# dependency rules    -------------------------------------------------------
# .SUFFIXES : .c .o
.PHONY    : drawl clean clean-o report-tell all keepdeps seedeps

all    : $(ALL_TARGET)

static : all

quietly: $(filter-out report-tell,$(ALL_TARGET))

xbuild :
        $(MAKE) -f $(_IN_WHERE)/GNUMakefile X_BUILD=1 \
    all MAKEFLAGS= USE_MSWIN32= USE_XFREE86=yes

_target_breakdown := which contains the sub-targets '\n  ' \
$(addprefix \",$(addsuffix \",$(ALL_TARGET)))

  ifeq "$(findstring all,$(MAKECMDGOALS))" ""
_target_breakdown =
  endif
report-tell:
        @echo -e '\nPlatform is $(RAW_un)\n' \
 We are now examining target \"$(MAKECMDGOALS)\" \
 $(_target_breakdown)'\n' with forced options: \
 $(addprefix '\n"',$(addsuffix '"',$(MAKEFLAGS)))'\n' \
 'And with "libpng-config" linker options:\n' \
 '  LDOPTS is "$(LDOPTS)"\n' \
 '  LINKb4LIBS is "$(LINKb4LIBS)"\n' \
 '  LINK_LIBS is "$(LINK_LIBS)"\n' \
 'And with cpp and cc flags:\n' \
 '"$(CPPFLAGS)"\n'

drawl :
        @echo '------------------------------------------------------------------------'

# mutable targets depending on which pass is running!

$(RPNG)$(Exe): $(ROBJS)
        $(LD) $(LDOPTS) $(LDFLAGS) -o $@ $(ROBJS) $(RLIBS) $(LINKb4LIBS) $(LINK_LIBS)
        @echo '------------------------------------------------------------------------'

$(RPNG2)$(Exe): $(ROBJ2)
        $(LD) $(LDOPTS) $(LDFLAGS) -o $@ $(ROBJ2) $(RLIBS) $(LINKb4LIBS) $(LINK_LIBS)
        @echo '------------------------------------------------------------------------'

$(WPNG)$(Exe): $(WOBJS)
        $(LD) $(LDOPTS) $(LDFLAGS) -o $@ $(WOBJS) $(LINKb4LIBS) $(LINK_LIBS)
        @echo '------------------------------------------------------------------------'

ifeq (,$(wildcard ./$(LOCALDEP_FILE)))
 #  do these fallback dep statements
$(RPNG).o:      $(RPNG).c readpng.h
$(RPNG2).o:     $(RPNG2).c readpng2.h
$(WPNG).o:      $(WPNG).c writepng.h

readpng.o:      readpng.c readpng.h
readpng2.o:     readpng2.c readpng2.h
writepng.o:     writepng.c writepng.h
else
  include $(LOCALDEP_FILE)
endif


# installation --------------------------------------------------------------

install: install-bins

   ifneq (,$(IFIS_cygwin))
install-bins: $(PROGRAMS)
        @for INSTITEM in $(PROGRAMS); do \
   $(INSTALL)  -m 0755 $$INSTITEM $(DESTDIR)$(BINPATH); \
   done && \
   echo Finished installing binaries to directory \"$(DESTDIR)$(BINPATH)\"

   else
install-bins: $(PROGRAMS) install_rpng install_rpng2 install_wpng
install_rpng:
        $(INSTALL) -m 0755 $(RPNG) $(DESTDIR)$(BINPATH)/$(patsubst %-x,%,$(RPNG))
install_rpng2:
        $(INSTALL) -m 0755 $(RPNG2) $(DESTDIR)$(BINPATH)/$(patsubst %-x,%,$(RPNG2))
install_wpng:
        $(INSTALL) -m 0755 $(WPNG) $(DESTDIR)$(BINPATH)/

   endif

# maintenance ---------------------------------------------------------------

ROBJSX = $(addsuffix .o,rpng-x rpng2-x)
clean-o:
        $(RM) $(ROBJS) $(ROBJ2) $(ROBJSX) $(WOBJS)

clean: clean-o
        $(RM) $(PROGRAMS)

sterile: clean
        $(RM) $(LOCALDEP_FILE)

define CUSTOM_MAKEDEPS_DELIMLINE
# DO NOT DELETE this LINE, delete entire file instead if desired ;-)
endef

NONcygEXCL = $(if $(IFIS_cygwin), , -\! -name '*-win.c' )

define DEPS1
makedepend -f - $$(libpng-config --cppflags --I_opts) \
 -s"$(CUSTOM_MAKEDEPS_DELIMLINE)" $$(find -type f \( -name '*.c' \
     $(NONcygEXCL) \) -a -print)  \
| perl -lane 'if($$.!=1) {' \
       -e '$$fna = substr($$F[0],2,length($$F[0])-3);' \
       -e '$$,=q[ ]; @G=grep{ ! m%^/usr/(?:lib|include)/(?!X11)% }@F[1 .. $$#F];' \
       -e 'printf("%-12s : @G\n",$$fna) if @G } else {print}'
endef

.PHONY : seedeps keepdeps
keepdeps:
        @if test ! -f $(LOCALDEP_FILE); then touch $(LOCALDEP_FILE); fi
        @$(DEPS1) >$(LOCALDEP_FILE)
$(LOCALDEP_FILE) :
        @$(DEPS1) >$@
seedeps :
        $(DEPS1)

# that's  all ---------------------------------------------------------------
# vim: ft=make foldmethod=marker
code syntax highlighting by GVIM, using the "zellner" theme.