1 #-------------------------------------------------------------------------
4 # Common rules for building shared libraries
6 # Copyright (c) 1998, Regents of the University of California
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
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 # Insert -L from LDFLAGS after any -L already present in SHLIB_LINK
82 SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) $(filter -L%, $(LDFLAGS)) $(filter-out -L%, $(SHLIB_LINK))
84 # Need a -L-free version of LDFLAGS to use in combination with SHLIB_LINK
85 LDFLAGS_NO_L = $(filter-out -L%, $(LDFLAGS))
87 ifdef SO_MAJOR_VERSION
88 # Default library naming convention used by the majority of platforms
89 ifeq ($(enable_shared), yes)
90 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
91 shlib_major = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
92 shlib_bare = lib$(NAME)$(DLSUFFIX)
94 # Testing the soname variable is a reliable way to determine whether a
95 # linkable library is being built.
96 soname = $(shlib_major)
98 # Naming convention for dynamically loadable modules
99 ifeq ($(enable_shared), yes)
100 shlib = $(NAME)$(DLSUFFIX)
106 # additional flags for backend modules
107 SHLIB_LINK := $(BE_DLLLIBS) $(SHLIB_LINK)
110 # For each platform we support shared libraries on, set shlib to the
111 # name of the library (if default above is not right), set
112 # LINK.shared to the command to link the library,
113 # and adjust SHLIB_LINK if necessary.
115 # Try to keep the sections in some kind of order, folks...
117 override CFLAGS += $(CFLAGS_SL)
118 ifdef SO_MAJOR_VERSION
119 # libraries ought to use this to refer to versioned gettext domain names
120 override CPPFLAGS += -DSO_MAJOR_VERSION=$(SO_MAJOR_VERSION)
123 ifeq ($(PORTNAME), aix)
124 ifdef SO_MAJOR_VERSION
125 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
128 exports_file = lib$(NAME).exp
131 ifeq ($(PORTNAME), darwin)
135 ifneq ($(SO_MAJOR_VERSION), 0)
136 version_link = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
138 LINK.shared = $(COMPILER) -dynamiclib -install_name $(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX) $(version_link) $(exported_symbols_list) -multiply_defined suppress
139 shlib = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
140 shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
144 LINK.shared = $(COMPILER) -bundle -multiply_defined suppress
146 BUILD.exports = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
147 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
148 ifneq (,$(exports_file))
149 exported_symbols_list = -exported_symbols_list $(exports_file)
153 ifeq ($(PORTNAME), openbsd)
155 LINK.shared = $(COMPILER) -shared
157 LINK.shared += -Wl,-x,-soname,$(soname)
161 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
165 ifeq ($(PORTNAME), bsdi)
166 ifeq ($(DLSUFFIX), .so)
167 LINK.shared = $(COMPILER) -shared
169 LINK.shared += -Wl,-x,-soname,$(soname)
173 ifeq ($(DLSUFFIX), .o)
174 LINK.shared = shlicc -O $(LDREL)
178 ifeq ($(PORTNAME), freebsd)
180 ifdef SO_MAJOR_VERSION
181 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
183 LINK.shared = $(COMPILER) -shared
185 LINK.shared += -Wl,-x,-soname,$(soname)
188 ifdef SO_MAJOR_VERSION
189 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
191 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
195 ifeq ($(PORTNAME), netbsd)
197 LINK.shared = $(COMPILER) -shared
199 LINK.shared += -Wl,-x,-soname,$(soname)
202 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
206 ifeq ($(PORTNAME), hpux)
207 ifdef SO_MAJOR_VERSION
208 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
210 ifeq ($(with_gnu_ld), yes)
211 LINK.shared = $(CC) $(LDFLAGS_NO_L) -shared
213 LINK.shared += -Wl,-h -Wl,$(soname)
216 # can't use the CC-syntax rpath pattern here
218 LINK.shared = $(LD) -b
220 LINK.shared += +h $(soname)
222 ifeq ($(enable_rpath), yes)
223 LINK.shared += +b '$(rpathdir)'
225 # On HPUX platforms, gcc is usually configured to search for libraries
226 # in /usr/local/lib, but ld won't do so. Add an explicit -L switch so
227 # ld can find the same libraries gcc does. Make sure it goes after any
228 # -L switches provided explicitly.
230 SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) -L/usr/local/lib $(filter-out -L%, $(SHLIB_LINK))
233 # do this last so above filtering doesn't pull out -L switches in LDFLAGS
235 SHLIB_LINK += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
239 ifeq ($(PORTNAME), irix)
240 ifdef SO_MAJOR_VERSION
241 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
243 LINK.shared = $(COMPILER) -shared
245 LINK.shared += -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
249 ifeq ($(PORTNAME), linux)
250 LINK.shared = $(COMPILER) -shared
252 LINK.shared += -Wl,-soname,$(soname)
254 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
255 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
256 ifneq (,$(exports_file))
257 LINK.shared += -Wl,--version-script=$(exports_file)
261 ifeq ($(PORTNAME), solaris)
263 LINK.shared = $(COMPILER) -shared
265 LINK.shared = $(COMPILER) -G
268 ifeq ($(with_gnu_ld), yes)
269 LINK.shared += -Wl,-soname,$(soname)
271 LINK.shared += -h $(soname)
276 ifeq ($(PORTNAME), sunos4)
277 LINK.shared = $(LD) -assert pure-text -Bdynamic
280 ifeq ($(PORTNAME), osf)
281 LINK.shared = $(LD) -shared -expect_unresolved '*'
284 ifeq ($(PORTNAME), sco)
286 LINK.shared = $(CC) -shared
288 LINK.shared = $(CC) -G
290 LINK.shared += -Wl,-z,text
292 LINK.shared += -Wl,-h,$(soname)
296 ifeq ($(PORTNAME), svr4)
297 LINK.shared = $(LD) -G
300 ifeq ($(PORTNAME), univel)
301 LINK.shared = $(LD) -G -z text
304 ifeq ($(PORTNAME), unixware)
306 LINK.shared = $(CC) -shared
308 LINK.shared = $(CC) -G
310 LINK.shared += -Wl,-z,text
312 LINK.shared += -Wl,-h,$(soname)
316 ifeq ($(PORTNAME), cygwin)
317 ifdef SO_MAJOR_VERSION
318 shlib = cyg$(NAME)$(DLSUFFIX)
323 ifeq ($(PORTNAME), win32)
324 ifdef SO_MAJOR_VERSION
325 shlib = lib$(NAME)$(DLSUFFIX)
330 ifeq ($(enable_rpath), yes)
331 SHLIB_LINK += $(rpath)
340 .PHONY: all-lib all-static-lib all-shared-lib
342 all-lib: all-shared-lib
344 # no static library when building a dynamically loadable module
345 all-lib: all-static-lib
348 all-static-lib: $(stlib)
350 all-shared-lib: $(shlib)
358 ifeq ($(enable_shared), yes)
360 ifeq (,$(filter cygwin win32,$(PORTNAME)))
361 ifneq ($(PORTNAME), aix)
365 $(LINK.shared) $(LDFLAGS_SL) $(OBJS) $(SHLIB_LINK) -o $@
367 # If we're using major and minor versions, then make a symlink to major-version-only.
368 ifneq ($(shlib), $(shlib_major))
370 $(LN_S) $(shlib) $(shlib_major)
372 # Make sure we have a link to a name without any version numbers
373 ifneq ($(shlib), $(shlib_bare))
375 $(LN_S) $(shlib) $(shlib_bare)
379 # Where possible, restrict the symbols exported by the library to just the
380 # official list, so as to avoid unintentional ABI changes. On recent Darwin
381 # this also quiets multiply-defined-symbol warnings in programs that use
382 # libpgport along with libpq.
383 ifneq (,$(SHLIB_EXPORTS))
385 $(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
387 $(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
392 else # PORTNAME == aix
395 $(shlib) $(stlib): $(OBJS)
396 $(LINK.static) $(stlib) $^
398 $(MKLDEXPORT) $(stlib) >$(exports_file)
399 $(COMPILER) $(LDFLAGS_NO_L) $(LDFLAGS_SL) -o $(shlib) $(stlib) -Wl,-bE:$(exports_file) $(SHLIB_LINK)
401 $(AR) $(AROPT) $(stlib) $(shlib)
403 endif # PORTNAME == aix
405 else # PORTNAME == cygwin || PORTNAME == win32
407 # Cygwin or Win32 case
409 # If SHLIB_EXPORTS is set, the rules below will build a .def file from
410 # that. Else we build a temporary one here.
411 ifeq (,$(SHLIB_EXPORTS))
412 DLL_DEFFILE = lib$(NAME)dll.def
413 exports_file = $(DLL_DEFFILE)
415 $(exports_file): $(OBJS)
416 $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $@ $^
418 DLL_DEFFILE = $(srcdir)/lib$(NAME)dll.def
421 $(shlib): $(OBJS) $(DLL_DEFFILE)
422 $(DLLWRAP) $(LDFLAGS_SL) -o $@ --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
424 $(stlib): $(shlib) $(DLL_DEFFILE)
425 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib $@
427 endif # PORTNAME == cygwin || PORTNAME == win32
429 endif # enable_shared
432 # We need several not-quite-identical variants of .DEF files to build
433 # DLLs for Windows. These are made from the single source file
434 # exports.txt. Since we can't assume that Windows boxes will have
435 # sed, the .DEF files are always built and included in distribution
438 ifneq (,$(SHLIB_EXPORTS))
439 distprep: $(srcdir)/lib$(NAME)dll.def $(srcdir)/lib$(NAME)ddll.def $(srcdir)/blib$(NAME)dll.def
441 UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
443 $(srcdir)/lib$(NAME)dll.def: $(SHLIB_EXPORTS)
444 echo '; DEF file for MS VC++' >$@
445 echo 'LIBRARY LIB$(UC_NAME)' >>$@
447 sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
449 $(srcdir)/lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
450 echo '; DEF file for MS VC++' >$@
451 echo 'LIBRARY LIB$(UC_NAME)D' >>$@
453 sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
455 $(srcdir)/blib$(NAME)dll.def: $(SHLIB_EXPORTS)
456 echo '; DEF file for Borland C++ Builder' >$@
457 echo 'LIBRARY BLIB$(UC_NAME)' >>$@
459 sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/ _\1@ \2/' $< >>$@
461 echo '; Aliases for MS compatible names' >> $@
462 sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/ \1= _\1/' $< | sed 's/ *$$//' >>$@
463 endif # SHLIB_EXPORTS
470 .PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
471 install-lib: install-lib-shared
473 install-lib: install-lib-static
476 install-lib-static: $(stlib) installdirs-lib
477 $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
478 ifeq ($(PORTNAME), darwin)
479 cd '$(DESTDIR)$(libdir)' && \
483 ifeq ($(enable_shared), yes)
484 install-lib-shared: $(shlib) installdirs-lib
486 # we don't install $(shlib) on AIX
487 # (see http://archives.postgresql.org/message-id/52EF20B2E3209443BC37736D00C3C1380A6E79FE@EXADV1.host.magwien.gv.at)
488 ifneq ($(PORTNAME), aix)
489 $(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
490 ifneq ($(PORTNAME), cygwin)
491 ifneq ($(PORTNAME), win32)
492 ifneq ($(shlib), $(shlib_major))
493 cd '$(DESTDIR)$(libdir)' && \
494 rm -f $(shlib_major) && \
495 $(LN_S) $(shlib) $(shlib_major)
497 ifneq ($(shlib), $(shlib_bare))
498 cd '$(DESTDIR)$(libdir)' && \
499 rm -f $(shlib_bare) && \
500 $(LN_S) $(shlib) $(shlib_bare)
506 $(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
508 else # not enable_shared
512 echo "* Module $(NAME) was not installed due to lack of shared library support."; \
515 endif # enable_shared
520 $(mkinstalldirs) '$(DESTDIR)$(libdir)'
522 $(mkinstalldirs) '$(DESTDIR)$(pkglibdir)'
530 .PHONY: uninstall-lib
533 rm -f '$(DESTDIR)$(libdir)/$(stlib)'
534 ifeq ($(enable_shared), yes)
535 rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
536 '$(DESTDIR)$(libdir)/$(shlib_major)' \
537 '$(DESTDIR)$(libdir)/$(shlib)'
538 endif # enable_shared
540 rm -f '$(DESTDIR)$(pkglibdir)/$(shlib)'
550 rm -f $(shlib) $(shlib_bare) $(shlib_major) $(stlib) $(exports_file)
552 ifneq (,$(SHLIB_EXPORTS))
553 maintainer-clean-lib:
554 rm -f $(srcdir)/lib$(NAME)dll.def $(srcdir)/lib$(NAME)ddll.def $(srcdir)/blib$(NAME)dll.def