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
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 # Insert -L from LDFLAGS after any -L already present in SHLIB_LINK
82 SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) $(filter -L%, $(LDFLAGS)) $(filter-out -L%, $(SHLIB_LINK))
84 # Need a -L-free version of LDFLAGS to use in combination with SHLIB_LINK
85 LDFLAGS_NO_L = $(filter-out -L%, $(LDFLAGS))
87 ifdef SO_MAJOR_VERSION
88 # Default library naming convention used by the majority of platforms
89 ifeq ($(enable_shared), yes)
90 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
91 shlib_major = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
92 shlib_bare = lib$(NAME)$(DLSUFFIX)
94 # Testing the soname variable is a reliable way to determine whether a
95 # linkable library is being built.
96 soname = $(shlib_major)
98 # Naming convention for dynamically loadable modules
99 ifeq ($(enable_shared), yes)
100 shlib = $(NAME)$(DLSUFFIX)
106 # additional flags for backend modules
107 SHLIB_LINK := $(BE_DLLLIBS) $(SHLIB_LINK)
110 # For each platform we support shared libraries on, set shlib to the
111 # name of the library (if default above is not right), set
112 # LINK.shared to the command to link the library,
113 # and adjust SHLIB_LINK if necessary.
115 # Try to keep the sections in some kind of order, folks...
117 override CFLAGS += $(CFLAGS_SL)
119 ifeq ($(PORTNAME), aix)
120 ifdef SO_MAJOR_VERSION
121 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
124 exports_file = lib$(NAME).exp
127 ifeq ($(PORTNAME), darwin)
131 ifneq ($(SO_MAJOR_VERSION), 0)
132 version_link = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
134 LINK.shared = $(COMPILER) -dynamiclib -install_name $(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX) $(version_link) $(exported_symbols_list) -multiply_defined suppress
135 shlib = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
136 shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
140 LINK.shared = $(COMPILER) -bundle -multiply_defined suppress
142 BUILD.exports = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
143 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
144 ifneq (,$(exports_file))
145 exported_symbols_list = -exported_symbols_list $(exports_file)
149 ifeq ($(PORTNAME), openbsd)
151 LINK.shared = $(COMPILER) -shared
153 LINK.shared += -Wl,-x,-soname,$(soname)
157 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
161 ifeq ($(PORTNAME), bsdi)
162 ifeq ($(DLSUFFIX), .so)
163 LINK.shared = $(COMPILER) -shared
165 LINK.shared += -Wl,-x,-soname,$(soname)
169 ifeq ($(DLSUFFIX), .o)
170 LINK.shared = shlicc -O $(LDREL)
174 ifeq ($(PORTNAME), freebsd)
176 ifdef SO_MAJOR_VERSION
177 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
179 LINK.shared = $(COMPILER) -shared
181 LINK.shared += -Wl,-x,-soname,$(soname)
184 ifdef SO_MAJOR_VERSION
185 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
187 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
191 ifeq ($(PORTNAME), netbsd)
193 LINK.shared = $(COMPILER) -shared
195 LINK.shared += -Wl,-x,-soname,$(soname)
198 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
202 ifeq ($(PORTNAME), hpux)
203 ifdef SO_MAJOR_VERSION
204 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
206 ifeq ($(with_gnu_ld), yes)
207 LINK.shared = $(CC) $(LDFLAGS_NO_L) -shared
209 LINK.shared += -Wl,-h -Wl,$(soname)
212 # can't use the CC-syntax rpath pattern here
214 LINK.shared = $(LD) -b
216 LINK.shared += +h $(soname)
218 ifeq ($(enable_rpath), yes)
219 LINK.shared += +b '$(rpathdir)'
221 # On HPUX platforms, gcc is usually configured to search for libraries
222 # in /usr/local/lib, but ld won't do so. Add an explicit -L switch so
223 # ld can find the same libraries gcc does. Make sure it goes after any
224 # -L switches provided explicitly.
226 SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) -L/usr/local/lib $(filter-out -L%, $(SHLIB_LINK))
229 # do this last so above filtering doesn't pull out -L switches in LDFLAGS
231 SHLIB_LINK += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
235 ifeq ($(PORTNAME), irix)
236 ifdef SO_MAJOR_VERSION
237 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
239 LINK.shared = $(COMPILER) -shared
241 LINK.shared += -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
245 ifeq ($(PORTNAME), linux)
246 LINK.shared = $(COMPILER) -shared
248 LINK.shared += -Wl,-soname,$(soname)
250 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
251 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
252 ifneq (,$(exports_file))
253 LINK.shared += -Wl,--version-script=$(exports_file)
257 ifeq ($(PORTNAME), solaris)
259 LINK.shared = $(COMPILER) -shared
261 LINK.shared = $(COMPILER) -G
264 ifeq ($(with_gnu_ld), yes)
265 LINK.shared += -Wl,-soname,$(soname)
267 LINK.shared += -h $(soname)
272 ifeq ($(PORTNAME), sunos4)
273 LINK.shared = $(LD) -assert pure-text -Bdynamic
276 ifeq ($(PORTNAME), osf)
277 LINK.shared = $(LD) -shared -expect_unresolved '*'
280 ifeq ($(PORTNAME), sco)
282 LINK.shared = $(CC) -shared
284 LINK.shared = $(CC) -G
286 LINK.shared += -Wl,-z,text
288 LINK.shared += -Wl,-h,$(soname)
292 ifeq ($(PORTNAME), svr4)
293 LINK.shared = $(LD) -G
296 ifeq ($(PORTNAME), univel)
297 LINK.shared = $(LD) -G -z text
300 ifeq ($(PORTNAME), unixware)
302 LINK.shared = $(CC) -shared
304 LINK.shared = $(CC) -G
306 LINK.shared += -Wl,-z,text
308 LINK.shared += -Wl,-h,$(soname)
312 ifeq ($(PORTNAME), cygwin)
313 ifdef SO_MAJOR_VERSION
314 shlib = cyg$(NAME)$(DLSUFFIX)
319 ifeq ($(PORTNAME), win32)
320 ifdef SO_MAJOR_VERSION
321 shlib = lib$(NAME)$(DLSUFFIX)
326 ifeq ($(enable_rpath), yes)
327 SHLIB_LINK += $(rpath)
336 .PHONY: all-lib all-static-lib all-shared-lib
338 all-lib: all-shared-lib
340 # no static library when building a dynamically loadable module
341 all-lib: all-static-lib
344 all-static-lib: $(stlib)
346 all-shared-lib: $(shlib)
354 ifeq ($(enable_shared), yes)
356 ifeq (,$(filter cygwin win32,$(PORTNAME)))
357 ifneq ($(PORTNAME), aix)
361 $(LINK.shared) $(LDFLAGS_SL) $(OBJS) $(SHLIB_LINK) -o $@
363 # If we're using major and minor versions, then make a symlink to major-version-only.
364 ifneq ($(shlib), $(shlib_major))
366 $(LN_S) $(shlib) $(shlib_major)
368 # Make sure we have a link to a name without any version numbers
369 ifneq ($(shlib), $(shlib_bare))
371 $(LN_S) $(shlib) $(shlib_bare)
375 # Where possible, restrict the symbols exported by the library to just the
376 # official list, so as to avoid unintentional ABI changes. On recent Darwin
377 # this also quiets multiply-defined-symbol warnings in programs that use
378 # libpgport along with libpq.
379 ifneq (,$(SHLIB_EXPORTS))
381 $(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
383 $(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
388 else # PORTNAME == aix
391 $(shlib) $(stlib): $(OBJS)
392 $(LINK.static) $(stlib) $^
394 $(MKLDEXPORT) $(stlib) >$(exports_file)
395 $(COMPILER) $(LDFLAGS_NO_L) $(LDFLAGS_SL) -o $(shlib) $(stlib) -Wl,-bE:$(exports_file) $(SHLIB_LINK)
397 $(AR) $(AROPT) $(stlib) $(shlib)
399 endif # PORTNAME == aix
401 else # PORTNAME == cygwin || PORTNAME == win32
403 # Cygwin or Win32 case
405 DLL_DEFFILE = lib$(NAME)dll.def
407 # If SHLIB_EXPORTS is set, the rules below will build a .def file from
408 # that. Else we build a temporary one here.
409 ifeq (,$(SHLIB_EXPORTS))
410 exports_file = $(DLL_DEFFILE)
412 $(exports_file): $(OBJS)
413 $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $@ $^
416 $(shlib): $(OBJS) $(DLL_DEFFILE)
417 $(DLLWRAP) $(LDFLAGS_SL) -o $@ --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
419 $(stlib): $(shlib) $(DLL_DEFFILE)
420 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib $@
422 endif # PORTNAME == cygwin || PORTNAME == win32
424 endif # enable_shared
427 # We need several not-quite-identical variants of .DEF files to build
428 # DLLs for Windows. These are made from the single source file
429 # exports.txt. Since we can't assume that Windows boxes will have
430 # sed, the .DEF files are always built and included in distribution
433 ifneq (,$(SHLIB_EXPORTS))
434 distprep: $(srcdir)/lib$(NAME)dll.def $(srcdir)/lib$(NAME)ddll.def $(srcdir)/blib$(NAME)dll.def
436 UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
438 $(srcdir)/lib$(NAME)dll.def: $(SHLIB_EXPORTS)
439 echo '; DEF file for MS VC++' >$@
440 echo 'LIBRARY LIB$(UC_NAME)' >>$@
442 sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
444 $(srcdir)/lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
445 echo '; DEF file for MS VC++' >$@
446 echo 'LIBRARY LIB$(UC_NAME)D' >>$@
448 sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
450 $(srcdir)/blib$(NAME)dll.def: $(SHLIB_EXPORTS)
451 echo '; DEF file for Borland C++ Builder' >$@
452 echo 'LIBRARY BLIB$(UC_NAME)' >>$@
454 sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/ _\1@ \2/' $< >>$@
456 echo '; Aliases for MS compatible names' >> $@
457 sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/ \1= _\1/' $< | sed 's/ *$$//' >>$@
458 endif # SHLIB_EXPORTS
465 .PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
466 install-lib: install-lib-shared
468 install-lib: install-lib-static
471 install-lib-static: $(stlib) installdirs-lib
472 $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
473 ifeq ($(PORTNAME), darwin)
474 cd '$(DESTDIR)$(libdir)' && \
478 ifeq ($(enable_shared), yes)
479 install-lib-shared: $(shlib) installdirs-lib
481 # we don't install $(shlib) on AIX
482 ifneq ($(PORTNAME), aix)
483 $(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
484 ifneq ($(PORTNAME), cygwin)
485 ifneq ($(PORTNAME), win32)
486 ifneq ($(shlib), $(shlib_major))
487 cd '$(DESTDIR)$(libdir)' && \
488 rm -f $(shlib_major) && \
489 $(LN_S) $(shlib) $(shlib_major)
491 ifneq ($(shlib), $(shlib_bare))
492 cd '$(DESTDIR)$(libdir)' && \
493 rm -f $(shlib_bare) && \
494 $(LN_S) $(shlib) $(shlib_bare)
500 $(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
502 else # not enable_shared
506 echo "* Module $(NAME) was not installed due to lack of shared library support."; \
509 endif # enable_shared
514 $(mkinstalldirs) '$(DESTDIR)$(libdir)'
516 $(mkinstalldirs) '$(DESTDIR)$(pkglibdir)'
524 .PHONY: uninstall-lib
527 rm -f '$(DESTDIR)$(libdir)/$(stlib)'
528 ifeq ($(enable_shared), yes)
529 rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
530 '$(DESTDIR)$(libdir)/$(shlib_major)' \
531 '$(DESTDIR)$(libdir)/$(shlib)'
532 endif # enable_shared
534 rm -f '$(DESTDIR)$(pkglibdir)/$(shlib)'
544 rm -f $(shlib) $(shlib_bare) $(shlib_major) $(stlib) $(exports_file)
546 ifneq (,$(SHLIB_EXPORTS))
547 maintainer-clean-lib:
548 rm -f $(srcdir)/lib$(NAME)dll.def $(srcdir)/lib$(NAME)ddll.def $(srcdir)/blib$(NAME)dll.def