1 #-------------------------------------------------------------------------
4 # Common rules for building shared libraries
6 # Copyright (c) 1998, Regents of the University of California
9 # $PostgreSQL: pgsql/src/Makefile.shlib,v 1.112 2008/02/26 10:45:24 petere Exp $
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 # SO_MAJOR_VERSION Major version number to use for shared library
23 # SO_MINOR_VERSION Minor version number to use for shared library
24 # OBJS List of object files to include in library
25 # SHLIB_LINK If shared library relies on other libraries,
26 # additional stuff to put in its link command
27 # SHLIB_EXPORTS (optional) Name of file containing list of symbols to
29 # (If you want a patchlevel, include it in SO_MINOR_VERSION, e.g., "6.2".)
31 # Optional flags when building DLL's (only applicable to win32 and cygwin
33 # DLLTOOL_DEFFLAGS Additional flags when creating the dll .def file
34 # DLLTOOL_LIBFLAGS Additional flags when creating the lib<module>.a file
35 # DLLWRAP_FLAGS Additional flags to dllwrap
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 # uninstall-lib remove the libraries from $(libdir)
46 # clean-lib delete the static and shared libraries from the build dir
47 # maintainer-clean-lib delete .def files built for win32
49 # Since `all-lib' is the first rule in this file you probably want to
50 # have the `all' target before including this file. In the most simple
51 # case it would look like this:
55 # Similarly, the install rule might look like
57 # install: install-lib
59 # plus any additional things you want to install. Et cetera.
61 # Got that? Look at src/interfaces/libpq/Makefile for an example.
63 # While the linker allows creation of most shared libraries,
64 # -Bsymbolic requires resolution of all symbols, making the
65 # compiler a better choice for shared library creation on ELF platforms.
66 # With the linker, -Bsymbolic requires the crt1.o startup object file.
70 COMPILER = $(CC) $(CFLAGS)
71 LINK.static = $(AR) $(AROPT)
75 ifeq ($(enable_shared), yes)
77 # Insert -L from LDFLAGS after any -L already present in SHLIB_LINK
78 SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) $(filter -L%, $(LDFLAGS)) $(filter-out -L%, $(SHLIB_LINK))
80 # Need a -L-free version of LDFLAGS to use in combination with SHLIB_LINK
81 LDFLAGS_NO_L = $(filter-out -L%, $(LDFLAGS))
83 # Default shlib 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)
88 # For each platform we support shared libraries on, set shlib to the
89 # name of the library (if default above is not right), set
90 # LINK.shared to the command to link the library,
91 # and adjust SHLIB_LINK if necessary.
93 # Try to keep the sections in some kind of order, folks...
95 override CFLAGS += $(CFLAGS_SL)
97 soname = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
99 ifeq ($(PORTNAME), aix)
100 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
102 exports_file = lib$(NAME).exp
105 ifeq ($(PORTNAME), darwin)
106 ifneq ($(SO_MAJOR_VERSION), 0)
107 version_link = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
109 ifeq ($(DLTYPE), library)
112 LINK.shared = $(COMPILER) -dynamiclib -install_name $(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX) $(version_link) $(exported_symbols_list) -multiply_defined suppress
114 # loadable module (default case)
116 LINK.shared = $(COMPILER) -bundle -multiply_defined suppress
118 shlib = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
119 shlib_major = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
120 BUILD.exports = $(AWK) '/^[^\#]/ {printf "_%s\n",$$1}' $< >$@
121 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
122 ifneq (,$(exports_file))
123 exported_symbols_list = -exported_symbols_list $(exports_file)
127 ifeq ($(PORTNAME), openbsd)
129 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
132 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
136 ifeq ($(PORTNAME), bsdi)
137 ifeq ($(DLSUFFIX), .so)
138 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
141 ifeq ($(DLSUFFIX), .o)
142 LINK.shared = shlicc -O $(LDREL)
146 ifeq ($(PORTNAME), freebsd)
148 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
149 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
151 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
152 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
156 ifeq ($(PORTNAME), netbsd)
158 LINK.shared = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
160 LINK.shared = $(LD) -x -Bshareable -Bforcearchive
164 ifeq ($(PORTNAME), hpux)
165 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
166 ifeq ($(with_gnu_ld), yes)
167 LINK.shared = $(CC) $(LDFLAGS_NO_L) -shared -Wl,-h -Wl,$(soname)
169 # can't use the CC-syntax rpath pattern here
171 ifeq ($(enable_rpath), yes)
172 LINK.shared = $(LD) +h $(soname) -b +b '$(rpathdir)'
174 LINK.shared = $(LD) +h $(soname) -b
176 # On HPUX platforms, gcc is usually configured to search for libraries
177 # in /usr/local/lib, but ld won't do so. Add an explicit -L switch so
178 # ld can find the same libraries gcc does. Make sure it goes after any
179 # -L switches provided explicitly.
181 SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) -L/usr/local/lib $(filter-out -L%, $(SHLIB_LINK))
184 # do this last so above filtering doesn't pull out -L switches in LDFLAGS
186 SHLIB_LINK += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
190 ifeq ($(PORTNAME), irix)
191 shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
192 LINK.shared = $(COMPILER) -shared -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
195 ifeq ($(PORTNAME), linux)
196 LINK.shared = $(COMPILER) -shared -Wl,-soname,$(soname)
197 BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
198 exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
199 ifneq (,$(exports_file))
200 LINK.shared += -Wl,--version-script=$(exports_file)
204 ifeq ($(PORTNAME), solaris)
206 LINK.shared = $(COMPILER) -shared # $(COMPILER) needed for -m64
208 # CFLAGS added for X86_64
209 LINK.shared = $(CC) -G $(CFLAGS)
211 ifeq ($(with_gnu_ld), yes)
212 LINK.shared += -Wl,-soname,$(soname)
214 LINK.shared += -h $(soname)
218 ifeq ($(PORTNAME), sunos4)
219 LINK.shared = $(LD) -assert pure-text -Bdynamic
222 ifeq ($(PORTNAME), osf)
223 LINK.shared = $(LD) -shared -expect_unresolved '*'
226 ifeq ($(PORTNAME), sco)
228 LINK.shared = $(CC) -shared
230 LINK.shared = $(CC) -G
232 LINK.shared += -Wl,-z,text -Wl,-h,$(soname)
235 ifeq ($(PORTNAME), svr4)
236 LINK.shared = $(LD) -G
239 ifeq ($(PORTNAME), univel)
240 LINK.shared = $(LD) -G -z text
243 ifeq ($(PORTNAME), unixware)
245 LINK.shared = $(CC) -shared
247 LINK.shared = $(CC) -G
249 LINK.shared += -Wl,-z,text -Wl,-h,$(soname)
252 ifeq ($(PORTNAME), cygwin)
253 shlib = cyg$(NAME)$(DLSUFFIX)
257 ifeq ($(PORTNAME), win32)
258 shlib = lib$(NAME)$(DLSUFFIX)
262 ifeq ($(enable_rpath), yes)
263 SHLIB_LINK += $(rpath)
266 endif # enable_shared
274 .PHONY: all-lib all-static-lib all-shared-lib
276 all-lib: all-static-lib all-shared-lib
278 all-static-lib: lib$(NAME).a
280 all-shared-lib: $(shlib)
283 lib$(NAME).a: $(OBJS)
288 ifeq ($(enable_shared), yes)
290 ifeq (,$(filter cygwin win32,$(PORTNAME)))
291 ifneq ($(PORTNAME), aix)
295 $(LINK.shared) $(LDFLAGS_SL) $(OBJS) $(SHLIB_LINK) -o $@
296 # If we're using major and minor versions, then make a symlink to major-version-only.
297 ifneq ($(shlib), $(shlib_major))
299 $(LN_S) $(shlib) $(shlib_major)
301 # Make sure we have a link to a name without any version numbers
302 ifneq ($(shlib), $(shlib_bare))
304 $(LN_S) $(shlib) $(shlib_bare)
307 # Where possible, restrict the symbols exported by the library to just the
308 # official list, so as to avoid unintentional ABI changes. On recent Darwin
309 # this also quiets multiply-defined-symbol warnings in programs that use
310 # libpgport along with libpq.
311 ifneq (,$(SHLIB_EXPORTS))
313 $(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
315 $(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
320 else # PORTNAME == aix
323 $(shlib) lib$(NAME).a: $(OBJS)
324 $(LINK.static) lib$(NAME).a $^
325 $(RANLIB) lib$(NAME).a
326 $(MKLDEXPORT) lib$(NAME).a >$(exports_file)
327 $(COMPILER) $(LDFLAGS_NO_L) $(LDFLAGS_SL) -o $(shlib) lib$(NAME).a -Wl,-bE:$(exports_file) $(SHLIB_LINK)
329 $(AR) $(AROPT) lib$(NAME).a $(shlib)
331 endif # PORTNAME == aix
333 else # PORTNAME == cygwin || PORTNAME == win32
335 # Cygwin or Win32 case
337 DLL_DEFFILE = lib$(NAME)dll.def
339 # If SHLIB_EXPORTS is set, the rules below will build a .def file from
340 # that. Else we build a temporary one here.
341 ifeq (,$(SHLIB_EXPORTS))
342 exports_file = $(DLL_DEFFILE)
344 $(exports_file): $(OBJS)
345 $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $@ $^
348 $(shlib): $(OBJS) $(DLL_DEFFILE)
349 $(DLLWRAP) $(LDFLAGS_SL) -o $@ --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
351 lib$(NAME).a: $(shlib) $(DLL_DEFFILE)
352 $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib $@
354 endif # PORTNAME == cygwin || PORTNAME == win32
356 endif # enable_shared
359 # We need several not-quite-identical variants of .DEF files to build
360 # DLLs for Windows. These are made from the single source file
361 # exports.txt. Since we can't assume that Windows boxes will have
362 # sed, the .DEF files are always built and included in distribution
365 ifneq (,$(SHLIB_EXPORTS))
372 def-files: $(srcdir)/lib$(NAME)dll.def $(srcdir)/lib$(NAME)ddll.def $(srcdir)/blib$(NAME)dll.def
374 UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
376 $(srcdir)/lib$(NAME)dll.def: $(SHLIB_EXPORTS)
377 echo '; DEF file for MS VC++' >$@
378 echo 'LIBRARY LIB$(UC_NAME)' >>$@
380 sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
382 $(srcdir)/lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
383 echo '; DEF file for MS VC++' >$@
384 echo 'LIBRARY LIB$(UC_NAME)D' >>$@
386 sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/ \1@ \2/' $< >>$@
388 $(srcdir)/blib$(NAME)dll.def: $(SHLIB_EXPORTS)
389 echo '; DEF file for Borland C++ Builder' >$@
390 echo 'LIBRARY BLIB$(UC_NAME)' >>$@
392 sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/ _\1@ \2/' $< >>$@
394 echo '; Aliases for MS compatible names' >> $@
395 sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/ \1= _\1/' $< | sed 's/ *$$//' >>$@
396 endif # SHLIB_EXPORTS
403 .PHONY: install-lib install-lib-static install-lib-shared
404 install-lib: install-lib-static install-lib-shared
406 install-lib-static: lib$(NAME).a
407 $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/lib$(NAME).a'
408 ifeq ($(PORTNAME), darwin)
409 cd '$(DESTDIR)$(libdir)' && \
413 ifeq ($(enable_shared), yes)
414 install-lib-shared: $(shlib)
415 # we don't install $(shlib) on AIX
416 ifneq ($(PORTNAME), aix)
417 $(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
418 ifneq ($(PORTNAME), cygwin)
419 ifneq ($(PORTNAME), win32)
420 ifneq ($(shlib), $(shlib_major))
421 cd '$(DESTDIR)$(libdir)' && \
422 rm -f $(shlib_major) && \
423 $(LN_S) $(shlib) $(shlib_major)
425 ifneq ($(shlib), $(shlib_bare))
426 cd '$(DESTDIR)$(libdir)' && \
427 rm -f $(shlib_bare) && \
428 $(LN_S) $(shlib) $(shlib_bare)
433 endif # enable_shared
440 .PHONY: uninstall-lib
442 rm -f '$(DESTDIR)$(libdir)/lib$(NAME).a'
443 ifeq ($(enable_shared), yes)
444 rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
445 '$(DESTDIR)$(libdir)/$(shlib_major)' \
446 '$(DESTDIR)$(libdir)/$(shlib)'
447 endif # enable_shared
457 ifeq ($(enable_shared), yes)
458 rm -f $(shlib_bare) $(shlib_major) $(shlib) $(exports_file)
461 ifneq (,$(SHLIB_EXPORTS))
462 maintainer-clean-lib:
463 rm -f $(srcdir)/lib$(NAME)dll.def $(srcdir)/lib$(NAME)ddll.def $(srcdir)/blib$(NAME)dll.def