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 $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) $(LDAP_LIBS_BE)
363
364$(stlib): $(OBJS) | $(SHLIB_PREREQS)
365	rm -f $@
366	$(LINK.static) $@ $^
367	$(RANLIB) $@
368
369else
370
371# Win32 case
372
373# See notes in src/backend/parser/Makefile about the following two rules
374$(stlib): $(shlib)
375	touch $@
376
377# XXX A backend that loads a module linked with libgcc_s_dw2-1.dll will exit
378# uncleanly, hence -static-libgcc.  (Last verified with MinGW-w64 compilers
379# from i686-4.9.1-release-win32-dwarf-rt_v3-rev1.)  Shared libgcc has better
380# support for C++/Java exceptions; while core PostgreSQL does not use them, it
381# would be nice to support shared libgcc for the benefit of extensions.
382#
383# If SHLIB_EXPORTS is set, the rules below will build a .def file from that.
384# Else we just use --export-all-symbols.
385ifeq (,$(SHLIB_EXPORTS))
386$(shlib): $(OBJS) | $(SHLIB_PREREQS)
387	$(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--export-all-symbols -Wl,--out-implib=$(stlib)
388else
389DLL_DEFFILE = lib$(NAME)dll.def
390
391$(shlib): $(OBJS) $(DLL_DEFFILE) | $(SHLIB_PREREQS)
392	$(CC) $(CFLAGS)  -shared -static-libgcc -o $@  $(OBJS) $(DLL_DEFFILE) $(LDFLAGS) $(LDFLAGS_SL) $(SHLIB_LINK) $(LIBS) -Wl,--out-implib=$(stlib)
393endif
394
395endif # PORTNAME == cygwin
396endif # PORTNAME == cygwin || PORTNAME == win32
397
398
399%.pc: $(MAKEFILE_LIST)
400	echo 'Name: lib$(NAME)' >$@
401	echo 'Description: PostgreSQL lib$(NAME) library' >>$@
402	echo 'Url: http://www.postgresql.org/' >>$@
403	echo 'Version: $(VERSION)' >>$@
404	echo 'Requires: ' >>$@
405	echo 'Requires.private: $(PKG_CONFIG_REQUIRES_PRIVATE)' >>$@
406	echo 'Cflags: -I$(includedir)' >>$@
407	echo 'Libs: -L$(libdir) -l$(NAME)' >>$@
408# Record -L flags that the user might have passed in to the PostgreSQL
409# build to locate third-party libraries (e.g., ldap, ssl).  Filter out
410# those that point inside the build or source tree.  Use sort to
411# remove duplicates.  Also record the -l flags necessary for static
412# linking, but not those already covered by Requires.private.
413	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)))' >>$@
414
415
416# We need several not-quite-identical variants of .DEF files to build
417# DLLs for Windows.  These are made from the single source file
418# exports.txt.  Since we can't assume that Windows boxes will have
419# sed, the .DEF files are always built and included in distribution
420# tarballs.
421
422ifneq (,$(SHLIB_EXPORTS))
423distprep: lib$(NAME)dll.def lib$(NAME)ddll.def
424
425UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
426
427lib$(NAME)dll.def: $(SHLIB_EXPORTS)
428	echo '; DEF file for Makefile.shlib (MinGW)' >$@
429	echo 'LIBRARY LIB$(UC_NAME).dll' >>$@
430	echo 'EXPORTS' >>$@
431	sed -e '/^#/d' -e 's/^\(.*[ 	]\)\([0-9][0-9]*\)/    \1@ \2/' $< >>$@
432
433lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
434	echo '; DEF file for Makefile.shlib (MinGW)' >$@
435	echo 'LIBRARY LIB$(UC_NAME)D.dll' >>$@
436	echo 'EXPORTS' >>$@
437	sed -e '/^#/d' -e 's/^\(.*[ 	]\)\([0-9][0-9]*\)/    \1@ \2/' $< >>$@
438
439endif # SHLIB_EXPORTS
440
441
442##
443## INSTALL
444##
445
446.PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
447install-lib: install-lib-shared
448ifdef soname
449install-lib: install-lib-static
450install-lib: install-lib-pc
451endif
452
453install-lib-pc: lib$(NAME).pc installdirs-lib
454	$(INSTALL_DATA) $< '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
455
456install-lib-static: $(stlib) installdirs-lib
457	$(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
458ifeq ($(PORTNAME), darwin)
459	cd '$(DESTDIR)$(libdir)' && \
460	$(RANLIB) $(stlib)
461endif
462
463install-lib-shared: $(shlib) installdirs-lib
464ifdef soname
465# we don't install $(shlib) on AIX
466# (see http://archives.postgresql.org/message-id/52EF20B2E3209443BC37736D00C3C1380A6E79FE@EXADV1.host.magwien.gv.at)
467ifneq ($(PORTNAME), aix)
468	$(INSTALL_SHLIB) $< '$(DESTDIR)$(libdir)/$(shlib)'
469ifneq ($(PORTNAME), cygwin)
470ifneq ($(PORTNAME), win32)
471ifneq ($(shlib), $(shlib_major))
472	cd '$(DESTDIR)$(libdir)' && \
473	rm -f $(shlib_major) && \
474	$(LN_S) $(shlib) $(shlib_major)
475endif
476ifneq ($(shlib), $(shlib_bare))
477	cd '$(DESTDIR)$(libdir)' && \
478	rm -f $(shlib_bare) && \
479	$(LN_S) $(shlib) $(shlib_bare)
480endif
481endif # not win32
482endif # not cygwin
483endif # not aix
484ifneq (,$(findstring $(PORTNAME),win32 cygwin))
485	$(INSTALL_SHLIB) $< '$(DESTDIR)$(bindir)/$(shlib)'
486endif
487else # no soname
488	$(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
489endif
490
491
492installdirs-lib:
493ifdef soname
494	$(MKDIR_P) '$(DESTDIR)$(libdir)' '$(DESTDIR)$(pkgconfigdir)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)')
495else
496	$(MKDIR_P) '$(DESTDIR)$(pkglibdir)'
497endif
498
499
500##
501## UNINSTALL
502##
503
504.PHONY: uninstall-lib
505uninstall-lib:
506ifdef soname
507	rm -f '$(DESTDIR)$(libdir)/$(stlib)'
508	rm -f '$(DESTDIR)$(libdir)/$(shlib_bare)' \
509	  '$(DESTDIR)$(libdir)/$(shlib_major)' \
510	  '$(DESTDIR)$(libdir)/$(shlib)' $(if $(findstring $(PORTNAME),win32 cygwin),'$(DESTDIR)$(bindir)/$(shlib)') \
511	  '$(DESTDIR)$(pkgconfigdir)/lib$(NAME).pc'
512else # no soname
513	rm -f '$(DESTDIR)$(pkglibdir)/$(shlib)'
514endif # no soname
515
516
517##
518## CLEAN
519##
520
521.PHONY: clean-lib
522clean-lib:
523	rm -f $(shlib) $(shlib_bare) $(shlib_major) $(stlib) $(exports_file) lib$(NAME).pc
524
525ifneq (,$(SHLIB_EXPORTS))
526maintainer-clean-lib:
527	rm -f lib$(NAME)dll.def lib$(NAME)ddll.def
528endif
529