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_EXPORTS (optional) Name of file containing list of symbols to
26 # export, in the format "function_name number"
28 # When building a shared library, the following version information
29 # must also be set. It should be omitted when building a dynamically
32 # SO_MAJOR_VERSION Major version number to use for shared library
33 # SO_MINOR_VERSION Minor version number to use for shared library
34 # (If you want a patchlevel, include it in SO_MINOR_VERSION, e.g., "6.2".)
36 # Optional flags when building DLL's (only applicable to win32 and cygwin
38 # DLLTOOL_DEFFLAGS Additional flags when creating the dll .def file
39 # DLLTOOL_LIBFLAGS Additional flags when creating the lib<module>.a file
40 # DLLWRAP_FLAGS Additional flags to dllwrap
42 # The module Makefile must also include
43 # $(top_builddir)/src/Makefile.global before including this file.
44 # (Makefile.global sets PORTNAME and other needed symbols.)
46 # This makefile provides the following (phony) targets:
48 # all-lib build the static and shared (if applicable) libraries
49 # install-lib install the libraries into $(libdir)
50 # installdirs-lib create installation directory $(libdir)
51 # uninstall-lib remove the libraries from $(libdir)
52 # clean-lib delete the static and shared libraries from the build dir
53 # maintainer-clean-lib delete .def files built for win32
55 # Since `all-lib' is the first rule in this file you probably want to
56 # have the `all' target before including this file. In the most simple
57 # case it would look like this:
61 # Similarly, the install rule might look like
63 # install: install-lib
65 # plus any additional things you want to install. Et cetera.
67 # Got that? Look at src/interfaces/libpq/Makefile for an example.
69 # While the linker allows creation of most shared libraries,
70 # -Bsymbolic requires resolution of all symbols, making the
71 # compiler a better choice for shared library creation on ELF platforms.
72 # With the linker, -Bsymbolic requires the crt1.o startup object file.
76 COMPILER = $(CC) $(CFLAGS)
77 LINK.static = $(AR) $(AROPT)
81 ifdef SO_MAJOR_VERSION
82 # Default library naming convention used by the majority of platforms
83 ifeq ($(enable_shared), yes)
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)
88 # Testing the soname variable is a reliable way to determine whether a
89 # linkable library is being built.
90 soname = $(shlib_major)
92 # Naming convention for dynamically loadable modules
93 ifeq ($(enable_shared), yes)
94 shlib = $(NAME)$(DLSUFFIX)
100 # additional flags for backend modules
101 SHLIB_LINK += $(BE_DLLLIBS)
104 # For each platform we support shared libraries on, set shlib to the
105 # name of the library (if default above is not right), set
106 # LINK.shared to the command to link the library,
107 # and adjust SHLIB_LINK if necessary.
109 # Try to keep the sections in some kind of order, folks...
111 override CFLAGS += $(CFLAGS_SL)
112 ifdef SO_MAJOR_VERSION
113 # libraries ought to use this to refer to versioned gettext domain names
114 override CPPFLAGS += -DSO_MAJOR_VERSION=$(SO_MAJOR_VERSION)
117 ifeq ($(PORTNAME), aix)
118 ifdef SO_MAJOR_VERSION
119 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
122 exports_file = lib$(NAME).exp
125 ifeq ($(PORTNAME), darwin)
129 ifneq ($(SO_MAJOR_VERSION), 0)
130 version_link = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
132 LINK.shared = $(COMPILER) -dynamiclib -install_name $(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX) $(version_link) $(exported_symbols_list) -multiply_defined suppress
133 shlib = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
134 shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
138 LINK.shared = $(COMPILER) -bundle -multiply_defined suppress
140 BUILD.exports = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
141 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
142 ifneq (,$(exports_file))
143 exported_symbols_list = -exported_symbols_list $(exports_file)
147 ifeq ($(PORTNAME), openbsd)
149 LINK.shared = $(COMPILER) -shared
151 LINK.shared += -Wl,-x,-soname,$(soname)
155 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
159 ifeq ($(PORTNAME), bsdi)
160 ifeq ($(DLSUFFIX), .so)
161 LINK.shared = $(COMPILER) -shared
163 LINK.shared += -Wl,-x,-soname,$(soname)
167 ifeq ($(DLSUFFIX), .o)
168 LINK.shared = shlicc -O $(LDREL)
172 ifeq ($(PORTNAME), freebsd)
174 ifdef SO_MAJOR_VERSION
175 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
177 LINK.shared = $(COMPILER) -shared
179 LINK.shared += -Wl,-x,-soname,$(soname)
182 ifdef SO_MAJOR_VERSION
183 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
185 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
189 ifeq ($(PORTNAME), netbsd)
191 LINK.shared = $(COMPILER) -shared
193 LINK.shared += -Wl,-x,-soname,$(soname)
196 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
200 ifeq ($(PORTNAME), hpux)
201 ifdef SO_MAJOR_VERSION
202 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
204 ifeq ($(with_gnu_ld), yes)
205 LINK.shared = $(CC) -shared
207 LINK.shared += -Wl,-h -Wl,$(soname)
210 LINK.shared = $(LD) -b
212 LINK.shared += +h $(soname)
214 # can't use the CC-syntax rpath pattern here, so instead:
216 ifeq ($(enable_rpath), yes)
217 LINK.shared += +b '$(rpathdir)'
219 # On HPUX platforms, gcc is usually configured to search for libraries
220 # in /usr/local/lib, but ld won't do so. Add an explicit -L switch so
221 # ld can find the same libraries gcc does. Make sure it goes after any
222 # -L switches provided explicitly.
224 SHLIB_LINK += -L/usr/local/lib
227 # And we need to link with libgcc, too
229 SHLIB_LINK += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
233 ifeq ($(PORTNAME), irix)
234 ifdef SO_MAJOR_VERSION
235 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
237 LINK.shared = $(COMPILER) -shared
239 LINK.shared += -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
243 ifeq ($(PORTNAME), linux)
244 LINK.shared = $(COMPILER) -shared
246 LINK.shared += -Wl,-soname,$(soname)
248 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
249 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
250 ifneq (,$(exports_file))
251 LINK.shared += -Wl,--version-script=$(exports_file)
255 ifeq ($(PORTNAME), solaris)
257 LINK.shared = $(COMPILER) -shared
259 LINK.shared = $(COMPILER) -G
262 ifeq ($(with_gnu_ld), yes)
263 LINK.shared += -Wl,-soname,$(soname)
265 LINK.shared += -h $(soname)
270 ifeq ($(PORTNAME), sunos4)
271 LINK.shared = $(LD) -assert pure-text -Bdynamic
274 ifeq ($(PORTNAME), osf)
275 LINK.shared = $(LD) -shared -expect_unresolved '*'
278 ifeq ($(PORTNAME), sco)
280 LINK.shared = $(CC) -shared
282 LINK.shared = $(CC) -G
284 LINK.shared += -Wl,-z,text
286 LINK.shared += -Wl,-h,$(soname)
290 ifeq ($(PORTNAME), svr4)
291 LINK.shared = $(LD) -G
294 ifeq ($(PORTNAME), univel)
295 LINK.shared = $(LD) -G -z text
298 ifeq ($(PORTNAME), unixware)
300 LINK.shared = $(CC) -shared
302 LINK.shared = $(CC) -G
304 LINK.shared += -Wl,-z,text
306 LINK.shared += -Wl,-h,$(soname)
310 ifeq ($(PORTNAME), cygwin)
311 ifdef SO_MAJOR_VERSION
312 shlib = cyg$(NAME)$(DLSUFFIX)
317 ifeq ($(PORTNAME), win32)
318 ifdef SO_MAJOR_VERSION
319 shlib = lib$(NAME)$(DLSUFFIX)
330 .PHONY: all-lib all-static-lib all-shared-lib
332 all-lib: all-shared-lib
334 # no static library when building a dynamically loadable module
335 all-lib: all-static-lib
338 all-static-lib: $(stlib)
340 all-shared-lib: $(shlib)
348 ifeq ($(enable_shared), yes)
350 ifeq (,$(filter cygwin win32,$(PORTNAME)))
351 ifneq ($(PORTNAME), aix)
355 $(LINK.shared) -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
357 # If we're using major and minor versions, then make a symlink to major-version-only.
358 ifneq ($(shlib), $(shlib_major))
360 $(LN_S) $(shlib) $(shlib_major)
362 # Make sure we have a link to a name without any version numbers
363 ifneq ($(shlib), $(shlib_bare))
365 $(LN_S) $(shlib) $(shlib_bare)
369 # Where possible, restrict the symbols exported by the library to just the
370 # official list, so as to avoid unintentional ABI changes. On recent Darwin
371 # this also quiets multiply-defined-symbol warnings in programs that use
372 # libpgport along with libpq.
373 ifneq (,$(SHLIB_EXPORTS))
375 $(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
377 $(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
382 else # PORTNAME == aix
385 $(shlib) $(stlib): $(OBJS)
386 $(LINK.static) $(stlib) $^
388 $(MKLDEXPORT) $(stlib) >$(exports_file)
389 $(COMPILER) -o $(shlib) $(stlib) -Wl,-bE:$(exports_file) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
391 $(AR) $(AROPT) $(stlib) $(shlib)
393 endif # PORTNAME == aix
395 else # PORTNAME == cygwin || PORTNAME == win32
397 # Cygwin or Win32 case
399 # If SHLIB_EXPORTS is set, the rules below will build a .def file from
400 # that. Else we build a temporary one here.
401 ifeq (,$(SHLIB_EXPORTS))
402 DLL_DEFFILE = lib$(NAME)dll.def
403 exports_file = $(DLL_DEFFILE)
405 $(exports_file): $(OBJS)
406 $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $@ $^
408 DLL_DEFFILE = lib$(NAME)dll.def
411 $(shlib): $(OBJS) $(DLL_DEFFILE)
412 $(DLLWRAP) -o $@ --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
414 $(stlib): $(shlib) $(DLL_DEFFILE)
415 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib $@
417 endif # PORTNAME == cygwin || PORTNAME == win32
419 endif # enable_shared
422 # We need several not-quite-identical variants of .DEF files to build
423 # DLLs for Windows. These are made from the single source file
424 # exports.txt. Since we can't assume that Windows boxes will have
425 # sed, the .DEF files are always built and included in distribution
428 ifneq (,$(SHLIB_EXPORTS))
429 distprep: lib$(NAME)dll.def lib$(NAME)ddll.def blib$(NAME)dll.def
431 UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
433 lib$(NAME)dll.def: $(SHLIB_EXPORTS)
434 echo '; DEF file for MS VC++' >$@
435 echo 'LIBRARY LIB$(UC_NAME)' >>$@
437 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
439 lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
440 echo '; DEF file for MS VC++' >$@
441 echo 'LIBRARY LIB$(UC_NAME)D' >>$@
443 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
445 blib$(NAME)dll.def: $(SHLIB_EXPORTS)
446 echo '; DEF file for Borland C++ Builder' >$@
447 echo 'LIBRARY BLIB$(UC_NAME)' >>$@
449 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ _\1@ \2/' $< >>$@
451 echo '; Aliases for MS compatible names' >> $@
452 sed -e '/^#/d' -e 's/^\(.*[ ]\)\([0-9][0-9]*\)/ \1= _\1/' $< | sed 's/ *$$//' >>$@
453 endif # SHLIB_EXPORTS
460 .PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
461 install-lib: install-lib-shared
463 install-lib: install-lib-static
466 install-lib-static: $(stlib) installdirs-lib
467 $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
468 ifeq ($(PORTNAME), darwin)
469 cd '$(DESTDIR)$(libdir)' && \
473 ifeq ($(enable_shared), yes)
474 install-lib-shared: $(shlib) installdirs-lib
476 # we don't install $(shlib) on AIX
477 # (see http://archives.postgresql.org/message-id/52EF20B2E3209443BC37736D00C3C1380A6E79FE@EXADV1.host.magwien.gv.at)
478 ifneq ($(PORTNAME), aix)
479 $(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
480 ifneq ($(PORTNAME), cygwin)
481 ifneq ($(PORTNAME), win32)
482 ifneq ($(shlib), $(shlib_major))
483 cd '$(DESTDIR)$(libdir)' && \
484 rm -f $(shlib_major) && \
485 $(LN_S) $(shlib) $(shlib_major)
487 ifneq ($(shlib), $(shlib_bare))
488 cd '$(DESTDIR)$(libdir)' && \
489 rm -f $(shlib_bare) && \
490 $(LN_S) $(shlib) $(shlib_bare)
496 $(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
498 else # not enable_shared
502 echo "* Module $(NAME) was not installed due to lack of shared library support."; \
505 endif # enable_shared
510 $(MKDIR_P) '$(DESTDIR)$(libdir)'
512 $(MKDIR_P) '$(DESTDIR)$(pkglibdir)'
520 .PHONY: uninstall-lib
523 rm -f '$(DESTDIR)$(libdir)/$(stlib)'
524 ifeq ($(enable_shared), yes)
525 rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
526 '$(DESTDIR)$(libdir)/$(shlib_major)' \
527 '$(DESTDIR)$(libdir)/$(shlib)'
528 endif # enable_shared
530 rm -f '$(DESTDIR)$(pkglibdir)/$(shlib)'
540 rm -f $(shlib) $(shlib_bare) $(shlib_major) $(stlib) $(exports_file)
542 ifneq (,$(SHLIB_EXPORTS))
543 maintainer-clean-lib:
544 rm -f lib$(NAME)dll.def lib$(NAME)ddll.def blib$(NAME)dll.def