#
# Makefile
#
# Build the Ianjtag tools.
#
# Needs GNU make
#

##########################################################################
# Tools, directories and other parameters.
#

#
# Adjust these if you wish to compile with support for the Corelis
# PCI1149 board, in which case you should build with "make
# all_pci1149" instead of "make all".
#

# Location of the "pci1149.h" header file.
PCI1149_INCDIRS = -I/usr/local/pci1149/include
# Location of the "libpci1149.a" library, if linking under linux, 
# or the "pci_32.lib" import-library if linking with cygwin.
PCI1149_LIBDIRS = -L/usr/local/pci1149/lib
# Name of library to link against.
PCI1149_LIBS = -lpci1149

#
# Toolchain
#

# Compile C program
CC         = gcc -c
# Flags to the C compiler (not to the preprocessor nor to the linker. 
# Just to the compiler)
CFLAGS     = -O2 -g
# Flags to the C preprocessor (defines, includes, etc.)
CPPFLAGS   = 
# Link object files to create executable (normaly you should use
# the C compiler as a front-end to the linker).
LD            = gcc
# Special flags to the linker
LDFLAGS    =
# Search-path for library files.
LDLIBDIRS  =
# Libraries to be included (like -lm -lnsl, and so on...).
LDLIBS     =

#########################################################################
# Implicit pattern rules.
#

# Build object files (.o) by compiling the respective C files.
%.o : %.c
	$(CC) $(CFLAGS) $(CPPFLAGS) $< -o $@

#########################################################################
# Default rule (what to build)
#

all : ianjflash

all_pci1149 : ianjflash_pci1149

#########################################################################
# Object dependencies
#

ianjflash-usage.c : ianjflash.txt
	./txt2c.sh < ianjflash.txt > ianjflash-usage.c 

gnu_getopt.o : gnu_getopt.c gnu_getopt.h

gnu_getopt_long.o : gnu_getopt_long.c gnu_getopt.h

jtag.o : jtag.c jtag.h

jtag1149.o : CFLAGS += $(PCI1149_INCDIRS)
jtag1149.o : jtag1149.c jtag.h

pjtag_sa1110.o : pjtag_sa1110.c pjtag_sa1110.h pjtag.h jtag.h

fjtag_isf2x16.o : fjtag_isf2x16.c fjtag_isf2x16.h fjtag.h pjtag.h

ianjflash.o : ianjflash.c ianjflash-usage.c \
	fjtag_isf2x16.h fjtag.h pjtag_sa1110.h pjtag.h pjtag.h \
	gnu_getopt.c gnu_getopt_long.c

#########################################################################
# Rules to build each tool from the respective *objects*
#

ianjflash : ianjflash.o jtag.o pjtag_sa1110.o \
	fjtag_isf2x16.o gnu_getopt.o gnu_getopt_long.o 
	$(LD) $(LDFLAGS) $(LDLIBDIRS) -o $@ $+ $(LDLIBS)

ianjflash_pci1149 : LDLIBS += $(PCI1149_LIBS)
ianjflash_pci1149 : LDLIBDIRS += $(PCI1149_LIBDIRS)
ianjflash_pci1149 : ianjflash.o jtag1149.o pjtag_sa1110.o \
	fjtag_isf2x16.o gnu_getopt.o gnu_getopt_long.o
	$(LD) $(LDFLAGS) $(LDLIBDIRS) -o $@ $+ $(LDLIBS)

# Debugging goals
# you should probably 'make clean' before attempting these

jtag_tst : CPPFLAGS += -DJTAG_DEBUG
jtag_tst : jtag.o
	$(LD) $(LDFLAGS) $(LDLIBDIRS) -o $@ $+ $(LDLIBS)

pjtag_tst : CPPFLAGS += -DPJTAG_DEBUG
pjtag_tst : jtag.o pjtag_sa1110.o
	$(LD) $(LDFLAGS) $(LDLIBDIRS) -o $@ $+ $(LDLIBS)

fjtag_tst : CPPFLAGS += -DFJTAG_DEBUG
fjtag_tst : jtag.o pjtag_sa1110.o fjtag_isf2x16.o
	$(LD) $(LDFLAGS) $(LDLIBDIRS) -o $@ $+ $(LDLIBS)

#########################################################################
# Utility rules
#

clean:
	rm -f *.o
	rm -f *~
	rm -f ianjflash-usage.c
	rm -f core

distclean: clean
	rm -f ianjflash
	rm -f ianjflash_pci1149

