#*=====================================================================*/
#*    serrano/prgm/project/bigloo/bigloo/Makefile.misc                 */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Fri Jul  5 10:55:55 2002                          */
#*    Last change :  Tue Mar 24 12:25:46 2020 (serrano)                */
#*    Copyright   :  2002-20 Manuel Serrano                            */
#*    -------------------------------------------------------------    */
#*    Various misc rules for Bigloo Makefiles.                         */
#*    -------------------------------------------------------------    */
#*    See Makefile.config (bottom) for the definition of the           */
#*    variables used in this Makefile. The variables have to be        */
#*    defined in Makefile.config in order to let client Makefiles      */
#*    redefined them easily.                                           */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    dummy                                                            */
#*---------------------------------------------------------------------*/
dummy: all

#*---------------------------------------------------------------------*/
#*    PARAMETER                                                        */
#*---------------------------------------------------------------------*/
INSTALL	= cp

#*---------------------------------------------------------------------*/
#*    Producing shared libraries                                       */
#*---------------------------------------------------------------------*/
bigloo-shared-lib:
	@ if [ "$(SHAREDLIBRARYSUPPORT)" = "yes" ]; then \
            if [ "$(LD)" = "true" ]; then \
              if [ "$(FORCELD)" = "true" ]; then \
                 echo > $(LIBDEST); \
              fi; \
            else \
	      case $(HOSTOS) in \
                mingw) \
                  $(MAKE) LDEXTRA="-Wl,--out-implib,$(notdir $(LIBDEST).a) $(LDEXTRA)" bigloo-shared && \
		  cp $(notdir $(LIBDEST).a) $(LIBDEST).a;; \
                *) \
                  $(MAKE) bigloo-shared || exit 1;; \
              esac; \
            fi; \
          fi

bigloo-shared:
	@ $(RM) -f $(LIBDEST)
	@ (close=""; \
           if [ "$(SHAREDLIBRARYCLOSED)" = "yes" ]; then \
             close="$(EXTRALIBS) $(BIGLOOLIB:%=-l%$(LIBSUFFIX)$(LIBSUFFIXTHREAD)-$(RELEASE)) $(BIGLOOEXTRALIBS:%=-l%$(LIBSUFFIX)$(LIBSUFFIXTHREAD)-$(RELEASE)) $(BIGLOOAPPLIBS:%=-l%$(LIBSUFFIX)$(LIBSUFFIXTHREAD)-$(APPRELEASE)) $(BIGLOOGCLIB:%=-l%$(LIBSUFFIXTHREAD)-$(RELEASE))"; \
           fi; \
           rpathopt=""; \
           if [ "$(LDRPATHOPT) " != " " ]; then \
             for p in $(RPATH); do \
               rpathopt="$(LDRPATHOPT)$$p $$rpathopt"; \
             done \
           fi; \
           if [ "$(LDINSTALLNAME) " != " " ]; then \
             if [ "$(LDINSTALLNAMEDIR) " = " " ]; then \
               ldinstallnamedir=$(DESTDIR)$(LIBDIR)/$(FILDIR); \
             else \
               ldinstallnamedir=$(LDINSTALLNAMEDIR); \
             fi; \
	     $(MAKE) shared-install-name \
                 LDRPATH="$$rpathopt" \
                 LDINSTALLNAMEDIR=$$ldinstallnamedir \
                 LDOPTS="-L$(LIBPATH) $(LDOPTS)" \
                 CLOSELIBS="$$close" \
                 RPATH= \
                 || exit 1; \
           else \
             if [ "$(LDSONAME) " = " " ]; then \
	       $(MAKE) shared-sans-soname \
                   LDRPATH="$$rpathopt" \
                   LDOPTS="-L$(LIBPATH) $(LDOPTS)" \
                   CLOSELIBS="$$close" \
                   RPATH= \
                   || exit 1; \
             else \
	       $(MAKE) shared-soname \
                   LDRPATH="$$rpathopt" \
                   LDOPTS="-L$(LIBPATH) $(LDOPTS)" \
                   CLOSELIBS="$$close" \
                   RPATH= \
                   || exit 1; \
             fi \
           fi)


shared-install-name:
	$(LD) -o $(notdir $(LIBDEST)) \
                 $(LDINSTALLNAME) $(LDINSTALLNAMEDIR)/$(notdir $(LIBDEST)) \
                 $(LDRPATH) \
                 $(OBJECTS) \
                 $(GCSTDOBJECTS) \
                 $(LDFLAGS) $(EXTRA_LD_OPT) \
                 $(CLOSELIBS) \
                 $(LDLIBS) $(LDOPTS) $(LDEXTRA) && \
         if [ "$(notdir $(LIBDEST))" != "$(LIBDEST)" ]; then \
           mv $(notdir $(LIBDEST)) $(LIBDEST); \
         fi

shared-sans-soname:
	$(LD) -o $(notdir $(LIBDEST)) \
                 $(LDRPATH) \
                 $(OBJECTS) \
                 $(GCSTDOBJECTS) \
                 $(LDFLAGS) \
                 $(CLOSELIBS) \
                 $(LDLIBS) $(LDOPTS) $(EXTRA_LD_OPT) $(LDEXTRA) && \
         if [ "$(notdir $(LIBDEST))" != "$(LIBDEST)" ]; then \
           mv $(notdir $(LIBDEST)) $(LIBDEST); \
         fi

shared-soname:
	$(LD) -o $(notdir $(LIBDEST)) \
                 $(LDSONAME)=$(SONAME) \
                 $(LDRPATH) \
                 $(OBJECTS) \
                 $(GCSTDOBJECTS) \
                 $(LDOPTS) $(EXTRA_LD_OPT) \
                 $(CLOSELIBS) \
                 $(LDLIBS) $(LDFLAGS) $(LDEXTRA) && \
         if [ "$(notdir $(LIBDEST))" != "$(LIBDEST)" ]; then \
           mv $(notdir $(LIBDEST)) $(LIBDEST); \
         fi

#*---------------------------------------------------------------------*/
#*    Installing/uninstalling a Bigloo library                         */
#*---------------------------------------------------------------------*/
install-lib:
	if [ -f $(BOOTLIBDIR)/$(SUBDIR)$(LIB).$(SHAREDSUFFIX) ]; then \
	   $(MAKE) install-shared-lib;  \
	fi
	if [ -f $(BOOTLIBDIR)/$(SUBDIR)$(LIB).a ]; then \
	   $(MAKE) install-static-lib; \
	fi

install-static-lib:
	if [ $(DESTDIR)$(LIBDIR)/$(FILDIR) != $(BOOTLIBDIR) ]; then \
	  $(INSTALL) $(BOOTLIBDIR)/$(SUBDIR)$(LIB).a \
             $(DESTDIR)$(LIBDIR)/$(FILDIR)/$(SUBDIR)$(LIB).a && \
          chmod $(MODFILE) $(DESTDIR)$(LIBDIR)/$(FILDIR)/$(SUBDIR)$(LIB).a && \
          $(RANLIB) $(DESTDIR)$(LIBDIR)/$(FILDIR)/$(SUBDIR)$(LIB).a || exit 3; \
        fi

install-shared-lib:
	if [ $(DESTDIR)$(LIBDIR)/$(FILDIR) != $(BOOTLIBDIR) ]; then \
          if [ -f $(BOOTLIBDIR)/$(SUBDIR)$(LIB).$(SHAREDSUFFIX) ]; then \
            $(INSTALL) $(BOOTLIBDIR)/$(SUBDIR)$(LIB).$(SHAREDSUFFIX) \
               $(DESTDIR)$(LIBDIR)/$(FILDIR)/$(SUBDIR)$(LIB).$(SHAREDSUFFIX) && \
            chmod $(MODFILE) $(DESTDIR)$(LIBDIR)/$(FILDIR)/$(SUBDIR)$(LIB).$(SHAREDSUFFIX);\
            if [ -f $(BOOTLIBDIR)/$(SUBDIR)$(LIB).$(SHAREDSUFFIX).a ]; then \
              $(INSTALL) $(BOOTLIBDIR)/$(SUBDIR)$(LIB).$(SHAREDSUFFIX).a \
               $(DESTDIR)$(LIBDIR)/$(FILDIR)/$(SUBDIR)$(LIB).$(SHAREDSUFFIX).a && \
            chmod $(MODFILE) $(DESTDIR)$(LIBDIR)/$(FILDIR)/$(SUBDIR)$(LIB).$(SHAREDSUFFIX).a;\
            fi \
          fi \
        fi

install-link-lib:
	if [ $(DESTDIR)$(LIBDIR)/$(FILDIR) != $(BOOTLIBDIR) ]; then \
	  (cd $(DESTDIR)$(LIBDIR)/$(FILDIR)/$(SUBDIR) && \
           $(RM) -f $(LIB).a && \
           $(LN_S) $(LN).a $(LIB).a && $(RANLIB) $(LIB).a) \
        fi
	if [ "$(SHAREDLIBRARYCLOSED)" = "yes" ]; then \
          $(MAKE) install-shared-lib; \
        else \
          $(MAKE) install-shared-ln-lib; \
        fi

uninstall-lib:
	$(RM) -f $(DESTDIR)$(LIBDIR)/$(FILDIR)/$(SUBDIR)$(LIB).a
	$(RM) -f $(DESTDIR)$(LIBDIR)/$(FILDIR)/$(SUBDIR)$(LIB).$(SHAREDSUFFIX)
	$(RM) -f $(DESTDIR)$(LIBDIR)/$(FILDIR)/$(SUBDIR)$(LIB).$(SHAREDSUFFIX).a
