]> granicus.if.org Git - postgresql/blobdiff - src/Makefile.shlib
Teach the system how to use hashing for UNION. (INTERSECT/EXCEPT will follow,
[postgresql] / src / Makefile.shlib
index 441feec22a5e63b101d118edd4c33ed7fe973be2..4e795e32fe322bbe4afc2d64d3bbd8319f059b36 100644 (file)
 # Copyright (c) 1998, Regents of the University of California
 #
 # IDENTIFICATION
-#    $Header: /cvsroot/pgsql/src/Makefile.shlib,v 1.18 2000/03/08 01:58:15 momjian Exp $
+#    $PostgreSQL: pgsql/src/Makefile.shlib,v 1.116 2008/04/08 09:50: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.
-# RESTRICTION: 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, eg, "6.2".)
+# 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.
 #
-# The module Makefile must also include $(SRCDIR)/Makefile.global before
-# including this file (Makefile.global sets PORTNAME and other needed symbols).
+# Before including this file, the module Makefile must define these
+# variables:
 #
-# The first rule in this file is a rule for "all", which causes both the
-# static and shared libraries to be built (as well as all the object files).
-# If you have other files that need to be made before building object files
-# and libraries, put another rule for "all" before you include this file.
+# NAME                  Name of library to build (no suffix nor "lib" prefix)
+# 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
+# SHLIB_EXPORTS         (optional) Name of file containing list of symbols to
+#                       export
 #
-# Your install rule should look like
+# When building a shared library, the following version information
+# must also be set.  It should be omitted when building a dynamically
+# loadable module.
 #
-#      install: install-headers install-lib $(install-shlib-dep)
+# SO_MAJOR_VERSION      Major version number to use for shared library
+# SO_MINOR_VERSION      Minor version number to use for shared library
+# (If you want a patchlevel, include it in SO_MINOR_VERSION, e.g., "6.2".)
 #
-# where install-headers is only needed if you have header files to install
-# (and, of course, it has to be provided by your makefile).  The rules
-# install-lib and install-shlib are provided by this makefile --- they
-# automatically install the plain and shared libraries into $(LIBDIR).
-# install-shlib-dep is a variable that expands to install-shlib if the
-# shared library needs to be installed, empty if not.
+# Optional flags when building DLL's (only applicable to win32 and cygwin
+# platforms).
+# DLLTOOL_DEFFLAGS      Additional flags when creating the dll .def file
+# DLLTOOL_LIBFLAGS      Additional flags when creating the lib<module>.a file
+# DLLWRAP_FLAGS         Additional flags to dllwrap
 #
-# Got that?  Look at src/interfaces/libpq/Makefile.in for an example.
+# 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)
+# installdirs-lib       create installation directory $(libdir)
+# uninstall-lib         remove the libraries from $(libdir)
+# clean-lib             delete the static and shared libraries from the build dir
+# maintainer-clean-lib  delete .def files built for win32
+#
+# 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.
+#
+# While the linker allows creation of most shared libraries,
+# -Bsymbolic requires resolution of all symbols, making the
+# compiler a better choice for shared library creation on ELF platforms.
+# With the linker, -Bsymbolic requires the crt1.o startup object file.
+# bjm 2001-02-10
+
 
+COMPILER = $(CC) $(CFLAGS)
+LINK.static = $(AR) $(AROPT)
 
-# shlib and install-shlib-dep default to empty, and stay that way if we're
-# on a platform where we don't know how to build a shared library.
-shlib := 
-install-shlib-dep :=
 
-# For each platform we support shlibs on, set shlib and install-shlib-dep,
-# 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.
+
+# Insert -L from LDFLAGS after any -L already present in SHLIB_LINK
+SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) $(filter -L%, $(LDFLAGS)) $(filter-out -L%, $(SHLIB_LINK))
+
+# Need a -L-free version of LDFLAGS to use in combination with SHLIB_LINK
+LDFLAGS_NO_L = $(filter-out -L%, $(LDFLAGS))
+
+ifdef SO_MAJOR_VERSION
+# Default library naming convention used by the majority of platforms
+ifeq ($(enable_shared), yes)
+shlib          = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
+shlib_major    = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
+shlib_bare     = lib$(NAME)$(DLSUFFIX)
+endif
+# Testing the soname variable is a reliable way to determine whether a
+# linkable library is being built.
+soname         = $(shlib_major)
+else
+# Naming convention for dynamically loadable modules
+ifeq ($(enable_shared), yes)
+shlib          = $(NAME)$(DLSUFFIX)
+endif
+endif
+stlib          = lib$(NAME).a
+
+ifndef soname
+# additional flags for backend modules
+SHLIB_LINK := $(BE_DLLLIBS) $(SHLIB_LINK)
+endif
+
+# For each platform we support shared libraries on, set shlib to the
+# name of the library (if default above is not right), set
+# LINK.shared to the command to link the library,
+# and adjust SHLIB_LINK if necessary.
 
 # Try to keep the sections in some kind of order, folks...
 
+override CFLAGS += $(CFLAGS_SL)
+
 ifeq ($(PORTNAME), aix)
-  install-shlib-dep    := install-shlib
-  shlib                                := lib$(NAME)$(DLSUFFIX)
-  SHLIB_LINK           += -lc
+  ifdef SO_MAJOR_VERSION
+    shlib              = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
+  endif
+  haslibarule   = yes
+  exports_file         = lib$(NAME).exp
 endif
 
-ifeq ($(PORTNAME), bsd)
-  ifdef BSD_SHLIB
-    install-shlib-dep  := install-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
+ifeq ($(PORTNAME), darwin)
+  ifdef soname
+    # linkable library
+    DLSUFFIX           = .dylib
+    ifneq ($(SO_MAJOR_VERSION), 0)
+      version_link     = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
     endif
-    CFLAGS             += $(CFLAGS_SL)
+    LINK.shared                = $(COMPILER) -dynamiclib -install_name $(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX) $(version_link) $(exported_symbols_list) -multiply_defined suppress
+    shlib              = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
+    shlib_major                = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
+  else
+    # loadable module
+    DLSUFFIX           = .so
+    LINK.shared                = $(COMPILER) -bundle -multiply_defined suppress
+  endif
+  BUILD.exports                = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
+  exports_file         = $(SHLIB_EXPORTS:%.txt=%.list)
+  ifneq (,$(exports_file))
+    exported_symbols_list = -exported_symbols_list $(exports_file)
   endif
 endif
 
-ifeq ($(PORTNAME), bsdi)
-  ifdef BSD_SHLIB
-    ifeq ($(DLSUFFIX), .so)
-      install-shlib-dep := install-shlib
-      shlib                    := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
-      LDFLAGS_SL               += -shared
-      CFLAGS                   += $(CFLAGS_SL)
+ifeq ($(PORTNAME), openbsd)
+  ifdef ELF_SYSTEM
+    LINK.shared                = $(COMPILER) -shared
+    ifdef soname
+      LINK.shared      += -Wl,-x,-soname,$(soname)
     endif
-    ifeq ($(DLSUFFIX), .o)
-      install-shlib-dep        := install-shlib
-      shlib                    := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
-      LD                       := shlicc
-      LDFLAGS_SL               += -O $(LDREL)
-      CFLAGS                   += $(CFLAGS_SL)
+    SHLIB_LINK         += -lc
+  else
+    LINK.shared                = $(LD) -x -Bshareable -Bforcearchive
+  endif
+endif
+
+ifeq ($(PORTNAME), bsdi)
+  ifeq ($(DLSUFFIX), .so)
+    LINK.shared                = $(COMPILER) -shared
+    ifdef soname
+      LINK.shared      += -Wl,-x,-soname,$(soname)
     endif
+    SHLIB_LINK         += -lc
+  endif
+  ifeq ($(DLSUFFIX), .o)
+    LINK.shared                = shlicc -O $(LDREL)
   endif
 endif
 
 ifeq ($(PORTNAME), freebsd)
-  ifdef BSD_SHLIB
-    install-shlib-dep  := install-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
+  ifdef ELF_SYSTEM
+    ifdef SO_MAJOR_VERSION
+      shlib            = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
     endif
-    CFLAGS             += $(CFLAGS_SL)
+    LINK.shared                = $(COMPILER) -shared
+    ifdef soname
+      LINK.shared      += -Wl,-x,-soname,$(soname)
+    endif
+  else
+    ifdef SO_MAJOR_VERSION
+      shlib            = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
+    endif
+    LINK.shared                = $(LD) -x -Bshareable -Bforcearchive
   endif
 endif
 
 ifeq ($(PORTNAME), netbsd)
-  ifdef BSD_SHLIB
-    install-shlib-dep  := install-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
+  ifdef ELF_SYSTEM
+    LINK.shared                = $(COMPILER) -shared
+    ifdef soname
+      LINK.shared      += -Wl,-x,-soname,$(soname)
     endif
-    CFLAGS             += $(CFLAGS_SL)
+  else
+    LINK.shared                = $(LD) -x -Bshareable -Bforcearchive
   endif
 endif
 
 ifeq ($(PORTNAME), hpux)
-  install-shlib-dep    := install-shlib
-# HPUX doesn't believe in version numbers for shlibs
-  shlib                                := lib$(NAME)$(DLSUFFIX)
-  LDFLAGS_SL           := -b
-  CFLAGS                       += $(CFLAGS_SL)
+  ifdef SO_MAJOR_VERSION
+    shlib                      = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
+  endif
+  ifeq ($(with_gnu_ld), yes)
+    LINK.shared                = $(CC) $(LDFLAGS_NO_L) -shared
+    ifdef soname
+      LINK.shared      += -Wl,-h -Wl,$(soname)
+    endif
+  else
+    # can't use the CC-syntax rpath pattern here
+    rpath =
+    LINK.shared                = $(LD) -b
+    ifdef soname
+      LINK.shared      += +h $(soname)
+    endif
+    ifeq ($(enable_rpath), yes)
+      LINK.shared      += +b '$(rpathdir)'
+    endif
+    # On HPUX platforms, gcc is usually configured to search for libraries
+    # in /usr/local/lib, but ld won't do so.  Add an explicit -L switch so
+    # ld can find the same libraries gcc does.  Make sure it goes after any
+    # -L switches provided explicitly.
+    ifeq ($(GCC), yes)
+      SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) -L/usr/local/lib $(filter-out -L%, $(SHLIB_LINK))
+    endif
+  endif
+  # do this last so above filtering doesn't pull out -L switches in LDFLAGS
+  ifeq ($(GCC), yes)
+    SHLIB_LINK         += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
+  endif
 endif
 
-ifeq ($(PORTNAME), irix5)
-  install-shlib-dep := install-shlib
-  shlib := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
-  LDFLAGS_SL := -shared -rpath $(LIBDIR)
-  CFLAGS += $(CFLAGS_SL)
+ifeq ($(PORTNAME), irix)
+  ifdef SO_MAJOR_VERSION
+    shlib              = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
+  endif
+  LINK.shared          = $(COMPILER) -shared
+  ifdef soname
+    LINK.shared                += -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
+  endif
 endif
 
 ifeq ($(PORTNAME), linux)
-  install-shlib-dep    := install-shlib
-  shlib                                := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
-  LDFLAGS_SL           := -Bdynamic -shared -soname $(shlib)
-  LDFLAGS_ODBC         := -Bsymbolic -lc -lm
-  SHLIB_LINK           += -lc
-  CFLAGS                       += $(CFLAGS_SL)
+  LINK.shared          = $(COMPILER) -shared
+  ifdef soname
+    LINK.shared                += -Wl,-soname,$(soname)
+  endif
+  BUILD.exports                = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
+  exports_file         = $(SHLIB_EXPORTS:%.txt=%.list)
+  ifneq (,$(exports_file))
+    LINK.shared                += -Wl,--version-script=$(exports_file)
+  endif
 endif
 
-ifeq ($(PORTNAME), solaris_i386)
-  install-shlib-dep    := install-shlib
-  shlib                                := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
-  LDFLAGS_SL           := -G
-  SHLIB_LINK           += -ldl -lsocket -lresolv -lnsl -lm -lc
-  CFLAGS                       += $(CFLAGS_SL)
+ifeq ($(PORTNAME), solaris)
+  ifeq ($(GCC), yes)
+    LINK.shared                = $(COMPILER) -shared # $(COMPILER) needed for -m64
+  else
+# CFLAGS added for X86_64
+    LINK.shared                = $(CC) -G $(CFLAGS)
+  endif
+  ifdef soname
+    ifeq ($(with_gnu_ld), yes)
+      LINK.shared      += -Wl,-soname,$(soname)
+    else
+      LINK.shared      += -h $(soname)
+    endif
+  endif
 endif
 
-ifeq ($(PORTNAME), solaris_sparc)
-  install-shlib-dep    := install-shlib
-  shlib                                := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
-  LDFLAGS_SL           := -G
-  SHLIB_LINK           += -ldl -lsocket -lresolv -lnsl -lm -lc
-  CFLAGS                       += $(CFLAGS_SL)
+ifeq ($(PORTNAME), sunos4)
+  LINK.shared          = $(LD) -assert pure-text -Bdynamic
+endif
+ifeq ($(PORTNAME), osf)
+  LINK.shared          = $(LD) -shared -expect_unresolved '*'
 endif
 
-ifeq ($(PORTNAME), alpha)
-  install-shlib-dep    := install-shlib
-  shlib                        := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
-  LDFLAGS_SL           += -shared -expect_unresolved '*'
+ifeq ($(PORTNAME), sco)
+  ifeq ($(GCC), yes)
+    LINK.shared                = $(CC) -shared
+  else
+    LINK.shared                = $(CC) -G
+    endif
+  LINK.shared          += -Wl,-z,text
+  ifdef soname
+    LINK.shared                += -Wl,-h,$(soname)
+  endif
 endif
 
 ifeq ($(PORTNAME), svr4)
-  install-shlib-dep    := install-shlib
-  shlib                                := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
-  LDFLAGS_SL           := -G
-  CFLAGS                       += $(CFLAGS_SL)
+  LINK.shared          = $(LD) -G
 endif
 
 ifeq ($(PORTNAME), univel)
-  install-shlib-dep    := install-shlib
-  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
+  LINK.shared          = $(LD) -G -z text
 endif
 
 ifeq ($(PORTNAME), unixware)
-  install-shlib-dep    := install-shlib
-  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
+  ifeq ($(GCC), yes)
+    LINK.shared                = $(CC) -shared
+  else
+    LINK.shared                = $(CC) -G
+  endif
+  LINK.shared          += -Wl,-z,text
+  ifdef soname
+    LINK.shared                += -Wl,-h,$(soname)
   endif
 endif
 
-ifeq ($(PORTNAME), win)
-  install-shlib-dep    := install-shlib
-  shlib                                := $(NAME)$(DLSUFFIX)
+ifeq ($(PORTNAME), cygwin)
+  ifdef SO_MAJOR_VERSION
+    shlib              = cyg$(NAME)$(DLSUFFIX)
+  endif
+  haslibarule   = yes
 endif
 
-# Default target definition.  Note shlib is empty if not building a shlib.
+ifeq ($(PORTNAME), win32)
+  ifdef SO_MAJOR_VERSION
+    shlib              = lib$(NAME)$(DLSUFFIX)
+  endif
+  haslibarule   = yes
+endif
 
-all: lib$(NAME).a $(shlib)
+ifeq ($(enable_rpath), yes)
+SHLIB_LINK += $(rpath)
+endif
 
-# 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`
+##
+## BUILD
+##
+
+.PHONY: all-lib all-static-lib all-shared-lib
+
+all-lib: all-shared-lib
+ifdef soname
+# no static library when building a dynamically loadable module
+all-lib: all-static-lib
 endif
+
+all-static-lib: $(stlib)
+
+all-shared-lib: $(shlib)
+
+ifndef haslibarule
+$(stlib): $(OBJS)
+       $(LINK.static) $@ $^
        $(RANLIB) $@
+endif #haslibarule
 
-endif
+ifeq ($(enable_shared), yes)
 
-ifneq ($(shlib),)
-ifneq ($(PORTNAME), win)
+ifeq (,$(filter cygwin win32,$(PORTNAME)))
 ifneq ($(PORTNAME), aix)
 
 # Normal case
 $(shlib): $(OBJS)
-       $(LD) $(LDFLAGS_SL) -o $@ $(OBJS) $(SHLIB_LINK)
-       if [ "$(shlib)" != "lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)" ]; then \
-               rm -f lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION); \
-               $(LN_S) $(shlib) lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION); \
-       fi
-       if [ "$(shlib)" != "lib$(NAME)$(DLSUFFIX)" ]; then \
-               rm -f lib$(NAME)$(DLSUFFIX); \
-               $(LN_S) $(shlib) lib$(NAME)$(DLSUFFIX); \
-       fi
+       $(LINK.shared) $(LDFLAGS_SL) $(OBJS) $(SHLIB_LINK) -o $@
+ifdef shlib_major
+# If we're using major and minor versions, then make a symlink to major-version-only.
+ifneq ($(shlib), $(shlib_major))
+       rm -f $(shlib_major)
+       $(LN_S) $(shlib) $(shlib_major)
+endif
+# Make sure we have a link to a name without any version numbers
+ifneq ($(shlib), $(shlib_bare))
+       rm -f $(shlib_bare)
+       $(LN_S) $(shlib) $(shlib_bare)
+endif
+endif # shlib_major
+
+# Where possible, restrict the symbols exported by the library to just the
+# official list, so as to avoid unintentional ABI changes.  On recent Darwin
+# this also quiets multiply-defined-symbol warnings in programs that use
+# libpgport along with libpq.
+ifneq (,$(SHLIB_EXPORTS))
+ifdef BUILD.exports
+$(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
+
+$(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
+       $(BUILD.exports)
+endif
+endif
 
-else
+else # PORTNAME == aix
 
 # AIX case
-$(shlib): lib$(NAME).a
-       $(MKLDEXPORT) lib$(NAME).a $(LIBDIR) > lib$(NAME)$(EXPSUFF)
-       $(LD) -H512 -bM:SRE -bI:$(SRCDIR)/backend/$(POSTGRES_IMP) -bE:lib$(NAME)$(EXPSUFF) -o $@ $< $(LDFLAGS) $(SHLIB_LINK)
+$(shlib) $(stlib): $(OBJS)
+       $(LINK.static) $(stlib) $^
+       $(RANLIB) $(stlib)
+       $(MKLDEXPORT) $(stlib) >$(exports_file)
+       $(COMPILER) $(LDFLAGS_NO_L) $(LDFLAGS_SL) -o $(shlib) $(stlib) -Wl,-bE:$(exports_file) $(SHLIB_LINK)
+       rm -f $(stlib)
+       $(AR) $(AROPT) $(stlib) $(shlib)
 
-endif
+endif # PORTNAME == aix
 
-else
+else # PORTNAME == cygwin || PORTNAME == win32
+
+# Cygwin or Win32 case
 
-# WIN case
-$(shlib) lib$(NAME).a: $(OBJS) $(SRCDIR)/utils/dllinit.o
-       $(DLLTOOL) --export-all --output-def $(NAME).def $(OBJS)
-       $(DLLWRAP) -o $(shlib) --dllname $(shlib) --def $(NAME).def $(OBJS) $(SRCDIR)/utils/dllinit.o $(DLLINIT) $(SHLIB_LINK)
-       $(DLLTOOL) --dllname $(shlib) --def $(NAME).def --output-lib lib$(NAME).a
+DLL_DEFFILE = lib$(NAME)dll.def
 
-$(SRCDIR)/utils/dllinit.o: $(SRCDIR)/utils/dllinit.c
-       $(MAKE) -C $(SRCDIR)/utils dllinit.o
+# If SHLIB_EXPORTS is set, the rules below will build a .def file from
+# that.  Else we build a temporary one here.
+ifeq (,$(SHLIB_EXPORTS))
+exports_file = $(DLL_DEFFILE)
 
+$(exports_file): $(OBJS)
+       $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $@ $^
 endif
+
+$(shlib): $(OBJS) $(DLL_DEFFILE)
+       $(DLLWRAP) $(LDFLAGS_SL) -o $@ --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
+
+$(stlib): $(shlib) $(DLL_DEFFILE)
+       $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib $@
+
+endif # PORTNAME == cygwin || PORTNAME == win32
+
+endif # enable_shared
+
+
+# We need several not-quite-identical variants of .DEF files to build
+# DLLs for Windows.  These are made from the single source file
+# exports.txt.  Since we can't assume that Windows boxes will have
+# sed, the .DEF files are always built and included in distribution
+# tarballs.
+
+ifneq (,$(SHLIB_EXPORTS))
+distprep: $(srcdir)/lib$(NAME)dll.def $(srcdir)/lib$(NAME)ddll.def $(srcdir)/blib$(NAME)dll.def
+
+UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
+
+$(srcdir)/lib$(NAME)dll.def: $(SHLIB_EXPORTS)
+       echo '; DEF file for MS VC++' >$@
+       echo 'LIBRARY LIB$(UC_NAME)' >>$@
+       echo 'EXPORTS' >>$@
+       sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/    \1@ \2/' $< >>$@
+
+$(srcdir)/lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
+       echo '; DEF file for MS VC++' >$@
+       echo 'LIBRARY LIB$(UC_NAME)D' >>$@
+       echo 'EXPORTS' >>$@
+       sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/    \1@ \2/' $< >>$@
+
+$(srcdir)/blib$(NAME)dll.def: $(SHLIB_EXPORTS)
+       echo '; DEF file for Borland C++ Builder' >$@
+       echo 'LIBRARY BLIB$(UC_NAME)' >>$@
+       echo 'EXPORTS' >>$@
+       sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/    _\1@ \2/' $< >>$@
+       echo >>$@
+       echo '; Aliases for MS compatible names' >> $@
+       sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/    \1= _\1/' $< | sed 's/ *$$//' >>$@
+endif # SHLIB_EXPORTS
+
+
+##
+## INSTALL
+##
+
+.PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
+install-lib: install-lib-shared
+ifdef soname
+install-lib: install-lib-static
 endif
 
-# Rules to install regular and shared libraries
+install-lib-static: $(stlib) installdirs-lib
+       $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
+ifeq ($(PORTNAME), darwin)
+       cd '$(DESTDIR)$(libdir)' && \
+       ranlib $(stlib)
+endif
 
-.PHONY: all install-lib install-shlib
+ifeq ($(enable_shared), yes)
+install-lib-shared: $(shlib) installdirs-lib
+ifdef soname
+# we don't install $(shlib) on AIX
+ifneq ($(PORTNAME), aix)
+       $(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
+ifneq ($(PORTNAME), cygwin)
+ifneq ($(PORTNAME), win32)
+ifneq ($(shlib), $(shlib_major))
+       cd '$(DESTDIR)$(libdir)' && \
+       rm -f $(shlib_major) && \
+       $(LN_S) $(shlib) $(shlib_major)
+endif
+ifneq ($(shlib), $(shlib_bare))
+       cd '$(DESTDIR)$(libdir)' && \
+       rm -f $(shlib_bare) && \
+       $(LN_S) $(shlib) $(shlib_bare)
+endif
+endif # not win32
+endif # not cygwin
+endif # not aix
+else # no soname
+       $(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
+endif
+else # not enable_shared
+ifndef soname
+install-lib-shared:
+       @echo "*****"; \
+        echo "* Module $(NAME) was not installed due to lack of shared library support."; \
+        echo "*****"
+endif
+endif # enable_shared
 
-install-lib: lib$(NAME).a
-       $(INSTALL) $(INSTL_LIB_OPTS) lib$(NAME).a $(LIBDIR)/lib$(NAME).a
 
-install-shlib: $(shlib)
-       $(INSTALL) $(INSTL_SHLIB_OPTS) $(shlib) $(LIBDIR)/$(shlib)
-ifneq ($(PORTNAME), win)
-       if [ "$(shlib)" != "lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)" ]; then \
-               cd $(LIBDIR); \
-               rm -f lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION); \
-               $(LN_S) $(shlib) lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION); \
-       fi
-       if [ "$(shlib)" != "lib$(NAME)$(DLSUFFIX)" ]; then \
-               cd $(LIBDIR); \
-               rm -f lib$(NAME)$(DLSUFFIX); \
-               $(LN_S) $(shlib) lib$(NAME)$(DLSUFFIX); \
-       fi
+installdirs-lib:
+ifdef soname
+       $(mkinstalldirs) '$(DESTDIR)$(libdir)'
+else
+       $(mkinstalldirs) '$(DESTDIR)$(pkglibdir)'
 endif
 
-# Rule to delete shared library during "make clean"
 
-.PHONY: clean-shlib
+##
+## UNINSTALL
+##
+
+.PHONY: uninstall-lib
+uninstall-lib:
+ifdef soname
+       rm -f '$(DESTDIR)$(libdir)/$(stlib)'
+ifeq ($(enable_shared), yes)
+       rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
+         '$(DESTDIR)$(libdir)/$(shlib_major)' \
+         '$(DESTDIR)$(libdir)/$(shlib)'
+endif # enable_shared
+else # no soname
+       rm -f '$(DESTDIR)$(pkglibdir)/$(shlib)'
+endif # no soname
+
+
+##
+## CLEAN
+##
+
+.PHONY: clean-lib
+clean-lib:
+       rm -f $(shlib) $(shlib_bare) $(shlib_major) $(stlib) $(exports_file)
 
-clean-shlib:
-       rm -f $(shlib) lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION) lib$(NAME)$(DLSUFFIX)
-ifeq ($(PORTNAME), win)
-       rm -rf $(NAME).def
+ifneq (,$(SHLIB_EXPORTS))
+maintainer-clean-lib:
+       rm -f $(srcdir)/lib$(NAME)dll.def $(srcdir)/lib$(NAME)ddll.def $(srcdir)/blib$(NAME)dll.def
 endif