# @configure_input@
#
# Makefile for iptables MPEG2 TS kernel module
#          and iptables userspace shared library.
#
# Author: Jesper Dangaard Brouer <netoptimizer@brouer.com>
#

# Configure substitution variables
# ================================
package     = @PACKAGE_NAME@
version     = @PACKAGE_VERSION@
# (Note the version comes from configure.ac AC_INIT)
prefix      = @prefix@
exec_prefix = @exec_prefix@
libexecdir  = @libexecdir@
#
# Detecting of xtlibdir has been moved to configure script
xtlibdir = @xtlibdir@
#
# Kernel builddir detected (via /lib/modules/$(shell uname -r)/build"])
#  or set by configure [--with-kbuild=PATH]
kbuilddir   = @kbuilddir@
#
# iptables variables detected by configure
iptables_bin = @IPTABLES@
iptables_dir = @IPTABLES_DIR@
# (Include path to locate xtables.h)
iptables_inc = @IPTABLES_INC@


# Targets
# -------
LIB_TARGETS := libxt_mpeg2ts.so
obj-m += xt_mpeg2ts.o
#obj-m += seq_example.o

# Jan Engelhardt's compat_xtables module stolen from xtables-addon project
#  git://xtables-addons.git.sf.net/gitroot/xtables-addons/xtables-addons/
obj-m += compat_xtables.o

# Need a directory with the kernel source/makefiles
# - Use kbuilddir if its defined, else detect it our self
ifdef kbuilddir
KERNEL_DIR ?= $(kbuilddir)
else
KERNEL_DIR ?= /lib/modules/$(shell uname -r)/build
endif

# Environment variables with ?= can be overwritten/redefined
#  when calling make
LIB_CFLAGS ?=-O2 -ggdb3 -Wall -fPIC

# Trick I found in kernel/Documentation/kbuild/modules.txt:
#
# Use make variable KBUILD_EXTRA_SYMBOLS in the Makefile
#   If it is impractical to copy Module.symvers from another
#   module, you can assign a space separated list of files to
#   KBUILD_EXTRA_SYMBOLS in your Makfile. These files will be
#   loaded by modpost during the initialisation of its symbol
#   tables.
#

# Its possible to compile the module against a 64-bit kernel,
# on a 32-bit system, by define ARCH=x86_64
#
# You can simply call:
#   make ARCH=x86_64
#
# ARCH ?= x86_64
# export ARCH

# Trick to modify kernels CFLAGS
#KBUILD_CFLAGS+=-g -O0
#KBUILD_CFLAGS+=-g -O3
KBUILD_CFLAGS+=-g -O2 -fno-inline

PWD = $(shell pwd)
CP = cp -v

# Detect iptables version
iptables_version = $(shell ($(iptables_bin) -V | awk {'print $$2'} | cut -c 2-))
IPT_FLAGS = -DIPTABLES_VERSION=\"$(iptables_version)\"

# TODO: What about the -DXTABLES_LIBDIR=\"$(xtlibdir)\"

# Configure does a version check, that tell/warn people if the iptables
# version is below 1.4.3 (as we depend >= 1.4.3)

all: lib modules

#testxtlibdir:
#	echo xtlibdir: $(xtlibdir)
#	echo $(PATH)

modules:
	@echo "\n  -=-=-=-=- Kernel modules -=-=-=-=-"
	$(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules

modules_install: modules
	@echo "\n  -=-=-=-=- INSTALL Kernel modules -=-=-=-=-"
	$(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules_install

sparse:
	@echo "\n  -=-=-=-=- SPARSE CHECK: Kernel modules -=-=-=-=-"
	$(MAKE) -C $(KERNEL_DIR) M=$(PWD) C=1 modules

# Install target for userspace lib
lib_install: lib
	@echo "\n  -=-=-=-=- INSTALL Userspace lib -=-=-=-=-"
	@if test -d $(xtlibdir); then \
	    $(CP) $(LIB_TARGETS) $(xtlibdir); \
	else \
	    echo "***ERROR*** - cannot find libexec (xtlibdir) install dir"; \
	    exit 2; \
	fi

install: lib_install modules_install


clean:
#	$(MAKE) -C $(KERNEL_DIR) M=$(PWD) clean
	rm -rf .tmp_versions
	rm -f *.o *~ *.so *.ko
	rm -f *.mod.c Module.symvers modules.order
	rm -f .*.cmd
	rm -f .xt_mpeg2ts.o.d


lib:	$(LIB_TARGETS)

lib%.so: lib%.o
	gcc -shared -o $@ $^;

lib%.o: lib%.c %.h
	@echo "\n  -=-=-=-=- Userspace lib -=-=-=-=-"
	gcc $(IPT_FLAGS) ${LIB_CFLAGS} -I$(iptables_inc) -D_INIT=lib$*_init -c -o $@ $<;


# Old approach
lib_manual:	libxt_mpeg2ts.c xt_mpeg2ts.h
	gcc $(IPT_FLAGS) libxt_mpeg2ts.c -D_INIT=libxt_mpeg2ts_init \
		-fPIC -I$(iptables_inc) -c -o libxt_mpeg2ts.o
	gcc -shared -o libxt_mpeg2ts.so libxt_mpeg2ts.o


# Configure/autotools trick to detect if Makefile.in is changed and
#  a rebuild in needed.
#
Makefile: Makefile.in ../config.status
	cd .. && ./config.status iptables-module/$@

../config.status: ../configure
	cd .. && ./config.status --recheck
