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
53 # Typically you would add `all-lib' to the `all' target so that `make all'
54 # builds the libraries. In the most simple case it would look like this:
58 # Similarly, the install rule might look like
60 # install: install-lib
62 # plus any additional things you want to install. Et cetera.
64 # Got that? Look at src/interfaces/libpq/Makefile for an example.
68 COMPILER = $(CC) $(CFLAGS)
69 LINK.static = $(AR) $(AROPT)
71 LDFLAGS_INTERNAL += $(SHLIB_LINK_INTERNAL)
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), darwin)
112 ifneq ($(SO_MAJOR_VERSION), 0)
113 version_link = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
115 LINK.shared = $(COMPILER) -dynamiclib -install_name '$(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)' $(version_link) $(exported_symbols_list)
116 shlib = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
117 shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
120 LINK.shared = $(COMPILER) -bundle
122 BUILD.exports = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
123 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
124 ifneq (,$(exports_file))
125 exported_symbols_list = -exported_symbols_list $(exports_file)
129 ifeq ($(PORTNAME), openbsd)
130 LINK.shared = $(COMPILER) -shared
132 LINK.shared += -Wl,-x,-soname,$(soname)
134 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
135 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
136 ifneq (,$(exports_file))
137 LINK.shared += -Wl,--version-script=$(exports_file)
142 ifeq ($(PORTNAME), freebsd)
143 ifdef SO_MAJOR_VERSION
144 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
146 LINK.shared = $(COMPILER) -shared
148 LINK.shared += -Wl,-x,-soname,$(soname)
150 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
151 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
152 ifneq (,$(exports_file))
153 LINK.shared += -Wl,--version-script=$(exports_file)
157 ifeq ($(PORTNAME), netbsd)
158 LINK.shared = $(COMPILER) -shared
160 LINK.shared += -Wl,-x,-soname,$(soname)
162 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
163 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
164 ifneq (,$(exports_file))
165 LINK.shared += -Wl,--version-script=$(exports_file)
169 ifeq ($(PORTNAME), linux)
170 LINK.shared = $(COMPILER) -shared
172 ifneq (,$(findstring linux-android,$(host_os)))
173 # Android uses unversioned shared libraries
174 shlib = $(shlib_bare)
175 soname = $(shlib_bare)
177 LINK.shared += -Wl,-soname,$(soname)
179 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
180 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
181 ifneq (,$(exports_file))
182 LINK.shared += -Wl,--version-script=$(exports_file)
186 ifeq ($(PORTNAME), solaris)
187 LINK.shared = $(COMPILER) -shared
189 LINK.shared += -Wl,-soname,$(soname)
191 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
192 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
193 ifneq (,$(exports_file))
194 LINK.shared += -Wl,-M$(exports_file)
198 ifeq ($(PORTNAME), cygwin)
199 LINK.shared = $(CC) -shared
200 ifdef SO_MAJOR_VERSION
201 shlib = cyg$(NAME)$(DLSUFFIX)
206 ifeq ($(PORTNAME), win32)
207 ifdef SO_MAJOR_VERSION
208 shlib = lib$(NAME)$(DLSUFFIX)
214 # If the shared library doesn't have an export file, mark all symbols not
215 # explicitly exported using PGDLLEXPORT as hidden. We can't pass these flags
216 # when building a library with explicit exports, as the symbols would be
217 # hidden before the linker script / exported symbol list takes effect.
219 # This is duplicated in pgxs.mk for MODULES style libraries.
220 ifeq ($(SHLIB_EXPORTS),)
221 # LDFLAGS_SL addition not strictly needed, CFLAGS used everywhere, but ...
222 override LDFLAGS_SL += $(CFLAGS_SL_MODULE)
223 override CFLAGS += $(CFLAGS_SL_MODULE)
224 override CXXFLAGS += $(CXXFLAGS_SL_MODULE)
232 .PHONY: all-lib all-static-lib all-shared-lib
234 all-lib: all-shared-lib
236 # no static library when building a dynamically loadable module
237 all-lib: all-static-lib
238 all-lib: lib$(NAME).pc
241 all-static-lib: $(stlib)
243 all-shared-lib: $(shlib)
245 # In this rule, "touch $@" works around a problem on some platforms wherein
246 # ar updates the library file's mod time with a value calculated to
247 # seconds precision. If the filesystem has sub-second timestamps, this can
248 # cause the library file to appear older than its input files, triggering
249 # parallel-make problems.
251 $(stlib): $(OBJS) | $(SHLIB_PREREQS)
257 ifeq (,$(filter cygwin win32,$(PORTNAME)))
260 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
261 $(LINK.shared) -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
263 # If we're using major and minor versions, then make a symlink to major-version-only.
264 ifneq ($(shlib), $(shlib_major))
266 $(LN_S) $(shlib) $(shlib_major)
268 # Make sure we have a link to a name without any version numbers
269 ifneq ($(shlib), $(shlib_bare))
271 $(LN_S) $(shlib) $(shlib_bare)
275 # Where possible, restrict the symbols exported by the library to just the
276 # official list, so as to avoid unintentional ABI changes. On recent macOS
277 # this also quiets multiply-defined-symbol warnings in programs that use
278 # libpgport along with libpq.
279 ifneq (,$(SHLIB_EXPORTS))
281 $(shlib): $(exports_file)
283 $(exports_file): $(SHLIB_EXPORTS)
288 else # PORTNAME == cygwin || PORTNAME == win32
290 ifeq ($(PORTNAME), cygwin)
294 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
295 $(CC) $(CFLAGS) -shared -o $@ -Wl,--out-implib=$(stlib) $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) $(LDAP_LIBS_BE)
297 # see notes in src/backend/parser/Makefile about use of this type of rule
305 # See notes in src/backend/parser/Makefile about the following two rules
309 # XXX A backend that loads a module linked with libgcc_s_dw2-1.dll will exit
310 # uncleanly, hence -static-libgcc. (Last verified with MinGW-w64 compilers
311 # from i686-4.9.1-release-win32-dwarf-rt_v3-rev1.) Shared libgcc has better
312 # support for C++/Java exceptions; while core PostgreSQL does not use them, it
313 # would be nice to support shared libgcc for the benefit of extensions.
315 # If SHLIB_EXPORTS is set, the rules below will build a .def file from that.
316 # Else we just use --export-all-symbols.
317 ifeq (,$(SHLIB_EXPORTS))
318 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
319 $(CC) $(CFLAGS) -shared -static-libgcc -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--export-all-symbols -Wl,--out-implib=$(stlib)
321 DLL_DEFFILE = lib$(NAME)dll.def
323 $(shlib): $(OBJS) $(DLL_DEFFILE) | $(SHLIB_PREREQS)
324 $(CC) $(CFLAGS) -shared -static-libgcc -o $@ $(OBJS) $(DLL_DEFFILE) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--out-implib=$(stlib)
326 UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
328 $(DLL_DEFFILE): $(SHLIB_EXPORTS)
329 echo 'LIBRARY LIB$(UC_NAME).dll' >$@
331 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
334 endif # PORTNAME == cygwin
335 endif # PORTNAME == cygwin || PORTNAME == win32
338 %.pc: $(MAKEFILE_LIST)
339 echo 'prefix=$(prefix)' >$@
340 echo 'exec_prefix=$(patsubst $(prefix),$${prefix},$(exec_prefix))' >>$@
341 echo 'libdir=$(patsubst $(exec_prefix)/%,$${exec_prefix}/%,$(libdir))' >>$@
342 echo 'includedir=$(patsubst $(prefix)/%,$${prefix}/%,$(includedir))' >>$@
344 echo 'Name: lib$(NAME)' >>$@
345 echo 'Description: PostgreSQL lib$(NAME) library' >>$@
346 echo 'URL: $(PACKAGE_URL)' >>$@
347 echo 'Version: $(VERSION)' >>$@
348 echo 'Requires: ' >>$@
349 echo 'Requires.private: $(PKG_CONFIG_REQUIRES_PRIVATE)' >>$@
350 echo 'Cflags: -I$${includedir}' >>$@
351 echo 'Libs: -L$${libdir} -l$(NAME)' >>$@
352 # Record -L flags that the user might have passed in to the PostgreSQL
353 # build to locate third-party libraries (e.g., ldap, ssl). Filter out
354 # those that point inside the build or source tree. Use sort to
355 # remove duplicates. Also record the -l flags necessary for static
356 # linking, but not those already covered by Requires.private.
357 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_INTERNAL:%_shlib=%) $(SHLIB_LINK)))' >>$@
364 .PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
365 install-lib: install-lib-shared
367 install-lib: install-lib-static
368 install-lib: install-lib-pc
371 install-lib-pc: lib$(NAME).pc installdirs-lib
372 $(INSTALL_DATA) $< '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
374 install-lib-static: $(stlib) installdirs-lib
375 $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
377 install-lib-shared: $(shlib) installdirs-lib
379 $(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
380 ifneq ($(PORTNAME), cygwin)
381 ifneq ($(PORTNAME), win32)
382 ifneq ($(shlib), $(shlib_major))
383 cd '$(DESTDIR)$(libdir)' && \
384 rm -f $(shlib_major) && \
385 $(LN_S) $(shlib) $(shlib_major)
387 ifneq ($(shlib), $(shlib_bare))
388 cd '$(DESTDIR)$(libdir)' && \
389 rm -f $(shlib_bare) && \
390 $(LN_S) $(shlib) $(shlib_bare)
394 ifneq (,$(findstring $(PORTNAME),win32 cygwin))
395 $(INSTALL_SHLIB) $< '$(DESTDIR)$(bindir)/$(shlib)'
398 $(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
404 $(MKDIR_P) '$(DESTDIR)$(libdir)' '$(DESTDIR)$(pkgconfigdir)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)')
406 $(MKDIR_P) '$(DESTDIR)$(pkglibdir)'
414 .PHONY: uninstall-lib
417 rm -f '$(DESTDIR)$(libdir)/$(stlib)'
418 rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
419 '$(DESTDIR)$(libdir)/$(shlib_major)' \
420 '$(DESTDIR)$(libdir)/$(shlib)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)/$(shlib)') \
421 '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
423 rm -f '$(DESTDIR)$(pkglibdir)/$(shlib)'
433 rm -f $(shlib) $(shlib_bare) $(shlib_major) $(stlib) $(exports_file) lib$(NAME).pc
434 ifneq (,$(DLL_DEFFILE))