#  -*-Makefile-*-  (for Emacs)    vim:set filetype=make:  (for vim)
#
#  Makefile.parent
#
#  Pencil Code Makefile for run directories that builds the code in the
#  src/ subdirectory by running `make' there.
#

include src/Makefile.local

# Build the code:
default: Makefile_inc
	touch src/.build-history
	echo $(USER)"@"`hostname` "("`date`")" >> src/.build-history
	( cd src && \
	  $(MAKE) FROM_PARENT=src/ ADAPT_MKFILE=$(ADAPT_MKFILE) \
	      -f Makefile.src Makefile && \
	  $(MAKE) FROM_PARENT=src/ \
	)

# Use calculated dependencies instead:
depend dep:
	(cd src; $(MAKE) FROM_PARENT=src/ -f Makefile.src depend)

# Clean up most files:
clean:
	(cd src; $(MAKE) FROM_PARENT=src/ -f Makefile.src clean)

# Clean up even more files:
cleann:
	(cd src; $(MAKE) FROM_PARENT=src/ -f Makefile.src cleann)
# Same thing:
cleanall: cleann

# Clean up log files.
cleanlog: qoutclean
	rm -f *.log lamhosts

# Try to remove everything generated by pc_setupsrc, compilation, etc.
distclean: cleann cleanlog
	rm -f {getconf,start,run,start_run}.csh
	(cd src; $(MAKE) FROM_PARENT=src/ -f Makefile.src distclean)
	rm -f Makefile

# Compile with -g
debug:
	(cd src; $(MAKE) FROM_PARENT=src/ debug)

# Clean up files produced by common queuing systems:
qoutclean:
	rm -f start.csh.[eo][0-9]* start.csh.[0-9]*out
	rm -f run.csh.[eo][0-9]* run.csh.[0-9]*out
	rm -f start_run.csh.[eo][0-9]* start_run.csh.[0-9]*out
	rm -f start.csh.p[eo][0-9]* start.csh.p[0-9]*out
	rm -f run.csh.p[eo][0-9]* run.csh.p[0-9]*out
	rm -f start_run.csh.p[eo][0-9]* start_run.csh.p[0-9]*out

# Clean up files produced by running MPI
mpiclean:
	rm -f copy-snapshots.log copy-snapshots2.log lamhosts remote-top.log
	rm -f run_command.log lamboot.log

# Divine our configuration
semiauto-conf:
	$(MAKE) -s cleanall
	@(cd src; $(MAKE) -s FROM_PARENT=src/ semiauto-conf)

# Create Makefile.inc, but need this to be a `phony' target (i.e. not a
# filename as target), so this file is always written.
# Don't know whether .PHONY declaration is portable, so we just slightly
# rename the target.
Makefile_inc:
	@echo "MAKING src/Makefile.inc"
	@printf \
	    "%s\n" \
	    '# src/Makefile.inc                   -*-Makefile-*- (for Emacs)' \
	    '# ----------------' \
	    '' \
	    "# This file was automatically generated at `date` by 'make'." \
	    '# Its main role is to set the precision (single vs. double) of a' \
	    '# run for compilation.' \
	    '' > src/Makefile.inc
	@case "$(REAL_PRECISION)" in \
	''|default|4) \
	    desired_precision='single'; \
	    echo '# Default flags: single precision' >> src/Makefile.inc; \
	    ;; \
	double|8) \
	    desired_precision='double'; \
	    echo 'FFLAGS_GENERAL=$$(FFLAGS_DOUBLE)' >> src/Makefile.inc; \
	    echo 'CFLAGS_GENERAL=$$(CFLAGS_DOUBLE)' >> src/Makefile.inc; \
	    case '$(MPICOMM)' in \
	    mpicomm) \
	        echo 'MPICOMM=mpicomm_double' >> src/Makefile.inc; \
	        ;; \
	    mpicomm_) \
	        echo 'MPICOMM=mpicomm_double_' >> src/Makefile.inc; \
	        ;; \
	    esac; \
	    ;; \
	*) \
	    echo "What is REAL_PRECISION=$(REAL_PRECISION) supposed to mean?"; \
	    exit 1; \
	    ;; \
	esac; \
	./src/scripts/precision-check src/.current-precision "$$desired_precision"