]> granicus.if.org Git - postgresql/blob - src/Makefile.shlib
Export only required symbols in libpq on Win32.
[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.86 2004/10/16 03:26:43 momjian 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 # SO_MAJOR_VERSION      Major version number to use for shared library
23 # SO_MINOR_VERSION      Minor version number to use for shared library
24 # OBJS                  List of object files to include in library
25 # SHLIB_LINK            If shared library relies on other libraries,
26 #                       additional stuff to put in its link command
27 # (If you want a patchlevel, include it in SO_MINOR_VERSION, e.g., "6.2".)
28 #
29 # Optional flags when building DLL's (only applicable to win32 and cygwin
30 # platforms).
31 # DLLTOOL_DEFFLAGS      Additional flags when creating the dll .def file
32 # DLLTOOL_LIBFLAGS      Additional flags when creating the lib<module>.a file
33 # DLLWRAP_FLAGS         Additional flags to dllwrap
34 # DLL_DEFFILE           Use pre-existing .def file instead of auto-generating
35 #                       one with all exports in it (win32 only).
36 #
37 # The module Makefile must also include
38 # $(top_builddir)/src/Makefile.global before including this file.
39 # (Makefile.global sets PORTNAME and other needed symbols.)
40 #
41 # This makefile provides the following (phony) targets:
42 #
43 # all-lib               build the static and shared (if applicable) libraries
44 # install-lib           install the libraries into $(libdir)
45 # uninstall-lib         remove the libraries from $(libdir)
46 # clean-lib             delete the static and shared libraries from the build dir
47 #
48 # Since `all-lib' is the first rule in this file you probably want to
49 # have the `all' target before including this file. In the most simple
50 # case it would look like this:
51 #
52 #     all: all-lib
53 #
54 # Similarly, the install rule might look like
55 #
56 #     install: install-lib
57 #
58 # plus any additional things you want to install. Et cetera.
59 #
60 # Got that?  Look at src/interfaces/libpq/Makefile for an example.
61 #
62 # While the linker allows creation of most shared libraries,
63 # -Bsymbolic requires resolution of all symbols, making the
64 # compiler a better choice for shared library creation on ELF platforms.
65 # With the linker, -Bsymbolic requires the crt1.o startup object file.
66 # bjm 2001-02-10
67
68
69 COMPILER = $(CC) $(CFLAGS)
70 LINK.static = $(AR) $(AROPT)
71
72
73
74 ifeq ($(enable_shared), yes)
75
76 # Default shlib naming convention used by the majority of platforms
77 shlib           = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
78 shlib_major     = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
79 shlib_bare      = lib$(NAME)$(DLSUFFIX)
80
81 # For each platform we support shared libraries on, set shlib to the
82 # name of the library (if default above is not right), set
83 # LINK.shared to the command to link the library,
84 # and adjust SHLIB_LINK if necessary.
85
86 # Try to keep the sections in some kind of order, folks...
87
88 override CFLAGS += $(CFLAGS_SL)
89
90 soname = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
91
92 ifeq ($(PORTNAME), aix)
93   shlib                 = lib$(NAME)$(DLSUFFIX)
94 #   SHLIB_LINK          += -lc
95 endif
96
97 ifeq ($(PORTNAME), darwin)
98   ifneq ($(SO_MAJOR_VERSION), 0)
99     version_link        := -compatibility_version $(SO_MAJOR_VERSION) -current_version $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
100   endif
101   ifeq ($(DLTYPE), library)
102     # linkable library
103     DLSUFFIX            := .dylib
104     LINK.shared         = $(COMPILER) -dynamiclib -install_name $(libdir)/lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX) $(version_link) -multiply_defined suppress
105   else
106     # loadable module (default case)
107     DLSUFFIX            := .so
108     LINK.shared         = $(COMPILER) -bundle
109   endif
110   shlib                 = lib$(NAME).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)$(DLSUFFIX)
111   shlib_major           = lib$(NAME).$(SO_MAJOR_VERSION)$(DLSUFFIX)
112 endif
113
114 ifeq ($(PORTNAME), openbsd)
115   ifdef ELF_SYSTEM
116     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
117     SHLIB_LINK          += -lc
118   else
119     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
120   endif
121 endif
122
123 ifeq ($(PORTNAME), bsdi)
124   ifeq ($(DLSUFFIX), .so)
125     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
126     SHLIB_LINK          += -lc
127   endif
128   ifeq ($(DLSUFFIX), .o)
129     LINK.shared         = shlicc -O $(LDREL)
130   endif
131 endif
132
133 ifeq ($(PORTNAME), freebsd)
134   ifdef ELF_SYSTEM
135     shlib               = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
136     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
137   else
138     shlib               = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
139     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
140   endif
141 endif
142
143 ifeq ($(PORTNAME), netbsd)
144   ifdef ELF_SYSTEM
145     LINK.shared         = $(COMPILER) -shared -Wl,-x,-soname,$(soname)
146   else
147     LINK.shared         = $(LD) -x -Bshareable -Bforcearchive
148   endif
149 endif
150
151 ifeq ($(PORTNAME), hpux)
152   shlib                 = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
153   ifeq ($(GCC), yes)
154     SHLIB_LINK          += `$(CC) $(LDFLAGS) -print-libgcc-file-name`
155   endif
156   ifeq ($(with_gnu_ld), yes)
157     LINK.shared         = $(CC) $(LDFLAGS) -shared -Wl,-h -Wl,$(soname) -Wl,+b -Wl,$(libdir)
158   else
159     LINK.shared         = $(LD) +h $(soname) -b +b $(libdir)
160   endif
161 endif
162
163 ifeq ($(PORTNAME), irix)
164   shlib                 = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
165   LINK.shared           = $(COMPILER) -shared -Wl,-set_version,sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
166 endif
167
168 ifeq ($(PORTNAME), linux)
169   LINK.shared           = $(COMPILER) -shared -Wl,-soname,$(soname)
170 endif
171
172 ifeq ($(PORTNAME), solaris)
173   ifeq ($(GCC), yes)
174     LINK.shared         = $(CC) -shared
175   else
176     LINK.shared         = $(CC) -G
177   endif
178   ifeq ($(with_gnu_ld), yes)
179     LINK.shared         += -Wl,-soname,$(soname)
180   else
181     LINK.shared         += -h $(soname)
182   endif
183 endif
184
185 ifeq ($(PORTNAME), sunos4)
186   LINK.shared           = $(LD) -assert pure-text -Bdynamic
187 endif
188  
189 ifeq ($(PORTNAME), osf)
190   LINK.shared           = $(LD) -shared -expect_unresolved '*'
191 endif
192
193 ifeq ($(PORTNAME), sco)
194   ifeq ($(GCC), yes)
195     LINK.shared         = $(CC) -shared
196   else
197     LINK.shared         = $(CC) -G
198     endif
199   LINK.shared           += -Wl,-z,text -Wl,-h,$(soname)
200 endif
201
202 ifeq ($(PORTNAME), svr4)
203   LINK.shared           = $(LD) -G
204 endif
205
206 ifeq ($(PORTNAME), univel)
207   LINK.shared           = $(LD) -G -z text
208 endif
209
210 ifeq ($(PORTNAME), unixware)
211   ifeq ($(GCC), yes)
212     LINK.shared         = $(CC) -shared
213   else
214     LINK.shared         = $(CC) -G
215   endif
216   LINK.shared           += -Wl,-z,text -Wl,-h,$(soname) 
217 endif
218
219 ifeq ($(PORTNAME), cygwin)
220   shlib                 = $(NAME)$(DLSUFFIX)
221   # needed for /contrib modules, not sure why
222   SHLIB_LINK            += -lpgport
223 endif
224
225 ifeq ($(PORTNAME), win32)
226   shlib                 = lib$(NAME)$(DLSUFFIX)
227 endif
228
229 ifeq ($(PORTNAME), beos)
230   shlib                 = lib$(NAME)$(DLSUFFIX)
231   LINK.shared           = $(LD) -nostart
232   SHLIB_LINK            += -ltermcap -lstdc++.r4 -lbind -lsocket -L/boot/develop/lib/x86
233 endif
234
235 SHLIB_LINK := $(filter -L%, $(LDFLAGS)) $(SHLIB_LINK)
236 ifeq ($(enable_rpath), yes)
237 SHLIB_LINK += $(rpath)
238 endif
239
240 endif # enable_shared
241
242
243
244 ##
245 ## BUILD
246 ##
247
248 .PHONY: all-lib all-static-lib all-shared-lib
249
250 all-lib: all-static-lib all-shared-lib
251
252 all-static-lib: $(PTHREAD_H_WIN32) $(top_builddir)/src/port/pg_config_paths.h lib$(NAME).a
253
254 all-shared-lib: $(PTHREAD_H_WIN32) $(top_builddir)/src/port/pg_config_paths.h $(shlib)
255
256 ifndef LORDER
257 MK_NO_LORDER := true
258 endif
259
260 lib$(NAME).a: $(OBJS)
261 ifdef MK_NO_LORDER
262         $(LINK.static) $@ $^
263 else
264         $(LINK.static) $@ `$(LORDER) $^ | tsort`
265 endif
266         $(RANLIB) $@
267
268 ifeq ($(enable_shared), yes)
269
270 ifneq ($(PORTNAME), win32)
271 ifneq ($(PORTNAME), cygwin)
272 ifneq ($(PORTNAME), beos)
273 ifneq ($(PORTNAME), aix)
274
275 # Normal case
276 $(shlib): $(OBJS)
277         $(LINK.shared) $(LDFLAGS_SL) $(OBJS) $(SHLIB_LINK) -o $@
278 # If we're using major and minor versions, then make a symlink to major-version-only.
279 ifneq ($(shlib), $(shlib_major))
280         rm -f $(shlib_major)
281         $(LN_S) $(shlib) $(shlib_major)
282 endif
283 # Make sure we have a link to a name without any version numbers
284 ifneq ($(shlib), $(shlib_bare))
285         rm -f $(shlib_bare)
286         $(LN_S) $(shlib) $(shlib_bare)
287 endif
288
289 else # PORTNAME == aix
290
291 # AIX case
292 $(shlib): lib$(NAME).a
293         $(MKLDEXPORT) lib$(NAME).a > lib$(NAME)$(EXPSUFF)
294         $(COMPILER) $(LDFLAGS_SL) -o $@ $< $(LDFLAGS) $(SHLIB_LINK) -Wl,-bI:$(top_builddir)/src/backend/$(POSTGRES_IMP) -Wl,-bE:lib$(NAME)$(EXPSUFF)
295         
296 endif # PORTNAME == aix
297
298 else # PORTNAME == beos
299
300 # BEOS case
301 $(shlib): $(OBJS)
302         ln -fs $(top_srcdir)/src/backend/postgres _APP_
303         $(CC) -Xlinker -soname=$@ $(LDFLAGS_SL) -o $@ _APP_ $(OBJS) $(SHLIB_LINK)
304
305 endif # PORTNAME == beos
306
307 else # PORTNAME == cygwin
308
309 # Cygwin case
310 $(shlib) lib$(NAME).a: $(OBJS)
311 ifndef DLL_DEFFILE
312         $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
313         $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(SHLIB_LINK)
314         $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a
315 else
316         $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
317         $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib lib$(NAME).a
318 endif
319
320 endif # PORTNAME == cygwin
321
322 else # PORTNAME == win32
323
324 # win32 case
325 $(shlib) lib$(NAME).a: $(OBJS)
326 ifndef DLL_DEFFILE
327         $(DLLTOOL) --export-all $(DLLTOOL_DEFFLAGS) --output-def $(NAME).def $(OBJS)
328         $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(NAME).def $(OBJS) $(SHLIB_LINK)
329         $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(NAME).def --output-lib lib$(NAME).a
330 else
331         $(DLLWRAP) $(LDFLAGS_SL) -o $(shlib) --dllname $(shlib) $(DLLWRAP_FLAGS) --def $(DLL_DEFFILE) $(OBJS) $(SHLIB_LINK)
332         $(DLLTOOL) --dllname $(shlib) $(DLLTOOL_LIBFLAGS) --def $(DLL_DEFFILE) --output-lib lib$(NAME).a
333 endif
334
335 endif # PORTNAME == win32
336
337 endif # enable_shared
338
339
340 ##
341 ## INSTALL
342 ##
343
344 .PHONY: install-lib install-lib-static install-lib-shared
345 install-lib: install-lib-static install-lib-shared
346
347 install-lib-static: lib$(NAME).a
348         $(INSTALL_STLIB) $< $(DESTDIR)$(libdir)/lib$(NAME).a
349 ifeq ($(PORTNAME), darwin)
350         cd $(DESTDIR)$(libdir) && \
351         ranlib lib$(NAME).a
352 endif
353
354 ifeq ($(enable_shared), yes)
355 install-lib-shared: $(shlib)
356         $(INSTALL_SHLIB) $< $(DESTDIR)$(libdir)/$(shlib)
357 ifneq ($(PORTNAME), cygwin)
358 ifneq ($(PORTNAME), win32)
359 ifneq ($(shlib), $(shlib_major))
360         cd $(DESTDIR)$(libdir) && \
361         rm -f $(shlib_major) && \
362         $(LN_S) $(shlib) $(shlib_major)
363 endif
364 ifneq ($(shlib), $(shlib_bare))
365         cd $(DESTDIR)$(libdir) && \
366         rm -f $(shlib_bare) && \
367         $(LN_S) $(shlib) $(shlib_bare)
368 endif
369 endif # not win32
370 endif # not cygwin
371 endif # enable_shared
372
373
374 ##
375 ## UNINSTALL
376 ##
377
378 .PHONY: uninstall-lib
379 uninstall-lib:
380         rm -f $(DESTDIR)$(libdir)/lib$(NAME).a
381 ifeq ($(enable_shared), yes)
382         rm -f $(DESTDIR)$(libdir)/$(shlib_bare) \
383           $(DESTDIR)$(libdir)/$(shlib_major) \
384           $(DESTDIR)$(libdir)/$(shlib)
385 endif # enable_shared
386
387
388 ##
389 ## CLEAN
390 ##
391
392 .PHONY: clean-lib
393 clean-lib:
394         rm -f lib$(NAME).a
395 ifeq ($(enable_shared), yes)
396         rm -f $(shlib_bare) $(shlib_major) $(shlib)
397 ifdef EXPSUFF
398         rm -f lib$(NAME)$(EXPSUFF)
399 endif
400 endif
401 ifeq ($(PORTNAME), cygwin)
402         rm -f $(NAME).dll $(NAME).def
403 endif
404
405 ifeq ($(PORTNAME), win32)
406         rm -f $(NAME).dll $(NAME).def
407 endif