# ============================================================================
# Name        : Makefile
# Author      : Geralf Hütter
# Version     :
# Copyright   : 
# Description : Makefile for UELlib
# Note        : - Path to UELlib is read from environmental variables in "src/abaqus_v6.env"
#               - "src/abaqus_v6.env" in $SRCPATH$ needs to be from desired version of Abaqus
#               - "src/abaqus_v6.env" options for compilation and linking are also in "abaqus_v6.env"
# ============================================================================

#folders of current project for source code and binaries to be created
BINPATH=bin/
SRCPATH=src/
#main file, to be located in $SRCPATH
MAINFILE=UEL.f
#add here all files which are included by main file in addition to those from UELlib
SRCINCFILES=$(addprefix $(SRCPATH), UelCXUT.f90 UXMAT1.f90 UXMAT2.f90)
#path to UELlib
#  Note: need to be absolute path since exported variables are also read in "abaqus_v6.env"
UELlibpath=$(abspath ../../)
#  subdirectories
UELlibsrcpath=$(UELlibpath)/lib/
UELlibbinpath=$(UELlibpath)/bin/
UELlibsrcfiles= ABQinterface.f90 Math.f90 UEL_lib.f90
#commands for compilation by Abaqus make utility
ABQMAKE=abq6123 directory=$(abspath $(BINPATH)) make library=
#  Paths, where compiler looks for *.mod files etc. (concatenate several paths with ':') 
ABQINCLPATHS=$(UELlibbinpath)
export  ABQINCLPATHS

.PHONY: build buildlibbin buildlibsrc clean

# default build target
#  either "buildlibbin" or "buildlibsrc" or "buildsrc"
build: buildlibbin

# Build the library by linking the precompiled UELlib
#   Note: only those object files are included by "abaqus make" into the linked library which have a name "*-std.o"
buildlibbin: $(SRCPATH)$(MAINFILE) $(SRCINCFILES) $(SRCPATH)abaqus_v6.env $(UELlibbinpath)UEL_lib-std.o
	cd $(BINPATH); rm -f libstandardU.so $(basename $(MAINFILE))-std.o UELtemp-std.o
	cp -u $(UELlibbinpath)UEL_lib-std.o $(BINPATH)
	cd $(SRCPATH); $(ABQMAKE)$(MAINFILE)

# Build the library by including the source code of UELlib
#  concatenated with actual source file to temporary file "UELtemp.f" 
buildsrc: $(SRCPATH)UELtemp.f

$(SRCPATH)UELtemp.f: $(addprefix $(UELlibsrcpath)/,$(UELlibsrcfiles)) $(UMATE) $(SRCPATH)$(MAINFILE)
	cat $^ > $(SRCPATH)UELtemp.f 

buildlibsrc: $(SRCPATH)$(MAINFILE) $(SRCINCFILES) $(SRCPATH)abaqus_v6.env $(SRCPATH)UELtemp.f 
	cd $(BINPATH); rm -f libstandardU.so $(basename $(MAINFILE))-std.o UELtemp-std.o UEL_lib-std.o
	cd $(SRCPATH); $(ABQMAKE)UELtemp.f

# rule for compiling UELlib
$(UELlibbinpath)UEL_lib-std.o:
	cd $(UELlibpath); make

# clean
clean:
	rm -f $(BINPATH)*.mod
	cd $(BINPATH); rm -f libstandardU.so $(basename $(MAINFILE))-std.o UELtemp-std.o 
	rm -f $(SRCPATH)UELtemp.f

