#----------------------------------------------------------------------
# MPEG-4 Audio VM
# makefile (requires GNU make)
#
# $Id: makefile,v 1.25 1997/11/18 20:44:46 purnhage Exp $
#
# Authors:
# BG    Bernhard Grill, Uni Erlangen <grl@titan.lte.e-technik.uni-erlangen.de>
# HP    Heiko Purnhagen, Uni Hannover <purnhage@tnt.uni-hannover.de>
# BT    Bodo Teichmann, FhG/IIS <tmn@iis.fhg.de>
# SE	Sebastien Etienne, CCETT Rennes
#
# Changes:
# 11-jun-96   BG    basic concept
# 13-jun-96   HP    first version
# 14-jun-96   HP    test stuff added
# 19-jun-96   HP    indiline.a added
# 20-jun-96   HP    modifications proposed by BG
# 28-jun-96   HP    fixed COPTS bug
# 04-jul-96   HP    restructured makefile, fixed CFLAGS/LIBS bug
# 04-jul-96   HP    included "pre-linked object file directory" by BG
# 05-jul-96   HP    fixed override CFLAGS/LIBS
# 20-aug-96   BG    OS2 modifications
# 26-aug-96   HP    removed PLOBJS, adapted to vm directory structure
# 26-aug-96   HP    CVS
# 13-sep-96   HP    removed -O option for Linux g++
# 25-oct-96   HP    adapted makefile options / added makefile.cfg
# 01-nov-96   HP    added "depend" target
# 03-dec-96   HP    added comment re. include dependency files
#                   added library dependency rules
# 21-jan-97   HP    added audio i/o module
# 31-jan-97   HP    seperated encoder and  -g directories
# 05-feb-97   HP    added COMLIBS
# 12-feb-97   HP    make CFLAGS and LDFLAGS values passed on and evaluated
# 14-feb-97   HP    included PICOLA speed control (by Panasonic)
# 11-mar-97   HP    included PICOLA source code
# 21-mar-97   BT    fixed CC and IRIX things, platform autodetect
# 26-mar-97   CCETT included G729  -g source code
# 20-may-97   HP    clean up
# 22-may-97   HP    added DEBUGPLOT (substitutes PLOTMTV)
# 03-jun-97   HP    disabled DEBUGPLOT as default
# 07-nov-97   HP    merged with FhG mods
#----------------------------------------------------------------------

#----------------------------------------------------------------------
# Notes:
#
# This makefile provides automatic handling of different system
# platforms. Therefore it is required to set the environment
# variable SYSTEM_NAME according to the actual platform.
# Currently, these platforms are supported:
#   SunOS
#   IRIX
#   Linux
#   Solaris
#
# If CFLAGS or LDFLAGS are changed for a subsequent invocation of make,
# object files and binaries are NOT remade automatically with the new
# options! Use "make clean" first ...
#
#----------------------------------------------------------------------

# HP 07-apr-97: to use the cc compiler of SGI, type "make SGICC=1"



# BG: OS2 fix
ifeq "$(SYSTEM_NAME)" "OS2"
SHELL = /bin/sh
export SHELL
endif

ifeq "$(SYSTEM_NAME)" ""
ifeq "$(OSTYPE)" "solaris"
SYSTEM_NAME = Solaris
endif
endif

ifeq "$(SYSTEM_NAME)" ""
ifeq "$(OSTYPE)" "sunos4"
SYSTEM_NAME = SunOS
endif
endif

ifeq "$(SYSTEM_NAME)" ""
ifeq "$(OSTYPE)" "iris4d"
SYSTEM_NAME = IRIX
endif
endif

ifeq "$(SYSTEM_NAME)" ""
ifeq "$(OSTYPE)" "linux"
SYSTEM_NAME = Linux
endif
endif


ifeq "$(SYSTEM_NAME)" ""
include error\ :\ SYSTEM_NAME\ environment\ variable_not\ defined\ \ \ \ \ \ \ \!!
include Options:\ SunOS\ IRIX\ Linux\ OS2\ Solaris\ \ \ \ \ \ \ \!!
endif


ifeq "$(MAKELEVEL)" "0"


# COMPILE TIME OPTIONS
# 
# Several options are available to enable/disable VM modules at
# compile time. These options can be specified in the make command line
# (e.g. make PAR=0) or in the makefile.cfg file. 

# makefile options:
# PAR=1		parametric core enabled
#    =0		parametric core disabled
# LPC=1		LPC-based core enabled
#    =0		LPC-based core disabled
# TF=1		t/f-based core enabled
#   =0		t/f-based core disabled
# G729=1	g729-based core enabled
#     =0	g729-based core disabled
# G723=1	g723-based core enabled
#     =0	g723-based core disabled
# PICOLA=1	PICOLA speed control enabled
#       =0	PICOLA speed control disabled
# DEBUGPLOT=1	DEBUGPLOT enabled (only used for debugging)
#          =0	DEBUGPLOT disabled

-include makefile.cfg

# defaults:
ifndef PAR
PAR = 1
endif
ifndef LPC
LPC = 1
endif
ifndef TF
TF = 1
endif
ifndef G729
G729 = 0
endif
ifndef G723
G723 = 0
endif
ifndef PICOLA
PICOLA = 1
endif
ifndef DEBUGPLOT
DEBUGPLOT = 0
endif


# compiler options
# NOTE: If CFLAGS or LDFLAGS are modified, object files are NOT remade
#       automatically !!!
#       Delete the object file manually to force recompile with the
#       new CFLAGS compiler options.

ifeq "$(DEBUGPLOT)" "1"
CFLAGS += -g -DDEBUGPLOT
LDFLAGS += -g
endif
# for more info on plotmtv see src_frame/plotmtv_interf.c
# see also src_frame/writevector.c

#CFLAGS += -g 
#CFLAGS += -O2
#CFLAGS += -pg

#LDFLAGS += -g 
#LDFLAGS += -g -fbounds-checking

# BG: separate sub-makefiles for the three codec families:
#       makefile.par
#       makefile.lpc
#       makefile.tf
#       makefile.g729

OBJS_ENC_PAR = enc_par_dmy.o
OBJS_ENC_LPC = enc_lpc_dmy.o
OBJS_ENC_TF  = enc_tf_dmy.o
OBJS_ENC_G729 = enc_g729_dmy.o
OBJS_ENC_G723 = enc_g723_dmy.o
OBJS_DEC_PAR = dec_par_dmy.o
OBJS_DEC_LPC = dec_lpc_dmy.o
OBJS_DEC_TF  = dec_tf_dmy.o
OBJS_DEC_G729  = dec_g729_dmy.o
OBJS_DEC_G723  = dec_g723_dmy.o

OBJS_ALL_G729 = $(OBJS_DEC_G729)  $(OBJS_ENC_G729)
OBJS_ALL_G723 = $(OBJS_DEC_G723)  $(OBJS_ENC_G723)

LIBS_ENC_PAR = 
LIBS_DEC_PAR = 
LIBS_ENC_LPC = 
LIBS_DEC_LPC = 
LIBS_ENC_TF = 
LIBS_DEC_TF = 
LIBS_ENC_G729 = 
LIBS_DEC_G729 = 
LIBS_ENC_G723 = 
LIBS_DEC_G723 = 

ifeq "$(PAR)" "1"
include makefile.par
endif
ifeq "$(LPC)" "1"
include makefile.lpc
endif
ifeq "$(TF)" "1"
include makefile.tf
endif
ifeq "$(G729)" "1"
include makefile.g729
endif
ifeq "$(G723)" "1"
include makefile.g723
endif

OBJS_DEC_PICOLA = 
LIBS_DEC_PICOLA = 
ifeq "$(PICOLA)" "1"
OBJS_DEC_PICOLA = pan_picola.o
else
OBJS_DEC_PICOLA = picola_dmy.o
endif



#############################################################################
#
# don't edit after this line !!!
#
# (unless you know what you are doing)
#


# object files

OBJS_COM = common.o cmdline.o bitstream.o audio.o fir_filt.o

ifeq "$(DEBUGPLOT)" "1"
OBJS_COM += plotmtv_interf.o writevector.o
endif
# plotmtv_interf.c is just a C interface to the freeware program plotmtv, 
# see src_frame/plotmtv_interf.c for further information
# see also src_frame/writevector.c

OBJS_ENC = mp4enc.o $(OBJS_COM) $(OBJS_ENC_PAR) $(OBJS_ENC_LPC) $(OBJS_ENC_TF)\
	 $(OBJS_ALL_G729) $(OBJS_ALL_G723)
OBJS_DEC = mp4dec.o $(OBJS_COM) $(OBJS_DEC_PAR) $(OBJS_DEC_LPC) $(OBJS_DEC_TF)\
	$(OBJS_DEC_PICOLA) $(OBJS_DEC_G729) $(OBJS_DEC_G723)

# libraries

# -lm is always included (see makelevel 1)   HP 961203
LIBS_COM = -ltsp
LIBS_ENC = $(LIBS_ENC_PAR) $(LIBS_ENC_LPC) $(LIBS_ENC_TF) $(LIB_ENC_G729) $(LIB_ENC_G723)
LIBS_DEC = $(LIBS_DEC_PAR) $(LIBS_DEC_LPC) $(LIBS_DEC_TF) $(LIBS_DEC_PICOLA)\
	$(LIB_DEC_G729)	$(LIB_DEC_G723)


# rules

default : mp4dec

all : mp4enc mp4dec

# on multiprocessor platforms, add "-j 4" after $(MAKE)   BT/HP 970520

mp4enc : 
	$(MAKE) $(MFLAGS) OBJS="$(OBJS_ENC)"\
	LIBS="$(LIBS_ENC)" COMLIBS="$(LIBS_COM)"\
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"\
	INCLDEP=1 ENCODE=1 TARGET=mp4enc binary

mp4dec : 
	$(MAKE) $(MFLAGS) OBJS="$(OBJS_DEC)"\
	LIBS="$(LIBS_DEC)" COMLIBS="$(LIBS_COM)"\
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"\
	INCLDEP=1 TARGET=mp4dec binary

depend :	
	$(MAKE) $(MFLAGS) OBJS="$(OBJS_ENC)" ENCODE=1 TARGET=mp4enc depend
	$(MAKE) $(MFLAGS) OBJS="$(OBJS_DEC)" TARGET=mp4dec depend

clean :	
	$(MAKE) $(MFLAGS) OBJS="$(OBJS_ENC)" ENCODE=1 TARGET=mp4enc clean
	$(MAKE) $(MFLAGS) OBJS="$(OBJS_DEC)" TARGET=mp4dec clean

makedirs : FORCE
	$(MAKE) $(MFLAGS) OBJS="" OBJ_DIR_SUFFIX="_enc" TARGET=mp4enc makedirs
	$(MAKE) $(MFLAGS) OBJS="" OBJ_DIR_SUFFIX="_dec" TARGET=mp4dec makedirs
FORCE:

endif    #MAKELEVEL == 0


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


ifeq "$(MAKELEVEL)" "1"

# -lm is always included   HP 961203
SYSLIBS = -lm

DEPFLAGS = -M
SED = sed 's$*.o$(OBJDIR)/$*.o $(OBJDIR)/$*.dg'

# automatic platform detection
##############################
ifeq "$(SYSTEM_NAME)" "SunOS"
#override CFLAGS += -ansi -pedantic-errors
override CFLAGS += -ansi -pedantic -Wall
PLTDIR = sunos
CC = g++
LINK = g++
else
############################
ifeq "$(SYSTEM_NAME)" "IRIX"
PLTDIR = sgi
ifneq "$(SGICC)" "1"
#gnu c++:
#override CFLAGS += -ansi -pedantic-error
override CFLAGS += -ansi -pedantic -Wall
CC = g++
LINK = g++
else
#sgi c++:
override CFLAGS += -ansi -pedantic
CC = CC
LINK = CC
SED = sed 'sa.out$(OBJDIR)/$*.o $(OBJDIR)/$*.dg'
endif
else
##############################
ifeq "$(SYSTEM_NAME)" "IRIX64"
PLTDIR = sgi64
ifneq "$(SGICC)" "1"
#gnu c++:
#override CFLAGS += -ansi -pedantic-error
override CFLAGS += -ansi -pedantic -Wall
CC = g++
LINK = g++
else
#sgi cc:
override CFLAGS += -ansi  -pedantic 
CC = cc
LINK = cc
endif
else
#############################
#KILL=
ifeq "$(SYSTEM_NAME)" "Linux"
ifneq "$(BOUNDS_CHECK)" "1"
#override CFLAGS += -ansi -pedantic-errors -DHAS_ULONG -Wall  -Wno-unused
#override CFLAGS += -ansi -pedantic -Wmissing-prototypes -DHAS_ULONG -Wall  -Wno-unused
override CFLAGS += -ansi -pedantic -Wall
#KILL=fuser -k $@
PLTDIR = linux
CC = g++
LINK = g++
else
GCC_HOME = $(HOME)/tools/gcc-2.7.2
override CFLAGS +=-B$(GCC_HOME)/ -fbounds-checking -ansi -pedantic-errors -fno-builtin -DHAS_ULONG -Wall  -Wno-unused
PLTDIR = linux_bcheck
CC = $(GCC_HOME)/xgcc
LINK = $(GCC_HOME)/xgcc -B$(GCC_HOME)/  -fbounds-checking -fno-builtin
endif
else
###########################
ifeq "$(SYSTEM_NAME)" "OS2"
#override CFLAGS += -ansi -pedantic-errors
override CFLAGS += -ansi -pedantic -Wall
PLTDIR = os2
CC = gcc
LINK = gcc -g
EXE_SUFFIX = .exe
else
#############################
ifeq "$(SYSTEM_NAME)" "Solaris"

ifeq "$(MAKE_CC)" "native"
CC = cc 
LINK = cc
SOLCC = native
PLTDIR = sol_native
override CFLAGS += -g -xs -Xc -DHAS_ULONG
override DEPFLAGS =  -xM1
override LDFLAGS +=  -g -xs -Xc -z i_full #

else
ifeq "$(MAKE_CC)" "gnu"
CC = gcc
LINK = gcc
PLTDIR = sol_gcc
#LINK = gcc -fbounds-checking
else
#override CFLAGS +=  -ansi -pedantic-errors -DHAS_ULONG -Wall  -Wno-unused
#override CFLAGS += -ansi -pedantic-errors -fbounds-checking -DHAS_ULONG -Wall
override CFLAGS += -ansi -pedantic -Wall
PLTDIR = sol
CC = g++
LINK = g++
endif
endif


else
#############################
PLTDIR = unkown
endif
endif
endif
endif
endif
endif


# set paths and directories

SRCPATH = src_frame: src_par: src_lpc: src_tf: src_g729: src_g723: src_g723_en
SRCPATHENC = src_frame_enc: src_par_enc: src_lpc_enc: src_tf_enc: src_g729_enc: src_g729_enc: src_g723_enc
BINDIR = $(PLTDIR)
OBJDIR = $(PLTDIR)/obj
LIBDIR = $(PLTDIR)/lib
LIBDIRENC = $(PLTDIR)/lib_enc

ifeq "$(ENCODE)" "1"

# enable encoder directories (make encoder)
INCL = -Isrc_frame -Iinclude -Isrc_frame_enc -Iinclude_enc\
	-Isrc_par -Isrc_lpc -Isrc_tf -Isrc_g729 -Isrc_g723\
	-Isrc_par_enc -Isrc_lpc_enc -Isrc_tf_enc -Isrc_g729_enc -Isrc_g723_enc
LIBPATH = -L$(LIBDIR) -L$(LIBDIRENC)
vpath %.c
vpath %.c $(SRCPATH): $(SRCPATHENC)

else

# disable encoder directories (make decoder -g)
INCL = -Isrc_frame -Iinclude -Isrc_par -Isrc_lpc -Isrc_tf
LIBPATH = -L$(LIBDIR)
vpath %.c
vpath %.c $(SRCPATH)

endif


# add directory and extension to filenames

LIB_SUFFIX = .a
LNK_SUFFIX = .lnk

DIROBJS = $(addprefix $(OBJDIR)/,$(OBJS))
DIRDEPS = $(DIROBJS:%.o=%.d)

ifeq "$(ENCODE)" "1"
DIRLIBS = $(addsuffix .a,$(subst -l,$(LIBDIRENC)/lib,$(LIBS)))
else
DIRLIBS = $(addsuffix .a,$(subst -l,$(LIBDIR)/lib,$(LIBS)))
endif
DIRLIBS += $(addsuffix .a,$(subst -l,$(LIBDIR)/lib,$(COMLIBS)))

ifeq "$(suffix $(TARGET))" "$(LIB_SUFFIX)"
ifeq "$(ENCODE)" "1"
DIRTARGET = $(LIBDIRENC)/$(TARGET)
else
DIRTARGET = $(LIBDIR)/$(TARGET)
endif
else
DIRTARGET = $(BINDIR)/$(TARGET)
endif


# compose list of object/library files for linker
# force linking if list of object/library files changed

LNK = $(DIROBJS) $(LIBPATH) $(LIBS) $(COMLIBS)
-include $(DIRTARGET)$(LNK_SUFFIX)

ifneq "$(strip $(OLDLNK))" "$(strip $(LNK))"
FORCE_LINKING = force
endif


# rules

force :
	@echo "*****" force linking

binary : $(DIRTARGET)$(EXE_SUFFIX)
	@echo "*****" made binary $<

library : $(DIRTARGET)
	@echo "*****" made library $<

depend : $(DIRDEPS)
	@echo "*****" made dependencies for $(DIRTARGET)

makedirs :
	@echo "making directories"
	-mkdir -p $(OBJDIR)
	-mkdir -p $(LIBDIR)

ifeq "$(suffix $(TARGET))" "$(LIB_SUFFIX)"
# compose library
$(DIRTARGET) : $(DIROBJS)
	@echo "*****" composing library $@ ...
	ar rv $(DIRTARGET) $^
	ranlib $(DIRTARGET)
else
# link
$(DIRTARGET)$(EXE_SUFFIX) : $(DIROBJS) $(DIRLIBS) $(FORCE_LINKING)
	@echo "*****" linking $@ ...
#	-$(KILL) 
	$(LINK) $(LDFLAGS) $(DIROBJS) $(LIBPATH)\
	$(LIBS) $(COMLIBS) $(SYSLIBS) -o $@
	@echo "OLDLNK = " $(LNK) > $(DIRTARGET)$(LNK_SUFFIX)
endif

# compile
$(DIROBJS) : $(OBJDIR)/%.o : %.c
	@echo "*****" compiling $< ...
	$(CC) -c $(CFLAGS) $(INCL) $< -o $@

# generate dependencies
$(DIRDEPS) : $(OBJDIR)/%.d : %.c
	@echo "*****" making dependencies for $< ...
	$(CC) $(DEPFLAGS) $(INCL) $< |	$(SED) > $@
#sed script is different for gnu g++ and sgi CC

# include dependency files *.d
# NOTE: If an included *.d file does not exist, this file becomes a target
#       that is to be made if a corresponding rule exists!
#       (here: "generate dependencies" rule)
ifeq "$(INCLDEP)" "1"
-include $(DIRDEPS)
endif

clean : 
	@echo "*****" cleaning ...
	-rm $(DIRDEPS)
	-rm $(DIROBJS)
ifeq "$(suffix $(TARGET))" "$(LIB_SUFFIX)"
	-rm $(DIRTARGET)
else
	-rm $(DIRTARGET)$(EXE_SUFFIX)
	-rm $(DIRTARGET)$(LNK_SUFFIX)
endif


endif	#(MAKELEVEL) == 1


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

# end of makefile

