#  -*-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

# Legacy stuff for adapt-mkfile users:
legacy_default: Makefile_inc Fortran_standard
	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/ \
	)

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

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

# Slender src/ directory:
slender:
	(cd src; $(MAKE) FROM_PARENT=src/ -f Makefile.src slender)

# 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 lamboot.log remote-top.log

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

# Compile tools for collecting, downscaling, and distributing data cubes
tools: default
# MR: which is the target "default"?

pc_collect pc_collect_pvar pc_reduce pc_distribute pc_distribute_z pc_extract pc_configtest pc_tecplot pc_tecplot_solid: Makefile_inc
	( cd src && \
	  $(MAKE) FROM_PARENT=src/ pre_and_post_processing/$@.x )
pc_meanfield_collect pc_h5collect: Makefile_inc
	( cd src && \
	  $(MAKE) FROM_PARENT=src/ experimental/$@.x )
read_all_videofiles read_videofiles combine_videofiles read_vectorfiles start run: Makefile_inc
	( cd src && \
	  $(MAKE) FROM_PARENT=src/ $@.x )
fast: Makefile_inc
	( cd src && \
	  $(MAKE) FROM_PARENT=src/ fast )
helper_min:  Makefile_inc
	( cd src && \
	  $(MAKE) FROM_PARENT=src/ helper_min )
#
# This should be triggered automatically, only if "pencil-code/src/Makefile.src" has changed, but is currently inactive:
setupsrc_inc: $(PENCIL_HOME)/src/Makefile.src
	pc_setupsrc

# Manually trigger "pc_setupsrc"
setup: setupsrc_inc

# 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|single|4) \
	    desired_precision='single'; \
	    echo '# Setting: single precision' >> src/Makefile.inc; \
	    echo 'PREC_EXT=' >> src/Makefile.inc; \
	    ;; \
	double|8) \
	    desired_precision='double'; \
	    echo 'FFLAGS_GENERAL=$$(FFLAGS_DOUBLE)' >> src/Makefile.inc; \
	    echo 'CFLAGS_GENERAL=$$(CFLAGS_DOUBLE)' >> src/Makefile.inc; \
	    echo 'PREC_EXT=_double' >> src/Makefile.inc; \
	    case '$(MPICOMM)' in \
	    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"

Fortran_standard:
	@case "$(FILE_IO)" in \
	'file_io_f2003') \
	    echo "FSTD=$(FSTD_2003)" > src/Makefile-standard.inc; \
	    ;; \
	*) \
	    echo "FSTD=$(FSTD_95)" > src/Makefile-standard.inc; \
	    ;; \
	esac;