CXX		:= `root-config --cxx`
CXXFLAGS	:= -O3 -Wall `root-config --auxcflags`
LDFLAGS		:= `root-config --ldflags`
SRC		:= ./src
INC		:= ./include
OBJ		:=./obj
INCLUDES	:=-I./$(INC) -I`root-config --incdir`
LIB		:=-lm `root-config --glibs` -lgsl -lgslcblas

MAIN		:= $(OBJ)/main.o
CLASSES		:= Rectangle
HEADERS		:= $(CLASSES:%=$(INC)/%.h)
SOURCES		:= $(CLASSES:%=$(SRC)/%.cpp)
OBJECTS		:= $(CLASSES:%=$(OBJ)/%.o)

default: mainnormal

printa:
	echo $(SOURCES)

$(OBJ)/%.o: $(SRC)/%.cxx
	@echo Compiling $< ...
	@if ! [ -d $(OBJ) ]; then mkdir -pv $(OBJ); fi
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

$(OBJ)/%.o: $(SRC)/%.C
	@echo Compiling $< ...
	@if ! [ -d $(OBJ) ]; then mkdir -pv $(OBJ); fi
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

$(OBJ)/%.o: $(SRC)/%.cpp
	@echo Compiling $< ...
	@if ! [ -d $(OBJ) ]; then mkdir -pv $(OBJ); fi
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<

.DEBUG:
	@$(MAKE) clean
	@rm .NODEBUG	
	@touch .DEBUG

.NODEBUG:
	@$(MAKE) clean
	@rm .DEBUG
	@touch .NODEBUG

maindebug: CXXFLAGS += -D__DEBUG__
maindebug: .DEBUG main

mainnormal: .NODEBUG main

main: $(OBJECTS) $(MAIN)
	@echo Linking $^ to $@
	$(CXX) $(LDFLAGS) $^ $(LIB) -o $@
# on Linux the order of the linking matters:
# you need to put, before, the objects requiring other libraries
# and then the needed libraries

clean:
	@rm -rfv $(OBJ)
	@rm -fv main

zippa:
	@rm -f Compilazione.zip
	@zip -r --exclude=*~ Compilazione.zip src include Makefile
