#############################################################################
#	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		#
#	This software is released under the GNU General Public License, version	#
#	3, with the exemptions that 1) the header files installed by this 	 	#
#	package can be included in closed-source code and 2) the libraries		#
#	built by this package can be statically linked into a closed-source 	#
#	binary. See the file LICENSE for the details.							#
#	This software is provided "AS IS", without any warranty or guarantee.	#
#	By using this software you acknowledge that neither Bendor Research nor	#
#	any of its personnel or affiliates can be held liable for any damages,	#
#	including, but not limited to, loss of business, loss of money, loss of #
#	life, loss of reputation, arising out of this software being faulty or	#
#	not performing as advertised, even if Bendor Research was aware of such	#
#	faults in the software. You use this software at your own risk, any and #
#	all liabilities that arise out of your using this software are strictly	#
#	yours.																	#
#############################################################################

#	Test whether we have TCL installed or not
#	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

CMD	 = TEMP=`mktemp -u` ;
CMD	+= echo '\#include <tcl.h>' > $$TEMP.c ;
CMD += echo 'int foo( Tcl_Interp *tcl )' >> $$TEMP.c ;
CMD += echo '{ (void) tcl; return 1; }' >> $$TEMP.c ;
CMD += if gcc -o /dev/null -c $$TEMP.c 2> /dev/null ;
CMD += then echo 1 ; fi ; rm -f $$TEMP.c
TCL	:= $(shell $(CMD))

#	List of shared libraries we build
#	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#	For every library XXX here you must have a directory named XXXlib
#	that contains the library sources. A make will be started in that
#	library with the target build_libs and a predefined variable SONAME
#	which will contain libXXX.so. You have to build $(BINDIR)/$(SONAME)
#	in that makefile. Note that the build order of the libraries is important.

LIBS		= lpc

#	Subdirs for sources
#	~~~~~~~~~~~~~~~~~~~
#	Not for libraries, only for actual programs

VPATH		=

#	Define the targets we will build
#	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#	For each target XXX here you must define an XXX_EXEC and XXX_FILE
#	below, which corresponds the name of the executable and the list of
#	the source files, respectively. If you define XXX_LIBS as well, those
#	libraries will be used when linking the final executive.

TARGETS		= CLK RSA

#	Define the scripts that simply get copied
#	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#	If there's an extension on the script, it will be removed. Also,
#	empty lines and comments are removed. The first line of the script
#	will be copied verbatim even if that is a commant.

SCRIPTS		=

#	Define the target components
#	~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#	XXX_EXEC is the name of the final executable(s) for XXX.
#	XXX_FILE is the name of the source file(s) for XXX
#	XXX_LIBS is the list of libraries the tool depends on

CLK_EXEC	= lpc-clk
CLK_FILE	= lpc-clk.c
CLK_LIBS	=

RSA_EXEC	= lpc-rsa
RSA_FILE	= lpc-rsa.c
RSA_LIBS	=

#	Additional targets if Tcl is available
#	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ifneq "$(TCL)" ""
LIBS	+= lpctcl
SCRIPTS	+= lpc-prg.tcl
else
WARNING  = warning
endif

#	Derived variables
#	~~~~~~~~~~~~~~~~~

EXECS	= $(foreach t,\
		  $(patsubst %,%_EXEC,$(TARGETS)),$(RESDIR)/$(value $(t)))

CFLAGS	= -g -O2 -std=gnu99 -pipe -MMD -idirafter $(PWD)/include \
		  -W -Werror -Wall -Wundef -Wshadow -Wmissing-declarations \
		  -Wpointer-arith -Wwrite-strings -Wmissing-field-initializers

LFLAGS	= -g -Werror -Wl,--fatal-warnings -L $(RESDIR)

HERE	= $(patsubst $(PWD)/%,%,$(shell pwd))

BINDIR	= $(OBJDIR)/tool
RESDIR	= $(OBJDIR)/bin

export BINDIR RESDIR

#	Targets and compilation rules
#	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

all: $(WARNING) build_libs build_scripts $(EXECS)
	@true

build_libs:	$(patsubst %,$(RESDIR)/lib%.so,$(LIBS))
	@true

build_scripts: $(patsubst %,$(RESDIR)/%,$(basename $(SCRIPTS)))
	@true

warning:
	@echo "###########################################################"
	@echo "Can't find a TCL installation."
	@echo "The TCL library and the programming tool will not be built."
	@echo "###########################################################"

.PHONY:	all build_libs build_scripts $(patsubst %,$(RESDIR)/lib%.so,$(LIBS))

#	Calculated rules
#	~~~~~~~~~~~~~~~~

define script
$(2):	$(1)
	@echo "Processing  " $$<
	@head -1 $$< > $$@
	@cat $$< | grep -v "^[[:space:]]*$$$$" | grep -v "^[[:space:]]*\#" >> $$@
	@chmod 755 $$@
endef

define library
$(RESDIR)/lib$(1).so:
	@make -C $(1)lib SONAME=lib$(1).so build_libs
endef

define linking
$(RESDIR)/$($(1)_EXEC):	$(patsubst %.c,$(BINDIR)/%.o,$($(1)_FILE))\
							$(patsubst %,$(RESDIR)/lib%.so,$($(1)_LIBS));
	@echo "Linking     " $$(patsubst $$(RESDIR)/%,%,$$@)
	@cc $(LFLAGS) -o $$@ $$^ $(patsubst %,-l%,$($(1)_LIBS)) -lm \
		2> $$(PWD)/$$(patsubst $$(RESDIR)/%,%,$$@).err
	@rm -f $$(PWD)/$$(patsubst $$(RESDIR)/%,%,$$@).err
endef

$(foreach t,$(TARGETS),$(eval $(call linking,$(t))))
$(foreach t,$(LIBS),$(eval $(call library,$(t))))
$(foreach t,$(SCRIPTS),$(eval $(call script,$(t),$(RESDIR)/$(basename $(t)))))

#	Stem rules
#	~~~~~~~~~~

$(BINDIR)/%.o: %.c
	@echo "Compiling   " $(HERE)/$<
	@cc $(CFLAGS) -c -MF $(patsubst %.o,%.d,$@) -o $@ $< 2> $(PWD)/$*.err
	@rm -f $(PWD)/$*.err

#	Get the dependencies
#	~~~~~~~~~~~~~~~~~~~~

-include $(BINDIR)/*.d

