1 #-------------------------------------------------------------------------
4 # Common rules for building shared libraries
6 # Copyright (c) 1998, Regents of the University of California
9 # $PostgreSQL: pgsql/src/Makefile.shlib,v 1.124 2010/07/05 18:54:37 tgl Exp $
11 #-------------------------------------------------------------------------
13 # This file should be included by any Postgres module Makefile that
14 # wants to build a shared library (if possible for the current
15 # platform). A static library is also built from the same object
16 # files. Only one library can be built per makefile.
18 # Before including this file, the module Makefile must define these
21 # NAME Name of library to build (no suffix nor "lib" prefix)
22 # OBJS List of object files to include in library
23 # SHLIB_LINK If shared library relies on other libraries,
24 # additional stuff to put in its link command
25 # SHLIB_EXPORTS (optional) Name of file containing list of symbols to
26 # export, in the format "function_name number"
28 # When building a shared library, the following version information
29 # must also be set. It should be omitted when building a dynamically
32 # SO_MAJOR_VERSION Major version number to use for shared library
33 # SO_MINOR_VERSION Minor version number to use for shared library
34 # (If you want a patchlevel, include it in SO_MINOR_VERSION, e.g., "6.2".)
36 # Optional flags when building DLL's (only applicable to win32 and cygwin
38 # DLLTOOL_DEFFLAGS Additional flags when creating the dll .def file
39 # DLLTOOL_LIBFLAGS Additional flags when creating the lib<module>.a file
40 # DLLWRAP_FLAGS Additional flags to dllwrap
42 # The module Makefile must also include
43 # $(top_builddir)/src/Makefile.global before including this file.
44 # (Makefile.global sets PORTNAME and other needed symbols.)
46 # This makefile provides the following (phony) targets:
48 # all-lib build the static and shared (if applicable) libraries
49 # install-lib install the libraries into $(libdir)
50 # installdirs-lib create installation directory $(libdir)
51 # uninstall-lib remove the libraries from $(libdir)
52 # clean-lib delete the static and shared libraries from the build dir
53 # maintainer-clean-lib delete .def files built for win32
55 # Since `all-lib' is the first rule in this file you probably want to
56 # have the `all' target before including this file. In the most simple
57 # case it would look like this:
61 # Similarly, the install rule might look like
63 # install: install-lib
65 # plus any additional things you want to install. Et cetera.
67 # Got that? Look at src/interfaces/libpq/Makefile for an example.
69 # While the linker allows creation of most shared libraries,
70 # -Bsymbolic requires resolution of all symbols, making the
71 # compiler a better choice for shared library creation on ELF platforms.
72 # With the linker, -Bsymbolic requires the crt1.o startup object file.
76 COMPILER = $(CC) $(CFLAGS)
77 LINK.static = $(AR) $(AROPT)
81 # Automatically append LDFLAGS and LDFLAGS_SL to SHLIB_LINK
82 SHLIB_LINK += $(LDFLAGS) $(LDFLAGS_SL)
84 ifdef SO_MAJOR_VERSION
85 # Default library naming convention used by the majority of platforms
86 ifeq ($(enable_shared), yes)
87 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
88 shlib_major = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
89 shlib_bare = lib$(NAME)$(DLSUFFIX)
91 # Testing the soname variable is a reliable way to determine whether a
92 # linkable library is being built.
93 soname = $(shlib_major)
95 # Naming convention for dynamically loadable modules
96 ifeq ($(enable_shared), yes)
97 shlib = $(NAME)$(DLSUFFIX)
103 # additional flags for backend modules
104 SHLIB_LINK += $(BE_DLLLIBS)
107 # For each platform we support shared libraries on, set shlib to the
108 # name of the library (if default above is not right), set
109 # LINK.shared to the command to link the library,
110 # and adjust SHLIB_LINK if necessary.
112 # Try to keep the sections in some kind of order, folks...
114 override CFLAGS += $(CFLAGS_SL)
115 ifdef SO_MAJOR_VERSION
116 # libraries ought to use this to refer to versioned gettext domain names
117 override CPPFLAGS += -DSO_MAJOR_VERSION=$(SO_MAJOR_VERSION)
120 ifeq ($(PORTNAME), aix)
121 ifdef SO_MAJOR_VERSION
122 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
125 exports_file = lib$(NAME).exp
128 ifeq ($(PORTNAME), darwin)
132 ifneq ($(SO_MAJOR_VERSION), 0)
133 version_link = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
135 LINK.shared = $(COMPILER) -dynamiclib -install_name $(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX) $(version_link) $(exported_symbols_list) -multiply_defined suppress
136 shlib = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
137 shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
141 LINK.shared = $(COMPILER) -bundle -multiply_defined suppress
143 BUILD.exports = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
144 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
145 ifneq (,$(exports_file))
146 exported_symbols_list = -exported_symbols_list $(exports_file)
150 ifeq ($(PORTNAME), openbsd)
152 LINK.shared = $(COMPILER) -shared
154 LINK.shared += -Wl,-x,-soname,$(soname)
158 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
162 ifeq ($(PORTNAME), bsdi)
163 ifeq ($(DLSUFFIX), .so)
164 LINK.shared = $(COMPILER) -shared
166 LINK.shared += -Wl,-x,-soname,$(soname)
170 ifeq ($(DLSUFFIX), .o)
171 LINK.shared = shlicc -O $(LDREL)
175 ifeq ($(PORTNAME), freebsd)
177 ifdef SO_MAJOR_VERSION
178 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
180 LINK.shared = $(COMPILER) -shared
182 LINK.shared += -Wl,-x,-soname,$(soname)
185 ifdef SO_MAJOR_VERSION
186 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
188 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
192 ifeq ($(PORTNAME), netbsd)
194 LINK.shared = $(COMPILER) -shared
196 LINK.shared += -Wl,-x,-soname,$(soname)
199 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
203 ifeq ($(PORTNAME), hpux)
204 ifdef SO_MAJOR_VERSION
205 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
207 ifeq ($(with_gnu_ld), yes)
208 LINK.shared = $(CC) -shared
210 LINK.shared += -Wl,-h -Wl,$(soname)
213 LINK.shared = $(LD) -b
215 LINK.shared += +h $(soname)
217 # can't use the CC-syntax rpath pattern here, so instead:
219 ifeq ($(enable_rpath), yes)
220 LINK.shared += +b '$(rpathdir)'
222 # On HPUX platforms, gcc is usually configured to search for libraries
223 # in /usr/local/lib, but ld won't do so. Add an explicit -L switch so
224 # ld can find the same libraries gcc does. Make sure it goes after any
225 # -L switches provided explicitly.
227 SHLIB_LINK += -L/usr/local/lib
230 # And we need to link with libgcc, too
232 SHLIB_LINK += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
236 ifeq ($(PORTNAME), irix)
237 ifdef SO_MAJOR_VERSION
238 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
240 LINK.shared = $(COMPILER) -shared
242 LINK.shared += -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
246 ifeq ($(PORTNAME), linux)
247 LINK.shared = $(COMPILER) -shared
249 LINK.shared += -Wl,-soname,$(soname)
251 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
252 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
253 ifneq (,$(exports_file))
254 LINK.shared += -Wl,--version-script=$(exports_file)
258 ifeq ($(PORTNAME), solaris)
260 LINK.shared = $(COMPILER) -shared
262 LINK.shared = $(COMPILER) -G
265 ifeq ($(with_gnu_ld), yes)
266 LINK.shared += -Wl,-soname,$(soname)
268 LINK.shared += -h $(soname)
273 ifeq ($(PORTNAME), sunos4)
274 LINK.shared = $(LD) -assert pure-text -Bdynamic
277 ifeq ($(PORTNAME), osf)
278 LINK.shared = $(LD) -shared -expect_unresolved '*'
281 ifeq ($(PORTNAME), sco)
283 LINK.shared = $(CC) -shared
285 LINK.shared = $(CC) -G
287 LINK.shared += -Wl,-z,text
289 LINK.shared += -Wl,-h,$(soname)
293 ifeq ($(PORTNAME), svr4)
294 LINK.shared = $(LD) -G
297 ifeq ($(PORTNAME), univel)
298 LINK.shared = $(LD) -G -z text
301 ifeq ($(PORTNAME), unixware)
303 LINK.shared = $(CC) -shared
305 LINK.shared = $(CC) -G
307 LINK.shared += -Wl,-z,text
309 LINK.shared += -Wl,-h,$(soname)
313 ifeq ($(PORTNAME), cygwin)
314 ifdef SO_MAJOR_VERSION
315 shlib = cyg$(NAME)$(DLSUFFIX)
320 ifeq ($(PORTNAME), win32)
321 ifdef SO_MAJOR_VERSION
322 shlib = lib$(NAME)$(DLSUFFIX)
333 .PHONY: all-lib all-static-lib all-shared-lib
335 all-lib: all-shared-lib
337 # no static library when building a dynamically loadable module
338 all-lib: all-static-lib
341 all-static-lib: $(stlib)
343 all-shared-lib: $(shlib)
351 ifeq ($(enable_shared), yes)
353 ifeq (,$(filter cygwin win32,$(PORTNAME)))
354 ifneq ($(PORTNAME), aix)
358 $(LINK.shared) -o $@ $(OBJS) $(SHLIB_LINK)
360 # If we're using major and minor versions, then make a symlink to major-version-only.
361 ifneq ($(shlib), $(shlib_major))
363 $(LN_S) $(shlib) $(shlib_major)
365 # Make sure we have a link to a name without any version numbers
366 ifneq ($(shlib), $(shlib_bare))
368 $(LN_S) $(shlib) $(shlib_bare)
372 # Where possible, restrict the symbols exported by the library to just the
373 # official list, so as to avoid unintentional ABI changes. On recent Darwin
374 # this also quiets multiply-defined-symbol warnings in programs that use
375 # libpgport along with libpq.
376 ifneq (,$(SHLIB_EXPORTS))
378 $(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
380 $(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
385 else # PORTNAME == aix
388 $(shlib) $(stlib): $(OBJS)
389 $(LINK.static) $(stlib) $^
391 $(MKLDEXPORT) $(stlib) >$(exports_file)
392 $(COMPILER) -o $(shlib) $(stlib) -Wl,-bE:$(exports_file) $(SHLIB_LINK)
394 $(AR) $(AROPT) $(stlib) $(shlib)
396 endif # PORTNAME == aix
398 else # PORTNAME == cygwin || PORTNAME == win32
400 # Cygwin or Win32 case
402 # If SHLIB_EXPORTS is set, the rules below will build a .def file from
403 # that. Else we build a temporary one here.
404 ifeq (,$(SHLIB_EXPORTS))
405 DLL_DEFFILE = lib$(NAME)dll.def
406 exports_file = $(DLL_DEFFILE)
408 $(exports_file): $(OBJS)
409 $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $@ $^
411 DLL_DEFFILE = lib$(NAME)dll.def
414 $(shlib): $(OBJS) $(DLL_DEFFILE)
415 $(DLLWRAP) -o $@ --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
417 $(stlib): $(shlib) $(DLL_DEFFILE)
418 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib $@
420 endif # PORTNAME == cygwin || PORTNAME == win32
422 endif # enable_shared
425 # We need several not-quite-identical variants of .DEF files to build
426 # DLLs for Windows. These are made from the single source file
427 # exports.txt. Since we can't assume that Windows boxes will have
428 # sed, the .DEF files are always built and included in distribution
431 ifneq (,$(SHLIB_EXPORTS))
432 distprep: lib$(NAME)dll.def lib$(NAME)ddll.def blib$(NAME)dll.def
434 UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
436 lib$(NAME)dll.def: $(SHLIB_EXPORTS)
437 echo '; DEF file for MS VC++' >$@
438 echo 'LIBRARY LIB$(UC_NAME)' >>$@
440 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
442 lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
443 echo '; DEF file for MS VC++' >$@
444 echo 'LIBRARY LIB$(UC_NAME)D' >>$@
446 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
448 blib$(NAME)dll.def: $(SHLIB_EXPORTS)
449 echo '; DEF file for Borland C++ Builder' >$@
450 echo 'LIBRARY BLIB$(UC_NAME)' >>$@
452 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ _\1@ \2/' $< >>$@
454 echo '; Aliases for MS compatible names' >> $@
455 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1= _\1/' $< | sed 's/ *$$//' >>$@
456 endif # SHLIB_EXPORTS
463 .PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
464 install-lib: install-lib-shared
466 install-lib: install-lib-static
469 install-lib-static: $(stlib) installdirs-lib
470 $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
471 ifeq ($(PORTNAME), darwin)
472 cd '$(DESTDIR)$(libdir)' && \
476 ifeq ($(enable_shared), yes)
477 install-lib-shared: $(shlib) installdirs-lib
479 # we don't install $(shlib) on AIX
480 # (see http://archives.postgresql.org/message-id/52EF20B2E3209443BC37736D00C3C1380A6E79FE@EXADV1.host.magwien.gv.at)
481 ifneq ($(PORTNAME), aix)
482 $(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
483 ifneq ($(PORTNAME), cygwin)
484 ifneq ($(PORTNAME), win32)
485 ifneq ($(shlib), $(shlib_major))
486 cd '$(DESTDIR)$(libdir)' && \
487 rm -f $(shlib_major) && \
488 $(LN_S) $(shlib) $(shlib_major)
490 ifneq ($(shlib), $(shlib_bare))
491 cd '$(DESTDIR)$(libdir)' && \
492 rm -f $(shlib_bare) && \
493 $(LN_S) $(shlib) $(shlib_bare)
499 $(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
501 else # not enable_shared
505 echo "* Module $(NAME) was not installed due to lack of shared library support."; \
508 endif # enable_shared
513 $(MKDIR_P) '$(DESTDIR)$(libdir)'
515 $(MKDIR_P) '$(DESTDIR)$(pkglibdir)'
523 .PHONY: uninstall-lib
526 rm -f '$(DESTDIR)$(libdir)/$(stlib)'
527 ifeq ($(enable_shared), yes)
528 rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
529 '$(DESTDIR)$(libdir)/$(shlib_major)' \
530 '$(DESTDIR)$(libdir)/$(shlib)'
531 endif # enable_shared
533 rm -f '$(DESTDIR)$(pkglibdir)/$(shlib)'
543 rm -f $(shlib) $(shlib_bare) $(shlib_major) $(stlib) $(exports_file)
545 ifneq (,$(SHLIB_EXPORTS))
546 maintainer-clean-lib:
547 rm -f lib$(NAME)dll.def lib$(NAME)ddll.def blib$(NAME)dll.def