Makefile_ident="@(#) LiS Makefile 2.8 6/21/99"
#
# Makefile for the kernel STREAMS device drivers.
#
# Note 1: Dependencies are done automagically by 'make dep', which also
# removes any old dependencies.
#
# This Makefile is intended to be run from the command line.  It can
# also be run from other makefiles.
#

LIS_HOME = $(shell pwd)
include config.mk

#
#  Our default target
#
default: all


#########################################################################
##                                                                     ##
##   Configuration                                                     ##
##                                                                     ##
#########################################################################

#
# A rule to run the configuration script
#
configure:
	./Configure "invoked from main makefile" config.in
	$(MAKE) clean
	$(MAKE) dep

#
#  If config.in does not exists, here is a rule to create it.
#
#  GNU make will use this rule if present, as we include the target of it.
#  This is why we have to do the "ifneq" stuff.
#
ifneq (config.in,$(wildcard config.in))

$(LIS_HOME)/config.in:
	./Configure "invoked from main makefile" config.in
	$(MAKE) clean
	$(MAKE) config

endif

ifeq ($(LIS_TARG),user)

STRCONF_XOPTS = -rmake_nodes -Muser_mknod

# The file Config.master is just the contents of the file Config.user
Config.master: Config.user
	cat $^ >$@

else

STRCONF_XOPTS = 

#
# The file Config.master consists of the concatenation of the Config
# file from the directory LiS/drivers/str and all of the individual
# Configs from binary packages in subdirectories of pkg.
# TODO: The pkg directory should have a "make dep" that creates a
# file pkg/Config.pkg.
#
PKG_CONFIG_FILES := $(shell find pkg -mindepth 2 -maxdepth 2 -type f -name Config)

Config.master: $(DRVRDIR)/Config $(PKG_CONFIG_FILES)
	cat $^ >$@

endif

$(UTILOBJ)/strconf:
	$(MAKE) -C $(UTILOBJ) strconf

# These are the files generated by the strconf utility
SC_CONFIG   := $(LIS_INCL)/sys/LiS/config.h
SC_MODCONF  := $(LIS_HOME)/head/modconf.inc
SC_DRVRCONF := $(LIS_HOME)/drvrconf.mk
SC_CONFMOD  := $(LIS_HOME)/conf.modules
ifeq ($(LIS_TARG),user)
SC_MKNODES  := $(LIBOBJ)/makenodes.c
else
SC_MKNODES  := $(UTILOBJ)/makenodes.c
endif
SC_GEN := $(SC_CONFIG) $(SC_MODCONF) $(SC_MKNODES) $(SC_DRVRCONF) $(SC_CONFMOD)

$(SC_GEN): Config.master $(UTILOBJ)/strconf
	$(UTILOBJ)/strconf			\
		-h$(SC_CONFIG)			\
		-o$(SC_MODCONF)			\
		-m$(SC_MKNODES)			\
		-l$(SC_DRVRCONF)		\
		-L$(SC_CONFMOD)			\
		$(STRCONF_XOPTS)		\
		$<
	$(MAKE) -C $(DRVROBJ) clean
	$(MAKE) -C $(DRVROBJ) dep
	$(MAKE) -C $(UTILOBJ) dep
	$(MAKE) -C $(LIBOBJ) dep

CONFIG_TARGETS = $(LIS_HOME)/config.in $(SC_GEN)

#
# This is our configuration proper.
#
# Use the target below for dependencies on the whole configuration.
#
config: $(CONFIG_TARGETS)

#########################################################################
##   End of: Configuration                                             ##
#########################################################################

TODO = config

#
# In the linux kernel we may need some syscall hooks
#
ifeq ($(LIS_TARG),linux)
TODO += khooks
endif

ifneq ($(LIS_TARG),user)
TODO += libc util
endif

#
# Does user want a loadable module or direct linkage into the kernel?
# If module then make sure the direct link patch is not in the kernel.
# If direct linkage then make sure the patch is present.
#
ifeq ($(LIS_TARG),linux)
ifeq ($(CONFIG_STREAMS),m)
TODO += rm_kpatch
else
TODO += ins_kpatch
endif
endif

#
# Build modules only when running in the Linux kernel
#
ifeq ($(LIS_TARG),linux)
TODO += modules
endif

#
# Does the user want us to install streams.o in the official
# modules directory?
#
ifeq ($(MOD_INSTALL),y)
TODO += modules_install
endif

#
# Does the user want us to make the kernel after we make STREAMS?
#
ifeq ($(CONFIG_MK_KERNEL),y)
TODO += kernel
endif

ifeq ($(LIS_TARG),user)
TODO += streams.o libc util
endif

all: $(TODO)

FORCE:

#
# You may have to be root to do these targets
#

install: all
	$(MAKE) -C $(LIBOBJ) $@
	$(MAKE) -C $(UTILOBJ) $@
	#strmakenodes

modules_install: streams.o modules
ifeq ($(CONFIG_STREAMS),m)
	cp modules/* $(MOD_INST_DIR)
	#./update_conf.modules ./conf.modules
	#/sbin/depmod -a
endif

#
# We don't have anything that will remove the /dev nodes (TBD)
#
uninstall:
	$(MAKE) -C $(LIBOBJ) $@
	$(MAKE) -C $(UTILOBJ) $@
ifeq ($(CONFIG_STREAMS),m)
	- ./update_conf.modules
	- (cd $(MOD_INST_DIR); rm -f streams.o streams-*.o)
endif


clean:
	-rm -f streams.o patches_ins.klnk patches_rm.klnk
	-rm -f modules/*
	$(MAKE) -C $(LIBOBJ) $@
	$(MAKE) -C $(HEADOBJ) $@
	$(MAKE) -C $(DRVROBJ) $@
	$(MAKE) -C $(UTILOBJ) $@
	$(MAKE) -C ./pkg $@


realclean: clean
	$(MAKE) -C $(LIBDIR)/linux $@
	$(MAKE) -C $(HEADDIR)/linux $@
	$(MAKE) -C $(DRVRDIR)/linux $@
	$(MAKE) -C $(UTILDIR)/linux $@
	$(MAKE) -C $(LIBDIR)/user $@
	$(MAKE) -C $(HEADDIR)/user $@
	$(MAKE) -C $(DRVRDIR)/user $@
	$(MAKE) -C $(UTILDIR)/user $@
	-rm -f pkg/proto/Space.o
	-rm -f $(SC_GEN)
	-rm -f Config.master
	-rm -f config.in
	-rm -f conf.modules.save

ifeq ($(LIS_TARG),linux)

# Also remove patches to the Linux kernel
realclean: rm_khooks rm_kpatch

endif

very-clean: clean realclean uninstall


dep:    
	$(MAKE) -C $(LIBOBJ)  dep
	$(MAKE) -C $(HEADOBJ) dep
	$(MAKE) -C $(DRVROBJ) dep
	$(MAKE) -C $(UTILOBJ) dep

libc: FORCE
	$(MAKE) -C $(LIBOBJ)

util: libc FORCE
	$(MAKE) -C $(UTILOBJ)


#
# The following four rules takes care of building streams.o
#
streams.o: $(DRVROBJ)/drivers.o $(HEADOBJ)/streamshead.o pkg/drivers.o
	ld -r -o $@ $^

#	ld -r -u sys_putpmsg -o $@ $^

$(HEADOBJ)/streamshead.o: config FORCE
	$(MAKE) -C $(HEADOBJ)

$(DRVROBJ)/drivers.o: config FORCE
	make -C $(DRVROBJ)

pkg/drivers.o: config FORCE
	make -C pkg


#
# Building STREAMS as a loadable module
#
ifeq ($(CONFIG_STREAMS),m)

modules: streams.o FORCE
	-rm -f modules/*
	ln -s $(LIS_HOME)/streams.o modules/streams.o
	$(MAKE) -C $(DRVROBJ) $@

endif

ifeq ($(CONFIG_STREAMS),k)

modules: streams.o FORCE
	-rm -f modules/*
	$(MAKE) -C $(DRVROBJ) $@

endif

#
# Some Linux versions need kernel patches.
# The following rules will take care of applying and removing these patches.
#
ifeq ($(LIS_TARG),linux)

khooks:
	./ins_hooks.sh $(KSRC)

rm_khooks:
	./rm_hooks.sh $(KSRC)

rm_kpatch:
	./rm_kpatch.sh

ins_kpatch:
	./ins_kpatch.sh

endif

#
# User wants us to make the kernel whenever we do a make of STREAMS
#
kernel:	streams.o
	$(MAKE) $(KERN_TARGET) -C $(KSRC)

all_but_kernel:			# Placeholder target from kernel build

#
# Make a user-space version of STREAMS for laboratory testing
#
user:
	$(MAKE) -C $(DRVROBJ)
	$(MAKE) -C $(HEADOBJ)
	$(MAKE) -C $(LIBOBJ)
	$(MAKE) -C $(UTILOBJ)

tags:	files
	ctags `cat files`

