OSDN Git Service

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