# Copyright (c) 2009-2011 Centro Svizzero di Calcolo Scientifico (CSCS)
# Licensed under the GPLv2.
# NAMES
TARGETS = node_attributes numa_node_attributes

# PROGRAMS
CC    = gcc
SHELL = /bin/bash	# for '<()' process substitution
XTPROCADMIN      := $(shell which xtprocadmin 2>/dev/null)
DOWN_NODES       := $(shell xtprocadmin | sed -n 's/^ *\([0-9]\+\).*down .*$$/\1/p' |tr '\n' '|')
APSTAT_NODE_ORDER = apstat -no  | sed -n '1d;/^Compute/,$$d;s/^ *\([0-9]\+\) .*$$/\1/p'

# PATHS
INCDIR = ../../include
LIBDIR = ../../libalps

# FLAGS
CFLAGS = -I$(INCDIR) -Wall -Werror #-pedantic
ALPSLIB= $(LIBDIR)/libalps.a
LDLIBS = -lexpat

# RULES
.PHONY: clean check check_xtprocadmin check_node_ordering

all: check $(TARGETS)

# when apbasil is available, compare the outputs directly
ifneq ($(XTPROCADMIN),)
check: check_xtprocadmin check_node_ordering
else
check: node_attributes
	@./$<
endif

check_xtprocadmin: node_attributes
	diff -sy --suppress-common-lines				\
		<(xtprocadmin -a cores,clockmhz,mem | grep -vw service) \
		<(./$< | sort -n)

check_node_ordering: node_attributes
	diff -ys --suppress-common-lines				\
		<($(APSTAT_NODE_ORDER))					\
		<(./$<|sed -n '1d;s/^ *\([0-9]\+\) .*$$/\1/p'|egrep -vx "($(DOWN_NODES))")

# cancel built-in rule first
% : %.c
% : %.c $(ALPSLIB)
	$(CC) $(CFLAGS) $^ $(LDLIBS) -o $@

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

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

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

distclean: clean
