#~~syntax:makefile
#==============================================================================
# ttyrpld - TTY replay daemon
#   Copyright (C) Jan Engelhardt <jengelh [at] linux01 gwdg de>, 2004
#   -- License restrictions apply (GPL2)
#
#   This file is part of ttyrpld.
#   ttyrpld is free software; you can redistribute it and/or modify it
#   under the terms of the GNU General Public License as published by
#   the Free Software Foundation; however only version 2 of the License.
#
#   ttyrpld 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
#   General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program kit; if not, write to:
#   Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
#   02111-1307, USA.
#
#   -- For details see doc/GPL2.txt.
#==============================================================================
DEBUG    := 1

### Programs ------------------------------------------------------------------
#
CC       := gcc
CFLAGS   := -D_REENTRANT -Iinclude -Wall -Waggregate-return \
            -Wmissing-declarations -Wmissing-prototypes -Wredundant-decls \
            -Wshadow -Wstrict-prototypes -Winline -pipe
LD       := gcc # ld
LDFLAGS  := -L. -Wl,-rpath,.

LN       := ln
RM       := rm

ifneq (${MARCH},)
  CFLAGS += -march=${MARCH}
endif
ifneq (${MCPU},)
  CFLAGS += -mcpu=${MCPU}
endif

ifeq (${DEBUG},1)
  CFLAGS += -ggdb3
  STRIP  := @true
else
  CFLAGS += -O2 -finline-functions -fomit-frame-pointer
  STRIP  := strip -s
endif
ifeq (${PROF},1)
  CFLAGS += -pg -fprofile-arcs
  STRIP  := @true
endif

CFLAGS  += ${EXT_CFLAGS}
LDFLAGS += ${EXT_LDFLAGS}

ifeq (${VERBOSE},)
  CC    := @${CC}
  LD    := @${LD}
  LN    := @${LN}
  RM    := @${RM}
  STRIP := @${STRIP}

  VECHO_CC  = @echo "[CC] " $@
  VECHO_LD  = @echo "[LD] " $@
  VECHO_DEP = @echo "[DEP]" $@
endif

#------------------------------------------------------------------------------
.PHONY: all kmod clean cleanx

all: rpld rplctl ttyreplay;

KERNEL_VER := $(shell uname -r | cut -d. -f1-2)
kmod:
	@echo "Compiling Kernel module...";
ifeq (${KERNEL_VER},2.6)
	make -C kernel-2.6;
else
ifeq (${KERNEL_VER},2.4)
	make -C kernel-2.4;
else
	@echo "*** Kernels other than 2.6 and 2.4 are not supported.";
	@echo "*** If I could not detect your kernel version,";
	@echo "*** please manually cd into kernel-x.y and run `make`.";
	@false;
endif
endif

ttyreplay: user/replay.o user/global.o;
	${VECHO_LD}
	${LD} ${LDFLAGS} -o $@ $^ -lHX -lpopt;
	${STRIP} $@;

rpld: user/rpld.o user/infod.o user/rplctl.o user/rdsh.o user/global.o;
	${VECHO_LD}
	${LD} ${LDFLAGS} -o $@ $^ -lHX -lpopt -lpthread;
	${STRIP} $@;

rplctl: rpld;
	${LN} -fs $< $@;

user/%.o: user/%.c;
	${VECHO_CC}
	${CC} -D_REENTRANT ${CFLAGS} -c -o $@ $<;

clean: ;
	${RM} -f user/*.o rpld rplctl ttyreplay;

cleanx: clean;
	${RM} -f user/*.d;

#--( C/H Dependencies )--------------------------------------------------------
Makefile: $(patsubst %.c,%.d,$(wildcard user/*.c))

user/%.d: user/%.c;
	${VECHO_DEP}
	${CC} ${CFLAGS} -MM -MF $@ -MT $(@:.d=.o) $<;

_DEP_FILES := $(wildcard user/*.d)
ifneq (${_DEP_FILES},)
-include ${_DEP_FILES}
endif

#==[ End of file ]=============================================================
