#~~syntax:makefile
#==============================================================================
# ttyrpld - TTY replay daemon
#   Copyright (C) Jan Engelhardt <jengelh [at] linux01 gwdg de>, 2004
#   -- License restrictions apply (Dual FAL/GPL2)
#   -- For details see doc/FAL.txt and doc/GPL2.txt.
#==============================================================================
DEBUG    := 1

### Programs ------------------------------------------------------------------
#
CC       := gcc
CFLAGS   := -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

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 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.";
	@false;
endif
endif

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

rpld: user/rpld.o user/shared.o;
	${VECHO_LD}
	${LD} ${LDFLAGS} -o $@ $^ -lHX -lpopt;
	${STRIP} $@;

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

clean: ;
	${RM} -f user/*.o rpld 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 *.d)
ifneq (${_DEP_FILES},)
-include ${_DEP_FILES}
endif

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