#
# Copyright (c) 2003 Linuxant inc.
# Copyright (c) 2001-2003 Conexant Systems, Inc.
#
# NOTE: The use and distribution of this software is governed by the terms in
# the file LICENSE, which is included in the package. You must read this and
# agree to these terms before using or distributing this software.
# 
TOP = ..

SUBDIRS= osspec

include common.mak

ALL_MODS= hsfosspec.o hsfbasic2.o hsfich.o hsfvia.o hsfali.o hsfyukon.o hsfengine.o hsfserial.o

# Build hsfscrdmp debug module?
ifeq ($(IMPORTED_SCR_SUPPORT),yes)
ifeq ($(IMPORTED_DMP_SUPPORT),yes)
ALL_MODS+= hsfscrdmp.o
endif
endif

all: $(ALL_MODS)

#mod_basic2.o mod_engine.o mod_ich.o mod_via.o mod_ali.o mod_yukon.o mod_osspec.o mod_scrdmp.o: CFLAGS += -DEXPORT_SYMTAB
# The following is a workaround as the previous line causes certain
# versions of GNU make to crash with the message:
# "make: expand.c:489: allocated_variable_append: Assertion `current_variable_set_list->next != 0' failed."
CFLAGS-mod_osspec = -DEXPORT_SYMTAB
CFLAGS-mod_basic2 = -DEXPORT_SYMTAB
CFLAGS-mod_ich = -DEXPORT_SYMTAB
CFLAGS-mod_via = -DEXPORT_SYMTAB
CFLAGS-mod_ali = -DEXPORT_SYMTAB
CFLAGS-mod_yukon = -DEXPORT_SYMTAB
CFLAGS-mod_engine = -DEXPORT_SYMTAB
CFLAGS-mod_scrdmp = -DEXPORT_SYMTAB

# Remove the following if your OS already has the new serial core
# (expected to be merged one day in linux 2.5)
#serial_hsf.o: CFLAGS += -DCNXTSERIAL_INCLUDE_CORE
# The following is a workaround as the previous line causes certain
# versions of GNU make to crash with the message:
# "make: expand.c:489: allocated_variable_append: Assertion `current_variable_set_list->next != 0' failed."
CFLAGS-serial_hsf = -DCNXTSERIAL_INCLUDE_CORE

serial_hsf.o: serial_core.c serial_core.h

hsfserial.o: serial_hsf.o
	@echo "> linking kernel module $@"
	@$(LD) -r -o $@ $^
	@echo ""

hsfengine.o hsfbasic2.o hsfich.o hsfvia.o hsfali.o hsfyukon.o: hsf%.o: mod_%.o $(IMPORTED)/hsf%.O
	@echo "> linking kernel module $@"
	@$(LD) -r -o $@ $^
	@echo ""

osspec/libosspec.a: $(SUBDIRS)

hsfosspec.o: mod_osspec.o osspec/libosspec.a
	@echo "> linking kernel module $@"
	@$(LD) -r -o $@ $^
	@echo ""

hsfscrdmp.o:: mod_scrdmp.o $(IMPORTED)/hsfdbgscr.O
	@echo "> linking kernel module $@"
	@$(LD) -r -o $@ $^
	@echo ""

HSF_MODS_DIR= $(KMODS_DIR)/misc

.PHONY: check_kernelver modules_install minstall mi
check_kernelver:
	@if [ -z "$(KERNELVER)" ]; then \
		echo 1>&2 "Unable to determine version of kernel source directory $(KERNELSRC)"; false; \
	else \
		true; \
	fi

modules_install minstall mi: check_kernelver all $(HSF_MODS_DIR)
	rm -f "$(HSF_MODS_DIR)/hsf"*.o
	$(INSTALL) -m 644 $(ALL_MODS) $(HSF_MODS_DIR)

.PHONY: link-identical-binaries install uninstall
link-identical-binaries: # to save space
	prevsum=""; md5sum `find binaries -type f -print` </dev/null | sort | while read sum file ; do \
		if [ "$$sum" = "$$prevsum" ] && cmp -s "$$file" "$$prevfile"; then \
			rm -f "$$file"; \
			ln "$$prevfile" "$$file"; \
		else \
			prevsum="$$sum"; \
			prevfile="$$file"; \
		fi; \
	done

install: uninstall $(HSFMODDIR) link-identical-binaries
	(cd $(TOP) && find config.mak imported -depth -print | cpio -pdm $(HSFLIBDIR))
	find . \( -name COPYING -o -name '*.sh' -o -name '*.[ch]' -o -name '*.mak' -o -name '[Mm]akefile' \) -print | cpio -pdm $(HSFMODDIR)
	find binaries -depth -print | cpio -pdm $(HSFMODDIR)

$(HSF_MODS_DIR) $(HSFMODDIR):
	$(MKDIR) -p $@

uninstall:
	rm -rf "$(HSFLIBDIR)/config.mak" "$(HSFLIBDIR)/imported" "$(HSFMODDIR)"

