]> granicus.if.org Git - postgresql/blob - src/Makefile.shlib
Always define stlib, since some platforms need it for building modules.
[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.115 2008/04/07 23:08:15 petere 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
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 # 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))
83
84 # Need a -L-free version of LDFLAGS to use in combination with SHLIB_LINK
85 LDFLAGS_NO_L = $(filter-out -L%, $(LDFLAGS))
86
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
104
105 ifndef soname
106 # additional flags for backend modules
107 SHLIB_LINK := $(BE_DLLLIBS) $(SHLIB_LINK)
108 endif
109
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.
114
115 # Try to keep the sections in some kind of order, folks...
116
117 override CFLAGS += $(CFLAGS_SL)
118
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
126
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
148
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
160
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
173
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
190
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
201
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
234
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
244
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
256
257 ifeq ($(PORTNAME), solaris)
258   ifeq ($(GCC), yes)
259     LINK.shared         = $(COMPILER) -shared # $(COMPILER) needed for -m64
260   else
261 # CFLAGS added for X86_64
262     LINK.shared         = $(CC) -G $(CFLAGS)
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   shlib                 = cyg$(NAME)$(DLSUFFIX)
315   haslibarule   = yes
316 endif
317
318 ifeq ($(PORTNAME), win32)
319   shlib                 = lib$(NAME)$(DLSUFFIX)
320   haslibarule   = yes
321 endif
322
323 ifeq ($(enable_rpath), yes)
324 SHLIB_LINK += $(rpath)
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) $(LDFLAGS_SL) $(OBJS) $(SHLIB_LINK) -o $@
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) $(LDFLAGS_NO_L) $(LDFLAGS_SL) -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 DLL_DEFFILE = lib$(NAME)dll.def
403
404 # If SHLIB_EXPORTS is set, the rules below will build a .def file from
405 # that.  Else we build a temporary one here.
406 ifeq (,$(SHLIB_EXPORTS))
407 exports_file = $(DLL_DEFFILE)
408
409 $(exports_file): $(OBJS)
410         $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $@ $^
411 endif
412
413 $(shlib): $(OBJS) $(DLL_DEFFILE)
414         $(DLLWRAP) $(LDFLAGS_SL) -o $@ --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
415
416 $(stlib): $(shlib) $(DLL_DEFFILE)
417         $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib $@
418
419 endif # PORTNAME == cygwin || PORTNAME == win32
420
421 endif # enable_shared
422
423
424 # We need several not-quite-identical variants of .DEF files to build
425 # DLLs for Windows.  These are made from the single source file
426 # exports.txt.  Since we can't assume that Windows boxes will have
427 # sed, the .DEF files are always built and included in distribution
428 # tarballs.
429
430 ifneq (,$(SHLIB_EXPORTS))
431 distprep: $(srcdir)/lib$(NAME)dll.def $(srcdir)/lib$(NAME)ddll.def $(srcdir)/blib$(NAME)dll.def
432
433 UC_NAME = $(shell echo $(NAME) | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
434
435 $(srcdir)/lib$(NAME)dll.def: $(SHLIB_EXPORTS)
436         echo '; DEF file for MS VC++' >$@
437         echo 'LIBRARY LIB$(UC_NAME)' >>$@
438         echo 'EXPORTS' >>$@
439         sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/    \1@ \2/' $< >>$@
440
441 $(srcdir)/lib$(NAME)ddll.def: $(SHLIB_EXPORTS)
442         echo '; DEF file for MS VC++' >$@
443         echo 'LIBRARY LIB$(UC_NAME)D' >>$@
444         echo 'EXPORTS' >>$@
445         sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/    \1@ \2/' $< >>$@
446
447 $(srcdir)/blib$(NAME)dll.def: $(SHLIB_EXPORTS)
448         echo '; DEF file for Borland C++ Builder' >$@
449         echo 'LIBRARY BLIB$(UC_NAME)' >>$@
450         echo 'EXPORTS' >>$@
451         sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/    _\1@ \2/' $< >>$@
452         echo >>$@
453         echo '; Aliases for MS compatible names' >> $@
454         sed -e '/^#/d' -e 's/^\(.* \)\([0-9][0-9]*\)/    \1= _\1/' $< | sed 's/ *$$//' >>$@
455 endif # SHLIB_EXPORTS
456
457
458 ##
459 ## INSTALL
460 ##
461
462 .PHONY: install-lib install-lib-static install-lib-shared installdirs-lib
463 install-lib: install-lib-shared
464 ifdef soname
465 install-lib: install-lib-static
466 endif
467
468 install-lib-static: $(stlib) installdirs-lib
469         $(INSTALL_STLIB) $< '$(DESTDIR)$(libdir)/$(stlib)'
470 ifeq ($(PORTNAME), darwin)
471         cd '$(DESTDIR)$(libdir)' && \
472         ranlib $(stlib)
473 endif
474
475 ifeq ($(enable_shared), yes)
476 install-lib-shared: $(shlib) installdirs-lib
477 ifdef soname
478 # we don't install $(shlib) on AIX
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         $(mkinstalldirs) '$(DESTDIR)$(libdir)'
512 else
513         $(mkinstalldirs) '$(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 $(srcdir)/lib$(NAME)dll.def $(srcdir)/lib$(NAME)ddll.def $(srcdir)/blib$(NAME)dll.def
546 endif