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 # Since `all-lib' is the first rule in this file you probably want to
51 # have the `all' target before including this file. In the most simple
52 # case it would look like this:
56 # Similarly, the install rule might look like
58 # install: install-lib
60 # plus any additional things you want to install. Et cetera.
62 # Got that? Look at src/interfaces/libpq/Makefile for an example.
64 # While the linker allows creation of most shared libraries,
65 # -Bsymbolic requires resolution of all symbols, making the
66 # compiler a better choice for shared library creation on ELF platforms.
67 # With the linker, -Bsymbolic requires the crt1.o startup object file.
71 COMPILER = $(CC) $(CFLAGS)
72 LINK.static = $(AR) $(AROPT)
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 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 = lib$(NAME).exp
117 ifeq ($(PORTNAME), darwin)
121 ifneq ($(SO_MAJOR_VERSION), 0)
122 version_link = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
124 LINK.shared = $(COMPILER) -dynamiclib -install_name '$(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)' $(version_link) $(exported_symbols_list) -multiply_defined suppress
125 shlib = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
126 shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
130 LINK.shared = $(COMPILER) -bundle -multiply_defined suppress
132 BUILD.exports = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
133 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
134 ifneq (,$(exports_file))
135 exported_symbols_list = -exported_symbols_list $(exports_file)
139 ifeq ($(PORTNAME), openbsd)
141 LINK.shared = $(COMPILER) -shared
143 LINK.shared += -Wl,-x,-soname,$(soname)
147 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
151 ifeq ($(PORTNAME), freebsd)
153 ifdef SO_MAJOR_VERSION
154 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
156 LINK.shared = $(COMPILER) -shared
158 LINK.shared += -Wl,-x,-soname,$(soname)
161 ifdef SO_MAJOR_VERSION
162 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
164 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
168 ifeq ($(PORTNAME), netbsd)
170 LINK.shared = $(COMPILER) -shared
172 LINK.shared += -Wl,-x,-soname,$(soname)
175 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
179 ifeq ($(PORTNAME), hpux)
180 ifdef SO_MAJOR_VERSION
181 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
183 ifeq ($(with_gnu_ld), yes)
184 LINK.shared = $(CC) -shared
186 LINK.shared += -Wl,-h -Wl,$(soname)
189 LINK.shared = $(LD) -b
191 LINK.shared += +h $(soname)
193 # can't use the CC-syntax rpath pattern here, so instead:
195 ifeq ($(enable_rpath), yes)
196 LINK.shared += +b '$(rpathdir)'
198 # On HPUX platforms, gcc is usually configured to search for libraries
199 # in /usr/local/lib, but ld won't do so. Add an explicit -L switch so
200 # ld can find the same libraries gcc does. Make sure it goes after any
201 # -L switches provided explicitly.
203 SHLIB_LINK += -L/usr/local/lib
206 # And we need to link with libgcc, too
208 SHLIB_LINK += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
212 ifeq ($(PORTNAME), linux)
213 LINK.shared = $(COMPILER) -shared
215 LINK.shared += -Wl,-soname,$(soname)
217 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
218 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
219 ifneq (,$(exports_file))
220 LINK.shared += -Wl,--version-script=$(exports_file)
224 ifeq ($(PORTNAME), solaris)
226 LINK.shared = $(COMPILER) -shared
228 LINK.shared = $(COMPILER) -G
231 ifeq ($(with_gnu_ld), yes)
232 LINK.shared += -Wl,-soname,$(soname)
234 LINK.shared += -h $(soname)
239 ifeq ($(PORTNAME), osf)
240 LINK.shared = $(LD) -shared -expect_unresolved '*'
243 ifeq ($(PORTNAME), sco)
245 LINK.shared = $(CC) -shared
247 LINK.shared = $(CC) -G
249 LINK.shared += -Wl,-z,text
251 LINK.shared += -Wl,-h,$(soname)
255 ifeq ($(PORTNAME), unixware)
257 LINK.shared = $(CC) -shared
259 LINK.shared = $(CC) -G
261 LINK.shared += -Wl,-z,text
263 LINK.shared += -Wl,-h,$(soname)
267 ifeq ($(PORTNAME), cygwin)
268 LINK.shared = $(CC) -shared
269 ifdef SO_MAJOR_VERSION
270 shlib = cyg$(NAME)$(DLSUFFIX)
275 ifeq ($(PORTNAME), win32)
276 ifdef SO_MAJOR_VERSION
277 shlib = lib$(NAME)$(DLSUFFIX)
288 .PHONY: all-lib all-static-lib all-shared-lib
290 all-lib: all-shared-lib
292 # no static library when building a dynamically loadable module
293 all-lib: all-static-lib
294 all-lib: lib$(NAME).pc
297 all-static-lib: $(stlib)
299 all-shared-lib: $(shlib)
302 $(stlib): $(OBJS) | $(SHLIB_PREREQS)
308 ifeq (,$(filter cygwin win32,$(PORTNAME)))
309 ifneq ($(PORTNAME), aix)
312 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
313 $(LINK.shared) -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
315 # If we're using major and minor versions, then make a symlink to major-version-only.
316 ifneq ($(shlib), $(shlib_major))
318 $(LN_S) $(shlib) $(shlib_major)
320 # Make sure we have a link to a name without any version numbers
321 ifneq ($(shlib), $(shlib_bare))
323 $(LN_S) $(shlib) $(shlib_bare)
327 # Where possible, restrict the symbols exported by the library to just the
328 # official list, so as to avoid unintentional ABI changes. On recent Darwin
329 # this also quiets multiply-defined-symbol warnings in programs that use
330 # libpgport along with libpq.
331 ifneq (,$(SHLIB_EXPORTS))
333 $(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
335 $(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
340 else # PORTNAME == aix
343 $(shlib) $(stlib): $(OBJS) | $(SHLIB_PREREQS)
344 $(LINK.static) $(stlib) $^
346 $(MKLDEXPORT) $(stlib) >$(exports_file)
347 $(COMPILER) -o $(shlib) $(stlib) -Wl,-bE:$(exports_file) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
349 $(AR) $(AROPT) $(stlib) $(shlib)
351 endif # PORTNAME == aix
353 else # PORTNAME == cygwin || PORTNAME == win32
355 ifeq ($(PORTNAME), cygwin)
359 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
360 $(CC) $(CFLAGS) -shared -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) $(LDAP_LIBS_BE)
362 $(stlib): $(OBJS) | $(SHLIB_PREREQS)
370 # There is no correct way to write a rule that generates two files.
371 # Rules with two targets don't have that meaning, they are merely
372 # shorthand for two otherwise separate rules. To be safe for parallel
373 # make, we must chain the dependencies like this. The semicolon is
374 # important, otherwise make will choose some built-in rule.
378 # If SHLIB_EXPORTS is set, the rules below will build a .def file from that.
379 # Else we just use --export-all-symbols.
380 ifeq (,$(SHLIB_EXPORTS))
381 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
382 $(CC) $(CFLAGS) -shared -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--export-all-symbols -Wl,--out-implib=$(stlib)
384 DLL_DEFFILE = lib$(NAME)dll.def
386 $(shlib): $(OBJS) $(DLL_DEFFILE) | $(SHLIB_PREREQS)
387 $(CC) $(CFLAGS) -shared -o $@ $(OBJS) $(DLL_DEFFILE) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--out-implib=$(stlib)
390 endif # PORTNAME == cgywin
391 endif # PORTNAME == cygwin || PORTNAME == win32
394 %.pc: $(MAKEFILE_LIST)
395 echo 'Name: lib$(NAME)' >$@
396 echo 'Description: PostgreSQL lib$(NAME) library' >>$@
397 echo 'Url: http://www.postgresql.org/' >>$@
398 echo 'Version: $(VERSION)' >>$@
399 echo 'Requires: ' >>$@
400 echo 'Requires.private: $(PKG_CONFIG_REQUIRES_PRIVATE)' >>$@
401 echo 'Cflags: -I$(includedir)' >>$@
402 echo 'Libs: -L$(libdir) -l$(NAME)' >>$@
403 # Record -L flags that the user might have passed in to the PostgreSQL
404 # build to locate third-party libraries (e.g., ldap, ssl). Filter out
405 # those that point inside the build or source tree. Use sort to
406 # remove duplicates. Also record the -l flags necessary for static
407 # linking, but not those already covered by Requires.private.
408 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)))' >>$@
411 # We need several not-quite-identical variants of .DEF files to build
412 # DLLs for Windows. These are made from the single source file
413 # exports.txt. Since we can't assume that Windows boxes will have
414 # sed, the .DEF files are always built and included in distribution
417 ifneq (,$(SHLIB_EXPORTS))
418 distprep: lib$(NAME)dll.def lib$(NAME)ddll.def blib$(NAME)dll.def
420 UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
422 lib$(NAME)dll.def: $(SHLIB_EXPORTS)
423 echo '; DEF file for MS VC++' >$@
424 echo 'LIBRARY LIB$(UC_NAME)' >>$@
426 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
428 lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
429 echo '; DEF file for MS VC++' >$@
430 echo 'LIBRARY LIB$(UC_NAME)D' >>$@
432 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
434 blib$(NAME)dll.def: $(SHLIB_EXPORTS)
435 echo '; DEF file for Borland C++ Builder' >$@
436 echo 'LIBRARY BLIB$(UC_NAME)' >>$@
438 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ _\1@ \2/' $< >>$@
440 echo '; Aliases for MS compatible names' >> $@
441 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1= _\1/' $< | sed 's/ *$$//' >>$@
442 endif # SHLIB_EXPORTS
449 .PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
450 install-lib: install-lib-shared
452 install-lib: install-lib-static
453 install-lib: install-lib-pc
456 install-lib-pc: lib$(NAME).pc installdirs-lib
457 $(INSTALL_DATA) $< '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
459 install-lib-static: $(stlib) installdirs-lib
460 $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
461 ifeq ($(PORTNAME), darwin)
462 cd '$(DESTDIR)$(libdir)' && \
466 install-lib-shared: $(shlib) installdirs-lib
468 # we don't install $(shlib) on AIX
469 # (see http://archives.postgresql.org/message-id/52EF20B2E3209443BC37736D00C3C1380A6E79FE@EXADV1.host.magwien.gv.at)
470 ifneq ($(PORTNAME), aix)
471 $(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
472 ifneq ($(PORTNAME), cygwin)
473 ifneq ($(PORTNAME), win32)
474 ifneq ($(shlib), $(shlib_major))
475 cd '$(DESTDIR)$(libdir)' && \
476 rm -f $(shlib_major) && \
477 $(LN_S) $(shlib) $(shlib_major)
479 ifneq ($(shlib), $(shlib_bare))
480 cd '$(DESTDIR)$(libdir)' && \
481 rm -f $(shlib_bare) && \
482 $(LN_S) $(shlib) $(shlib_bare)
488 $(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
494 $(MKDIR_P) '$(DESTDIR)$(libdir)' '$(DESTDIR)$(pkgconfigdir)'
496 $(MKDIR_P) '$(DESTDIR)$(pkglibdir)'
504 .PHONY: uninstall-lib
507 rm -f '$(DESTDIR)$(libdir)/$(stlib)'
508 rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
509 '$(DESTDIR)$(libdir)/$(shlib_major)' \
510 '$(DESTDIR)$(libdir)/$(shlib)' \
511 '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
513 rm -f '$(DESTDIR)$(pkglibdir)/$(shlib)'
523 rm -f $(shlib) $(shlib_bare) $(shlib_major) $(stlib) $(exports_file) lib$(NAME).pc
525 ifneq (,$(SHLIB_EXPORTS))
526 maintainer-clean-lib:
527 rm -f lib$(NAME)dll.def lib$(NAME)ddll.def blib$(NAME)dll.def