#Makefile to build libcppsub.a
#
SHR = ../share/Make.flags
ifneq ("$(wildcard $(CODESYSDIR)/source/misc/makeflags.mk)","")
	SHR = $(CODESYSDIR)/source/misc/makeflags.mk
endif
-include ../share/Make.local
include $(SHR)
 
# use f90

ifdef DEBUG
	FFLAGS = $(DFFLAGS)
	CXXFLAGS = $(DCXXFLAGS)
endif


ifdef CODESYSDIR
	OBJ = ${LOCAL}
	ARC = $(OBJ)/lib/cppsub.a
else
	ARC = $(OBJ)/lib/libcppsub.a
endif

ifneq ($(MAKELEVEL),0)
# if OBJ was defined for main Makefile one level down
ifeq ("${OBJ}",".")
	override OBJ=..
endif
endif
 
OBJDIR = $(OBJ)/obj/cppsub


# don't rebuild library if using installed one in $PREFIX 
ifeq ($(MAKELEVEL),0)
	THISLIB=$(ARC)
endif

srcdir = $(shell pwd)

# c++ sources
FSRC=$(subst .f90,.o,$(wildcard *.f90))
CSRC=$(subst .cpp,.o,$(wildcard *.cpp))
ASRC=$(FSRC) $(CSRC)
CMEM = $(foreach file,$(ASRC),$(ARC)($(file)))


ifeq ($(MACHINE),CRAY)
	HH = $(notdir $(wildcard ../include/cppsub/*.h))
	H =  $(foreach file,$(HH),$(OBJDIR)/$(file))
endif

MEM = $(CMEM) 


.PHONY: clean realclean

libs: libcppsub

libcppsub: FORCE $(ARC)
 
all: libs

$(OLDLIB): timestamp.inf
	@echo "--- DETECTED adpak.a source update"
	@echo "--- Re-Making $(ARC)"
	@$(MAKE) libs
 
$(ARC): $(MEM)
	@$(RANLIB) $@ > /dev/null

FORCE:
	@test -d $(OBJ)/lib || mkdir -p $(OBJ)/lib
	@test -d $(OBJDIR) || mkdir -p $(OBJDIR)



$(ARC)(%.o): %.cpp
	$(cxx_proc)
	$(make_ar)

#  compile free form fortran 90 code
$(ARC)(%.o): %.f90
	$(FC90) $(FFLAGS) $(MODFLAGS) -I./ $(INCFLAGS) $(OUT) $(OBJDIR)/$*.o $<
	$(mv_mod)
	$(make_ar)

#---------------------------------------------------
exec:
	@echo 

clean: 
	@rm -f $(OBJDIR)/*

realclean: clean
	@rm -f $(ARC)


install:
	@test -d $(LIBDIR) || mkdir -p $(LIBDIR)
	@if test -f $(ARC); then \
	  umask 133; cp $(ARC) $(LIBDIR)/; \
	else test -f $(LIBDIR)/libcppsub.a || echo ">>> can not find $(ARC)";fi

uninstall:
	rm -f $(LIBDIR)/libcppsub.a



