#===========================================================================
#
# Makeopus.include
#
# author: Scott Binegar 
#
# Note: The environment variable SHAREROOT must be defined in order to
#       build OPUS.  SHAREROOT should be defined as the directory path
#	to the SHARE tree that you wish to build OPUS upon. Example:
#
#	setenv SHAREROOT /project/devcl/odosw1/dptbuild/share03_2_00
#
# 10/08/01  44600  WMiller    Change shared lib sys to osys
# 10/15/01  44631  J.Baum     Add shared lib mt
# 11/08/01  44751  WMiller    Add shared libs: opus_jvm & jfof
# 02/28/02  45317  WMiller    Rework library dependencies
# 02/28/02  45317  Binegar    Change location of FITS_PP 
# 10/21/02  45889  Rose       Add the CFITSIO library   #
# 08/11/03  41512  Sontag     Hush dependency generation.
# 12/11/03  49769  jschultz   fix for new cfitsio library
# 03/04/04  50605  Sontag     Move CFITSIO settings to .defs files
# 07/27/04  49860  Sontag     Remove forced mkdir of cxx_repository
# 09/16/05  53337  Sontag     Put ACE_ROOT in LD_LIB._64 too
# 05/05/06  55743  Sontag     Fix to the dep-generating greps
# 12/23/09  63939  MSwam      Changes for Linux
#===========================================================================

#
# only find TOPDIR if it hasn't been passed down to us
#
ifndef TOPDIR
  TOPDIR:=$(shell pwd | sed -e 's?/src.*??')
endif

# SHAREROOT must be defined if SHARE source and libs need to be included
ifneq ($(SHAREROOT),)
  # Determine the operating system
  include $(SHAREROOT)/platform.defs

  share_lib_dir = $(SHAREROOT)/lib/$(OS_SUB_DIR)
  share_src_apps = $(SHAREROOT)/src/apps
  share_src_libs = $(SHAREROOT)/src/libs
  share_inc_dir = $(SHAREROOT)/inc
  INCDIRS=-I$(share_inc_dir)
  LINKDIRS=-L$(share_lib_dir)
else
  warn_text = ERROR: Environment variable SHAREROOT is not defined! 
  WARN = $(warning $(warn_text))
.PHONY: warn
warn: 
	@echo $(warn_text)
	@false
endif

#
# Common Makefile definitions 
#
SRCINC=$(TOPDIR)/inc
OBJDIR=$(TOPDIR)/obj/$(OS_SUB_DIR)
LIBDIR=$(TOPDIR)/lib/$(OS_SUB_DIR)
BINDIR=$(TOPDIR)/bin/$(OS_SUB_DIR)
CXXREPOS=$(OBJDIR)/cxx_repository/ 
UIDDIR=$(TOPDIR)/uid/$(OS_SUB_DIR)

#
# Make search paths
#
vpath %.o $(OBJDIR)
vpath %.so $(LIBDIR) $(share_lib_dir)
vpath %.h $(SRCINC) $(share_inc_dir)
vpath %.hxx $(SRCINC) $(share_inc_dir)
vpath %.c $(share_src_libs) $(share_src_apps)
vpath %.cpp $(share_src_libs) $(share_src_apps)
vpath %.cxx $(share_src_libs) $(share_src_apps)
vpath %.jar $(BINDIR)
vpath %.uid $(UIDDIR)


# Include the platform specific defs
include $(TOPDIR)/src/$(OS_SUB_DIR).defs

#===================================================================================
#
# The following section contains common rules for compiling source, linking, etc 
#
#===================================================================================

.DELETE_ON_ERROR:

# Add ACE paths to LD_LIBRARY_PATH as necessary
# (needed for builds done by dptbuild, which doesn't have these dirs
#  in its settings)
LD_LIBRARY_PATH := $(ACE_ROOT)/ace:$(ACE_ROOT)/lib:$(LD_LIBRARY_PATH)
LD_LIBRARY_PATH_64 := $(ACE_ROOT)/ace:$(ACE_ROOT)/lib:$(LD_LIBRARY_PATH_64)

# Define rules for generating dependency files
# Remove any system files (path begins with /usr)
# Remove any ACE_ROOT files
# sleep 1 is necessary to prevent "time skew" warnings
define gen-deps
	@echo "Building dependency file: $@"
	@[ -d dep ] || mkdir dep
	@set -e; $(CXX) $(DEPFLAGS) $(CXXFLAGS) $< \
	| grep -v '^ *\/usr\/' \
	| grep -v '^ *$(ACE_ROOT)' > $@; \
	echo "    " >> $@; \
	echo "$@ : $<" >> $@; \
	[ -s $@ ] || rm -f $@
	@sleep 1
endef

dep/%.d: %.c
	$(gen-deps)

dep/%.d: %.cpp
	$(gen-deps)

dep/%.d: %.cxx
	$(gen-deps)


# Define rule for compiling c files:
%.o: %.c
	@echo "Compiling: $<"
	@[ -d $(OBJDIR) ] || mkdir -p $(OBJDIR)
	$(CC) -c $(CCFLAGS) $< -o $(OBJDIR)/$@


# Define rule for compiling cpp files:
%.o: %.cpp
	@echo "Compiling: $<"
	@[ -d $(OBJDIR) ] || mkdir -p $(OBJDIR)
	$(CXX) -c $(CXXFLAGS) $< -o $(OBJDIR)/$@

# Define rule for compiling cxx files :
%.o: %.cxx
	@echo "Compiling: $<"
	@[ -d $(OBJDIR) ] || mkdir -p $(OBJDIR)
	$(CXX) -c $(CXXFLAGS) $< -o $(OBJDIR)/$@

# Define rule for compiling FORTRAN files:
%.o: %.for
	@echo "Compiling: $<"
	@[ -d $(OBJDIR) ] || mkdir -p $(OBJDIR)
	$(FOR) -c $(FORFLAGS) $< -o $(OBJDIR)/$@


# Define rule for compiling java files:
%.class : %.java
ifdef CLASSPATH
	$(JAVAC) -classpath ".:$(LOCAL_JARS):$(CLASSPATH)" $<
else
	$(JAVAC) $<
endif

# Define rule for compiling UIL files:
%.uid : %.uil
	@echo "Compiling uil: $<"
	@[ -d $(UIDDIR) ] || mkdir -p $(UIDDIR)
	@uil $< -o $(UIDDIR)/$@

#
# Define targets if there are sources (some things aren't built on all platforms yet)
#
ifndef sources

nosource: FORCE
  ifndef uil
	@echo "$(bin_name) $(shared_lib_name) is not built on $(OS_SUB_DIR)"
  else
	@echo "UIL file $(tgt_file) not built on $(OS_SUB_DIR)"
  endif

else # sources not defined

# Create the list of objects and depenency files from the sources file list
objects := $(patsubst %.c,%.o, $(sources))
objects := $(patsubst %.cpp,%.o, $(objects))
objects := $(patsubst %.cxx,%.o, $(objects))

deps := $(objects:.o=.d)
deps := $(addprefix dep/, $(deps))

uid_objects := $(patsubst %.uil,%.uid, $(sources))

objects := $(patsubst %.for,%.o, $(objects))

#
# Define rule for generating share object files
#
ifdef shared_lib_name

tgt_file =  $(LIBDIR)/lib$(shared_lib_name).so

$(LIBDIR)/lib$(shared_lib_name).so : $(objects)
	@echo "Building: $@ "
	@[ -d $(LIBDIR) ] || mkdir -p $(LIBDIR)
	cd $(OBJDIR); $(CXX) $(SOFLAGS) $(objects) -o $@ $(SOLIBS)

$(shared_lib_name) : $(LIBDIR)/lib$(shared_lib_name).so
	@;

endif

#
# Define rule for generating archive object files
#
ifdef lib_name

tgt_file =  $(LIBDIR)/lib$(lib_name).a

$(LIBDIR)/lib$(lib_name).a : $(objects)
	@echo "Building: $@ "
	@[ -d $(LIBDIR) ] || mkdir -p $(LIBDIR)
	cd $(OBJDIR); $(AR) -cru $@ $(objects)

$(lib_name) : $(LIBDIR)/lib$(lib_name).a
	@;

endif

#
# Define rule for generating binary image files
#
ifdef bin_name

tgt_file =  $(BINDIR)/$(bin_name)

$(BINDIR)/$(bin_name) : $(objects)
	@echo "Building: $(bin_name)"
	@[ -d $(BINDIR) ] || mkdir -p $(BINDIR)
	cd $(OBJDIR); $(CXX) $(LINKFLAGS) $(LINKLIBS) $(objects) -o $@
	@chmod ugo+x '$(@)'

$(bin_name) : $(BINDIR)/$(bin_name)
	@;
endif

#
# Define targets for uil files 
# 
ifdef uil
 
tgt_file := $(prefix $(UIDDIR)/$(uid_objects)
 
 
uil : $(uid_objects) 
	@echo "UIL updated"
endif
 
# if local_clean is not defined in the *.make file, use the generic clean 
ifeq ($(local_clean),)
.PHONY : clean
clean :
  ifneq ($(uil),)
	@echo "Cleaning up uil files.."
	cd $(UIDDIR); rm -f $(uid_objects)
  else
	@echo "Cleaning up files for: $(bin_name) $(shared_lib_name)"
	@cd $(OBJDIR); rm -f $(objects)
	@rm -f $(deps)
	@rm -f $(tgt_file)
  endif

endif


#
# Do not include the dependency files if $deps is not defined or 
# if this is a "clean".
#
ifdef deps
  ifneq ($(MAKECMDGOALS),clean) 
    -include $(deps)
  endif
endif

endif # if sources not defined

FORCE:
