
MESA_DIR = ../..

#################################################################

# STEP 1: set up the standard info

include $(MESA_DIR)/utils/makefile_header

# #################################################################

ifeq ($(USE_GYRE),NO)

# STEP 2: build a stub library

LIB = libgyre.a
LIB_OBJS = gyre_lib_stub.o

$(LIB) : $(LIB_OBJS)
	$(LIB_TOOL) $(LIB) $(LIB_OBJS)

#################################################################

COMPILE = $(COMPILE_TO_DEPLOY) $(FCfree)
#COMPILE = $(COMPILE_TO_TEST) $(FCfree)

gyre_lib_stub.o: $(MOD_PUBLIC_DIR)/gyre_lib_stub.f
	$(COMPILE) $<         

clean:
	-@rm -f *.o *.mod $(LIB)

else

#################################################################

# STEP 2: build the library

GYRE_DIRS = ../gyre/src/extern/core:../gyre/src/common:../gyre/src/ivp:../gyre/src/bvp:../gyre/src/coeffs:../gyre/src/matrix:../gyre/src/ad:../gyre/src/rad:../gyre/src/frontend

FPX3FLAGS = -DGFORTRAN_PR_56052 -DGFORTRAN_PR_56872 -DGFORTRAN_PR57922 -DOMP -DDOUBLE_PRECISION -I${GYRE_DIRS}:../gyre/src/mesa

LIB = libgyre.a

libgyre.a : gyre_lib.o
	@echo AR libgyre.a
	@rm -f libgyre.a
	@ar crs libgyre.a *.o

#################################################################

# Vars

COMPILE = $(COMPILE_TO_DEPLOY) $(FCfree) -Wno-uninitialized
#COMPILE = $(COMPILE_TO_TEST) $(FCfree) -Wno-uninitialized

# Rules

vpath %.fpp ${GYRE_DIRS}
vpath %.inc ${GYRE_DIRS}
vpath %.f ${GYRE_DIRS}

.PRECIOUS : %.f90

%.o : %.mod

%.f90 : %.fpp
	@fpx3 ${FPX3FLAGS} < $< > $@

%.o %.mod : %.f90
	@echo FC $<
	@${COMPILE} -c $<
	@if [ -e $(basename $@).mod ]; then touch $(basename $@).mod; fi

clean:
	-@rm -f *.o *.f90 *.fpp *.mod .depend ${LIB}

# Dependencies

gyre_lib.fpp : ../gyre/src/mesa/gyre_lib.fpp
	@cp $< $@

ifneq ($(MAKECMDGOALS),clean)
$(shell fpx3_deps ${FPX3FLAGS} gyre_lib > .depend )
-include .depend
endif

endif
