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