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 Stuff to append to library's link command
24 # (typically, -L and -l switches for external libraries)
25 # SHLIB_LINK_INTERNAL -L and -l switches for Postgres-supplied libraries
26 # SHLIB_PREREQS Order-only prerequisites for library build target
27 # SHLIB_EXPORTS (optional) Name of file containing list of symbols to
28 # export, in the format "function_name number"
30 # Don't use SHLIB_LINK for references to files in the build tree, or the
31 # wrong things will happen --- use SHLIB_LINK_INTERNAL for those!
33 # When building a shared library, the following version information
34 # must also be set. It should be omitted when building a dynamically
37 # SO_MAJOR_VERSION Major version number to use for shared library
38 # SO_MINOR_VERSION Minor version number to use for shared library
39 # (If you want a patchlevel, include it in SO_MINOR_VERSION, e.g., "6.2".)
41 # The module Makefile must also include
42 # $(top_builddir)/src/Makefile.global before including this file.
43 # (Makefile.global sets PORTNAME and other needed symbols.)
45 # This makefile provides the following (phony) targets:
47 # all-lib build the static and shared (if applicable) libraries
48 # install-lib install the libraries into $(libdir)
49 # installdirs-lib create installation directory $(libdir)
50 # uninstall-lib remove the libraries from $(libdir)
51 # clean-lib delete the static and shared libraries from the build dir
52 # maintainer-clean-lib delete .def files built for win32
54 # Typically you would add `all-lib' to the `all' target so that `make all'
55 # builds the libraries. In the most simple case it would look like this:
59 # Similarly, the install rule might look like
61 # install: install-lib
63 # plus any additional things you want to install. Et cetera.
65 # Got that? Look at src/interfaces/libpq/Makefile for an example.
69 COMPILER = $(CC) $(CFLAGS)
70 LINK.static = $(AR) $(AROPT)
72 LDFLAGS_INTERNAL += $(SHLIB_LINK_INTERNAL)
76 ifdef SO_MAJOR_VERSION
77 # Default library naming convention used by the majority of platforms
78 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
79 shlib_major = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
80 shlib_bare = lib$(NAME)$(DLSUFFIX)
81 # Testing the soname variable is a reliable way to determine whether a
82 # linkable library is being built.
83 soname = $(shlib_major)
84 pkgconfigdir = $(libdir)/pkgconfig
86 # Naming convention for dynamically loadable modules
87 shlib = $(NAME)$(DLSUFFIX)
92 # additional flags for backend modules
93 SHLIB_LINK += $(BE_DLLLIBS)
96 # For each platform we support shared libraries on, set shlib to the
97 # name of the library (if default above is not right), set
98 # LINK.shared to the command to link the library,
99 # and adjust SHLIB_LINK if necessary.
101 # Try to keep the sections in some kind of order, folks...
103 override CFLAGS += $(CFLAGS_SL)
104 override CXXFLAGS += $(CFLAGS_SL)
105 ifdef SO_MAJOR_VERSION
106 # libraries ought to use this to refer to versioned gettext domain names
107 override CPPFLAGS += -DSO_MAJOR_VERSION=$(SO_MAJOR_VERSION)
110 ifeq ($(PORTNAME), aix)
111 ifdef SO_MAJOR_VERSION
112 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
115 # $(exports_file) is also usable as an import file
116 exports_file = lib$(NAME).exp
119 ifeq ($(PORTNAME), darwin)
123 ifneq ($(SO_MAJOR_VERSION), 0)
124 version_link = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
126 LINK.shared = $(COMPILER) -dynamiclib -install_name '$(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)' $(version_link) $(exported_symbols_list) -multiply_defined suppress
127 shlib = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
128 shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
132 LINK.shared = $(COMPILER) -bundle -multiply_defined suppress
134 BUILD.exports = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
135 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
136 ifneq (,$(exports_file))
137 exported_symbols_list = -exported_symbols_list $(exports_file)
141 ifeq ($(PORTNAME), openbsd)
143 LINK.shared = $(COMPILER) -shared
145 LINK.shared += -Wl,-x,-soname,$(soname)
147 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
148 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
149 ifneq (,$(exports_file))
150 LINK.shared += -Wl,--version-script=$(exports_file)
154 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
158 ifeq ($(PORTNAME), freebsd)
160 ifdef SO_MAJOR_VERSION
161 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
163 LINK.shared = $(COMPILER) -shared
165 LINK.shared += -Wl,-x,-soname,$(soname)
167 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
168 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
169 ifneq (,$(exports_file))
170 LINK.shared += -Wl,--version-script=$(exports_file)
173 ifdef SO_MAJOR_VERSION
174 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
176 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
180 ifeq ($(PORTNAME), netbsd)
182 LINK.shared = $(COMPILER) -shared
184 LINK.shared += -Wl,-x,-soname,$(soname)
186 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
187 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
188 ifneq (,$(exports_file))
189 LINK.shared += -Wl,--version-script=$(exports_file)
192 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
196 ifeq ($(PORTNAME), hpux)
197 ifdef SO_MAJOR_VERSION
198 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
200 ifeq ($(with_gnu_ld), yes)
201 LINK.shared = $(CC) -shared -Wl,-Bsymbolic
203 LINK.shared += -Wl,-h -Wl,$(soname)
206 LINK.shared = $(LD) -b -Bsymbolic
208 LINK.shared += +h $(soname)
210 # can't use the CC-syntax rpath pattern here, so instead:
212 ifeq ($(enable_rpath), yes)
213 LINK.shared += +s +b '$(rpathdir)'
215 # On HPUX platforms, gcc is usually configured to search for libraries
216 # in /usr/local/lib, but ld won't do so. Add an explicit -L switch so
217 # ld can find the same libraries gcc does. Make sure it goes after any
218 # -L switches provided explicitly.
220 SHLIB_LINK += -L/usr/local/lib
223 # And we need to link with libgcc, too
225 SHLIB_LINK += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
229 ifeq ($(PORTNAME), linux)
230 LINK.shared = $(COMPILER) -shared
232 LINK.shared += -Wl,-soname,$(soname)
234 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
235 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
236 ifneq (,$(exports_file))
237 LINK.shared += -Wl,--version-script=$(exports_file)
241 ifeq ($(PORTNAME), solaris)
243 LINK.shared = $(COMPILER) -shared -Wl,-Bsymbolic
245 LINK.shared = $(COMPILER) -G -Bsymbolic
248 ifeq ($(with_gnu_ld), yes)
249 LINK.shared += -Wl,-soname,$(soname)
251 LINK.shared += -h $(soname)
256 ifeq ($(PORTNAME), cygwin)
257 LINK.shared = $(CC) -shared
258 ifdef SO_MAJOR_VERSION
259 shlib = cyg$(NAME)$(DLSUFFIX)
264 ifeq ($(PORTNAME), win32)
265 ifdef SO_MAJOR_VERSION
266 shlib = lib$(NAME)$(DLSUFFIX)
277 .PHONY: all-lib all-static-lib all-shared-lib
279 all-lib: all-shared-lib
281 # no static library when building a dynamically loadable module
282 all-lib: all-static-lib
283 all-lib: lib$(NAME).pc
286 all-static-lib: $(stlib)
288 all-shared-lib: $(shlib)
290 # In this rule, "touch $@" works around a problem on some platforms wherein
291 # ranlib updates the library file's mod time with a value calculated to
292 # seconds precision. If the filesystem has sub-second timestamps, this can
293 # cause the library file to appear older than its input files, triggering
294 # parallel-make problems.
296 $(stlib): $(OBJS) | $(SHLIB_PREREQS)
304 ifeq (,$(filter cygwin win32,$(PORTNAME)))
305 ifneq ($(PORTNAME), aix)
308 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
309 $(LINK.shared) -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
311 # If we're using major and minor versions, then make a symlink to major-version-only.
312 ifneq ($(shlib), $(shlib_major))
314 $(LN_S) $(shlib) $(shlib_major)
316 # Make sure we have a link to a name without any version numbers
317 ifneq ($(shlib), $(shlib_bare))
319 $(LN_S) $(shlib) $(shlib_bare)
323 # Where possible, restrict the symbols exported by the library to just the
324 # official list, so as to avoid unintentional ABI changes. On recent macOS
325 # this also quiets multiply-defined-symbol warnings in programs that use
326 # libpgport along with libpq.
327 ifneq (,$(SHLIB_EXPORTS))
329 $(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
331 $(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
336 else # PORTNAME == aix
340 # See notes in src/backend/parser/Makefile about the following two rules
344 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
346 $(LINK.static) $(stlib) $^
348 $(MKLDEXPORT) $(stlib) $(shlib) >$(exports_file)
349 $(COMPILER) -o $(shlib) $(stlib) -Wl,-bE:$(exports_file) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
351 $(AR) $(AROPT) $(stlib) $(shlib)
353 endif # PORTNAME == aix
355 else # PORTNAME == cygwin || PORTNAME == win32
357 ifeq ($(PORTNAME), cygwin)
361 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
362 $(CC) $(CFLAGS) -shared -o $@ -Wl,--out-implib=$(stlib) $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) $(LDAP_LIBS_BE)
364 # see notes in src/backend/parser/Makefile about use of this type of rule
372 # See notes in src/backend/parser/Makefile about the following two rules
376 # XXX A backend that loads a module linked with libgcc_s_dw2-1.dll will exit
377 # uncleanly, hence -static-libgcc. (Last verified with MinGW-w64 compilers
378 # from i686-4.9.1-release-win32-dwarf-rt_v3-rev1.) Shared libgcc has better
379 # support for C++/Java exceptions; while core PostgreSQL does not use them, it
380 # would be nice to support shared libgcc for the benefit of extensions.
382 # If SHLIB_EXPORTS is set, the rules below will build a .def file from that.
383 # Else we just use --export-all-symbols.
384 ifeq (,$(SHLIB_EXPORTS))
385 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
386 $(CC) $(CFLAGS) -shared -static-libgcc -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--export-all-symbols -Wl,--out-implib=$(stlib)
388 DLL_DEFFILE = lib$(NAME)dll.def
390 $(shlib): $(OBJS) $(DLL_DEFFILE) | $(SHLIB_PREREQS)
391 $(CC) $(CFLAGS) -shared -static-libgcc -o $@ $(OBJS) $(DLL_DEFFILE) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--out-implib=$(stlib)
394 endif # PORTNAME == cygwin
395 endif # PORTNAME == cygwin || PORTNAME == win32
398 %.pc: $(MAKEFILE_LIST)
399 echo 'Name: lib$(NAME)' >$@
400 echo 'Description: PostgreSQL lib$(NAME) library' >>$@
401 echo 'Url: http://www.postgresql.org/' >>$@
402 echo 'Version: $(VERSION)' >>$@
403 echo 'Requires: ' >>$@
404 echo 'Requires.private: $(PKG_CONFIG_REQUIRES_PRIVATE)' >>$@
405 echo 'Cflags: -I$(includedir)' >>$@
406 echo 'Libs: -L$(libdir) -l$(NAME)' >>$@
407 # Record -L flags that the user might have passed in to the PostgreSQL
408 # build to locate third-party libraries (e.g., ldap, ssl). Filter out
409 # those that point inside the build or source tree. Use sort to
410 # remove duplicates. Also record the -l flags necessary for static
411 # linking, but not those already covered by Requires.private.
412 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)))' >>$@
415 # We need several not-quite-identical variants of .DEF files to build
416 # DLLs for Windows. These are made from the single source file
417 # exports.txt. Since we can't assume that Windows boxes will have
418 # sed, the .DEF files are always built and included in distribution
421 ifneq (,$(SHLIB_EXPORTS))
422 distprep: lib$(NAME)dll.def lib$(NAME)ddll.def
424 UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
426 lib$(NAME)dll.def: $(SHLIB_EXPORTS)
427 echo '; DEF file for Makefile.shlib (MinGW)' >$@
428 echo 'LIBRARY LIB$(UC_NAME).dll' >>$@
430 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
432 lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
433 echo '; DEF file for Makefile.shlib (MinGW)' >$@
434 echo 'LIBRARY LIB$(UC_NAME)D.dll' >>$@
436 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
438 endif # SHLIB_EXPORTS
445 .PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
446 install-lib: install-lib-shared
448 install-lib: install-lib-static
449 install-lib: install-lib-pc
452 install-lib-pc: lib$(NAME).pc installdirs-lib
453 $(INSTALL_DATA) $< '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
455 install-lib-static: $(stlib) installdirs-lib
456 $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
457 ifeq ($(PORTNAME), darwin)
458 cd '$(DESTDIR)$(libdir)' && \
462 install-lib-shared: $(shlib) installdirs-lib
464 # we don't install $(shlib) on AIX
465 # (see http://archives.postgresql.org/message-id/52EF20B2E3209443BC37736D00C3C1380A6E79FE@EXADV1.host.magwien.gv.at)
466 ifneq ($(PORTNAME), aix)
467 $(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
468 ifneq ($(PORTNAME), cygwin)
469 ifneq ($(PORTNAME), win32)
470 ifneq ($(shlib), $(shlib_major))
471 cd '$(DESTDIR)$(libdir)' && \
472 rm -f $(shlib_major) && \
473 $(LN_S) $(shlib) $(shlib_major)
475 ifneq ($(shlib), $(shlib_bare))
476 cd '$(DESTDIR)$(libdir)' && \
477 rm -f $(shlib_bare) && \
478 $(LN_S) $(shlib) $(shlib_bare)
483 ifneq (,$(findstring $(PORTNAME),win32 cygwin))
484 $(INSTALL_SHLIB) $< '$(DESTDIR)$(bindir)/$(shlib)'
487 $(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
493 $(MKDIR_P) '$(DESTDIR)$(libdir)' '$(DESTDIR)$(pkgconfigdir)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)')
495 $(MKDIR_P) '$(DESTDIR)$(pkglibdir)'
503 .PHONY: uninstall-lib
506 rm -f '$(DESTDIR)$(libdir)/$(stlib)'
507 rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
508 '$(DESTDIR)$(libdir)/$(shlib_major)' \
509 '$(DESTDIR)$(libdir)/$(shlib)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)/$(shlib)') \
510 '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
512 rm -f '$(DESTDIR)$(pkglibdir)/$(shlib)'
522 rm -f $(shlib) $(shlib_bare) $(shlib_major) $(stlib) $(exports_file) lib$(NAME).pc
524 ifneq (,$(SHLIB_EXPORTS))
525 maintainer-clean-lib:
526 rm -f lib$(NAME)dll.def lib$(NAME)ddll.def