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