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