#
# 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= $(HCFTARGET)osspec.o $(HCFTARGET)engine.o $(HCFTARGET)serial.o

all: $(ALL_MODS)

#mod_engine.o mod_osspec.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_engine = -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_$(HCFTARGET).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_hcf = -DCNXTSERIAL_INCLUDE_CORE

serial_hcf.o: serial_core.c serial_core.h

$(HCFTARGET)serial.o: serial_hcf.o
	@echo "> linking kernel module $@"
	@$(LD) -r -o $@ $^
	@echo ""

$(HCFTARGET)engine.o: $(HCFTARGET)%.o: mod_%.o $(IMPORTED)/hcf%-$(IMPORTED_ARCH).O
	@echo "> linking kernel module $@"
	@$(LD) -r -o $@ $^
	@echo ""

osspec/libosspec.a: $(SUBDIRS)

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

HCF_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 $(HCF_MODS_DIR)
	rm -f "$(HCF_MODS_DIR)/$(HCFTARGET)"*.o
	$(INSTALL) -m 644 $(ALL_MODS) $(HCF_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 $(HCFMODDIR) link-identical-binaries
	(cd $(TOP) && find config.mak imported -depth -print | cpio -pdm $(HCFLIBDIR))
	find . \( -name COPYING -o -name '*.sh' -o -name '*.[ch]' -o -name '*.mak' -o -name '[Mm]akefile' \) -print | cpio -pdm $(HCFMODDIR)
	find binaries -depth -print | cpio -pdm $(HCFMODDIR)

$(HCF_MODS_DIR) $(HCFMODDIR):
	$(MKDIR) -p $@

uninstall:
	rm -rf "$(HCFLIBDIR)/config.mak" "$(HCFLIBDIR)/imported" "$(HCFMODDIR)"
