
PROJECT=benchmark

BIN=$(PROJECT)
OBJ=benchmark.o mcs_lock.o

DEPEND_OPTIONS=-MMD -MP -MF ".$*.d.tmp" -MT "$*.o" -MT ".$*.d"
DOM=then mv -f ".$*.d.tmp" ".$*.d"; else rm -f ".$*.d.tmp"; exit 1; fi

ifndef PAPI_HOME 
PAPI_HOME=/usr/local/
endif

LIBLOCK=../liblock

CFLAGS=-g -Wall -Werror -Wno-format-zero-length -O0 -lm -I$(PAPI_HOME)/include -I$(PAPI_HOME) -I$(LIBLOCK) -D_GNU_SOURCE
CXXFLAGS=$(CFLAGS)
LDFLAGS=$(CXXFLAGS) \
	-Wl,-rpath,$(PAPI_HOME)/lib  -L$(PAPI_HOME)/lib \
	-Wl,-rpath,$(PAPI_HOME) -L$(PAPI_HOME) \
	-pthread -lpapi -lnuma

Echo=@echo [$(PROJECT)]: 

ifndef VERBOSE
  Verb := @
endif

DEPENDENCIES=$(patsubst %.o, .%.d, $(OBJ))

.PHONY: all bootstrap tidy clean distclean .force
.SECONDARY: 
.SUFFIXES:

all: bootstrap

bootstrap: $(BIN)

$(BIN): $(OBJ) $(LIBLOCK)/liblock.a 
	$(Echo) Linking $@
	$(Verb) g++ $(LDFLAGS) -o $@ $^ 

$(LIBLOCK)/liblock.a: .force
	make -C $(LIBLOCK)

.force:

%.o: %.cc Makefile
	$(Echo) Compiling $<
	$(Verb) if g++ $(CXXFLAGS)  $(DEPEND_OPTIONS) -c "$<" -o "$@"; $(DOM)

%.o: %.cpp Makefile
	$(Echo) Compiling $<
	$(Verb) if g++ $(CXXFLAGS)  $(DEPEND_OPTIONS) -c "$<" -o "$@"; $(DOM)

%.o: %.cxx Makefile
	$(Echo) Compiling $<
	$(Verb) if g++ $(CXXFLAGS)  $(DEPEND_OPTIONS) -c "$<" -o "$@"; $(DOM)

%.o: %.c Makefile
	$(Echo) Compiling $<
	$(Verb) if gcc $(CFLAGS)  $(DEPEND_OPTIONS) -c "$<" -o "$@"; $(DOM)

%.s: %.c Makefile
	$(Echo) "Compiling $< (asm)"
	$(Verb) if gcc $(CFLAGS)  $(DEPEND_OPTIONS) -S "$<" -o "$@"; $(DOM)

tidy:
	rm -f *~ \#*

clean:
	$(Echo) Cleaning compilation files
	$(Verb) rm -f *.o .*.d $(MAIN)

distclean: clean
	$(Echo) Cleaning distribution
	$(Verb) rm -f $(BIN) $(PROJECT).a

ifneq ($(MAKECMDGOALS),tidy)
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),distclean)
-include $(DEPENDENCIES)
endif
endif
endif

