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