# pkg-Makefile -- build tools for Click
# Eddie Kohler
#
# Copyright (c) 2006 Regents of the University of California
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, subject to the conditions
# listed in the Click LICENSE file. These conditions include: you must
# preserve this copyright notice, and you cannot mention the copyright
# holders in advertising related to the Software without their permission.
# The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
# notice is a summary of the Click LICENSE file; the license in that file is
# legally binding.

VPATH ?= .:$(top_srcdir)/$(subdir):$(top_srcdir)

include $(clickdatadir)/pkg-config.mk

ifeq ($(CLICK_PACKAGE_MAKING),userlevel)
include $(clickdatadir)/pkg-userlevel.mk
endif

ifeq ($(CLICK_PACKAGE_MAKING),linuxmodule)
include $(clickdatadir)/pkg-linuxmodule.mk
endif

ifeq ($(CLICK_PACKAGE_MAKING),bsdmodule)
include $(clickdatadir)/pkg-bsdmodule.mk
endif

ifeq ($(CLICK_PACKAGE_MAKING),)

INSTALL ?= $(CLICKINSTALL)
INSTALL_DATA ?= $(CLICKINSTALL) -m 644
mkinstalldirs ?= $(clickdatadir)/mkinstalldirs

CLICK_BUILDTOOL = $(clickbindir)/click-buildtool
CLICK_ELEM2PACKAGE = $(CLICK_BUILDTOOL) elem2package $(ELEM2PACKAGE_INCLUDES)

# set TARGETS, etc. based on MAKE_UPACKAGE, MAKE_KPACKAGE, and MAKE_BPACKAGE
ifeq ($(MAKE_UPACKAGE),1)
TARGETS += $(package).uo
DRIVERS += userlevel
endif
ifeq ($(MAKE_KPACKAGE),1)
TARGETS += $(package).ko
DRIVERS += linuxmodule
endif
ifeq ($(MAKE_BPACKAGE),1)
TARGETS += $(package).bo
DRIVERS += bsdmodule
endif

all: $(TARGETS)

$(package).uo: Makefile always
	$(MAKE) CLICK_PACKAGE_MAKING=userlevel $(package).uo

$(package).ko: Makefile always
	$(MAKE) CLICK_PACKAGE_MAKING=linuxmodule $(package).ko

ifneq ($(wildcard $(srcdir)/Makefile.in),)
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
	cd $(top_builddir) \
	  && CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status
endif

ifneq ($(wildcard $(top_srcdir)/configure.ac),)
$(top_builddir)/config.status: $(top_srcdir)/configure
	cd $(top_builddir) && ./config.status --recheck
$(top_srcdir)/configure: $(top_srcdir)/configure.ac
	cd $(top_srcdir) && autoconf
endif

elemlist:
	for i in $(DRIVERS); do $(MAKE) CLICK_PACKAGE_MAKING=$$i elemlist; done

elementmap-$(package).xml: $(clickbindir)/click-mkelemmap always
	r="$(DRIVERS) $(package)"; echo $(srcdir) | $(CLICK_BUILDTOOL) findelem -r "$$r" -P | $(clickbindir)/click-mkelemmap -r "$(package)" -t "$(DRIVERS)" -s `cd $(srcdir) && pwd` > elementmap-$(package).xml

install: install-obj install-man elementmap-$(package).xml always
	$(mkinstalldirs) $(DESTDIR)$(clickdatadir)
	$(INSTALL_DATA) elementmap-$(package).xml $(DESTDIR)$(clickdatadir)/elementmap-$(package).xml
install-obj: $(TARGETS) always
	$(mkinstalldirs) $(DESTDIR)$(libdir)
	for i in $(TARGETS); do $(INSTALL_DATA) $$i $(DESTDIR)$(libdir)/$$i; done
install-man: always elementmap-$(package).xml
	$(mkinstalldirs) $(DESTDIR)$(mandir)
	$(mkinstalldirs) $(DESTDIR)$(mandir)/mann
	$(clickbindir)/click-elem2man -L -d $(DESTDIR)$(mandir)/mann -P $(package) -e $(clickdatadir)/elementmap.xml elementmap-$(package).xml

uninstall: uninstall-obj uninstall-man always
	/bin/rm -f $(DESTDIR)$(clickdatadir)/elementmap-$(package).xml
uninstall-obj: always
	for i in $(TARGETS); do /bin/rm -f $(DESTDIR)$(libdir)/$$i; done
uninstall-man: always elementmap-$(package).xml
	$(clickbindir)/click-elem2man -L -d $(DESTDIR)$(mandir)/mann -P $(package) -e $(clickdatadir)/elementmap.xml -u elementmap-$(package).xml

clean: always
	@for i in $(DRIVERS); do make CLICK_PACKAGE_MAKING=$$i clean; done
	-rm -f elementmap-$(package).xml conftest.*
distclean: clean
	-rm -f Makefile config.h config.status config.cache config.log

always:
	@:

.PHONY: all always clean distclean elemlist \
	install install-obj install-man uninstall uninstall-obj uninstall-man

endif
