PROGNAME = testmacro.exe
SRC_DIR     := src
# := is a "simply expanded variable" and these variables are expanded only once before execution
# also src means source
BUILD_DIR   := build
SOURCEFILES := $(wildcard $(SRC_DIR)/*.cxx)
# wildcard *.cxx lists all the .cxx files in the current directory, or in this case, the directory specified by SRC_DIR
ROOTSOURCES := $(wildcard $(ROOT_SRCS)/*.cxx)
# this syntax is used because wildcard expansion does not normally take place when a variable is set, or inside the arguments of a function. In order to have expansion in these places you use $(wildcard pattern...).
OBJS        := $(patsubst $(SRC_DIR)/%.cxx, $(BUILD_DIR)/%.o, $(SOURCEFILES)) 
ROOTOBJS    := $(patsubst $(ROOT_SRCS)/%.cxx, $(BUILD_DIR)/%.o, $(ROOTSOURCES))

ROOTCFLAGS   := $(shell root-config --cflags)
# shell is the equivalent of `cmd` passing the command to terminal and then expanding out the result and allocates it to the variable.
ROOTLIBS     := $(shell root-config --libs)
ROOTGLIBS    := $(shell root-config --glibs)
LDFLAGS = -O $(ARCH)
LIBS += $(ROOTLIBS) -lRooFit -lHtml -lMinuit -lTMVA -lRooFitCore 
CFLAGS += $(ROOTCFLAGS) $(ARCH)

#LIBS += -lProof
#LIBS += -lProofPlayer
LIBS += -lTreePlayer
LIBS += -lHistPainter
LIBS += -lXMLParser
#LIBS += -llcg_RootCollection


$(PROGNAME):    $(OBJS) $(ROOTOBJS) $(INCLUDES)
	g++ -o $@ $(OBJS) $(ROOTOBJS) $(LDFLAGS) $(LIBS)


$(DEBUG):    $(OBJS) $(ROOTOBJS) $(INCLUDES)
	g++ -g -o $@ $(OBJS) $(ROOTOBJS) $(LDFLAGS) $(LIBS)


%/.d:
	mkdir -p $(@D)

$(BUILD_DIR)/%.o: $(ROOT_SRCS)/%.cxx $(BUILD_DIR)/.d
	g++ ${CFLAGS} -c  -o $@ $<

$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cxx $(BUILD_DIR)/.d
	g++ ${CFLAGS} -c  -o $@ $<

all: $(PROGNAME)

grid:
	$(MAKE) $(MAKEFILE) ARCH="-m32"
# i think if gmake grid is run then the arch variable is set and gmake reruns the makefile with that variable now in place, which appears in LDFLAGS and CFLAGS

test:
	@echo OBJS: $(ROOTOBJS)

clean:
	@rm -f ${PROGNAME}
	@rm -rf ${BUILD_DIR}

# automatic variables
# $@ The file name of the target of the rule. If the target is an archive member, then .$@. is the name of the archive file.
# $%The target member name, when the target is an archive member.
# $< The name of the first prerequisite.


