#
# Makefile for Linux and GNU make
#
# libkb -- a free, advanced and portable low-level keyboard library
# Copyright (C) 1995, 1996 Markus Franz Xaver Johannes Oberhumer
# For conditions of distribution and use, see copyright notice in kb.h 
#


# This is a ready-to-run Makefile.
# do a 'make' to build the library and the test programs.

# 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
# // Tools needed: find, install, mmv, nm, perl, sed, tr, zip
# ************************************************************************/

LIBKB = libkb$A		# name of the library

FIND  := find
TOUCH := touch
REN   := mmv -rp

PWD        := $(shell pwd)
DIRNAME    := $(shell echo $(PWD) | sed -e 's|.*[/\\]||')
DISTNAME   := $(DIRNAME)
VERSION    := $(shell echo $(DISTNAME) | sed -e 's/[^0-9]//g')
VERSION_SO := $(shell echo $(DISTNAME) | sed -e 's/[^0-9.]//g')
UNAME_M    := $(shell uname -m)


# /***********************************************************************
# // Directories
# ************************************************************************/

SRCDIR = include:src:samples:util

vpath %.c     $(SRCDIR)
vpath %.cc    $(SRCDIR)
vpath %.cpp   $(SRCDIR)
vpath %.h     $(SRCDIR)
vpath %.hh    $(SRCDIR)
vpath %.pl    $(SRCDIR)



# /***********************************************************************
# // Compiler and linker flags
# ************************************************************************/

O := .o#	# object extension
A := .a#	# library extension
E := .out#	# executable extension

CC            := gcc
## CFLAGS        += -v			# be verbose
CFLAGS        += -DKB_DEBUG=1		# log some debug infos
CFLAGS        += -Iinclude
CFLAGS        += -Wall -W -pedantic	# all warnings on
CFLAGS        += -O2 			# optimize 
CFLAGS        += -fomit-frame-pointer 	# optimize (no debugging possible)
ifeq ($(UNAME_M),i486)
CFLAGS        += -m486 			# optimize for 486
endif

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

### debugging
ifeq (1,2)
CFLAGS        += -g 			# include debug information
else
LDFLAGS       += -s			# strip executable
endif

### bounds checking
ifeq (1,2)
CFLAGS        += -fbounds-checking
LDFLAGS       += -fbounds-checking
endif

### curses
ifeq (1,1)
CFLAGS_CURSES += -I/usr/include/ncurses
LDLIBS_CURSES += -lncurses		# use ncurses 1.8.5 or higher
else
LDLIBS_CURSES += -lcurses		# use curses
endif


### DLL - doesn't work, wait for an ELF version !
JUMP_DIR      := $(PWD)/jump/
JUMP_LIB      := libkb
export JUMP_DIR JUMP_LIB

ifeq (1,2)
CFLAGS_LIBKB  += -B/usr/dll/jump/
MFX_JUMP      := /home/mfx/util/tools-2.16/tools/
DLL_TARGETS   := all_dll_targets
endif


CFLAGS        := $(strip $(CFLAGS))
CFLAGS_LIBKB  := $(strip $(CFLAGS_LIBKB))
LDFLAGS       := $(strip $(LDFLAGS))
LDLIBS        := $(strip $(LDLIBS))


# /***********************************************************************
# // includes and rules
# ************************************************************************/

.PHONY: default all static_test_pgm dll_test_pgm clean

default: all

ifneq ($(strip $(wildcard Makefile.inc)),)
include Makefile.inc
else
include makefile.inc
endif


# rules for compiling the library
$(OBJS) : %$O : %.c
	$(COMPILE.c) $(CFLAGS_LIBKB) $< $(OUTPUT_OPTION)

# other rules
kbtst$O : %$O : %.c
	$(COMPILE.c) $(CFLAGS_CURSES) $< $(OUTPUT_OPTION)

_kbname.hh: mkkbname.pl kb.h
	perl -w $^ > $@

mktables$E: mktables$O
	$(CC) $(LDFLAGS) -o $@ $<

8: 8$O 
	$(CC) $(LDFLAGS) -o $@ $^ 


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

all: uppercase lsm  8  all_static_targets  $(DLL_TARGETS)

all_static_targets:  lib static_test_pgm 

all_dll_targets:     $(JUMP_DIR) jump1  lib  dll_lib dll_test_pgm


clean:
	-rm -f $(strip $(OBJS)) >& /dev/null
	-rm -f *.o *.out >& /dev/null
	-rm -f libkb*.a libkb*.sa libkb*.so.* >& /dev/null
	-rm -f libkb*.tar.gz libkb*.zip >& /dev/null
	-(cd $(JUMP_DIR) && rm -f *.s jump.log jump.params jump.undefs >& /dev/null)



static_test_pgm: lib kbtstlnx$E simple$E sigalrm$E keycodes$E tube$E

kbtstlnx$E: kbtst$O $(LIBKB)
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) $(LDLIBS_CURSES)

simple$E: simple$O $(LIBKB)
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

sigalrm$E: sigalrm$O $(LIBKB)
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

keycodes$E: keycodes$O $(LIBKB)
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

tube$E: tube$O $(LIBKB)
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) -lvgagl -lvga -lm
##	chmod a+rs,go-w $@



dll_test_pgm: dll_lib kbtstlnx_dll$E simple_dll$E sigalrm_dll$E keycodes_dll$E tube_dll$E

kbtstlnx_dll$E: kbtst$O libkb.sa
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) $(LDLIBS_CURSES)

simple_dll$E: simple$O libkb.sa
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

sigalrm_dll$E: sigalrm$O libkb.sa
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

keycodes_dll$E: keycodes$O libkb.sa
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

tube_dll$E: tube$O libkb.sa
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) -lvgagl -lvga -lm
##	chmod a+rs,go-w $@



# /***********************************************************************
# // library targets
# ************************************************************************/

.PHONY: lib 

lib: $(LIBKB)

$(LIBKB): $(OBJS)
	-rm -f $@ >& /dev/null
	$(AR) rcs $@ $^


.PHONY: dll_lib image stubs

dll_lib: jump2 image stubs

image: libkb.so.0
stubs: libkb.sa

libkb.so.0: libkb.so.$(VERSION_SO)
	 -rm -f $@
	 ln -s $< $@

libkb.so.$(VERSION_SO): $(LIBKB)
	 $(MFX_JUMP)mkimage -a 0x67b00000 -l libkb -v $(VERSION_SO) \
	 	-j 0x00001000 -g 4096 \
		-- $(MKIMAGEFLAGS) $(LIBKB) \
		-lc `$(CC) --print-libgcc-file-name` -lc; 
	 strip $@

libkb.sa: $(LIBKB)
	 $(MFX_JUMP)mkstubs -a 0x67b00000 -l libkb -v $(VERSION_SO) \
	 	-j 0x00001000 -g 4096 \
		-- libkb;

$(JUMP_DIR):
	mkdir -p $(JUMP_DIR)
	
.PHONY: jump1 jump2
jump1: $(JUMP_DIR)/jump.import
jump2: $(JUMP_DIR)/jump.funcs $(JUMP_DIR)/jump.vars

$(JUMP_DIR)/jump.import:
	nm -o -g --no-cplus /usr/lib/libc.sa | grep __GOT__ | sed -e 's/ __GOT__/ _/' > $@

$(JUMP_DIR)/jump.funcs: $(JUMP_DIR)/jump.log
	cd $(JUMP_DIR)  &&  getfuncs

$(JUMP_DIR)/jump.vars: $(JUMP_DIR)/jump.log
	cd $(JUMP_DIR)  &&  getvars  &&  getsize > jump.vars.new  &&  cp jump.vars.new jump.vars


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

.PHONY: hdr_install lib_install install

hdr_install : kb.h kbmlock.h
	install -m 444 -o root -g root $^  $(INSTALL_INCLUDEDIR)/

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

install : all hdr_install lib_install
	@echo 'libkb' library installed.


# /***********************************************************************
# // uppercase
# // if you unzip the MSDOS distribution (libkbXXX.zip),
# // make should automatically rename all files to uppercase as needed
# ************************************************************************/

.PHONY: uppercase 

uppercase: $(UPPER_MAKE) $(UPPER_FILE)

$(UPPER_MAKE):
	-$(REN) `echo $@ | sed -e 's/^M/m/'` $@

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


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

.PHONY: chmod chown_root tar zip rar distclean dist pack

chmod:
	chmod -R go-w *

chown_root:
	chown -R root:root *

tar: mkdist.pl uppercase lsm
	$(FIND) -type f -print | perl $< $(DIRNAME) | (cd .. && tar -cvf- -T-) | (cd .. && gzip -9 > $(DIRNAME)/$(DISTNAME).tar.gz)

zip: mkdist.pl uppercase lsm
	-rm $(DISTNAME).zip
	$(FIND) -type f -print | perl $< $(DIRNAME) | (cd .. && zip -9 -o -@ $(DIRNAME)/$(DISTNAME).zip)
	$(TOUCH) -c $(DISTNAME).zip

# get the RAR archiver from ftp://ftp.elf.stuba.sk/pub/pc/pack
rar: mkdist.pl uppercase lsm
	-rm $(DISTNAME).rar
	$(FIND) -type f -print | perl $< $(DIRNAME) > /tmp/libkb.tmp
	(cd .. && rar a -s -ds -m5 -tl $(DIRNAME)/$(DISTNAME).rar @/tmp/libkb.tmp)
	$(TOUCH) -c $(DISTNAME).rar


distclean: 


distclean: 

dist: grep distclean chmod all distexe touch tar

pack: chmod zip


# /***********************************************************************
# // Autoconf (preliminary)
# ************************************************************************/

.PHONY: autoscan

autoscan: 
	autoscan
