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 # Optional flags when building DLL's (only applicable to win32 and cygwin
39 # DLLTOOL_DEFFLAGS Additional flags when creating the dll .def file
40 # DLLTOOL_LIBFLAGS Additional flags when creating the lib<module>.a file
41 # DLLWRAP_FLAGS Additional flags to dllwrap
43 # The module Makefile must also include
44 # $(top_builddir)/src/Makefile.global before including this file.
45 # (Makefile.global sets PORTNAME and other needed symbols.)
47 # This makefile provides the following (phony) targets:
49 # all-lib build the static and shared (if applicable) libraries
50 # install-lib install the libraries into $(libdir)
51 # installdirs-lib create installation directory $(libdir)
52 # uninstall-lib remove the libraries from $(libdir)
53 # clean-lib delete the static and shared libraries from the build dir
54 # maintainer-clean-lib delete .def files built for win32
56 # Since `all-lib' is the first rule in this file you probably want to
57 # have the `all' target before including this file. In the most simple
58 # case it would look like this:
62 # Similarly, the install rule might look like
64 # install: install-lib
66 # plus any additional things you want to install. Et cetera.
68 # Got that? Look at src/interfaces/libpq/Makefile for an example.
70 # While the linker allows creation of most shared libraries,
71 # -Bsymbolic requires resolution of all symbols, making the
72 # compiler a better choice for shared library creation on ELF platforms.
73 # With the linker, -Bsymbolic requires the crt1.o startup object file.
77 COMPILER = $(CC) $(CFLAGS)
78 LINK.static = $(AR) $(AROPT)
82 ifdef SO_MAJOR_VERSION
83 # Default library naming convention used by the majority of platforms
84 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
85 shlib_major = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
86 shlib_bare = lib$(NAME)$(DLSUFFIX)
87 # Testing the soname variable is a reliable way to determine whether a
88 # linkable library is being built.
89 soname = $(shlib_major)
90 pkgconfigdir = $(libdir)/pkgconfig
92 # Naming convention for dynamically loadable modules
93 shlib = $(NAME)$(DLSUFFIX)
98 # additional flags for backend modules
99 SHLIB_LINK += $(BE_DLLLIBS)
102 # For each platform we support shared libraries on, set shlib to the
103 # name of the library (if default above is not right), set
104 # LINK.shared to the command to link the library,
105 # and adjust SHLIB_LINK if necessary.
107 # Try to keep the sections in some kind of order, folks...
109 override CFLAGS += $(CFLAGS_SL)
110 ifdef SO_MAJOR_VERSION
111 # libraries ought to use this to refer to versioned gettext domain names
112 override CPPFLAGS += -DSO_MAJOR_VERSION=$(SO_MAJOR_VERSION)
115 ifeq ($(PORTNAME), aix)
116 ifdef SO_MAJOR_VERSION
117 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
120 exports_file = lib$(NAME).exp
123 ifeq ($(PORTNAME), darwin)
127 ifneq ($(SO_MAJOR_VERSION), 0)
128 version_link = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
130 LINK.shared = $(COMPILER) -dynamiclib -install_name '$(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)' $(version_link) $(exported_symbols_list) -multiply_defined suppress
131 shlib = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
132 shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
136 LINK.shared = $(COMPILER) -bundle -multiply_defined suppress
138 BUILD.exports = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
139 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
140 ifneq (,$(exports_file))
141 exported_symbols_list = -exported_symbols_list $(exports_file)
145 ifeq ($(PORTNAME), openbsd)
147 LINK.shared = $(COMPILER) -shared
149 LINK.shared += -Wl,-x,-soname,$(soname)
153 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
157 ifeq ($(PORTNAME), freebsd)
159 ifdef SO_MAJOR_VERSION
160 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
162 LINK.shared = $(COMPILER) -shared
164 LINK.shared += -Wl,-x,-soname,$(soname)
167 ifdef SO_MAJOR_VERSION
168 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
170 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
174 ifeq ($(PORTNAME), netbsd)
176 LINK.shared = $(COMPILER) -shared
178 LINK.shared += -Wl,-x,-soname,$(soname)
181 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
185 ifeq ($(PORTNAME), hpux)
186 ifdef SO_MAJOR_VERSION
187 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
189 ifeq ($(with_gnu_ld), yes)
190 LINK.shared = $(CC) -shared
192 LINK.shared += -Wl,-h -Wl,$(soname)
195 LINK.shared = $(LD) -b
197 LINK.shared += +h $(soname)
199 # can't use the CC-syntax rpath pattern here, so instead:
201 ifeq ($(enable_rpath), yes)
202 LINK.shared += +b '$(rpathdir)'
204 # On HPUX platforms, gcc is usually configured to search for libraries
205 # in /usr/local/lib, but ld won't do so. Add an explicit -L switch so
206 # ld can find the same libraries gcc does. Make sure it goes after any
207 # -L switches provided explicitly.
209 SHLIB_LINK += -L/usr/local/lib
212 # And we need to link with libgcc, too
214 SHLIB_LINK += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
218 ifeq ($(PORTNAME), irix)
219 ifdef SO_MAJOR_VERSION
220 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
222 LINK.shared = $(COMPILER) -shared
224 LINK.shared += -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
228 ifeq ($(PORTNAME), linux)
229 LINK.shared = $(COMPILER) -shared
231 LINK.shared += -Wl,-soname,$(soname)
233 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
234 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
235 ifneq (,$(exports_file))
236 LINK.shared += -Wl,--version-script=$(exports_file)
240 ifeq ($(PORTNAME), solaris)
242 LINK.shared = $(COMPILER) -shared
244 LINK.shared = $(COMPILER) -G
247 ifeq ($(with_gnu_ld), yes)
248 LINK.shared += -Wl,-soname,$(soname)
250 LINK.shared += -h $(soname)
255 ifeq ($(PORTNAME), osf)
256 LINK.shared = $(LD) -shared -expect_unresolved '*'
259 ifeq ($(PORTNAME), sco)
261 LINK.shared = $(CC) -shared
263 LINK.shared = $(CC) -G
265 LINK.shared += -Wl,-z,text
267 LINK.shared += -Wl,-h,$(soname)
271 ifeq ($(PORTNAME), unixware)
273 LINK.shared = $(CC) -shared
275 LINK.shared = $(CC) -G
277 LINK.shared += -Wl,-z,text
279 LINK.shared += -Wl,-h,$(soname)
283 ifeq ($(PORTNAME), cygwin)
284 ifdef SO_MAJOR_VERSION
285 shlib = cyg$(NAME)$(DLSUFFIX)
290 ifeq ($(PORTNAME), win32)
291 ifdef SO_MAJOR_VERSION
292 shlib = lib$(NAME)$(DLSUFFIX)
303 .PHONY: all-lib all-static-lib all-shared-lib
305 all-lib: all-shared-lib
307 # no static library when building a dynamically loadable module
308 all-lib: all-static-lib
309 all-lib: lib$(NAME).pc
312 all-static-lib: $(stlib)
314 all-shared-lib: $(shlib)
317 $(stlib): $(OBJS) | $(SHLIB_PREREQS)
323 ifeq (,$(filter cygwin win32,$(PORTNAME)))
324 ifneq ($(PORTNAME), aix)
327 $(shlib): $(OBJS) | $(SHLIB_PREREQS)
328 $(LINK.shared) -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
330 # If we're using major and minor versions, then make a symlink to major-version-only.
331 ifneq ($(shlib), $(shlib_major))
333 $(LN_S) $(shlib) $(shlib_major)
335 # Make sure we have a link to a name without any version numbers
336 ifneq ($(shlib), $(shlib_bare))
338 $(LN_S) $(shlib) $(shlib_bare)
342 # Where possible, restrict the symbols exported by the library to just the
343 # official list, so as to avoid unintentional ABI changes. On recent Darwin
344 # this also quiets multiply-defined-symbol warnings in programs that use
345 # libpgport along with libpq.
346 ifneq (,$(SHLIB_EXPORTS))
348 $(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
350 $(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
355 else # PORTNAME == aix
358 $(shlib) $(stlib): $(OBJS) | $(SHLIB_PREREQS)
359 $(LINK.static) $(stlib) $^
361 $(MKLDEXPORT) $(stlib) >$(exports_file)
362 $(COMPILER) -o $(shlib) $(stlib) -Wl,-bE:$(exports_file) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
364 $(AR) $(AROPT) $(stlib) $(shlib)
366 endif # PORTNAME == aix
368 else # PORTNAME == cygwin || PORTNAME == win32
370 # Cygwin or Win32 case
372 # If SHLIB_EXPORTS is set, the rules below will build a .def file from
373 # that. Else we build a temporary one here.
374 ifeq (,$(SHLIB_EXPORTS))
375 DLL_DEFFILE = lib$(NAME)dll.def
376 exports_file = $(DLL_DEFFILE)
378 $(exports_file): $(OBJS)
379 $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $@ $^
381 DLL_DEFFILE = lib$(NAME)dll.def
384 $(shlib): $(OBJS) $(DLL_DEFFILE) | $(SHLIB_PREREQS)
385 $(DLLWRAP) -o $@ --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
387 $(stlib): $(shlib) $(DLL_DEFFILE) | $(SHLIB_PREREQS)
388 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib $@
390 endif # PORTNAME == cygwin || PORTNAME == win32
393 %.pc: $(MAKEFILE_LIST)
394 echo 'Name: lib$(NAME)' >$@
395 echo 'Description: PostgreSQL lib$(NAME) library' >>$@
396 echo 'Url: http://www.postgresql.org/' >>$@
397 echo 'Version: $(VERSION)' >>$@
398 echo 'Requires: ' >>$@
399 echo 'Requires.private: $(PKG_CONFIG_REQUIRES_PRIVATE)' >>$@
400 echo 'Cflags: -I$(includedir)' >>$@
401 echo 'Libs: -L$(libdir) -l$(NAME)' >>$@
402 # Record -L flags that the user might have passed in to the PostgreSQL
403 # build to locate third-party libraries (e.g., ldap, ssl). Filter out
404 # those that point inside the build or source tree. Use sort to
405 # remove duplicates. Also record the -l flags necessary for static
406 # linking, but not those already covered by Requires.private.
407 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)))' >>$@
410 # We need several not-quite-identical variants of .DEF files to build
411 # DLLs for Windows. These are made from the single source file
412 # exports.txt. Since we can't assume that Windows boxes will have
413 # sed, the .DEF files are always built and included in distribution
416 ifneq (,$(SHLIB_EXPORTS))
417 distprep: lib$(NAME)dll.def lib$(NAME)ddll.def blib$(NAME)dll.def
419 UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
421 lib$(NAME)dll.def: $(SHLIB_EXPORTS)
422 echo '; DEF file for MS VC++' >$@
423 echo 'LIBRARY LIB$(UC_NAME)' >>$@
425 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
427 lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
428 echo '; DEF file for MS VC++' >$@
429 echo 'LIBRARY LIB$(UC_NAME)D' >>$@
431 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
433 blib$(NAME)dll.def: $(SHLIB_EXPORTS)
434 echo '; DEF file for Borland C++ Builder' >$@
435 echo 'LIBRARY BLIB$(UC_NAME)' >>$@
437 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ _\1@ \2/' $< >>$@
439 echo '; Aliases for MS compatible names' >> $@
440 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1= _\1/' $< | sed 's/ *$$//' >>$@
441 endif # SHLIB_EXPORTS
448 .PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
449 install-lib: install-lib-shared
451 install-lib: install-lib-static
452 install-lib: install-lib-pc
455 install-lib-pc: lib$(NAME).pc installdirs-lib
456 $(INSTALL_DATA) $< '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
458 install-lib-static: $(stlib) installdirs-lib
459 $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
460 ifeq ($(PORTNAME), darwin)
461 cd '$(DESTDIR)$(libdir)' && \
465 install-lib-shared: $(shlib) installdirs-lib
467 # we don't install $(shlib) on AIX
468 # (see http://archives.postgresql.org/message-id/52EF20B2E3209443BC37736D00C3C1380A6E79FE@EXADV1.host.magwien.gv.at)
469 ifneq ($(PORTNAME), aix)
470 $(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
471 ifneq ($(PORTNAME), cygwin)
472 ifneq ($(PORTNAME), win32)
473 ifneq ($(shlib), $(shlib_major))
474 cd '$(DESTDIR)$(libdir)' && \
475 rm -f $(shlib_major) && \
476 $(LN_S) $(shlib) $(shlib_major)
478 ifneq ($(shlib), $(shlib_bare))
479 cd '$(DESTDIR)$(libdir)' && \
480 rm -f $(shlib_bare) && \
481 $(LN_S) $(shlib) $(shlib_bare)
487 $(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
493 $(MKDIR_P) '$(DESTDIR)$(libdir)' '$(DESTDIR)$(pkgconfigdir)'
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)' \
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 blib$(NAME)dll.def