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