#
#  Makefile for LiS drivers.
#
#  This one builds drivers for the Linux kernel.
#
LIS_HOME = ../../..
include $(LIS_HOME)/config.mk

-include $(LIS_HOME)/drvrconf.mk

OPT = -DLINUX -D__KERNEL__
MODOPT = -DMODULE
CC = gcc -DLiS $(OPT) $(XOPTS)


# Object names known to all targets
OBJS := loop relay clone minimux printk timod sad

# Object names only known to the Linux kernel target.
TOBJS := ip_strm_mod ldl

# Remove unknown object names
LINKS := $(filter $(OBJS),$(MODCONF_LINKS))
LOADS := $(filter $(OBJS),$(MODCONF_LOADS))
TLINKS := $(filter $(TOBJS),$(MODCONF_LINKS))
TLOADS := $(filter $(TOBJS),$(MODCONF_LOADS))

# Linked object file names
DRVRS := $(LINKS:%=%.o)
TDRVRS := $(TLINKS:%=%.o)

# Loadable object file names
MODLS := $(LOADS:%=streams-%.o)
TMODLS := $(TLOADS:%=streams-%.o)

#
# Module support
#
ifdef CONFIG_MODULES

ifdef CONFIG_MODVERSIONS
OPT += -DMODVERSIONS
endif 

MODULES := $(MODLS:%=$(LIS_HOME)/modules/%) 

else   # not CONFIG_MODULES

ifneq ($(strip $(MODLS) $(TMODLS)),)
first_rule:
	echo "Configured loadables, but no kernel module support." >&2
	/bin/false
endif  # ifdef MODLS

endif  # ifdef CONFIG_MODULES


#
# Our default target
#
default: drivers.o

ifneq ($(strip $(MODLS) $(TMODLS)),)

# A target that makes modules
modules: $(MODLS) $(TMODLS)
	ln -s $(^:%=$(DRVROBJ)/%) $(LIS_HOME)/modules

else  # No modules to be made here

modules:

endif

#
# Rules
#
clean: common-clean
	-rm -f $(OBJS:%=streams-%.o)
	-rm -f $(TOBJS:%=streams-%.o)

realclean: clean common-realclean
	-rm -f .depend.modules

# How to compile a loadable only known here ( $(TMODLS) )
streams-%.o: %.c
	$(CC) $(MODOPT) -DLIS_OBJNAME=$* -c -o $@ $^

# How to compile a loadable known to all targets ( $(MODLS) )
streams-%.o: $(DRVRDIR)/%.c
	$(CC) $(MODOPT) -DLIS_OBJNAME=$* -c -o $@ $^

#
# Common rules
#

include $(DRVRDIR)/mk.bdy

#
# Dependency stuff
#

-include .depend.modules

dep: common-dep
ifneq ($(strip $(MODLS) $(TMODLS)),)
	$(CC) $(MODOPT) -M $(MODLS:streams-%.o=$(DRVRDIR)/%.c) \
 $(TMODLS:streams-%.o=%.c) >.depend.modules
else
	>.depend.modules
endif

