1#-------------------------------------------------------------------------
2#
3# Makefile.shlib
4#    Common rules for building shared libraries
5#
6# Copyright (c) 1998, Regents of the University of California
7#
8# IDENTIFICATION
9#    src/Makefile.shlib
10#
11#-------------------------------------------------------------------------
12
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.
17#
18# Before including this file, the module Makefile must define these
19# variables:
20#
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            Stuff to append to library's link command
24#                       (typically, -L and -l switches for external libraries)
25# SHLIB_LINK_INTERNAL   -L and -l switches for Postgres-supplied libraries
26# SHLIB_PREREQS         Order-only prerequisites for library build target
27# SHLIB_EXPORTS         (optional) Name of file containing list of symbols to
28#                       export, in the format "function_name  number"
29#
30# Don't use SHLIB_LINK for references to files in the build tree, or the
31# wrong things will happen --- use SHLIB_LINK_INTERNAL for those!
32#
33# When building a shared library, the following version information
34# must also be set.  It should be omitted when building a dynamically
35# loadable module.
36#
37# SO_MAJOR_VERSION      Major version number to use for shared library
38# SO_MINOR_VERSION      Minor version number to use for shared library
39# (If you want a patchlevel, include it in SO_MINOR_VERSION, e.g., "6.2".)
40#
41# The module Makefile must also include
42# $(top_builddir)/src/Makefile.global before including this file.
43# (Makefile.global sets PORTNAME and other needed symbols.)
44#
45# This makefile provides the following (phony) targets:
46#
47# all-lib               build the static and shared (if applicable) libraries
48# install-lib           install the libraries into $(libdir)
49# installdirs-lib       create installation directory $(libdir)
50# uninstall-lib         remove the libraries from $(libdir)
51# clean-lib             delete the static and shared libraries from the build dir
52# maintainer-clean-lib  delete .def files built for win32
53#
54# Typically you would add `all-lib' to the `all' target so that `make all'
55# builds the libraries.  In the most simple case it would look like this:
56#
57#     all: all-lib
58#
59# Similarly, the install rule might look like
60#
61#     install: install-lib
62#
63# plus any additional things you want to install. Et cetera.
64#
65# Got that?  Look at src/interfaces/libpq/Makefile for an example.
66#
67# While the linker allows creation of most shared libraries,
68# -Bsymbolic requires resolution of all symbols, making the
69# compiler a better choice for shared library creation on ELF platforms.
70# With the linker, -Bsymbolic requires the crt1.o startup object file.
71# bjm 2001-02-10
72
73
74COMPILER = $(CC) $(CFLAGS)
75LINK.static = $(AR) $(AROPT)
76
77LDFLAGS_INTERNAL += $(SHLIB_LINK_INTERNAL)
78
79
80
81ifdef SO_MAJOR_VERSION
82# Default library naming convention used by the majority of platforms
83shlib		= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
84shlib_major	= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
85shlib_bare	= lib$(NAME)$(DLSUFFIX)
86# Testing the soname variable is a reliable way to determine whether a
87# linkable library is being built.
88soname		= $(shlib_major)
89pkgconfigdir = $(prefix)/libdata/pkgconfig
90else
91# Naming convention for dynamically loadable modules
92shlib		= $(NAME)$(DLSUFFIX)
93endif
94stlib		= lib$(NAME).a
95
96ifndef soname
97# additional flags for backend modules
98SHLIB_LINK += $(BE_DLLLIBS)
99endif
100
101# For each platform we support shared libraries on, set shlib to the
102# name of the library (if default above is not right), set
103# LINK.shared to the command to link the library,
104# and adjust SHLIB_LINK if necessary.
105
106# Try to keep the sections in some kind of order, folks...
107
108override CFLAGS += $(CFLAGS_SL)
109ifdef SO_MAJOR_VERSION
110# libraries ought to use this to refer to versioned gettext domain names
111override CPPFLAGS += -DSO_MAJOR_VERSION=$(SO_MAJOR_VERSION)
112endif
113
114ifeq ($(PORTNAME), aix)
115  ifdef SO_MAJOR_VERSION
116    shlib		= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
117  endif
118  haslibarule   = yes
119  # $(exports_file) is also usable as an import file
120  exports_file		= lib$(NAME).exp
121endif
122
123ifeq ($(PORTNAME), darwin)
124  ifdef soname
125    # linkable library
126    DLSUFFIX		= .dylib
127    ifneq ($(SO_MAJOR_VERSION), 0)
128      version_link	= -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
129    endif
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)
133  else
134    # loadable module
135    DLSUFFIX		= .so
136    LINK.shared		= $(COMPILER) -bundle -multiply_defined suppress -Wl,-undefined,dynamic_lookup
137  endif
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)
142  endif
143endif
144
145ifeq ($(PORTNAME), openbsd)
146  ifdef ELF_SYSTEM
147    LINK.shared		= $(COMPILER) -shared
148    ifdef soname
149      LINK.shared	+= -Wl,-x,-soname,$(soname)
150    endif
151    SHLIB_LINK		+= -lc
152  else
153    LINK.shared		= $(LD) -x -Bshareable -Bforcearchive
154  endif
155endif
156
157ifeq ($(PORTNAME), freebsd)
158  ifdef ELF_SYSTEM
159    ifdef SO_MAJOR_VERSION
160      shlib		= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
161    endif
162    LINK.shared		= $(COMPILER) -shared
163    ifdef soname
164      LINK.shared	+= -Wl,-x,-soname,$(soname)
165    endif
166  else
167    ifdef SO_MAJOR_VERSION
168      shlib		= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
169    endif
170    LINK.shared		= $(LD) -x -Bshareable -Bforcearchive
171  endif
172endif
173
174ifeq ($(PORTNAME), netbsd)
175  ifdef ELF_SYSTEM
176    LINK.shared		= $(COMPILER) -shared
177    ifdef soname
178      LINK.shared	+= -Wl,-x,-soname,$(soname)
179    endif
180  else
181    LINK.shared		= $(LD) -x -Bshareable -Bforcearchive
182  endif
183endif
184
185ifeq ($(PORTNAME), hpux)
186  ifdef SO_MAJOR_VERSION
187    shlib			= lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
188  endif
189  ifeq ($(with_gnu_ld), yes)
190    LINK.shared		= $(CC) -shared
191    ifdef soname
192      LINK.shared	+= -Wl,-h -Wl,$(soname)
193    endif
194  else
195    LINK.shared		= $(LD) -b
196    ifdef soname
197      LINK.shared	+= +h $(soname)
198    endif
199    # can't use the CC-syntax rpath pattern here, so instead:
200    rpath =
201    ifeq ($(enable_rpath), yes)
202      LINK.shared	+= +b '$(rpathdir)'
203    endif
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.
208    ifeq ($(GCC), yes)
209      SHLIB_LINK	+= -L/usr/local/lib
210    endif
211  endif
212  # And we need to link with libgcc, too
213  ifeq ($(GCC), yes)
214    SHLIB_LINK		+= `$(CC) $(LDFLAGS) -print-libgcc-file-name`
215  endif
216endif
217
218ifeq ($(PORTNAME), linux)
219  LINK.shared		= $(COMPILER) -shared
220  ifdef soname
221    LINK.shared		+= -Wl,-soname,$(soname)
222  endif
223  BUILD.exports		= ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
224  exports_file		= $(SHLIB_EXPORTS:%.txt=%.list)
225  ifneq (,$(exports_file))
226    LINK.shared		+= -Wl,--version-script=$(exports_file)
227  endif
228endif
229
230ifeq ($(PORTNAME), solaris)
231  ifeq ($(GCC), yes)
232    LINK.shared		= $(COMPILER) -shared
233  else
234    LINK.shared		= $(COMPILER) -G
235  endif
236  ifdef soname
237    ifeq ($(with_gnu_ld), yes)
238      LINK.shared	+= -Wl,-soname,$(soname)
239    else
240      LINK.shared	+= -h $(soname)
241    endif
242  endif
243endif
244
245ifeq ($(PORTNAME), sco)
246  ifeq ($(GCC), yes)
247    LINK.shared		= $(CC) -shared
248  else
249    LINK.shared		= $(CC) -G
250    endif
251  LINK.shared		+= -Wl,-z,text
252  ifdef soname
253    LINK.shared		+= -Wl,-h,$(soname)
254  endif
255endif
256
257ifeq ($(PORTNAME), unixware)
258  ifeq ($(GCC), yes)
259    LINK.shared		= $(CC) -shared
260  else
261    LINK.shared		= $(CC) -G
262  endif
263  LINK.shared		+= -Wl,-z,text
264  ifdef soname
265    LINK.shared		+= -Wl,-h,$(soname)
266  endif
267endif
268
269ifeq ($(PORTNAME), cygwin)
270  LINK.shared		= $(CC) -shared
271  ifdef SO_MAJOR_VERSION
272    shlib		= cyg$(NAME)$(DLSUFFIX)
273  endif
274  haslibarule   = yes
275endif
276
277ifeq ($(PORTNAME), win32)
278  ifdef SO_MAJOR_VERSION
279    shlib		= lib$(NAME)$(DLSUFFIX)
280  endif
281  haslibarule   = yes
282endif
283
284
285
286##
287## BUILD
288##
289
290.PHONY: all-lib all-static-lib all-shared-lib
291
292all-lib: all-shared-lib
293ifdef soname
294# no static library when building a dynamically loadable module
295all-lib: all-static-lib
296all-lib: lib$(NAME).pc
297endif
298
299all-static-lib: $(stlib)
300
301all-shared-lib: $(shlib)
302
303# In this rule, "touch $@" works around a problem on some platforms wherein
304# ranlib updates the library file's mod time with a value calculated to
305# seconds precision.  If the filesystem has sub-second timestamps, this can
306# cause the library file to appear older than its input files, triggering
307# parallel-make problems.
308ifndef haslibarule
309$(stlib): $(OBJS) | $(SHLIB_PREREQS)
310	rm -f $@
311	$(LINK.static) $@ $^
312	$(RANLIB) $@
313	touch $@
314endif #haslibarule
315
316
317ifeq (,$(filter cygwin win32,$(PORTNAME)))
318ifneq ($(PORTNAME), aix)
319
320# Normal case
321$(shlib): $(OBJS) | $(SHLIB_PREREQS)
322	$(LINK.shared) -o $@ $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
323ifdef shlib_major
324# If we're using major and minor versions, then make a symlink to major-version-only.
325ifneq ($(shlib), $(shlib_major))
326	rm -f $(shlib_major)
327	$(LN_S) $(shlib) $(shlib_major)
328endif
329# Make sure we have a link to a name without any version numbers
330ifneq ($(shlib), $(shlib_bare))
331	rm -f $(shlib_bare)
332	$(LN_S) $(shlib) $(shlib_bare)
333endif
334endif # shlib_major
335
336# Where possible, restrict the symbols exported by the library to just the
337# official list, so as to avoid unintentional ABI changes.  On recent Darwin
338# this also quiets multiply-defined-symbol warnings in programs that use
339# libpgport along with libpq.
340ifneq (,$(SHLIB_EXPORTS))
341ifdef BUILD.exports
342$(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
343
344$(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
345	$(BUILD.exports)
346endif
347endif
348
349else # PORTNAME == aix
350
351# AIX case
352
353# See notes in src/backend/parser/Makefile about the following two rules
354$(stlib): $(shlib)
355	touch $@
356
357$(shlib): $(OBJS) | $(SHLIB_PREREQS)
358	rm -f $(stlib)
359	$(LINK.static) $(stlib) $^
360	$(RANLIB) $(stlib)
361	$(MKLDEXPORT) $(stlib) $(shlib) >$(exports_file)
362	$(COMPILER) -o $(shlib) $(stlib) -Wl,-bE:$(exports_file) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK)
363	rm -f $(stlib)
364	$(AR) $(AROPT) $(stlib) $(shlib)
365
366endif # PORTNAME == aix
367
368else # PORTNAME == cygwin || PORTNAME == win32
369
370ifeq ($(PORTNAME), cygwin)
371
372# Cygwin case
373
374$(shlib): $(OBJS) | $(SHLIB_PREREQS)
375	$(CC) $(CFLAGS)  -shared -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) $(LDAP_LIBS_BE)
376
377$(stlib): $(OBJS) | $(SHLIB_PREREQS)
378	rm -f $@
379	$(LINK.static) $@ $^
380	$(RANLIB) $@
381
382else
383
384# Win32 case
385
386# See notes in src/backend/parser/Makefile about the following two rules
387$(stlib): $(shlib)
388	touch $@
389
390# XXX A backend that loads a module linked with libgcc_s_dw2-1.dll will exit
391# uncleanly, hence -static-libgcc.  (Last verified with MinGW-w64 compilers
392# from i686-4.9.1-release-win32-dwarf-rt_v3-rev1.)  Shared libgcc has better
393# support for C++/Java exceptions; while core PostgreSQL does not use them, it
394# would be nice to support shared libgcc for the benefit of extensions.
395#
396# If SHLIB_EXPORTS is set, the rules below will build a .def file from that.
397# Else we just use --export-all-symbols.
398ifeq (,$(SHLIB_EXPORTS))
399$(shlib): $(OBJS) | $(SHLIB_PREREQS)
400	$(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--export-all-symbols -Wl,--out-implib=$(stlib)
401else
402DLL_DEFFILE = lib$(NAME)dll.def
403
404$(shlib): $(OBJS) $(DLL_DEFFILE) | $(SHLIB_PREREQS)
405	$(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(DLL_DEFFILE) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--out-implib=$(stlib)
406endif
407
408endif # PORTNAME == cygwin
409endif # PORTNAME == cygwin || PORTNAME == win32
410
411
412%.pc: $(MAKEFILE_LIST)
413	echo 'Name: lib$(NAME)' >$@
414	echo 'Description: PostgreSQL lib$(NAME) library' >>$@
415	echo 'Url: http://www.postgresql.org/' >>$@
416	echo 'Version: $(VERSION)' >>$@
417	echo 'Requires: ' >>$@
418	echo 'Requires.private: $(PKG_CONFIG_REQUIRES_PRIVATE)' >>$@
419	echo 'Cflags: -I$(includedir)' >>$@
420	echo 'Libs: -L$(libdir) -l$(NAME)' >>$@
421# Record -L flags that the user might have passed in to the PostgreSQL
422# build to locate third-party libraries (e.g., ldap, ssl).  Filter out
423# those that point inside the build or source tree.  Use sort to
424# remove duplicates.  Also record the -l flags necessary for static
425# linking, but not those already covered by Requires.private.
426	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)))' >>$@
427
428
429# We need several not-quite-identical variants of .DEF files to build
430# DLLs for Windows.  These are made from the single source file
431# exports.txt.  Since we can't assume that Windows boxes will have
432# sed, the .DEF files are always built and included in distribution
433# tarballs.
434
435ifneq (,$(SHLIB_EXPORTS))
436distprep: lib$(NAME)dll.def lib$(NAME)ddll.def blib$(NAME)dll.def
437
438UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
439
440lib$(NAME)dll.def: $(SHLIB_EXPORTS)
441	echo '; DEF file for win32.mak release build and for Makefile.shlib (MinGW)' >$@
442	echo 'LIBRARY LIB$(UC_NAME).dll' >>$@
443	echo 'EXPORTS' >>$@
444	sed -e '/^#/d' -e 's/^\(.*[ 	]\)\([0-9][0-9]*\)/    \1@ \2/' $< >>$@
445
446lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
447	echo '; DEF file for win32.mak debug build' >$@
448	echo 'LIBRARY LIB$(UC_NAME)D.dll' >>$@
449	echo 'EXPORTS' >>$@
450	sed -e '/^#/d' -e 's/^\(.*[ 	]\)\([0-9][0-9]*\)/    \1@ \2/' $< >>$@
451
452blib$(NAME)dll.def: $(SHLIB_EXPORTS)
453	echo '; DEF file for bcc32.mak (Borland C++ Builder)' >$@
454	echo 'LIBRARY BLIB$(UC_NAME)' >>$@
455	echo 'EXPORTS' >>$@
456	sed -e '/^#/d' -e 's/^\(.*[ 	]\)\([0-9][0-9]*\)/    _\1@ \2/' $< >>$@
457	echo >>$@
458	echo '; Aliases for MS compatible names' >> $@
459	sed -e '/^#/d' -e 's/^\(.*[ 	]\)\([0-9][0-9]*\)/    \1= _\1/' $< | sed 's/ *$$//' >>$@
460endif # SHLIB_EXPORTS
461
462
463##
464## INSTALL
465##
466
467.PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
468install-lib: install-lib-shared
469ifdef soname
470install-lib: install-lib-static
471install-lib: install-lib-pc
472endif
473
474install-lib-pc: lib$(NAME).pc installdirs-lib
475	$(INSTALL_DATA) $< '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
476
477install-lib-static: $(stlib) installdirs-lib
478	$(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
479ifeq ($(PORTNAME), darwin)
480	cd '$(DESTDIR)$(libdir)' && \
481	$(RANLIB) $(stlib)
482endif
483
484install-lib-shared: $(shlib) installdirs-lib
485ifdef soname
486# we don't install $(shlib) on AIX
487# (see http://archives.postgresql.org/message-id/52EF20B2E3209443BC37736D00C3C1380A6E79FE@EXADV1.host.magwien.gv.at)
488ifneq ($(PORTNAME), aix)
489	$(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
490ifneq ($(PORTNAME), cygwin)
491ifneq ($(PORTNAME), win32)
492ifneq ($(shlib), $(shlib_major))
493	cd '$(DESTDIR)$(libdir)' && \
494	rm -f $(shlib_major) && \
495	$(LN_S) $(shlib) $(shlib_major)
496endif
497ifneq ($(shlib), $(shlib_bare))
498	cd '$(DESTDIR)$(libdir)' && \
499	rm -f $(shlib_bare) && \
500	$(LN_S) $(shlib) $(shlib_bare)
501endif
502endif # not win32
503endif # not cygwin
504endif # not aix
505ifneq (,$(findstring $(PORTNAME),win32 cygwin))
506	$(INSTALL_SHLIB) $< '$(DESTDIR)$(bindir)/$(shlib)'
507endif
508else # no soname
509	$(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
510endif
511
512
513installdirs-lib:
514ifdef soname
515	$(MKDIR_P) '$(DESTDIR)$(libdir)' '$(DESTDIR)$(pkgconfigdir)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)')
516else
517	$(MKDIR_P) '$(DESTDIR)$(pkglibdir)'
518endif
519
520
521##
522## UNINSTALL
523##
524
525.PHONY: uninstall-lib
526uninstall-lib:
527ifdef soname
528	rm -f '$(DESTDIR)$(libdir)/$(stlib)'
529	rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
530	  '$(DESTDIR)$(libdir)/$(shlib_major)' \
531	  '$(DESTDIR)$(libdir)/$(shlib)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)/$(shlib)') \
532	  '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
533else # no soname
534	rm -f '$(DESTDIR)$(pkglibdir)/$(shlib)'
535endif # no soname
536
537
538##
539## CLEAN
540##
541
542.PHONY: clean-lib
543clean-lib:
544	rm -f $(shlib) $(shlib_bare) $(shlib_major) $(stlib) $(exports_file) lib$(NAME).pc
545
546ifneq (,$(SHLIB_EXPORTS))
547maintainer-clean-lib:
548	rm -f lib$(NAME)dll.def lib$(NAME)ddll.def blib$(NAME)dll.def
549endif
550