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