# Copyright (c) 2009-2011 Centro Svizzero di Calcolo Scientifico (CSCS)
# Licensed under the GPLv2.
# NAMES
TARGETS = get_torus_type extract_node_coordinates print_bits
OBJS    = guess_torus_from_inventory.o
# PROGRAMS
CC    = gcc
RM    = rm -vf
SHELL = /bin/bash

# PATHS
INCDIR = ../../include
LIBDIR = ../../libalps
HEADERS= $(wildcard $(INCDIR)/*.h)
ALPSLIB= $(LIBDIR)/libalps.a

# FLAGS
CFLAGS = -I$(INCDIR) -Wall -Werror #-pedantic
LDLIBS = -lexpat

# RULES
.PHONY: clean check

all: $(TARGETS) check
	./get_torus_type

check: extract_node_coordinates
ifneq ($(shell which xtprocadmin 2>/dev/null),)
	@for DIM in 2 3; do\
		echo "Testing '$<' for $${DIM} dimensions: ";\
		if cmp -s <(./$< $$DIM | sort -n)\
			  <(./get_coords_from_sdb.pl);\
		then\
			exec echo -e "Test PASSED for $${DIM}D\n";\
		fi;\
	done;\
	echo $< FAILED; exit -1
else
	./$<
endif

# First delete the internal rule '<name>: <name>.c'
%: %.c
%: %.c $(OBJS) $(ALPSLIB)
	$(CC) $(CFLAGS) $^ $(LDLIBS) -o $@

$(ALPSLIB): $(wildcard $(LIBDIR)/*.c) $(HEADERS)
	$(MAKE) -C $(dir $(ALPSLIB))

tags: $(wildcard *.c $(LIBDIR)/*.c) $(HEADERS)
	ctags $^ > $@

clean:
	@$(RM) *.o *~ ? core a.out tags *.orig *.rej
	@$(RM) $(TARGETS)
	@rcsclean

distclean: clean
