#/*====================================================================*
# -  Copyright (C) 2001 Leptonica.  All rights reserved.
# -
# -  Redistribution and use in source and binary forms, with or without
# -  modification, are permitted provided that the following conditions
# -  are met:
# -  1. Redistributions of source code must retain the above copyright
# -     notice, this list of conditions and the following disclaimer.
# -  2. Redistributions in binary form must reproduce the above
# -     copyright notice, this list of conditions and the following
# -     disclaimer in the documentation and/or other materials
# -     provided with the distribution.
# -
# -  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# -  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# -  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# -  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ANY
# -  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# -  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# -  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# -  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# -  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# -  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# -  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# *====================================================================*/

#   makefile  (for linux)
#
#   Hand-built -- editable -- simple -- makefile
#
#   For a nodebug version:             make
#   For a debug version:               make DEBUG=yes debug
#   For a shared library version:      make SHARED=yes shared
#   For all versions:                  make all
#   With nonstandard header directories
#                    make EXTRAINCLUDES="-I<nonstandard-incl-dir>"
#
#   To remove all writes to stderr:   add -DNO_CONSOLE_IO to compiler line
#
#   To remove object files in src: make clean
#   To remove object files and executables in prog: make clean
#
#   Customization for endianness of machine hardware:
#   When using the gnu compiler, endianness is automatically
#   determined and set properly.  Otherwise, set the $CPPFLAGS variable:
#       On little-endian machines (e.g., i386, x86-64):
#          CPPFLAGS = $(INCLUDES) -DL_LITTLE_ENDIAN
#       On big-endian machines (e.g., Mac Power PC, Sun Sparc):
#          CPPFLAGS = $(INCLUDES) -DL_BIG_ENDIAN
#
#   Customization for I/O with external libraries (jpeg, png, tiff, webp, gif):
#   Set flags in environ.h.  The default is to have libjpeg, libpng,
#   libtiff and libz, but not libgif.
#
#   Customization for non-POSIX-compliant GNU functions
#   fmemopen() and open_memstream().
#   The default is not to use, because they only work on linux.
#   To use these, #define HAVE_FMEMOPEN to 1 in environ.h.
#
#   Customization for Cygwin:
#   (1) Use the appropriate $CC
#
#   Compiling under Microsoft Visual Studio
#   (1) Download the vs2000 package.
#   (2) You can also substitute arrayaccess.h.vc for arrayaccess.h, to
#       use the inline macros rather than function calls which are slower.
#
#   To generate function prototypes, you need a program called
#       xtractprotos.  Build it with this command:
#          make xtractprotos
#       Then use it with 'make allheaders'

# Tools used by the Makefile
RM		= rm -f
TEST		= test
MKDIR		= mkdir -p
LIBRARIAN	= ar cq
RANLIB		= ranlib

# Libraries are built into a binary tree determined by the environmental
# variable  BINARY_BASE_DIR
ifndef BINARY_BASE_DIR
BINARY_BASE_DIR = ..
endif

BASE_OBJ =	$(BINARY_BASE_DIR)/obj
OBJ_NODEBUG =	$(BINARY_BASE_DIR)/obj/nodebug
OBJ_DEBUG =	$(BINARY_BASE_DIR)/obj/debug
OBJ_SHARED =	$(BINARY_BASE_DIR)/obj/shared

BASE_LIB =	$(BINARY_BASE_DIR)/lib
LIB_NODEBUG =	$(BINARY_BASE_DIR)/lib/nodebug
LIB_DEBUG =	$(BINARY_BASE_DIR)/lib/debug
LIB_SHARED =	$(BINARY_BASE_DIR)/lib/shared


#   Include files
INCLUDES =	-I./ $(EXTRAINCLUDES)
PROTOTYPE_DIR =	.

#   Which flags to use?
#     - gcc 4.3.3 enforces checking return values of built-in C functions
#       such as fgets().  This should compile with the -Werror flag,
#       but it is turned off in the distribution (just in case...).
#     - use g++ to apply stricter rules.  Libraries made with g++ may not
#       link to programs compiled with gcc (depends on the glibc version).
#     - use -Wunused to identify unused varables
#     - use -DNO_CONSOLE_IO to remove all L_INFO, L_WARNING, L_ERROR and
#        ERROR_* logging, and to remove all DEBUG information dependent
#        on whether or not NO_CONSOLE_IO has been defined.
#     - remove -fPIC for Cygwin
CC =		gcc -ansi -D_BSD_SOURCE -DANSI -fPIC
#CC =		gcc -ansi -D_BSD_SOURCE -DANSI -Werror -fPIC
#CC =		g++ -D_BSD_SOURCE -fPIC
#CC =		g++ -Werror -D_BSD_SOURCE -fPIC
#CC =		g++ -Wunused -D_BSD_SOURCE -fPIC
#CC =		gcc -ansi -DNO_CONSOLE_IO -D_BSD_SOURCE -DANSI -fPIC
#CC =		gcc -ansi -D_BSD_SOURCE -DANSI

#   Test for processor endianness (valid with gnu make only)
ENDIANNESS := $(shell $(CC) -o endiantest endiantest.c; ./endiantest; rm -f endiantest)

# Conditional compilation (depending on processor endianness)
CPPFLAGS =      $(INCLUDES) -D$(ENDIANNESS)
#CPPFLAGS =	$(INCLUDES) -DL_LITTLE_ENDIAN
#CPPFLAGS =	$(INCLUDES) -DL_BIG_ENDIAN

#   Shared library linker options
SONAME_OPTION = -Wl,-h,

ifdef  SHARED
    OPTIMIZE =		-O2 -fPIC
else
    ifdef  DEBUG
	OPTIMIZE =	-g
    else
	OPTIMIZE =	-O2
    endif
endif


OPTIONS =
CFLAGS =		$(OPTIMIZE) $(OPTIONS)
LIBRARIAN_SHARED =	gcc -shared

#  Libraries differing only in their minor revision numbers
#  are required to have the same interface.  By using
#  "-h" in the ld, the "soname" is <libname>.X, where X is
#  the major revision number.
#  Links are created among the files <libname>.X.Y,
#  <libname>.X, and <libname>, where Y is the minor revision number.
MAJOR_REV = 1
MINOR_REV = 73

#########################################################

# Libraries

LEPTLIB =		liblept.a
LEPTLIB_SHARED =	liblept.so

#########################################################

LEPTLIB_C =	adaptmap.c affine.c \
		affinecompose.c arrayaccess.c \
		bardecode.c baseline.c bbuffer.c \
		bilateral.c bilinear.c binarize.c \
		binexpand.c binreduce.c \
		blend.c bmf.c bmpio.c bmpiostub.c \
		bootnumgen1.c bootnumgen2.c \
		boxbasic.c boxfunc1.c boxfunc2.c \
		boxfunc3.c boxfunc4.c \
		bytearray.c ccbord.c ccthin.c classapp.c \
		colorcontent.c coloring.c \
		colormap.c colormorph.c \
		colorquant1.c colorquant2.c \
		colorseg.c colorspace.c \
		compare.c conncomp.c convertfiles.c \
		convolve.c correlscore.c \
		dewarp1.c dewarp2.c dewarp3.c dewarp4.c dnabasic.c \
		dwacomb.2.c dwacomblow.2.c \
		edge.c encoding.c enhance.c \
		fhmtauto.c fhmtgen.1.c fhmtgenlow.1.c \
		finditalic.c flipdetect.c fliphmtgen.c \
		fmorphauto.c fmorphgen.1.c fmorphgenlow.1.c \
		fpix1.c fpix2.c \
		gifio.c gifiostub.c gplot.c graphics.c \
		graymorph.c grayquant.c grayquantlow.c \
		heap.c jbclass.c \
		jp2kheader.c jp2kheaderstub.c jp2kio.c jp2kiostub.c \
		jpegio.c jpegiostub.c kernel.c \
		libversions.c list.c map.c maze.c \
		morph.c morphapp.c morphdwa.c morphseq.c \
		numabasic.c numafunc1.c numafunc2.c \
		pageseg.c paintcmap.c \
		parseprotos.c partition.c \
		pdfio1.c pdfio1stub.c pdfio2.c pdfio2stub.c \
		pix1.c pix2.c pix3.c pix4.c pix5.c \
		pixabasic.c pixacc.c \
		pixafunc1.c pixafunc2.c \
		pixalloc.c pixarith.c \
		pixcomp.c pixconv.c pixlabel.c pixtiling.c \
		pngio.c pngiostub.c \
		pnmio.c pnmiostub.c \
		projective.c \
		psio1.c psio1stub.c psio2.c psio2stub.c \
		ptabasic.c ptafunc1.c \
		ptra.c quadtree.c queue.c rank.c rbtree.c \
		readbarcode.c readfile.c \
		recogbasic.c recogdid.c recogident.c recogtrain.c \
		regutils.c \
		rop.c ropiplow.c roplow.c \
		rotate.c rotateam.c rotateamlow.c \
		rotateorth.c rotateshear.c \
		runlength.c sarray.c \
		scale.c scalelow.c \
		seedfill.c seedfilllow.c \
		sel1.c sel2.c selgen.c \
		shear.c skew.c spixio.c \
		stack.c stringcode.c sudoku.c \
		textops.c tiffio.c tiffiostub.c \
		utils.c viewfiles.c \
		warper.c watershed.c \
		webpio.c webpiostub.c writefile.c \
		zlibmem.c zlibmemstub.c

LEPTLIB_H =	allheaders.h alltypes.h \
		array.h arrayaccess.h bbuffer.h \
		bmf.h bmfdata.h bmp.h ccbord.h \
		dewarp.h environ.h gplot.h \
		heap.h imageio.h \
		jbclass.h list.h morph.h \
		pix.h ptra.h queue.h rbtree.h \
		readbarcode.h recog.h regutils.h \
		stack.h stringcode.h sudoku.h watershed.h

##################################################################

#  Main targets

nodebug: dirs $(LEPTLIB:%=$(LIB_NODEBUG)/%)

all:
	make -f makefile TARGET=$(TARGET) nodebug
	make -f makefile TARGET=$(TARGET) DEBUG=true debug
	make -f makefile TARGET=$(TARGET) SHARED=true shared

DEBUG_LIBS = $(LEPTLIB:%=$(LIB_DEBUG)/%)
SHARED_LIBS = $(LEPTLIB_SHARED:%=$(LIB_SHARED)/%)
debug:	dirs $(DEBUG_LIBS)
shared:	dirs $(SHARED_LIBS)

##################################################################

#  Proto targets

#  Note that both of the targets below can be generated by xtractprotos
#  (a) without requiring the existence of leptprotos.h and (b) with
#  an empty allheaders.h.  Both generate a new allheaders.h, and
#  'make allprotos' additionally generates leptprotos.h
#
#  In the past we generated leptprotos.h that held the function prototypes,
#  and included it in allheaders.h.  We now insert the function prototypes
#  directly in allheaders.h, and do not generate a separate prototype
#  file leptprotos.h.
allheaders:     $(LEPTLIB_C)
	@$(TEST) -f xtractprotos || echo "First run 'make xtractprotos'"
	./xtractprotos -protos=inline -prestring=LEPT_DLL $(LEPTLIB_C)


#  You can still generate the file leptprotos.h and have it #included
#  in allheaders.h.  If you do this, be sure to add leptprotos.h to LEPTLIB_H.
allprotos:      leptprotos
leptprotos:     $(LEPTLIB_C)
	@$(TEST) -f xtractprotos || echo "First run 'make xtractprotos'"
	./xtractprotos -protos=leptprotos.h -prestring=LEPT_DLL $(LEPTLIB_C)

##################################################################

#  xtractprotos

xtractprotos:	dirs leptlib
	cd ../prog; make xtractprotos; cp xtractprotos ../src

xtractprotos.o:	xtractprotos.c

##################################################################

#   Rule to make optimized library

$(LIB_NODEBUG)/%.a:
		$(RM) $@
		$(LIBRARIAN) $@ $<
		$(RANLIB) $@

#   Rule to make debuggable library

$(LIB_DEBUG)/%.a:
		$(RM) $@
		$(LIBRARIAN) $@ $<
		$(RANLIB) $@

#   Rule to make shared library

$(LIB_SHARED)/%.so:
		$(RM) $@
		$(LIBRARIAN_SHARED) $(SONAME_OPTION)$(notdir $@).$(MAJOR_REV) -o $@ $<
	mv $@ $@.$(MAJOR_REV).$(MINOR_REV)
	cd $(LIB_SHARED); rm $(notdir $@).$(MAJOR_REV); \
	  ln -s $(notdir $@).$(MAJOR_REV).$(MINOR_REV) $(notdir $@).$(MAJOR_REV)
	cd $(LIB_SHARED); rm $(notdir $@); \
	  ln -s $(notdir $@).$(MAJOR_REV) $(notdir $@)

##################################################################

#   No-debug library dependencies and rules

leptlib:	$(LIB_NODEBUG)/$(LEPTLIB)
$(LIB_NODEBUG)/$(LEPTLIB):	$(LEPTLIB_C:%.c=$(OBJ_NODEBUG)/%.o)
		$(RM) $@
		$(LIBRARIAN) $@ $(LEPTLIB_C:%.c=$(OBJ_NODEBUG)/%.o)
		$(RANLIB) $@

#   Debug library dependencies and rules

leptlibd:	$(LIB_DEBUG)/$(LEPTLIB)
$(LIB_DEBUG)/$(LEPTLIB):	$(LEPTLIB_C:%.c=$(OBJ_DEBUG)/%.o)
		$(RM) $@
		$(LIBRARIAN) $@ $(LEPTLIB_C:%.c=$(OBJ_DEBUG)/%.o)
		$(RANLIB) $@

#   Shared library dependencies, rules and links

leptlibs:	$(LIB_SHARED)/$(LEPTLIB_SHARED)
$(LIB_SHARED)/$(LEPTLIB_SHARED):	$(LEPTLIB_C:%.c=$(OBJ_SHARED)/%.o)
		$(RM) $@
		$(LIBRARIAN_SHARED) $(SONAME_OPTION)$(notdir $@).$(MAJOR_REV) -o $@ $(LEPTLIB_C:%.c=$(OBJ_SHARED)/%.o)
	mv $@ $@.$(MAJOR_REV).$(MINOR_REV)
	cd $(LIB_SHARED); rm $(notdir $@).$(MAJOR_REV); \
	  ln -s $(notdir $@).$(MAJOR_REV).$(MINOR_REV) $(notdir $@).$(MAJOR_REV)
	cd $(LIB_SHARED); rm $(notdir $@); \
	  ln -s $(notdir $@).$(MAJOR_REV) $(notdir $@)

#########################################################

#   Rules for compiling source

$(OBJ_NODEBUG)/%.o:	%.c  $(LEPTLIB_H)
		@$(TEST) -d $(OBJ_NODEBUG) || $(MKDIR) $(OBJ_NODEBUG)
		$(COMPILE.c) -o $@ $<

$(OBJ_DEBUG)/%.o:	%.c  $(LEPTLIB_H)
		@$(TEST) -d $(OBJ_DEBUG) || $(MKDIR) $(OBJ_DEBUG)
		$(COMPILE.c) -o $@ $<

$(OBJ_SHARED)/%.o:	%.c  $(LEPTLIB_H)
		@$(TEST) -d $(OBJ_SHARED) || $(MKDIR) $(OBJ_SHARED)
		$(COMPILE.c) -o $@ $<

###########################################################

#   Prepare a local environment

dirs:
		@$(TEST) -d $(BASE_OBJ) || $(MKDIR) $(BASE_OBJ)
		@$(TEST) -d $(OBJ_NODEBUG) || $(MKDIR) $(OBJ_NODEBUG)
		@$(TEST) -d $(OBJ_DEBUG) || $(MKDIR) $(OBJ_DEBUG)
		@$(TEST) -d $(OBJ_SHARED) || $(MKDIR) $(OBJ_SHARED)
		@$(TEST) -d $(BASE_LIB) || $(MKDIR) $(BASE_LIB)
		@$(TEST) -d $(LIB_NODEBUG) || $(MKDIR) $(LIB_NODEBUG)
		@$(TEST) -d $(LIB_DEBUG) || $(MKDIR) $(LIB_DEBUG)
		@$(TEST) -d $(LIB_SHARED) || $(MKDIR) $(LIB_SHARED)


###########################################################

clean:		
		$(RM) $(OBJ_NODEBUG)/*.o $(OBJ_DEBUG)/*.o \
			$(OBJ_SHARED)/*.o \
			$(LIB_NODEBUG)/*.a $(LIB_DEBUG)/*.a \
			$(LIB_SHARED)/*.so $(LIB_SHARED)/*.so.? \
			$(LIB_SHARED)/*.so.?.* \
			xtractprotos.o xtractprotos

###########################################################

depend:
	/usr/bin/makedepend -DNO_PROTOS $(CPPFLAGS) $(LEPTLIB_C)

###########################################################
# DO NOT DELETE THIS LINE -- make depend depends on it.




