Better errors. Use install path, if it exists
[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 # Add these rules only when LIBTOOL is being used.
17 ifneq "${LIBTOOL}" ""
18
19 # JLIBTOOL - check if we're using the local (fast) jlibtool, rather
20 #   than the GNU (slow) libtool shell script.  If so, add rules
21 #   to build it.
22
23 ifeq "${LIBTOOL}" "JLIBTOOL"
24     JLIBTOOL := ${BUILD_DIR}/make/jlibtool
25
26     # Add a rule to build jlibtool BEFORE any other targets.  This
27     # means that we can use it to build the later targets.
28     all install: ${JLIBTOOL}
29
30     # Note that we need to use a compilation rule that does NOT
31     # include referencing ${LIBTOOL}, as we don't have a jlibtool
32     # binary!
33     jlibtool ${JLIBTOOL}: ${top_makedir}/jlibtool.c
34         @mkdir -p $(dir ${JLIBTOOL})
35         @echo CC jlibtool.c
36         @${CC} $< -o ${JLIBTOOL}
37
38     clean: jlibtool_clean
39
40     .PHONY: jlibtool_clean
41     jlibtool_clean:
42         @rm -f ${JLIBTOOL}
43
44     # Tell GNU Make to use this value, rather than anything specified
45     # on the command line.
46     override LIBTOOL := ${JLIBTOOL}
47 endif    # else we're not using jlibtool
48
49 # When using libtool, it produces a '.libs' directory.  Ensure that it
50 # is removed on "make clean", too.
51 #
52 clean: .libs_clean
53
54 .PHONY: .libs_clean
55 .libs_clean:
56         @rm -rf ${BUILD_DIR}/.libs/
57
58 # Re-define compilers and linkers
59 #
60 OBJ_EXT = lo
61 COMPILE.c = ${LIBTOOL} --silent --mode=compile ${CC}
62 COMPILE.cxx = ${LIBTOOL} --mode=compile ${CXX}
63 LINK.c = ${LIBTOOL} --silent --mode=link ${CC}
64 LINK.cxx = ${LIBTOOL} --mode=link ${CXX}
65 PROGRAM_INSTALL = ${LIBTOOL} --silent --mode=install ${INSTALL}
66
67
68 # LIBTOOL_ENDINGS - Given a library ending in ".a" or ".so", replace that
69 #   extension with ".la".
70 #
71 define LIBTOOL_ENDINGS
72 $(patsubst %.a,%.la,$(patsubst %.so,%.la,${1}))
73 endef
74
75 # ADD_TARGET_RULE.la - Build a ".la" target.
76 #
77 #   USE WITH EVAL
78 #
79 define ADD_TARGET_RULE.la
80     # So "make ${1}" works
81     .PHONY: ${1}
82     ${1}: $${${1}_BUILD}/${1}
83
84     # Create libtool library ${1}
85     $${${1}_BUILD}/${1}: $${${1}_OBJS} $${${1}_PREREQS}
86             @$(strip mkdir -p $(dir $${${1}_BUILD}/${1}))
87             @$(ECHO) LINK $${${1}_BUILD}/${1}
88             @$${${1}_LINKER} -o $${${1}_BUILD}/${1} $${RPATH_FLAGS} $${LDFLAGS} \
89                 $${${1}_LDFLAGS} $${${1}_OBJS} $${LDLIBS} $${${1}_LDLIBS}
90             @$${${1}_POSTMAKE}
91
92 endef
93
94 # ADD_RELINK_RULE.exe - Parametric "function" that adds a rule to relink
95 #   the target before installation, so that the paths are correct.
96 #
97 #   USE WITH EVAL
98 #
99 define ADD_RELINK_RULE.exe
100     ${1}: $${${1}_BUILD}/$${${1}_RELINK}
101
102     # used to fix up RPATH for ${1} on install.
103     $${${1}_BUILD}/$${${1}_RELINK}: $${${1}_OBJS} $${${1}_PRBIN} $${${1}_R_PRLIBS}
104             @$(strip mkdir -p $${${1}_BUILD}/${RELINK}/)
105             @$${${1}_LINKER} -o $${${1}_BUILD}/$${${1}_RELINK} $${RELINK_FLAGS} $${LDFLAGS} \
106                 $${${1}_LDFLAGS} $${${1}_OBJS} $${${1}_R_PRLIBS} \
107                 $${LDLIBS} $${${1}_LDLIBS}
108             @$${${1}_POSTMAKE}
109 endef
110
111 # ADD_RELINK_RULE.la - Parametric "function" that adds a rule to relink
112 #   the target before installation, so that the paths are correct.
113 #
114 #   USE WITH EVAL
115 #
116 define ADD_RELINK_RULE.la
117     ${1}: $${${1}_BUILD}/$${${1}_RELINK}
118
119     # used to fix up RPATH for ${1} on install.
120     $${${1}_BUILD}/$${${1}_RELINK}: $${${1}_OBJS} $${${1}_PREREQS}
121             @$(strip mkdir -p $${${1}_BUILD}/${RELINK}/)
122             @$${${1}_LINKER} -o $${${1}_BUILD}/$${${1}_RELINK} $${RELINK_FLAGS} $${LDFLAGS} \
123                 $${${1}_LDFLAGS} $${${1}_OBJS} $${LDLIBS} $${${1}_LDLIBS}
124             @$${${1}_POSTMAKE}
125
126 endef
127
128 # By default, if libdir is defined, we build shared libraries.
129 # However, we can disable shared libraries if explicitly told to.
130 ifneq "${libdir}" ""
131     ifneq "bm_shared_libs" "no"
132         bm_shared_libs := yes
133     endif
134 endif
135
136 # Default to building static libraries, too.
137 ifneq "${bm_static_libs}" "no"
138     bm_static_libs := yes
139 endif
140
141 # Check if we build shared libraries.
142 ifeq "${bm_shared_libs}" "yes"
143     # RPATH  : flags use to build executables that can be run
144     #          from the build directory / source tree.
145     # RELINK : flags use to build executables that are installed,
146     #          with no dependency on the source. 
147     RPATH_FLAGS := -rpath $(abspath ${BUILD_DIR})/lib/.libs -rdynamic
148     RELINK_FLAGS := -rpath ${libdir} -rdynamic
149     RELINK := relink/
150
151     ifneq "${bm_static_libs}" "yes"
152         RPATH_FLAGS += --shared
153         RELINK_FLAGS += --shared
154     endif
155 else
156     ifneq "${bm_static_libs}" "yes"
157         $(error Building without static libraries requires you to set 'INSTALL' or 'libdir')
158     endif
159
160     RPATH_FLAGS := -static
161 endif
162
163 # UPDATE_TARGET_ENDINGS - Function to turn target into a libtool target
164 #   e.g. "libfoo.a" -> libfoo.la"
165 #
166 #   If the target is an executable, then its extension doesn't change
167 #   when we use libtool, and we don't do any re-writing.
168 #
169 #   USE WITH EVAL
170 #
171 define ADD_LIBTOOL_SUFFIX
172     ifneq "$$(call LIBTOOL_ENDINGS,$${TGT})" "$${TGT}"
173         TGT_NOLIBTOOL := $${TGT}
174         TGT := $$(call LIBTOOL_ENDINGS,$${TGT})
175         $${TGT}_NOLIBTOOL := $${TGT_NOLIBTOOL}
176     endif
177
178     ifneq "$${RELINK_FLAGS}" ""
179         $${TGT}_RELINK := ${RELINK}$${TGT}
180     endif
181
182     # re-write all of the dependencies to have the libtool endings.
183     TGT_PREREQS := $$(call LIBTOOL_ENDINGS,$${TGT_PREREQS})
184 endef
185
186 # ADD_LIBTOOL_TARGET - Function to ensure that the object files depend
187 #   on our jlibtool target.  This ensures that jlibtool is built before
188 #   it's used to build the object files.
189 #
190 #   USE WITH EVAL
191 #
192 define ADD_LIBTOOL_TARGET
193     ifneq "${JLIBTOOL}" ""
194         $${$${TGT}_OBJS}: $${JLIBTOOL}
195     endif
196
197     ifneq "$${$${TGT}_NOLIBTOOL}" ""
198         $$(notdir $${$${TGT}_NOLIBTOOL}): $${TGT}
199     endif
200
201     # If we need to relink, add the relink targets now.
202     ifneq "$${$${TGT}_RELINK}" ""
203         # add rules to relink the target
204
205         $${TGT}_R_PRLIBS := $$(subst /lib/,/lib/relink/,$${$${TGT}_PRLIBS})
206
207         $$(eval $$(call ADD_RELINK_RULE$${$${TGT}_SUFFIX},$${TGT}))
208     endif
209
210 endef
211
212
213 endif