Properly access a buffer's LSN using existing access macros instead of abusing
[PostgreSQL.git] / src / Makefile.shlib
blob2c1f961debcf6ee2859f87bab0bab10b53f50e3a
1 #-------------------------------------------------------------------------
3 # Makefile.shlib
4 #    Common rules for building shared libraries
6 # Copyright (c) 1998, Regents of the University of California
8 # IDENTIFICATION
9 #    $PostgreSQL$
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
19 # variables:
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
26 #                       export
28 # When building a shared library, the following version information
29 # must also be set.  It should be omitted when building a dynamically
30 # loadable module.
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
37 # platforms).
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:
59 #     all: all-lib
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.
73 # bjm 2001-02-10
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)
93 endif
94 # Testing the soname variable is a reliable way to determine whether a
95 # linkable library is being built.
96 soname          = $(shlib_major)
97 else
98 # Naming convention for dynamically loadable modules
99 ifeq ($(enable_shared), yes)
100 shlib           = $(NAME)$(DLSUFFIX)
101 endif
102 endif
103 stlib           = lib$(NAME).a
105 ifndef soname
106 # additional flags for backend modules
107 SHLIB_LINK := $(BE_DLLLIBS) $(SHLIB_LINK)
108 endif
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)
122   endif
123   haslibarule   = yes
124   exports_file          = lib$(NAME).exp
125 endif
127 ifeq ($(PORTNAME), darwin)
128   ifdef soname
129     # linkable library
130     DLSUFFIX            = .dylib
131     ifneq ($(SO_MAJOR_VERSION), 0)
132       version_link      = -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
133     endif
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)
137   else
138     # loadable module
139     DLSUFFIX            = .so
140     LINK.shared         = $(COMPILER) -bundle -multiply_defined suppress
141   endif
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)
146   endif
147 endif
149 ifeq ($(PORTNAME), openbsd)
150   ifdef ELF_SYSTEM
151     LINK.shared         = $(COMPILER) -shared
152     ifdef soname
153       LINK.shared       += -Wl,-x,-soname,$(soname)
154     endif
155     SHLIB_LINK          += -lc
156   else
157     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
158   endif
159 endif
161 ifeq ($(PORTNAME), bsdi)
162   ifeq ($(DLSUFFIX), .so)
163     LINK.shared         = $(COMPILER) -shared
164     ifdef soname
165       LINK.shared       += -Wl,-x,-soname,$(soname)
166     endif
167     SHLIB_LINK          += -lc
168   endif
169   ifeq ($(DLSUFFIX), .o)
170     LINK.shared         = shlicc -O $(LDREL)
171   endif
172 endif
174 ifeq ($(PORTNAME), freebsd)
175   ifdef ELF_SYSTEM
176     ifdef SO_MAJOR_VERSION
177       shlib             = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
178     endif
179     LINK.shared         = $(COMPILER) -shared
180     ifdef soname
181       LINK.shared       += -Wl,-x,-soname,$(soname)
182     endif
183   else
184     ifdef SO_MAJOR_VERSION
185       shlib             = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
186     endif
187     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
188   endif
189 endif
191 ifeq ($(PORTNAME), netbsd)
192   ifdef ELF_SYSTEM
193     LINK.shared         = $(COMPILER) -shared
194     ifdef soname
195       LINK.shared       += -Wl,-x,-soname,$(soname)
196     endif
197   else
198     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
199   endif
200 endif
202 ifeq ($(PORTNAME), hpux)
203   ifdef SO_MAJOR_VERSION
204     shlib                       = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
205   endif
206   ifeq ($(with_gnu_ld), yes)
207     LINK.shared         = $(CC) $(LDFLAGS_NO_L) -shared
208     ifdef soname
209       LINK.shared       += -Wl,-h -Wl,$(soname)
210     endif
211   else
212     # can't use the CC-syntax rpath pattern here
213     rpath =
214     LINK.shared         = $(LD) -b
215     ifdef soname
216       LINK.shared       += +h $(soname)
217     endif
218     ifeq ($(enable_rpath), yes)
219       LINK.shared       += +b '$(rpathdir)'
220     endif
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.
225     ifeq ($(GCC), yes)
226       SHLIB_LINK := $(filter -L%, $(SHLIB_LINK)) -L/usr/local/lib $(filter-out -L%, $(SHLIB_LINK))
227     endif
228   endif
229   # do this last so above filtering doesn't pull out -L switches in LDFLAGS
230   ifeq ($(GCC), yes)
231     SHLIB_LINK          += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
232   endif
233 endif
235 ifeq ($(PORTNAME), irix)
236   ifdef SO_MAJOR_VERSION
237     shlib               = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
238   endif
239   LINK.shared           = $(COMPILER) -shared
240   ifdef soname
241     LINK.shared         += -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
242   endif
243 endif
245 ifeq ($(PORTNAME), linux)
246   LINK.shared           = $(COMPILER) -shared
247   ifdef soname
248     LINK.shared         += -Wl,-soname,$(soname)
249   endif
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)
254   endif
255 endif
257 ifeq ($(PORTNAME), solaris)
258   ifeq ($(GCC), yes)
259     LINK.shared         = $(COMPILER) -shared
260   else
261     LINK.shared         = $(COMPILER) -G
262   endif
263   ifdef soname
264     ifeq ($(with_gnu_ld), yes)
265       LINK.shared       += -Wl,-soname,$(soname)
266     else
267       LINK.shared       += -h $(soname)
268     endif
269   endif
270 endif
272 ifeq ($(PORTNAME), sunos4)
273   LINK.shared           = $(LD) -assert pure-text -Bdynamic
274 endif
276 ifeq ($(PORTNAME), osf)
277   LINK.shared           = $(LD) -shared -expect_unresolved '*'
278 endif
280 ifeq ($(PORTNAME), sco)
281   ifeq ($(GCC), yes)
282     LINK.shared         = $(CC) -shared
283   else
284     LINK.shared         = $(CC) -G
285     endif
286   LINK.shared           += -Wl,-z,text
287   ifdef soname
288     LINK.shared         += -Wl,-h,$(soname)
289   endif
290 endif
292 ifeq ($(PORTNAME), svr4)
293   LINK.shared           = $(LD) -G
294 endif
296 ifeq ($(PORTNAME), univel)
297   LINK.shared           = $(LD) -G -z text
298 endif
300 ifeq ($(PORTNAME), unixware)
301   ifeq ($(GCC), yes)
302     LINK.shared         = $(CC) -shared
303   else
304     LINK.shared         = $(CC) -G
305   endif
306   LINK.shared           += -Wl,-z,text
307   ifdef soname
308     LINK.shared         += -Wl,-h,$(soname)
309   endif
310 endif
312 ifeq ($(PORTNAME), cygwin)
313   ifdef SO_MAJOR_VERSION
314     shlib               = cyg$(NAME)$(DLSUFFIX)
315   endif
316   haslibarule   = yes
317 endif
319 ifeq ($(PORTNAME), win32)
320   ifdef SO_MAJOR_VERSION
321     shlib               = lib$(NAME)$(DLSUFFIX)
322   endif
323   haslibarule   = yes
324 endif
326 ifeq ($(enable_rpath), yes)
327 SHLIB_LINK += $(rpath)
328 endif
333 ## BUILD
336 .PHONY: all-lib all-static-lib all-shared-lib
338 all-lib: all-shared-lib
339 ifdef soname
340 # no static library when building a dynamically loadable module
341 all-lib: all-static-lib
342 endif
344 all-static-lib: $(stlib)
346 all-shared-lib: $(shlib)
348 ifndef haslibarule
349 $(stlib): $(OBJS)
350         $(LINK.static) $@ $^
351         $(RANLIB) $@
352 endif #haslibarule
354 ifeq ($(enable_shared), yes)
356 ifeq (,$(filter cygwin win32,$(PORTNAME)))
357 ifneq ($(PORTNAME), aix)
359 # Normal case
360 $(shlib): $(OBJS)
361         $(LINK.shared) $(LDFLAGS_SL) $(OBJS) $(SHLIB_LINK) -o $@
362 ifdef shlib_major
363 # If we're using major and minor versions, then make a symlink to major-version-only.
364 ifneq ($(shlib), $(shlib_major))
365         rm -f $(shlib_major)
366         $(LN_S) $(shlib) $(shlib_major)
367 endif
368 # Make sure we have a link to a name without any version numbers
369 ifneq ($(shlib), $(shlib_bare))
370         rm -f $(shlib_bare)
371         $(LN_S) $(shlib) $(shlib_bare)
372 endif
373 endif # shlib_major
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))
380 ifdef BUILD.exports
381 $(shlib): $(SHLIB_EXPORTS:%.txt=%.list)
383 $(SHLIB_EXPORTS:%.txt=%.list): %.list: %.txt
384         $(BUILD.exports)
385 endif
386 endif
388 else # PORTNAME == aix
390 # AIX case
391 $(shlib) $(stlib): $(OBJS)
392         $(LINK.static) $(stlib) $^
393         $(RANLIB) $(stlib)
394         $(MKLDEXPORT) $(stlib) >$(exports_file)
395         $(COMPILER) $(LDFLAGS_NO_L) $(LDFLAGS_SL) -o $(shlib) $(stlib) -Wl,-bE:$(exports_file) $(SHLIB_LINK)
396         rm -f $(stlib)
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 $@ $^
414 endif
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
431 # tarballs.
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)' >>$@
441         echo 'EXPORTS' >>$@
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' >>$@
447         echo 'EXPORTS' >>$@
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)' >>$@
453         echo 'EXPORTS' >>$@
454         sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/    _\1@ \2/' $< >>$@
455         echo >>$@
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
462 ## INSTALL
465 .PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
466 install-lib: install-lib-shared
467 ifdef soname
468 install-lib: install-lib-static
469 endif
471 install-lib-static: $(stlib) installdirs-lib
472         $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
473 ifeq ($(PORTNAME), darwin)
474         cd '$(DESTDIR)$(libdir)' && \
475         ranlib $(stlib)
476 endif
478 ifeq ($(enable_shared), yes)
479 install-lib-shared: $(shlib) installdirs-lib
480 ifdef soname
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)
490 endif
491 ifneq ($(shlib), $(shlib_bare))
492         cd '$(DESTDIR)$(libdir)' && \
493         rm -f $(shlib_bare) && \
494         $(LN_S) $(shlib) $(shlib_bare)
495 endif
496 endif # not win32
497 endif # not cygwin
498 endif # not aix
499 else # no soname
500         $(INSTALL_SHLIB) $< '$(DESTDIR)$(pkglibdir)/$(shlib)'
501 endif
502 else # not enable_shared
503 ifndef soname
504 install-lib-shared:
505         @echo "*****"; \
506          echo "* Module $(NAME) was not installed due to lack of shared library support."; \
507          echo "*****"
508 endif
509 endif # enable_shared
512 installdirs-lib:
513 ifdef soname
514         $(mkinstalldirs) '$(DESTDIR)$(libdir)'
515 else
516         $(mkinstalldirs) '$(DESTDIR)$(pkglibdir)'
517 endif
521 ## UNINSTALL
524 .PHONY: uninstall-lib
525 uninstall-lib:
526 ifdef soname
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
533 else # no soname
534         rm -f '$(DESTDIR)$(pkglibdir)/$(shlib)'
535 endif # no soname
539 ## CLEAN
542 .PHONY: clean-lib
543 clean-lib:
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
549 endif