]> granicus.if.org Git - postgresql/blob - src/Makefile.shlib
Support for DESTDIR make variable. This is used as in `make install
[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 #    $Header: /cvsroot/pgsql/src/Makefile.shlib,v 1.23 2000/09/17 13:02:29 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 # 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 # The module Makefile must also include
30 # $(top_builddir)/src/Makefile.global before including this file.
31 # (Makefile.global sets PORTNAME and other needed symbols.)
32 #
33 # This makefile provides the following (phony) targets:
34 #
35 # all-lib               build the static and shared (if applicable) libraries
36 # install-lib           install the libraries into $(libdir)
37 # uninstall-lib         remove the libraries from $(libdir)
38 # clean-lib             delete the static and shared libraries from the build dir
39 #
40 # Since `all-lib' is the first rule in this file you probably want to
41 # have the `all' target before including this file. In the most simple
42 # case it would look like this:
43 #
44 #     all: all-lib
45 #
46 # Similarly, the install rule might look like
47 #
48 #     install: install-lib
49 #
50 # plus any additional things you want to install. Et cetera.
51 #
52 # Got that?  Look at src/interfaces/libpq/Makefile for an example.
53
54
55 # shlib is empty by default. If we know how to build a shared library
56 # it will contain the name of the file, otherwise it will remain
57 # empty. Thus `ifdef shlib' could be used in the containing make file
58 # to test whether shared libraries are available.
59 shlib := 
60
61 # For each platform we support shared libraries on, set shlib and
62 # update flags as needed to build a shared lib. Note we depend on
63 # Makefile.global (or really Makefile.port) to supply DLSUFFIX and
64 # other symbols.
65
66 # Try to keep the sections in some kind of order, folks...
67
68 ifeq ($(PORTNAME), aix)
69   shlib                 := lib$(NAME)$(DLSUFFIX)
70   SHLIB_LINK            += -lc
71 endif
72
73 ifeq ($(PORTNAME), bsd)
74   ifdef BSD_SHLIB
75     shlib               := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
76     ifdef ELF_SYSTEM
77       LDFLAGS_SL        := -x -Bshareable -soname $(shlib)
78     else
79       LDFLAGS_SL        := -x -Bshareable -Bforcearchive
80     endif
81     CFLAGS              += $(CFLAGS_SL)
82   endif
83 endif
84
85 ifeq ($(PORTNAME), bsdi)
86   ifdef BSD_SHLIB
87     ifeq ($(DLSUFFIX), .so)
88       shlib                     := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
89       LDFLAGS_SL                += -shared -soname $(shlib)
90       CFLAGS                    += $(CFLAGS_SL)
91     endif
92     ifeq ($(DLSUFFIX), .o)
93       shlib                     := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
94       LD                        := shlicc
95       LDFLAGS_SL                += -O $(LDREL)
96       CFLAGS                    += $(CFLAGS_SL)
97     endif
98   endif
99 endif
100
101 ifeq ($(PORTNAME), freebsd)
102   ifdef BSD_SHLIB
103     ifdef ELF_SYSTEM
104       shlib             := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
105       LDFLAGS_SL        := -x -shared -soname $(shlib)
106     else
107       shlib             := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
108       LDFLAGS_SL        := -x -Bshareable -Bforcearchive
109     endif
110     CFLAGS              += $(CFLAGS_SL)
111   endif
112 endif
113
114 ifeq ($(PORTNAME), netbsd)
115   ifdef BSD_SHLIB
116     soname              := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
117     shlib               := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
118     ifdef ELF_SYSTEM
119       LD                := $(CC)
120       LDFLAGS_SL        := -shared -Wl,-soname -Wl,$(soname)
121       ifneq ($(SHLIB_LINK),)
122         LDFLAGS_SL      += -Wl,-R$(libdir)
123       endif
124     else
125       LDFLAGS_SL        := -x -Bshareable -Bforcearchive
126     endif
127     CFLAGS              += $(CFLAGS_SL)
128   endif
129 endif
130
131 ifeq ($(PORTNAME), hpux)
132 # HPUX doesn't believe in version numbers for shlibs
133   shlib                 := lib$(NAME)$(DLSUFFIX)
134   LDFLAGS_SL            := -b
135   CFLAGS                += $(CFLAGS_SL)
136 endif
137
138 ifeq ($(PORTNAME), irix5)
139   shlib := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
140   LDFLAGS_SL := -shared -rpath $(libdir) -set_version sgi$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
141   CFLAGS += $(CFLAGS_SL)
142 endif
143
144 ifeq ($(PORTNAME), linux)
145   shlib                 := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
146   LD                    := $(CC)
147   LDFLAGS_SL            := -shared -Wl,-soname,$(shlib)
148   LDFLAGS_ODBC          := -lm
149   CFLAGS                += $(CFLAGS_SL)
150 endif
151
152 ifeq ($(PORTNAME), solaris_i386)
153   shlib                 := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
154   LDFLAGS_SL            := -G
155   SHLIB_LINK            += -ldl -lsocket -lresolv -lnsl -lm -lc
156   CFLAGS                += $(CFLAGS_SL)
157 endif
158
159 ifeq ($(PORTNAME), solaris_sparc)
160   shlib                 := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
161   LDFLAGS_SL            := -G
162   SHLIB_LINK            += -ldl -lsocket -lresolv -lnsl -lm -lc
163   CFLAGS                += $(CFLAGS_SL)
164 endif
165
166 ifeq ($(PORTNAME), alpha)
167   shlib                 := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
168   LDFLAGS_SL            += -shared -expect_unresolved '*'
169 endif
170
171 ifeq ($(PORTNAME), svr4)
172   shlib                 := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
173   LDFLAGS_SL            := -G
174   CFLAGS                += $(CFLAGS_SL)
175 endif
176
177 ifeq ($(PORTNAME), univel)
178   shlib                 := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
179   LDFLAGS_SL            := -G -z text
180   CFLAGS                += $(CFLAGS_SL)
181   ifeq ($(CXX), CC)
182     CXXFLAGS += -Xw
183     COMPILE.cc = $(CXX) $(CXXFLAGS:ll,alloca=ll) $(CPPFLAGS) $(TARGET_ARCH) -c
184   endif
185 endif
186
187 ifeq ($(PORTNAME), unixware)
188   shlib                 := lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
189   LDFLAGS_SL            := -G -z text
190   CFLAGS                += $(CFLAGS_SL)
191   ifeq ($(CXX), CC)
192     CXXFLAGS += -Xw
193     COMPILE.cc = $(CXX) $(CXXFLAGS:ll,alloca=ll) $(CPPFLAGS) $(TARGET_ARCH) -c
194   endif
195 endif
196
197 ifeq ($(PORTNAME), win)
198   shlib                 := $(NAME)$(DLSUFFIX)
199 endif
200
201
202 # Note that in what follows, shlib is empty when not building a shared
203 # library.
204
205
206 ##
207 ## BUILD
208 ##
209
210 .PHONY: all-lib
211 all-lib: lib$(NAME).a $(shlib)
212
213 # Rules to build regular and shared libraries
214
215 ifneq ($(PORTNAME), win)
216
217 lib$(NAME).a: $(OBJS)
218 ifdef MK_NO_LORDER
219         $(AR) $(AROPT) $@ $(OBJS) 
220 else
221         $(AR) $(AROPT) $@ `lorder $(OBJS) | tsort`
222 endif
223         $(RANLIB) $@
224
225 endif # not win
226
227 ifdef shlib
228 ifneq ($(PORTNAME), win)
229 ifneq ($(PORTNAME), aix)
230
231 # Normal case
232 $(shlib): $(OBJS)
233         $(LD) $(LDFLAGS_SL) -o $@ $(OBJS) $(SHLIB_LINK)
234 # If we're using major and minor versions, then make a symlink to major-version-only.
235 ifneq ($(shlib), lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION))
236         rm -f lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
237         $(LN_S) $(shlib) lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
238 endif
239 # Make sure we have a link to a name without any version numbers
240 ifneq ($(shlib), lib$(NAME)$(DLSUFFIX))
241         rm -f lib$(NAME)$(DLSUFFIX)
242         $(LN_S) $(shlib) lib$(NAME)$(DLSUFFIX)
243 endif
244
245 else # PORTNAME == aix
246
247 # AIX case
248 $(shlib): lib$(NAME).a
249         $(MKLDEXPORT) lib$(NAME).a $(libdir) > lib$(NAME)$(EXPSUFF)
250         $(LD) -H512 -bM:SRE -bI:$(top_builddir)/src/backend/$(POSTGRES_IMP) -bE:lib$(NAME)$(EXPSUFF) -o $@ $< $(LDFLAGS) $(SHLIB_LINK)
251
252 endif # PORTNAME == aix
253
254 else # PORTNAME == win
255
256 # WIN case
257 $(shlib) lib$(NAME).a: $(OBJS) $(top_builddir)/src/utils/dllinit.o
258         $(DLLTOOL) --export-all --output-def $(NAME).def $(OBJS)
259         $(DLLWRAP) -o $(shlib) --dllname $(shlib) --def $(NAME).def $(OBJS) $(top_builddir)/src/utils/dllinit.o $(DLLINIT) $(SHLIB_LINK)
260         $(DLLTOOL) --dllname $(shlib) --def $(NAME).def --output-lib lib$(NAME).a
261
262 $(top_builddir)/src/utils/dllinit.o: $(top_srcdir)/src/utils/dllinit.c
263         $(MAKE) -C $(top_builddir)/src/utils dllinit.o
264
265 endif # PORTNAME == win
266 endif # shlib
267
268
269 ##
270 ## INSTALL
271 ##
272
273 .PHONY: install-lib install-lib-static install-lib-shared
274 install-lib: install-lib-static install-lib-shared
275
276 install-lib-static: lib$(NAME).a
277         $(INSTALL_DATA) $< $(DESTDIR)$(libdir)/lib$(NAME).a
278
279 ifdef shlib
280 install-lib-shared: $(shlib)
281         $(INSTALL_SHLIB) $< $(DESTDIR)$(libdir)/$(shlib)
282 ifneq ($(PORTNAME), win)
283 ifneq ($(shlib), lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION))
284         cd $(DESTDIR)$(libdir) && \
285         rm -f lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION) && \
286         $(LN_S) $(shlib) lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION)
287 endif
288 ifneq ($(shlib), lib$(NAME)$(DLSUFFIX))
289         cd $(DESTDIR)$(libdir) && \
290         rm -f lib$(NAME)$(DLSUFFIX) && \
291         $(LN_S) $(shlib) lib$(NAME)$(DLSUFFIX)
292 endif
293
294 endif # not win
295 endif # shlib
296
297
298 ##
299 ## UNINSTALL
300 ##
301
302 .PHONY: uninstall-lib
303 uninstall-lib:
304         rm -f $(DESTDIR)$(libdir)/lib$(NAME).a
305 ifdef shlib
306         rm -f $(DESTDIR)$(libdir)/lib$(NAME)$(DLSUFFIX) \
307           $(DESTDIR)$(libdir)/lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION) \
308           $(DESTDIR)$(libdir)/lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
309 endif # shlib
310
311
312 ##
313 ## CLEAN
314 ##
315
316 .PHONY: clean-lib
317 clean-lib:
318         rm -f lib$(NAME).a
319         rm -f $(shlib) lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION) lib$(NAME)$(DLSUFFIX)
320 ifeq ($(PORTNAME), win)
321         rm -rf $(NAME).def
322 endif