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