#
#  Makefile -- GNU makefile for the LZO library 
#
#  This file is part of the LZO real-time data compression library.
#
#  Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
#
#  The LZO library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Library General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  The LZO library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#  Library General Public License for more details.
#
#  You should have received a copy of the GNU Library General Public
#  License along with the LZO library; see the file COPYING.LIB.
#  If not, write to the Free Software Foundation, Inc.,
#  675 Mass Ave, Cambridge, MA 02139, USA.
#
#  Markus F.X.J. Oberhumer
#  markus.oberhumer@jk.uni-linz.ac.at
#


# This is a ready-to-run Makefile.
# do a 'make' to build the library and the test programs.
# if autodetection fails, do a 'make target=linux' or 'make target=djgpp2'

# do a 'make install' as root.
# you might want to change these, but probably not.
INSTALL_INCLUDEDIR := /usr/local/include
INSTALL_LIBDIR := /usr/local/lib


# /***********************************************************************
# // Configuration (simple dumb autoconf)
# ************************************************************************/

LIBLZO = liblzo$A	# name of the library

# LZO_DEBUG = 1		# turn assertions on, include debug info
# LZO_CXX = 1		# compile as C++ library (interface still has C linkage)


SRCDIR := src


ifeq ($(target),)
ifeq ($(OSTYPE),Linux)
target=linux
endif
endif

ifeq ($(target),)
ifneq ($(COMSPEC),)		# if MSDOS
target=watcomc
target=borlandc_16
target=djgpp2
endif
endif

# else assume generic gcc
ifeq ($(target),)
target=gcc
endif


###
### Linux + gcc
###

ifeq ($(target),linux)
CC=gcc
# Do NOT use '-O3' on an Intel 80x86 !
CFLAGS = -O2 -fforce-addr -Wall -W -Iinclude
CFLAGS += -pedantic
UNAME_M := $(shell uname -m)
ifeq ($(UNAME_M),i486)
CFLAGS += -m486
endif
endif


###
### djgpp v2 + gcc 2.7.2 (MSDOS, 32 bit)
###

ifeq ($(target),djgpp2)
# Do NOT use '-O3' on an Intel 80x86 !
CC=gcc
CFLAGS  = -O2 -fforce-addr -Wall -W -pedantic -Iinclude
CFLAGS += -m486 -malign-jumps=0 -malign-loops=2 -malign-functions=2 
ifeq ($(strip $(LZO_CXX)),)
CFLAGS += -Wp,-lang-c89
endif
ifeq (1,2)
CFLAGS += -fbounds-checking
LDLIBS += -fbounds-checking
endif
##CFLAGS += -E -Wp,-dM
##CFLAGS_LZO += -DLZO_COLLECT_STATS

ifeq (1,2)
CFLAGS += -DMFX
LZO_TEST_OBJ += lzv$(O) lzrw1o$(O)
LDLIBS += -llzrw -lz
endif
endif


###
### Watcom C/32 10.5 (MSDOS, 32 bit)
###

ifeq ($(target),watcomc)
A:=.lib
O:=.obj
LIBLZO = lzo$A

ifeq ($(model),)
model = -zq -mf -5r
endif
CC=wcl386 $(model)
CFLAGS = -oneatx -fp3 -wx -Iinclude
ifneq ($(strip $(LZO_CXX)),)
CFLAGS_LZO += -cc++		# treat files as C++ source
endif
ifneq ($(LZO_DEBUG),)
CFLAGS += -d1
else
endif
AR_LIB = wlib -b
OUTPUT_OPTION = -fo=$@
LINK.o_OUTPUT_OPTION  = -fe=$@

ifeq (1,2)
CFLAGS += -DMFX
LZO_TEST_OBJ += lzv$(O) lzrw1o$(O)
LDLIBS += /"l lzrw"
LDLIBS += /"l z"
LINK.o_OUTPUT_OPTION += -l=pmodew	# link for PMODE/W
endif
endif


###
### Borland C 4.0 (MSDOS, 16 bit)
###

ifeq ($(target),borlandc_16)
A:=.lib
O:=.obj
LIBLZO = lzo$A

ifeq ($(model),)
model = -ml
endif
CC=bcc $(model)
CFLAGS = -O2 -w -w-rch -Iinclude -4
ifneq ($(strip $(LZO_CXX)),)
CFLAGS_LZO += -P		# treat files as C++ source
endif
OUTPUT_OPTION = -o$@
LINK.o_OUTPUT_OPTION = -e$@
endif


###
### generic gcc
###

ifeq ($(target),gcc)
CC=gcc
CFLAGS = -O2 -Wall -Iinclude
endif


###
### gcc common stuff
###

ifeq ($(CC),gcc)
A:=.a
O:=.o

AR_LIB = $(AR) rcs
LINK.o_OUTPUT_OPTION = -o $@

ifneq ($(LZO_DEBUG),)
CFLAGS += -g
else
CFLAGS += -fomit-frame-pointer
LDFLAGS += -s
endif

ifneq ($(strip $(LZO_CXX)),)
CFLAGS_LZO += -x c++		# treat files as C++ source
else
CFLAGS_LZO += -Wno-unused	# don't warn about unused parameters
endif

### even more warnings for all library source files
CFLAGS_LZO += -Wno-uninitialized
CFLAGS_LZO += -Wstrict-prototypes
CFLAGS_LZO += -Wmissing-prototypes -Wmissing-declarations
ifeq (1,2)
CFLAGS_LZO += -Wpointer-arith -Wwrite-strings -Wshadow -Wcast-align
CFLAGS_LZO += -Wnested-externs
##CFLAGS_LZO += -Wredundant-decls
##CFLAGS_LZO += -Werror
endif
endif


###
### other flags
###

ifneq ($(LZO_DEBUG),)
CFLAGS += -DLZO_DEBUG
endif

ifeq ($(COMSPEC),)				# if not MSDOS
E:=
CFLAGS_LZO   += -I$(SRCDIR)
CFLAGS_LZO1B += -I$(SRCDIR)/lzo1b
CFLAGS_LZO1C += -I$(SRCDIR)/lzo1c
CFLAGS_LZV   += -Icontrib/lzv
CFLAGS_LZRW1O+= -Icontrib/lzrw1o
else
E:=.exe
CFLAGS_LZO   += -I$(SRCDIR)
CFLAGS_LZO1B += -I$(SRCDIR)\\lzo1b
CFLAGS_LZO1C += -I$(SRCDIR)\\lzo1c
CFLAGS_LZV   += -Icontrib\\lzv
CFLAGS_LZRW1O+= -Icontrib\\lzrw1o
ifndef LZO_<
LZO_<=$(subst /,\\,$<)
endif
endif

ifndef LZO_<
LZO_<=$<
endif


# /***********************************************************************
# // main targets
# ************************************************************************/

ifeq ($(strip $(COMSPEC)),)
VPATH = .:$(SRCDIR):extra
else
VPATH = .;$(SRCDIR);extra
endif

.PHONY: default all test run_test

default all: uppercase lib lzo_test$(E) simple$(E) tlzo1c$(E)

test: default run_test

run_test: lzo_test$(E)
	lzo_test$(E) -m1  -n500 lzo_test.c
	lzo_test$(E) -m9  -n500 lzo_test.c
	lzo_test$(E) -m11 -n500 lzo_test.c
	lzo_test$(E) -m19 -n500 lzo_test.c


# /***********************************************************************
# //
# ************************************************************************/

lzo_test$(E) : %$(E) : %$(O) $(LZO_TEST_OBJ) $(LIBLZO)
	$(strip $(LINK.o) $(LINK.o_OUTPUT_OPTION) $^ $(LOADLIBES) $(LDLIBS))

tlzo1c$(E) : %$(E) : %$(O) lzo1c_c$(O) lzo1c_d$(O) lzo_util$(O)
	$(strip $(LINK.o) $(LINK.o_OUTPUT_OPTION) $^ $(LOADLIBES) $(LDLIBS))

simple$(E) : %$(E) : %$(O) $(LIBLZO)
	$(strip $(LINK.o) $(LINK.o_OUTPUT_OPTION) $^ $(LOADLIBES) $(LDLIBS))


lzo_test$(O) simple$(O) : %$(O) : %.c
	$(strip $(COMPILE.c) $(OUTPUT_OPTION) $(LZO_<))

tlzo1c$(O) : %$(O) : lzo_test.c
	$(strip $(COMPILE.c) -DONLY_LZO1C_1 $(OUTPUT_OPTION) $(LZO_<))

lzo1c_c$(O) lzo1c_d$(O) : %$(O) : %.c
	$(strip $(COMPILE.c) $(CFLAGS_LZO) $(OUTPUT_OPTION) $(LZO_<))

lzv$(O) : %$(O) : contrib/lzv/%.c
	$(strip $(COMPILE.c) $(CFLAGS_LZV) $(OUTPUT_OPTION) $(LZO_<))

lzrw1o$(O) : %$(O) : contrib/lzrw1o/%.c
	$(strip $(COMPILE.c) $(CFLAGS_LZO) $(CFLAGS_LZRW1O) $(OUTPUT_OPTION) $(LZO_<))


# /***********************************************************************
# // library files and targets
# ************************************************************************/

LZO1_OBJS := lzo1$(O) lzo1_99$(O)

LZO1A_OBJS := lzo1a$(O) lzo1a_99$(O)

LZO1B_ALG_OBJS := \
	lzo1b_d1$(O) lzo1b_d2$(O) lzo1b_xx$(O) lzo1b_cc$(O) lzo1b_rr$(O) 
LZO1B_COMPR_OBJS := \
	lzo1b_1$(O) lzo1b_2$(O) lzo1b_3$(O) lzo1b_4$(O) lzo1b_5$(O) \
	lzo1b_6$(O) lzo1b_7$(O) lzo1b_8$(O) lzo1b_9$(O) \
	lzo1b_99$(O)
LZO1B_OBJS := $(LZO1B_ALG_OBJS) $(LZO1B_COMPR_OBJS)

LZO1C_ALG_OBJS := \
	lzo1c_d1$(O) lzo1c_d2$(O) lzo1c_xx$(O) lzo1c_cc$(O) lzo1c_rr$(O) \
	lzo1c_9x$(O)
LZO1C_COMPR_OBJS := \
	lzo1c_1$(O) lzo1c_2$(O) lzo1c_3$(O) lzo1c_4$(O) lzo1c_5$(O) \
	lzo1c_6$(O) lzo1c_7$(O) lzo1c_8$(O) lzo1c_9$(O) \
	lzo1c_99$(O) 
LZO1C_OBJS := $(LZO1C_ALG_OBJS) $(LZO1C_COMPR_OBJS)

LZO1D_OBJS := lzo1d_d1$(O) lzo1d_d2$(O) lzo1d_9x$(O)

## LZO1E_OBJS := lzo1e_d$(O) lzo1e_c$(O)

## LZO1F_OBJS := lzo1f_d$(O) lzo1f_c$(O) lzo1f_9x$(O)
LZO1F_OBJS := lzo1f_d1$(O) lzo1f_d2$(O) lzo1f_c$(O) lzo1f_9x$(O)

## LZO1G_OBJS := lzo1g_d$(O) lzo1g_c$(O)


LIB_OBJ := lzo_util$(O) $(LZO1_OBJS) $(LZO1A_OBJS) $(LZO1B_OBJS) \
	$(LZO1C_OBJS) $(LZO1D_OBJS) $(LZO1E_OBJS) $(LZO1F_OBJS) \
	$(LZO1G_OBJS)


.PHONY: lib lib_obj nm

lib: $(LIBLZO)

nm: $(LIBLZO)
	nm --extern-only $<

lib_obj: $(LIB_OBJ)

$(LIBLZO) : $(LIB_OBJ)
	-$(RM) $@
	$(AR_LIB) $@ $(LIB_OBJ)



# /***********************************************************************
# // compile rules
# ************************************************************************/

lzo_util$(O) : %$(O) : %.c
	$(strip $(COMPILE.c) $(CFLAGS_LZO) $(OUTPUT_OPTION) $(LZO_<))


$(LZO1_OBJS) : %$(O) : %.c
	$(strip $(COMPILE.c) $(CFLAGS_LZO) $(CFLAGS_LZO1) $(OUTPUT_OPTION) $(LZO_<))


$(LZO1A_OBJS) : %$(O) : %.c
	$(strip $(COMPILE.c) $(CFLAGS_LZO) $(CFLAGS_LZO1A) $(OUTPUT_OPTION) $(LZO_<))


$(LZO1B_ALG_OBJS) : %$(O) : %.c
	$(strip $(COMPILE.c) $(CFLAGS_LZO) $(CFLAGS_LZO1B) $(OUTPUT_OPTION) $(LZO_<))

$(LZO1B_COMPR_OBJS) : %$(O) : $(SRCDIR)/lzo1b/%.c
	$(strip $(COMPILE.c) $(CFLAGS_LZO) $(CFLAGS_LZO1B) $(OUTPUT_OPTION) $(LZO_<))


$(LZO1C_ALG_OBJS) : %$(O) : %.c
	$(strip $(COMPILE.c) $(CFLAGS_LZO) $(CFLAGS_LZO1C) $(OUTPUT_OPTION) $(LZO_<))

$(LZO1C_COMPR_OBJS) : %$(O) : $(SRCDIR)/lzo1c/%.c
	$(strip $(COMPILE.c) $(CFLAGS_LZO) $(CFLAGS_LZO1C) $(OUTPUT_OPTION) $(LZO_<))


$(LZO1D_OBJS) : %$(O) : %.c
	$(strip $(COMPILE.c) $(CFLAGS_LZO) $(CFLAGS_LZO1D) $(OUTPUT_OPTION) $(LZO_<))


$(LZO1E_OBJS) : %$(O) : %.c
	$(strip $(COMPILE.c) $(CFLAGS_LZO) $(CFLAGS_LZO1E) $(OUTPUT_OPTION) $(LZO_<))


$(LZO1F_OBJS) : %$(O) : %.c
	$(strip $(COMPILE.c) $(CFLAGS_LZO) $(CFLAGS_LZO1F) $(OUTPUT_OPTION) $(LZO_<))


$(LZO1G_OBJS) : %$(O) : %.c
	$(strip $(COMPILE.c) $(CFLAGS_LZO) $(CFLAGS_LZO1G) $(OUTPUT_OPTION) $(LZO_<))


# /***********************************************************************
# // installation
# ************************************************************************/

.PHONY: hdr_install lib_install install

hdr_install : include/lzoconf.h include/lzo1.h include/lzo1a.h include/lzo1b.h include/lzo1c.h
	install -m 444 -o root -g root $^  $(INSTALL_INCLUDEDIR)/

lib_install : $(LIBLZO)
	install -m 444 -o root -g root $^  $(INSTALL_LIBDIR)/

install : hdr_install lib_install
	@echo 'LZO' library installed.


# /***********************************************************************
# // distribution
# ************************************************************************/

.PHONY: clean realclean distclean dist pack zip tar uue rar rar2

VERSION:=0.15
LZO_TGZ:=lzo-$(VERSION).tar.gz

clean realclean distclean:
	-$(RM) *.o *.obj tlzo1c$(E) simple$(E) lzo_test$(E) $(LIBLZO)

pack zip: distclean
	zip -r9oD lzo.zip .

dist tar: distclean
	chmod +x util/*.pl
	(cd .. && find lzo-$(VERSION) -type f) | sort | (cd .. && tar -cvf- -T-) | (cd .. && gzip -9 > $(LZO_TGZ))

uue: tar
	uuencode ../$(LZO_TGZ) $(LZO_TGZ) > ../lzo.uue
	
rar:
	rar a -r -s -m5 -tl lzo.rar @etc/dist.lst
	
rar2:
	rar2 a -r -s -m5 -tl -rr lzo.rar @etc/dist.lst
	
mem:
	mem


# /***********************************************************************
# // uppercase
# // if you unzip the MSDOS distribution, then make should
# // automatically rename all files to uppercase as needed
# ************************************************************************/

REN:=mv -i

UPPER_FILES := CHANGE.LOG COPYING.LIB LZO.lsm Makefile README.1ST

.PHONY: uppercase grep

uppercase: $(UPPER_FILES)

$(UPPER_FILES):
	$(REN) `echo $@ | tr A-Z a-z` $@

grep:
	-mfxgrep -lcB -e '\r' -r
	-mfxgrep -lc  -e '\t' $(UPPER_FILES)
	-mfxgrep -lc  -e '\t' include/*.h


# /***********************************************************************
# // additional dependencies
# ************************************************************************/

lzo1_99$(O)         : $(SRCDIR)/config1.h
lzo1a_99$(O)        : $(SRCDIR)/config1a.h

$(LZO1B_OBJS)       : $(SRCDIR)/config1b.h
$(LZO1B_COMPR_OBJS) : $(SRCDIR)/lzo1b/compr.h $(SRCDIR)/lzo1b_c.ch
lzo1b_d1$(O)        : $(SRCDIR)/lzo1b_d.ch
lzo1b_d2$(O)        : $(SRCDIR)/lzo1b_d.ch
lzo1b_rr$(O)        : $(SRCDIR)/lzo1b_r.ch

$(LZO1C_OBJS)       : $(SRCDIR)/config1c.h
$(LZO1C_COMPR_OBJS) : $(SRCDIR)/lzo1c/compr.h $(SRCDIR)/lzo1b_c.ch
lzo1c_d1$(O)        : $(SRCDIR)/lzo1b_d.ch
lzo1c_d2$(O)        : $(SRCDIR)/lzo1b_d.ch
lzo1c_rr$(O)        : $(SRCDIR)/lzo1b_r.ch
lzo1c_9x$(O)        : $(SRCDIR)/lzo_mchw.ch $(SRCDIR)/lzo_swd.ch

$(LZO1D_OBJS)       : $(SRCDIR)/config1d.h
lzo1d_d1$(O)        : $(SRCDIR)/lzo1d_d.ch
lzo1d_d2$(O)        : $(SRCDIR)/lzo1d_d.ch
lzo1d_9x$(O)        : $(SRCDIR)/lzo_mchw.ch $(SRCDIR)/lzo_swd.ch

$(LZO1F_OBJS)       : $(SRCDIR)/config1f.h
lzo1f_d1$(O)        : $(SRCDIR)/lzo1f_d.ch
lzo1f_d2$(O)        : $(SRCDIR)/lzo1f_d.ch
lzo1f_9x$(O)        : $(SRCDIR)/lzo_mchw.ch $(SRCDIR)/lzo_swd.ch


# /***********************************************************************
# // special stuff
# ************************************************************************/

mkrsp$(E): mkfiles/mkrsp.c
	gcc -Wall -W -O2 -s $< -o $@

mkfiles/bcc16.rsp: mkrsp$(E)
	$< -o $@ -bcc $(LIB_OBJ:.o=.obj)

mkfiles/wcc.rsp: mkrsp$(E)
	$< -o $@ -wcc $(LIB_OBJ:.o=.obj)
	
# lib_bcc16: mkfiles/bcc16.rsp $(LIB_OBJ)
# 	-$(RM) $(LIBLZO)
# 	tlib $(LIBLZO) /C @$(subst /,\\,$<)

