#-------------------------------------------------------------------------
#
# Makefile.shlib
#    Common rules for building shared libraries
#
# Copyright (c) 1998, Regents of the University of California
#
# IDENTIFICATION
#    $Header: /cvsroot/pgsql/src/Makefile.shlib,v 1.23 2000/09/17 13:02:29 petere Exp $
#
#-------------------------------------------------------------------------

# This file should be included by any Postgres module Makefile that
# wants to build a shared library (if possible for the current
# platform). A static library is also built from the same object
# files. Only one library can be built per makefile.
#
# Before including this file, the module Makefile must define these
# variables:
#
# NAME                  Name of library to build (no suffix nor "lib" prefix)
# SO_MAJOR_VERSION      Major version number to use for shared library
# SO_MINOR_VERSION      Minor version number to use for shared library
# OBJS                  List of object files to include in library
# SHLIB_LINK            If shared library relies on other libraries,
#                       additional stuff to put in its link command
# (If you want a patchlevel, include it in SO_MINOR_VERSION, e.g., "6.2".)
#
# The module Makefile must also include
# $(top_builddir)/src/Makefile.global before including this file.
# (Makefile.global sets PORTNAME and other needed symbols.)
#
# This makefile provides the following (phony) targets:
#
# all-lib               build the static and shared (if applicable) libraries
# install-lib           install the libraries into $(libdir)
# uninstall-lib         remove the libraries from $(libdir)
# clean-lib             delete the static and shared libraries from the build dir
#
# Since `all-lib' is the first rule in this file you probably want to
# have the `all' target before including this file. In the most simple
# case it would look like this:
#
#     all: all-lib
#
# Similarly, the install rule might look like
#
#     install: install-lib
#
# plus any additional things you want to install. Et cetera.
#
# Got that?  Look at src/interfaces/libpq/Makefile for an example.


# shlib is empty by default. If we know how to build a shared library
# it will contain the name of the file, otherwise it will remain
# empty. Thus `ifdef shlib' could be used in the containing make file
# to test whether shared libraries are available.
shlib := 

# For each platform we support shared libraries on, set shlib and
# update flags as needed to build a shared lib. Note we depend on
# Makefile.global (or really Makefile.port) to supply DLSUFFIX and
# other symbols.

# Try to keep the sections in some kind of order, folks...

ifeq ($(PORTNAME), aix)
  shlib			:= lib$(NAME)$(DLSUFFIX)
  SHLIB_LINK		+= -lc
endif

ifeq ($(PORTNAME), bsd)
  ifdef BSD_SHLIB
    shlib		:= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
    ifdef ELF_SYSTEM
      LDFLAGS_SL	:= -x -Bshareable -soname $(shlib)
    else
      LDFLAGS_SL	:= -x -Bshareable -Bforcearchive
    endif
    CFLAGS		+= $(CFLAGS_SL)
  endif
endif

ifeq ($(PORTNAME), bsdi)
  ifdef BSD_SHLIB
    ifeq ($(DLSUFFIX), .so)
      shlib			:= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
      LDFLAGS_SL                += -shared -soname $(shlib)
      CFLAGS			+= $(CFLAGS_SL)
    endif
    ifeq ($(DLSUFFIX), .o)
      shlib			:= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
      LD			:= shlicc
      LDFLAGS_SL	 	+= -O $(LDREL)
      CFLAGS			+= $(CFLAGS_SL)
    endif
  endif
endif

ifeq ($(PORTNAME), freebsd)
  ifdef BSD_SHLIB
    ifdef ELF_SYSTEM
      shlib		:= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
      LDFLAGS_SL	:= -x -shared -soname $(shlib)
    else
      shlib		:= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
      LDFLAGS_SL	:= -x -Bshareable -Bforcearchive
    endif
    CFLAGS		+= $(CFLAGS_SL)
  endif
endif

ifeq ($(PORTNAME), netbsd)
  ifdef BSD_SHLIB
    soname		:= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
    shlib		:= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
    ifdef ELF_SYSTEM
      LD		:= $(CC)
      LDFLAGS_SL	:= -shared -Wl,-soname -Wl,$(soname)
      ifneq ($(SHLIB_LINK),)
        LDFLAGS_SL	+= -Wl,-R$(libdir)
      endif
    else
      LDFLAGS_SL	:= -x -Bshareable -Bforcearchive
    endif
    CFLAGS		+= $(CFLAGS_SL)
  endif
endif

ifeq ($(PORTNAME), hpux)
# HPUX doesn't believe in version numbers for shlibs
  shlib			:= lib$(NAME)$(DLSUFFIX)
  LDFLAGS_SL		:= -b
  CFLAGS		+= $(CFLAGS_SL)
endif

ifeq ($(PORTNAME), irix5)
  shlib := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
  LDFLAGS_SL := -shared -rpath $(libdir) -set_version sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
  CFLAGS += $(CFLAGS_SL)
endif

ifeq ($(PORTNAME), linux)
  shlib			:= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
  LD			:= $(CC)
  LDFLAGS_SL		:= -shared -Wl,-soname,$(shlib)
  LDFLAGS_ODBC		:= -lm
  CFLAGS		+= $(CFLAGS_SL)
endif

ifeq ($(PORTNAME), solaris_i386)
  shlib			:= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
  LDFLAGS_SL		:= -G
  SHLIB_LINK		+= -ldl -lsocket -lresolv -lnsl -lm -lc
  CFLAGS		+= $(CFLAGS_SL)
endif

ifeq ($(PORTNAME), solaris_sparc)
  shlib			:= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
  LDFLAGS_SL		:= -G
  SHLIB_LINK		+= -ldl -lsocket -lresolv -lnsl -lm -lc
  CFLAGS		+= $(CFLAGS_SL)
endif

ifeq ($(PORTNAME), alpha)
  shlib			:= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
  LDFLAGS_SL		+= -shared -expect_unresolved '*'
endif

ifeq ($(PORTNAME), svr4)
  shlib			:= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
  LDFLAGS_SL		:= -G
  CFLAGS		+= $(CFLAGS_SL)
endif

ifeq ($(PORTNAME), univel)
  shlib			:= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
  LDFLAGS_SL		:= -G -z text
  CFLAGS		+= $(CFLAGS_SL)
  ifeq ($(CXX), CC)
    CXXFLAGS += -Xw
    COMPILE.cc = $(CXX) $(CXXFLAGS:ll,alloca=ll) $(CPPFLAGS) $(TARGET_ARCH) -c
  endif
endif

ifeq ($(PORTNAME), unixware)
  shlib			:= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
  LDFLAGS_SL		:= -G -z text
  CFLAGS		+= $(CFLAGS_SL)
  ifeq ($(CXX), CC)
    CXXFLAGS += -Xw
    COMPILE.cc = $(CXX) $(CXXFLAGS:ll,alloca=ll) $(CPPFLAGS) $(TARGET_ARCH) -c
  endif
endif

ifeq ($(PORTNAME), win)
  shlib			:= $(NAME)$(DLSUFFIX)
endif


# Note that in what follows, shlib is empty when not building a shared
# library.


##
## BUILD
##

.PHONY: all-lib
all-lib: lib$(NAME).a $(shlib)

# Rules to build regular and shared libraries

ifneq ($(PORTNAME), win)

lib$(NAME).a: $(OBJS)
ifdef MK_NO_LORDER
	$(AR) $(AROPT) $@ $(OBJS) 
else
	$(AR) $(AROPT) $@ `lorder $(OBJS) | tsort`
endif
	$(RANLIB) $@

endif # not win

ifdef shlib
ifneq ($(PORTNAME), win)
ifneq ($(PORTNAME), aix)

# Normal case
$(shlib): $(OBJS)
	$(LD) $(LDFLAGS_SL) -o $@ $(OBJS) $(SHLIB_LINK)
# If we're using major and minor versions, then make a symlink to major-version-only.
ifneq ($(shlib), lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION))
	rm -f lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
	$(LN_S) $(shlib) lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
endif
# Make sure we have a link to a name without any version numbers
ifneq ($(shlib), lib$(NAME)$(DLSUFFIX))
	rm -f lib$(NAME)$(DLSUFFIX)
	$(LN_S) $(shlib) lib$(NAME)$(DLSUFFIX)
endif

else # PORTNAME == aix

# AIX case
$(shlib): lib$(NAME).a
	$(MKLDEXPORT) lib$(NAME).a $(libdir) > lib$(NAME)$(EXPSUFF)
	$(LD) -H512 -bM:SRE -bI:$(top_builddir)/src/backend/$(POSTGRES_IMP) -bE:lib$(NAME)$(EXPSUFF) -o $@ $< $(LDFLAGS) $(SHLIB_LINK)

endif # PORTNAME == aix

else # PORTNAME == win

# WIN case
$(shlib) lib$(NAME).a: $(OBJS) $(top_builddir)/src/utils/dllinit.o
	$(DLLTOOL) --export-all --output-def $(NAME).def $(OBJS)
	$(DLLWRAP) -o $(shlib) --dllname $(shlib) --def $(NAME).def $(OBJS) $(top_builddir)/src/utils/dllinit.o $(DLLINIT) $(SHLIB_LINK)
	$(DLLTOOL) --dllname $(shlib) --def $(NAME).def --output-lib lib$(NAME).a

$(top_builddir)/src/utils/dllinit.o: $(top_srcdir)/src/utils/dllinit.c
	$(MAKE) -C $(top_builddir)/src/utils dllinit.o

endif # PORTNAME == win
endif # shlib


##
## INSTALL
##

.PHONY: install-lib install-lib-static install-lib-shared
install-lib: install-lib-static install-lib-shared

install-lib-static: lib$(NAME).a
	$(INSTALL_DATA) $< $(DESTDIR)$(libdir)/lib$(NAME).a

ifdef shlib
install-lib-shared: $(shlib)
	$(INSTALL_SHLIB) $< $(DESTDIR)$(libdir)/$(shlib)
ifneq ($(PORTNAME), win)
ifneq ($(shlib), lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION))
	cd $(DESTDIR)$(libdir) && \
	rm -f lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION) && \
	$(LN_S) $(shlib) lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
endif
ifneq ($(shlib), lib$(NAME)$(DLSUFFIX))
	cd $(DESTDIR)$(libdir) && \
	rm -f lib$(NAME)$(DLSUFFIX) && \
	$(LN_S) $(shlib) lib$(NAME)$(DLSUFFIX)
endif

endif # not win
endif # shlib


##
## UNINSTALL
##

.PHONY: uninstall-lib
uninstall-lib:
	rm -f $(DESTDIR)$(libdir)/lib$(NAME).a
ifdef shlib
	rm -f $(DESTDIR)$(libdir)/lib$(NAME)$(DLSUFFIX) \
	  $(DESTDIR)$(libdir)/lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION) \
	  $(DESTDIR)$(libdir)/lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
endif # shlib


##
## CLEAN
##

.PHONY: clean-lib
clean-lib:
	rm -f lib$(NAME).a
	rm -f $(shlib) lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION) lib$(NAME)$(DLSUFFIX)
ifeq ($(PORTNAME), win)
	rm -rf $(NAME).def
endif
