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_PREREQS Order-only prerequisites for library build target
26 # SHLIB_EXPORTS (optional) Name of file containing list of symbols to
27 # export, in the format "function_name number"
29 # When building a shared library, the following version information
30 # must also be set. It should be omitted when building a dynamically
33 # SO_MAJOR_VERSION Major version number to use for shared library
34 # SO_MINOR_VERSION Minor version number to use for shared library
35 # (If you want a patchlevel, include it in SO_MINOR_VERSION, e.g., "6.2".)
37 # The module Makefile must also include
38 # $(top_builddir)/src/Makefile.global before including this file.
39 # (Makefile.global sets PORTNAME and other needed symbols.)
41 # This makefile provides the following (phony) targets:
43 # all-lib build the static and shared (if applicable) libraries
44 # install-lib install the libraries into $(libdir)
45 # installdirs-lib create installation directory $(libdir)
46 # uninstall-lib remove the libraries from $(libdir)
47 # clean-lib delete the static and shared libraries from the build dir
48 # maintainer-clean-lib delete .def files built for win32
50 # Typically you would add `all-lib' to the `all' target so that `make all'
51 # builds the libraries. In the most simple case it would look like this:
55 # Similarly, the install rule might look like
57 # install: install-lib
59 # plus any additional things you want to install. Et cetera.
61 # Got that? Look at src/interfaces/libpq/Makefile for an example.
63 # While the linker allows creation of most shared libraries,
64 # -Bsymbolic requires resolution of all symbols, making the
65 # compiler a better choice for shared library creation on ELF platforms.
66 # With the linker, -Bsymbolic requires the crt1.o startup object file.
70 COMPILER = $(CC) $(CFLAGS)
71 LINK.static = $(AR) $(AROPT)
75 ifdef SO_MAJOR_VERSION
76 # Default library naming convention used by the majority of platforms
77 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
78 shlib_major = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
79 shlib_bare = lib$(NAME)$(DLSUFFIX)
80 # Testing the soname variable is a reliable way to determine whether a
81 # linkable library is being built.
82 soname = $(shlib_major)
83 pkgconfigdir = $(libdir)/pkgconfig
85 # Naming convention for dynamically loadable modules
86 shlib = $(NAME)$(DLSUFFIX)
91 # additional flags for backend modules
92 SHLIB_LINK += $(BE_DLLLIBS)
95 # For each platform we support shared libraries on, set shlib to the
96 # name of the library (if default above is not right), set
97 # LINK.shared to the command to link the library,
98 # and adjust SHLIB_LINK if necessary.
100 # Try to keep the sections in some kind of order, folks...
102 override CFLAGS += $(CFLAGS_SL)
103 override CXXFLAGS += $(CFLAGS_SL)
104 ifdef SO_MAJOR_VERSION
105 # libraries ought to use this to refer to versioned gettext domain names
106 override CPPFLAGS += -DSO_MAJOR_VERSION=$(SO_MAJOR_VERSION)
109 ifeq ($(PORTNAME), aix)
110 ifdef SO_MAJOR_VERSION
111 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
114 # $(exports_file) is also usable as an import file
115 exports_file = lib$(NAME).exp
118 ifeq ($(PORTNAME), darwin)
122 ifneq ($(SO_MAJOR_VERSION), 0)
123 version_link = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
125 LINK.shared = $(COMPILER) -dynamiclib -install_name '$(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)' $(version_link) $(exported_symbols_list) -multiply_defined suppress
126 shlib = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
127 shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
131 LINK.shared = $(COMPILER) -bundle -multiply_defined suppress
133 BUILD.exports = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
134 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
135 ifneq (,$(exports_file))
136 exported_symbols_list = -exported_symbols_list $(exports_file)
140 ifeq ($(PORTNAME), openbsd)
142 LINK.shared = $(COMPILER) -shared
144 LINK.shared += -Wl,-x,-soname,$(soname)
148 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
152 ifeq ($(PORTNAME), freebsd)
154 ifdef SO_MAJOR_VERSION
155 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
157 LINK.shared = $(COMPILER) -shared
159 LINK.shared += -Wl,-x,-soname,$(soname)
162 ifdef SO_MAJOR_VERSION
163 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
165 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
169 ifeq ($(PORTNAME), netbsd)
171 LINK.shared = $(COMPILER) -shared
173 LINK.shared += -Wl,-x,-soname,$(soname)
176 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
180 ifeq ($(PORTNAME), hpux)
181 ifdef SO_MAJOR_VERSION
182 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
184 ifeq ($(with_gnu_ld), yes)
185 LINK.shared = $(CC) -shared
187 LINK.shared += -Wl,-h -Wl,$(soname)
190 LINK.shared = $(LD) -b
192 LINK.shared += +h $(soname)
194 # can't use the CC-syntax rpath pattern here, so instead:
196 ifeq ($(enable_rpath), yes)
197 LINK.shared += +s +b '$(rpathdir)'
199 # On HPUX platforms, gcc is usually configured to search for libraries
200 # in /usr/local/lib, but ld won't do so. Add an explicit -L switch so
201 # ld can find the same libraries gcc does. Make sure it goes after any
202 # -L switches provided explicitly.
204 SHLIB_LINK += -L/usr/local/lib
207 # And we need to link with libgcc, too
209 SHLIB_LINK += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
213 ifeq ($(PORTNAME), linux)
214 LINK.shared = $(COMPILER) -shared
216 LINK.shared += -Wl,-soname,$(soname)
218 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
219 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
220 ifneq (,$(exports_file))
221 LINK.shared += -Wl,--version-script=$(exports_file)
225 ifeq ($(PORTNAME), solaris)
227 LINK.shared = $(COMPILER) -shared
229 LINK.shared = $(COMPILER) -G
232 ifeq ($(with_gnu_ld), yes)
233 LINK.shared += -Wl,-soname,$(soname)
235 LINK.shared += -h $(soname)
240 ifeq ($(PORTNAME), cygwin)
241 LINK.shared = $(CC) -shared
242 ifdef SO_MAJOR_VERSION
243 shlib = cyg$(NAME)$(DLSUFFIX)
248 ifeq ($(PORTNAME), win32)
249 ifdef SO_MAJOR_VERSION
250 shlib = lib$(NAME)$(DLSUFFIX)
261 .PHONY: all-lib all-static-lib all-shared-lib
263 all-lib: all-shared-lib
265 # no static library when building a dynamically loadable module
266 all-lib: all-static-lib
267 all-lib: lib$(NAME).pc
270 all-static-lib: $(stlib)
272 all-shared-lib: $(shlib)
275 $(stlib): $(OBJS) | $(SHLIB_PREREQS)
282 ifeq (,$(filter cygwin win32,$(PORTNAME)))
283 ifneq ($(PORTNAME), aix)
286 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
287 $(LINK.shared) -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
289 # If we're using major and minor versions, then make a symlink to major-version-only.
290 ifneq ($(shlib), $(shlib_major))
292 $(LN_S) $(shlib) $(shlib_major)
294 # Make sure we have a link to a name without any version numbers
295 ifneq ($(shlib), $(shlib_bare))
297 $(LN_S) $(shlib) $(shlib_bare)
301 # Where possible, restrict the symbols exported by the library to just the
302 # official list, so as to avoid unintentional ABI changes. On recent macOS
303 # this also quiets multiply-defined-symbol warnings in programs that use
304 # libpgport along with libpq.
305 ifneq (,$(SHLIB_EXPORTS))
307 $(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
309 $(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
314 else # PORTNAME == aix
318 # See notes in src/backend/parser/Makefile about the following two rules
322 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
324 $(LINK.static) $(stlib) $^
326 $(MKLDEXPORT) $(stlib) $(shlib) >$(exports_file)
327 $(COMPILER) -o $(shlib) $(stlib) -Wl,-bE:$(exports_file) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
329 $(AR) $(AROPT) $(stlib) $(shlib)
331 endif # PORTNAME == aix
333 else # PORTNAME == cygwin || PORTNAME == win32
335 ifeq ($(PORTNAME), cygwin)
339 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
340 $(CC) $(CFLAGS) -shared -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) $(LDAP_LIBS_BE)
342 $(stlib): $(OBJS) | $(SHLIB_PREREQS)
351 # See notes in src/backend/parser/Makefile about the following two rules
355 # XXX A backend that loads a module linked with libgcc_s_dw2-1.dll will exit
356 # uncleanly, hence -static-libgcc. (Last verified with MinGW-w64 compilers
357 # from i686-4.9.1-release-win32-dwarf-rt_v3-rev1.) Shared libgcc has better
358 # support for C++/Java exceptions; while core PostgreSQL does not use them, it
359 # would be nice to support shared libgcc for the benefit of extensions.
361 # If SHLIB_EXPORTS is set, the rules below will build a .def file from that.
362 # Else we just use --export-all-symbols.
363 ifeq (,$(SHLIB_EXPORTS))
364 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
365 $(CC) $(CFLAGS) -shared -static-libgcc -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--export-all-symbols -Wl,--out-implib=$(stlib)
367 DLL_DEFFILE = lib$(NAME)dll.def
369 $(shlib): $(OBJS) $(DLL_DEFFILE) | $(SHLIB_PREREQS)
370 $(CC) $(CFLAGS) -shared -static-libgcc -o $@ $(OBJS) $(DLL_DEFFILE) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--out-implib=$(stlib)
373 endif # PORTNAME == cygwin
374 endif # PORTNAME == cygwin || PORTNAME == win32
377 %.pc: $(MAKEFILE_LIST)
378 echo 'Name: lib$(NAME)' >$@
379 echo 'Description: PostgreSQL lib$(NAME) library' >>$@
380 echo 'Url: http://www.postgresql.org/' >>$@
381 echo 'Version: $(VERSION)' >>$@
382 echo 'Requires: ' >>$@
383 echo 'Requires.private: $(PKG_CONFIG_REQUIRES_PRIVATE)' >>$@
384 echo 'Cflags: -I$(includedir)' >>$@
385 echo 'Libs: -L$(libdir) -l$(NAME)' >>$@
386 # Record -L flags that the user might have passed in to the PostgreSQL
387 # build to locate third-party libraries (e.g., ldap, ssl). Filter out
388 # those that point inside the build or source tree. Use sort to
389 # remove duplicates. Also record the -l flags necessary for static
390 # linking, but not those already covered by Requires.private.
391 echo 'Libs.private: $(sort $(filter-out -L.% -L$(top_srcdir)/%,$(filter -L%,$(LDFLAGS) $(SHLIB_LINK)))) $(filter-out $(PKG_CONFIG_REQUIRES_PRIVATE:lib%=-l%),$(filter -l%,$(SHLIB_LINK)))' >>$@
394 # We need several not-quite-identical variants of .DEF files to build
395 # DLLs for Windows. These are made from the single source file
396 # exports.txt. Since we can't assume that Windows boxes will have
397 # sed, the .DEF files are always built and included in distribution
400 ifneq (,$(SHLIB_EXPORTS))
401 distprep: lib$(NAME)dll.def lib$(NAME)ddll.def
403 UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
405 lib$(NAME)dll.def: $(SHLIB_EXPORTS)
406 echo '; DEF file for Makefile.shlib (MinGW)' >$@
407 echo 'LIBRARY LIB$(UC_NAME).dll' >>$@
409 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
411 lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
412 echo '; DEF file for Makefile.shlib (MinGW)' >$@
413 echo 'LIBRARY LIB$(UC_NAME)D.dll' >>$@
415 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
417 endif # SHLIB_EXPORTS
424 .PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
425 install-lib: install-lib-shared
427 install-lib: install-lib-static
428 install-lib: install-lib-pc
431 install-lib-pc: lib$(NAME).pc installdirs-lib
432 $(INSTALL_DATA) $< '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
434 install-lib-static: $(stlib) installdirs-lib
435 $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
436 ifeq ($(PORTNAME), darwin)
437 cd '$(DESTDIR)$(libdir)' && \
441 install-lib-shared: $(shlib) installdirs-lib
443 # we don't install $(shlib) on AIX
444 # (see http://archives.postgresql.org/message-id/52EF20B2E3209443BC37736D00C3C1380A6E79FE@EXADV1.host.magwien.gv.at)
445 ifneq ($(PORTNAME), aix)
446 $(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
447 ifneq ($(PORTNAME), cygwin)
448 ifneq ($(PORTNAME), win32)
449 ifneq ($(shlib), $(shlib_major))
450 cd '$(DESTDIR)$(libdir)' && \
451 rm -f $(shlib_major) && \
452 $(LN_S) $(shlib) $(shlib_major)
454 ifneq ($(shlib), $(shlib_bare))
455 cd '$(DESTDIR)$(libdir)' && \
456 rm -f $(shlib_bare) && \
457 $(LN_S) $(shlib) $(shlib_bare)
462 ifneq (,$(findstring $(PORTNAME),win32 cygwin))
463 $(INSTALL_SHLIB) $< '$(DESTDIR)$(bindir)/$(shlib)'
466 $(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
472 $(MKDIR_P) '$(DESTDIR)$(libdir)' '$(DESTDIR)$(pkgconfigdir)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)')
474 $(MKDIR_P) '$(DESTDIR)$(pkglibdir)'
482 .PHONY: uninstall-lib
485 rm -f '$(DESTDIR)$(libdir)/$(stlib)'
486 rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
487 '$(DESTDIR)$(libdir)/$(shlib_major)' \
488 '$(DESTDIR)$(libdir)/$(shlib)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)/$(shlib)') \
489 '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
491 rm -f '$(DESTDIR)$(pkglibdir)/$(shlib)'
501 rm -f $(shlib) $(shlib_bare) $(shlib_major) $(stlib) $(exports_file) lib$(NAME).pc
503 ifneq (,$(SHLIB_EXPORTS))
504 maintainer-clean-lib:
505 rm -f lib$(NAME)dll.def lib$(NAME)ddll.def