Merge pull request #1281 from alanbuxey/patch-11
[freeradius.git] / scripts / libtool.mk
1 # Copyright 2008, 2009, 2010 Dan Moulding, Alan T. DeKok
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 #
17 #  You can watch what it's doing by:
18 #
19 #       $ VERBOSE=1 make ... args ...
20 #
21 ifeq "${VERBOSE}" ""
22     Q=@
23 else
24     Q=
25 endif
26
27 # Add these rules only when LIBTOOL is being used.
28 ifneq "${LIBTOOL}" ""
29
30 # JLIBTOOL - check if we're using the local (fast) jlibtool, rather
31 #   than the GNU (slow) libtool shell script.  If so, add rules
32 #   to build it.
33
34 ifeq "${LIBTOOL}" "JLIBTOOL"
35     JLIBTOOL := ${BUILD_DIR}/make/jlibtool
36
37     # Add a rule to build jlibtool BEFORE any other targets.  This
38     # means that we can use it to build the later targets.
39     all install: ${JLIBTOOL}
40
41     # Note that we need to use a compilation rule that does NOT
42     # include referencing ${LIBTOOL}, as we don't have a jlibtool
43     # binary!
44     ${JLIBTOOL}: ${top_makedir}/jlibtool.c
45         $(Q)mkdir -p $(dir $@)
46         $(Q)echo CC jlibtool.c
47         $(Q)${CC} $< -o $@
48
49     clean: jlibtool_clean
50
51     .PHONY: jlibtool_clean
52     jlibtool_clean:
53         $(Q)rm -f ${JLIBTOOL}
54
55     # Tell GNU Make to use this value, rather than anything specified
56     # on the command line.
57     override LIBTOOL := ${JLIBTOOL}
58 endif    # else we're not using jlibtool
59
60 # When using libtool, it produces a '.libs' directory.  Ensure that it
61 # is removed on "make clean", too.
62 #
63 clean: .libs_clean
64
65 .PHONY: .libs_clean
66 .libs_clean:
67         $(Q)rm -rf ${BUILD_DIR}/.libs/
68
69 # Re-define compilers and linkers
70 #
71 OBJ_EXT = lo
72 COMPILE.c = ${LIBTOOL} --silent --mode=compile ${CC}
73 COMPILE.cxx = ${LIBTOOL} --mode=compile ${CXX}
74 LINK.c = ${LIBTOOL} --silent --mode=link ${CC}
75 LINK.cxx = ${LIBTOOL} --mode=link ${CXX}
76 PROGRAM_INSTALL = ${LIBTOOL} --silent --mode=install ${INSTALL}
77
78
79 # LIBTOOL_ENDINGS - Given a library ending in ".a" or ".so", replace that
80 #   extension with ".la".
81 #
82 define LIBTOOL_ENDINGS
83 $(patsubst %.a,%.la,$(patsubst %.so,%.la,${1}))
84 endef
85
86 # ADD_TARGET_RULE.la - Build a ".la" target.
87 #
88 #   USE WITH EVAL
89 #
90 define ADD_TARGET_RULE.la
91     # So "make ${1}" works
92     .PHONY: ${1}
93     ${1}: $${${1}_BUILD}/${1}
94
95     # Create libtool library ${1}
96     $${${1}_BUILD}/${1}: $${${1}_OBJS} $${${1}_PRLIBS}
97             $(Q)$(strip mkdir -p $(dir $${${1}_BUILD}/${1}))
98             @$(ECHO) LINK $${${1}_BUILD}/${1}
99             $(Q)$${${1}_LINKER} -o $${${1}_BUILD}/${1} $${RPATH_FLAGS} $${LDFLAGS} \
100                 $${${1}_LDFLAGS} $${${1}_OBJS} $${LDLIBS} $${${1}_LDLIBS} \
101                 $${${1}_PRLIBS}
102             $(Q)$${${1}_POSTMAKE}
103
104     ifneq "${ANALYZE.c}" ""
105         scan.${1}: $${${1}_PLISTS}
106     endif
107 endef
108
109 # ADD_RELINK_RULE.exe - Parametric "function" that adds a rule to relink
110 #   the target before installation, so that the paths are correct.
111 #
112 #   USE WITH EVAL
113 #
114 define ADD_RELINK_RULE.exe
115     ${1}: $${${1}_BUILD}/$${RELINK}${1}
116
117     # used to fix up RPATH for ${1} on install.
118     $${${1}_BUILD}/$${${1}_RELINK}: $${${1}_OBJS} $${${1}_PRBIN} $${${1}_R_PRLIBS}
119             $(Q)$(strip mkdir -p $${${1}_BUILD}/${RELINK}/)
120             $(Q)$${${1}_LINKER} -o $${${1}_BUILD}/$${RELINK}${1} $${RELINK_FLAGS} $${LDFLAGS} \
121                 $${${1}_LDFLAGS} $${${1}_OBJS} $${${1}_R_PRLIBS} \
122                 $${LDLIBS} $${${1}_LDLIBS}
123             $(Q)$${${1}_POSTMAKE}
124 endef
125
126 # ADD_RELINK_RULE.la - Parametric "function" that adds a rule to relink
127 #   the target before installation, so that the paths are correct.
128 #
129 #   USE WITH EVAL
130 #
131 define ADD_RELINK_RULE.la
132     ${1}: $${${1}_BUILD}/$${RELINK}${1}
133
134     # used to fix up RPATH for ${1} on install.
135     $${${1}_BUILD}/$${${1}_RELINK}: $${${1}_OBJS} $${${1}_PRLIBS}
136             $(Q)$(strip mkdir -p $${${1}_BUILD}/${RELINK}/)
137             $(Q)$${${1}_LINKER} -o $${${1}_BUILD}/$${RELINK}${1} $${RELINK_FLAGS} $${LDFLAGS} \
138                 $${${1}_LDFLAGS} $${${1}_OBJS} $${LDLIBS} $${${1}_LDLIBS}
139             $(Q)$${${1}_POSTMAKE}
140
141 endef
142
143 # By default, if libdir is defined, we build shared libraries.
144 # However, we can disable shared libraries if explicitly told to.
145 ifneq "${libdir}" ""
146     ifneq "${bm_shared_libs}" "no"
147         bm_shared_libs := yes
148     endif
149 endif
150
151 # Default to building static libraries, too.
152 ifneq "${bm_static_libs}" "no"
153     bm_static_libs := yes
154 endif
155
156 # Check if we build shared libraries.
157 ifeq "${bm_shared_libs}" "yes"
158     RELINK := local/
159
160     # RPATH  : flags use to build executables that are installed,
161     #          with no dependency on the source.
162     # RELINL : flags use to build executables that can be run
163     #          from the build directory / source tree.
164     RPATH_FLAGS := -rpath ${libdir}
165     RELINK_FLAGS := -rpath $(abspath ${BUILD_DIR})/lib/${RELINK}/.libs
166
167     RELINK_FLAGS_MIN := -rpath ${libdir}
168
169     ifneq "${bm_static_libs}" "yes"
170         RPATH_FLAGS += --shared
171         RELINK_FLAGS += --shared
172     endif
173 else
174     ifneq "${bm_static_libs}" "yes"
175         $(error Building without static libraries requires you to set 'INSTALL' or 'libdir')
176     endif
177
178     RPATH_FLAGS := -static
179 endif
180
181 # UPDATE_TARGET_ENDINGS - Function to turn target into a libtool target
182 #   e.g. "libfoo.a" -> libfoo.la"
183 #
184 #   If the target is an executable, then its extension doesn't change
185 #   when we use libtool, and we don't do any re-writing.
186 #
187 #   USE WITH EVAL
188 #
189 define ADD_LIBTOOL_SUFFIX
190     ifneq "$$(call LIBTOOL_ENDINGS,$${TGT})" "$${TGT}"
191         TGT_NOLIBTOOL := $${TGT}
192         TGT := $$(call LIBTOOL_ENDINGS,$${TGT})
193         $${TGT}_NOLIBTOOL := $${TGT_NOLIBTOOL}
194     endif
195
196     ifneq "$${RELINK_FLAGS}" ""
197         $${TGT}_RELINK := ${RELINK}$${TGT}
198     endif
199
200     # re-write all of the dependencies to have the libtool endings.
201     TGT_PREREQS := $$(call LIBTOOL_ENDINGS,$${TGT_PREREQS})
202 endef
203
204 # ADD_LIBTOOL_TARGET - Function to ensure that the object files depend
205 #   on our jlibtool target.  This ensures that jlibtool is built before
206 #   it's used to build the object files.
207 #
208 #   USE WITH EVAL
209 #
210 define ADD_LIBTOOL_TARGET
211     ifneq "${JLIBTOOL}" ""
212         $${$${TGT}_OBJS}: $${JLIBTOOL}
213     endif
214
215     ifneq "$${$${TGT}_NOLIBTOOL}" ""
216         $$(notdir $${$${TGT}_NOLIBTOOL}): $${TGT}
217     endif
218
219     # If we need to relink, add the relink targets now.
220     ifneq "$${$${TGT}_RELINK}" ""
221         # add rules to relink the target
222
223         $${TGT}_R_PRLIBS := $$(subst /lib/,/lib/${RELINK},$${$${TGT}_PRLIBS})
224
225         $$(eval $$(call ADD_RELINK_RULE$${$${TGT}_SUFFIX},$${TGT}))
226
227         $$(eval $$(call ADD_CLEAN_RULE,$${$${TGT}_RELINK}_libtool))
228
229         ifneq "$${$${TGT}_NOLIBTOOL}" ""
230             $$(eval $$(call ADD_CLEAN_RULE,$${$${TGT}_NOLIBTOOL}_libtool))
231         endif
232     endif
233
234 endef
235
236
237 endif