## Makefile (D. Prendergast Jul 2008)
##

PREFIX=.
include $(PREFIX)/Common/common-rules.mk

pre:
	$(MAKE) donkey && $(MAKE) common

default: all

list:
	@echo
	@echo "BerkeleyGW Makefile"
	@echo
	@echo "Available make targets:"
	@echo
	@echo "make default"
	@echo "make all"
	@echo "make all-flavors"
	@echo "make install"
	@echo "make check"
	@echo "make check-save"
ifdef TESTSCRIPT
	@echo "make check-jobscript"
	@echo "make check-jobscript-save"
endif
	@echo "make common"
	@echo "make library"
	@echo "make epsilon"
	@echo "make sigma"
	@echo "make bse"
	@echo "make plotxct"
	@echo "make meanfield"
	@echo "make nonlinearoptics"
	@echo "make visual"
	@echo
	@echo "make clean"
	@echo "make clean-flavored"
	@echo "make cleanall"
	@echo

help: list


# FHJ: This is the master code which defines the rules to make and clean the targets.
# The rules we define are make-*, clean-*, clean-flavored-*, and cleanall-*.
DIRS = Common bin library Epsilon Sigma BSE PlotXct MeanField NonLinearOptics Visual

# FHJ: Defines targets such as make-Epsilon.
MAKE_DIRS = $(addprefix make-,$(DIRS))
# FHJ: Full clean: clean everything, flavorfull and flavorless.
CLEAN_DIRS = $(addprefix clean-,$(DIRS))
# FHJ: Flavored clean: clean everything that depends on the flavor, but saves flavorless builds.
CLEAN_FLAVORED_DIRS = $(addprefix clean-flavored-,$(DIRS))
CLEANALL_DIRS = $(addprefix cleanall-,$(DIRS))
.PHONY: $(MAKE_DIRS) $(CLEAN_DIRS) $(CLEAN_FLAVORED_DIRS) $(CLEANALL_DIRS)
# FHJ: These fancy rules are called static pattern rules. We used them instead
# of the implicit pattern rules because they play nice with .PHONY.
$(MAKE_DIRS): make-%:
	cd $* && $(MAKE)
$(CLEAN_DIRS): clean-%:
	cd $* && $(MAKE) clean
$(CLEAN_FLAVORED_DIRS): clean-flavored-%:
	cd $* && $(MAKE) clean-flavored
$(CLEANALL_DIRS): cleanall-%:
	cd $* && $(MAKE) cleanall

# FHJ: These are just shortcuts if the user wants to build
# only a specific subdirectory
# Note that the rule common is already define in Common/common-rules.mk
library: common
	$(MAKE) make-library
epsilon: common
	$(MAKE) make-Epsilon
sigma: common
	$(MAKE) make-Sigma
bse: common
	$(MAKE) make-BSE
plotxct: common
	$(MAKE) make-PlotXct
meanfield: common
	$(MAKE) make-MeanField
nonlinearoptics: common
	$(MAKE) make-NonLinearOptics
.PHONY: common library epsilon sigma bse plotxct meanfield nonlinearoptics phonon
.PHONY: all donkey

#
# All
#
all_: $(MAKE_DIRS) manual
all:
	$(MAKE) pre && $(MAKE) all_

all-flavors:
	$(MAKE) clean
	cp flavor_real.mk flavor.mk
	$(MAKE) all
	$(MAKE) clean-flavored
	cp flavor_cplx.mk flavor.mk
	$(MAKE) all

# Deliberately make this always be executed, by not mentioning the file manual.html
# It depends on so many files it would be tedious to list them all here.
manual: printsvninfo
	bin/assemble_manual.sh > documentation/users/manual.html

install: all
ifdef INSTDIR
	mkdir -p $(INSTDIR)/bin
	install bin/*.x $(INSTDIR)/bin/
	install bin/*.sh $(INSTDIR)/bin/
	install bin/*.py $(INSTDIR)/bin/
	install bin/*.pl $(INSTDIR)/bin/

	mkdir -p $(INSTDIR)/lib
	install library/*.a $(INSTDIR)/lib/
	mkdir -p $(INSTDIR)/include
	install library/*.mod $(INSTDIR)/include/

	mkdir -p $(INSTDIR)/share
	mkdir -p $(INSTDIR)/share/BerkeleyGW
# install cannot work on a whole directory
	cp -rf examples  $(INSTDIR)/share/BerkeleyGW/
	cp -rf testsuite $(INSTDIR)/share/BerkeleyGW/
	install manual.html $(INSTDIR)/share/BerkeleyGW/
else
	$(error Error: Please define installation prefix INSTDIR via 'make install INSTDIR='.)
endif

check: all
	cd testsuite && $(MAKE) check

check-save: all
	cd testsuite && $(MAKE) check-save

ifdef TESTSCRIPT
check-jobscript: all
	cd testsuite && $(MAKE) check-jobscript
check-jobscript-save: all
	cd testsuite && $(MAKE) check-jobscript-save
endif

#
# Clean
#
clean: $(CLEAN_DIRS) clean-manual
clean-flavored: $(CLEAN_FLAVORED_DIRS) clean-manual
cleanall: cleanall-bin $(CLEANALL_DIRS)

clean-manual:
	-$(REMOVE) manual.html
