#!/usr/bin/make -f
# "Modern" Fortran has USE and INCLUDE which makes modular builds
# difficult. However, standard old fort77(1posix) plays well with Gcc
# and Make. (C) Andrew Buckeridge 2013. I hear by release this in to
# the public domain so that it may be used with both free and
# proprietary software or any other use.

# Name the binary you are making
PROG	=	eplus
# "Modern" Fortran (deps need to be resolved _before_ compile)
MFS	:=	$(wildcard *.f90)
# Old Fortran (deps can be resolved at link edit)
OFS	:=	$(wildcard *.f)
# C source (deps can be resolved at link edit)
CS	:=	$(wildcard *.c)
# Object files
OBJS	:=	$(MFS:%.f90=%.o) $(OFS:%.f=%.o) $(CS:%.c=%.o)
LIBS	=	-lgfortran
# Libs -lc -lm -lquadmath are also pulled when gcc below is used
LD	=	gcc
CC	=	gcc
MFC	=	gfortran
# fort77 is an f2c wrapper on Debian and can provide Fortran 66
#OFC	=	fort77
# gfortran also provides Old Fortran 77
OFC	=	gfortran
# C well defined and understood so should always compile clean
# http://www.fecyk.ca/spamalbum/lyrics/write-in-c.txt
CFLAGS	=	-std=c1x -Wuninitialized -Wall -Werror -Wfatal-errors \
		-pedantic-errors
# IMPLICIT NONE is the only MIL-STD-1753 that can save you.
# INCLUDE is toxic and removal of line numbers is only aesthetic.
OFFLAGS	=	-std=legacy -ffixed-form -fmax-identifier-length=6 \
		-ffixed-line-length-72 -fimplicit-none \
		-Wall -Wsurprising -pedantic-errors
# Ftran is dirty so only warn, allow 63 char identifiers CPP and defines
MFFLAGS =	-std=gnu -fimplicit-none -cpp -DLINUX \
		-Wall -Wsurprising -pedantic-errors
# Make it small
OFLAGS	=	-Os
# Make it use advances in architecture
AFLAGS	=	-march=nocona -msse3 -mfpmath=sse
#AFLAGS	=	-mcpu=G4 -mpowerpc-gfxopt -maltivec -mabi=altivec -fsigned-char
# Stripped dynamic
LDFLAGS	=	-Bdynamic -s

all: depends.mak $(PROG)

$(PROG): $(OBJS)
	$(LD) $(OFLAGS) $(AFLAGS) $(LDFLAGS) -o $(@) $(^) $(LIBS)

depends.mak: $(MFS)
	./usemod.awk $(^) >$(@)

%.o: %.c
	$(CC) $(OFLAGS) $(AFLAGS) $(CFLAGS) -c -o $(@) $(<)

%.o: %.f
	$(OFC) $(OFLAGS) $(AFLAGS) $(OFFLAGS) -c -o $(@) $(<)

# "Modern" Fortran has hidden deps
include depends.mak

%.o: %.f90
	$(MFC) $(OFLAGS) $(AFLAGS) $(MFFLAGS) -c -o $(@) $(<)

relink:
	rm -f -- $(PROG) depends.mak

clean:
	rm -f -- $(PROG) $(OBJS) depends.mak *.mod
