build-system: Allow building with static pjproject
[asterisk/asterisk.git] / main / Makefile
1 #
2 # Asterisk -- An open source telephony toolkit.
3 #
4 # Makefile to build main Asterisk binary
5 #
6 # Copyright (C) 1999-2006, Digium, Inc.
7 #
8 # Mark Spencer <markster@digium.com>
9 #
10 # This program is free software, distributed under the terms of
11 # the GNU General Public License
12 #
13
14 -include $(ASTTOPDIR)/menuselect.makeopts $(ASTTOPDIR)/menuselect.makedeps $(ASTTOPDIR)/makeopts.embed_rules $(ASTTOPDIR)/makeopts
15
16 all: asterisk
17
18 include $(ASTTOPDIR)/Makefile.moddir_rules
19
20 # Must include the extra ast_expr2.c, ast_expr2f.c, in case they need to be regenerated (because to force regeneration, we delete them)
21 SRC:=$(wildcard *.c) ast_expr2.c ast_expr2f.c
22 ifeq ($(AST_ASTERISKSSL),yes)
23 SRC:=$(filter-out libasteriskssl.c,$(SRC))
24 endif
25 ifeq ($(PJPROJECT_BUNDLED),yes)
26 SRC:=$(filter-out libasteriskpj.c,$(SRC))
27 endif
28 OBJSFILTER=fskmodem_int.o fskmodem_float.o cygload.o buildinfo.o
29 OBJS=$(filter-out $(OBJSFILTER),$(SRC:.c=.o))
30
31 # we need to link in the objects statically, not as a library, because
32 # otherwise modules will not have them available if none of the static
33 # objects use it.
34 OBJS+=stdtime/localtime.o
35
36 ASTSSL_LIBS:=$(OPENSSL_LIB)
37 AST_LIBS+=$(BKTR_LIB)
38 AST_LIBS+=$(LIBXML2_LIB)
39 AST_LIBS+=$(LIBXSLT_LIB)
40 AST_LIBS+=$(SQLITE3_LIB)
41 AST_LIBS+=$(ASTSSL_LIBS)
42 AST_LIBS+=$(JANSSON_LIB)
43 AST_LIBS+=$(URIPARSER_LIB)
44 AST_LIBS+=$(UUID_LIB)
45 AST_LIBS+=$(CRYPT_LIB)
46 AST_LIBS+=$(AST_CLANG_BLOCKS_LIBS)
47 AST_LIBS+=$(RT_LIB)
48
49 ifneq ($(findstring $(OSARCH), linux-gnu uclinux linux-uclibc kfreebsd-gnu),)
50   ifneq ($(findstring LOADABLE_MODULES,$(MENUSELECT_CFLAGS)),)
51   AST_LIBS+=-ldl
52   endif
53   ifneq (x$(CAP_LIB),x)
54     AST_LIBS+=$(CAP_LIB)
55   endif
56   AST_LIBS+=-lpthread $(EDITLINE_LIB) -lm -lresolv
57 else
58   AST_LIBS+=$(EDITLINE_LIB) -lm
59 endif
60
61 ifneq ($(findstring BETTER_BACKTRACES,$(MENUSELECT_CFLAGS)),)
62   AST_LIBS+=$(BFD_LIB)
63 endif
64
65 ifneq ($(findstring darwin,$(OSARCH)),)
66   AST_LIBS+=-lresolv
67   ASTLINK=-mmacosx-version-min=10.6 -Wl,-undefined,dynamic_lookup -force_flat_namespace
68   ASTLINK+=/usr/lib/bundle1.o
69 else
70 # These are used for all but Darwin
71   ifneq ($(findstring LOADABLE_MODULES,$(MENUSELECT_CFLAGS)),)
72     ASTLINK+=-Wl,--export-dynamic
73   else
74     ASTLINK+=${GC_LDFLAGS}
75   endif
76   ifneq ($(findstring BSD,$(OSARCH)),)
77     LDFLAGS+=-L/usr/local/lib
78   endif
79 endif
80
81 ifeq ($(OSARCH),FreeBSD)
82   # -V is understood by BSD Make, not by GNU make.
83   BSDVERSION=$(shell make -V OSVERSION -f /usr/share/mk/bsd.port.subdir.mk)
84   AST_LIBS+=$(shell if test $(BSDVERSION) -lt 502102 ; then echo "-lc_r"; else echo "-pthread"; fi)
85   AST_LIBS+=-lcrypto
86 endif
87
88 ifneq ($(findstring $(OSARCH), mingw32 cygwin ),)
89   AST_LIBS+=-lminires -ldl
90   ASTLINK+=-shared -Wl,--out-implib,libasterisk.a
91 endif
92 ifeq ($(OSARCH),NetBSD)
93   AST_LIBS+=-lpthread -lcrypto -lm -L/usr/pkg/lib $(EDITLINE_LIB)
94 endif
95
96 ifeq ($(OSARCH),OpenBSD)
97   AST_LIBS+=-lcrypto -lpthread -lm $(EDITLINE_LIB)
98 endif
99
100 ifeq ($(OSARCH),SunOS)
101   AST_LIBS+=-lpthread -ldl -lrt -lnsl -lsocket -lresolv
102   ASTSSL_LIBS+=-L/opt/ssl/lib -L/usr/local/ssl/lib
103   ASTLINK=
104 endif
105
106 ifneq ($(findstring USE_HOARD_ALLOCATOR,$(MENUSELECT_CFLAGS)),)
107   ifneq ($(HOARD_LIB),)
108     AST_LIBS+=$(HOARD_LIB)
109   endif
110 endif
111
112 ifeq ($(GNU_LD),1)
113 ASTLINK+=-Wl,--version-script,asterisk.exports
114   ifeq ($(HAVE_DYNAMIC_LIST),1)
115   ASTLINK+=-Wl,--dynamic-list,asterisk.dynamics
116   endif
117 endif
118
119 .PHONY: CHECK_SUBDIR
120 CHECK_SUBDIR:   # do nothing, just make sure that we recurse in the subdir/
121
122 editline/libedit.a: CHECK_SUBDIR
123         cd editline && test -f config.h || CFLAGS="$(PTHREAD_CFLAGS) $(subst $(ASTTOPDIR),../../,$(_ASTCFLAGS:-Werror=) $(ASTCFLAGS))" LDFLAGS="$(_ASTLDFLAGS) $(ASTLDFLAGS)" ./configure --build=$(BUILD_PLATFORM) --host=$(HOST_PLATFORM) --with-ncurses=$(NCURSES_DIR) --with-curses=$(CURSES_DIR) --with-termcap=$(TERMCAP_DIR) --with-tinfo=$(TINFO_DIR)
124         $(MAKE) -C editline libedit.a
125
126 ifneq ($(findstring REBUILD_PARSERS,$(MENUSELECT_CFLAGS)),)
127 ast_expr2.c ast_expr2.h: ast_expr2.y
128 else
129 ast_expr2.c ast_expr2.h:
130 endif
131         $(ECHO_PREFIX) echo "   [BISON] $< -> $@"
132         $(CMD_PREFIX) $(BISON) -o $@ -d --name-prefix=ast_yy ast_expr2.y
133
134 ifneq ($(findstring REBUILD_PARSERS,$(MENUSELECT_CFLAGS)),)
135 ast_expr2f.c: ast_expr2.fl
136 else
137 ast_expr2f.c:
138 endif
139         $(ECHO_PREFIX) echo "   [FLEX] $< -> $@"
140         $(CMD_PREFIX) $(FLEX) -o $@ ast_expr2.fl
141         $(CMD_PREFIX) sed 's@#if __STDC_VERSION__ >= 199901L@#if !defined __STDC_VERSION__ || __STDC_VERSION__ >= 199901L@' $@ > $@.fix
142         $(CMD_PREFIX) echo "#include \"asterisk.h\"" > $@
143         $(CMD_PREFIX) echo >> $@
144         $(CMD_PREFIX) cat $@.fix >> $@
145         $(CMD_PREFIX) rm $@.fix
146
147 ast_expr2f.o: _ASTCFLAGS+=-Wno-unused
148
149 testexpr2: ast_expr2f.c ast_expr2.c ast_expr2.h
150         $(CC) -g -c -Iinclude -DSTANDALONE ast_expr2f.c
151         $(CC) -g -c -Iinclude -DSTANDALONE ast_expr2.c
152         $(CC) -g -o testexpr2 ast_expr2f.o ast_expr2.o -lm
153         rm ast_expr2.o ast_expr2f.o
154
155 ifneq ($(LIBEDIT_INTERNAL),no)
156 LIBEDIT_OBJ=editline/libedit.a
157 LIBEDIT_INCLUDE=-I. -Ieditline
158 endif
159
160 db.o: _ASTCFLAGS+=$(SQLITE3_INCLUDE)
161 asterisk.o: _ASTCFLAGS+=$(LIBEDIT_INCLUDE)
162 cli.o: _ASTCFLAGS+=$(LIBEDIT_INCLUDE)
163 json.o: _ASTCFLAGS+=$(JANSSON_INCLUDE)
164 bucket.o: _ASTCFLAGS+=$(URIPARSER_INCLUDE)
165 crypt.o: _ASTCFLAGS+=$(CRYPT_INCLUDE)
166 uuid.o: _ASTCFLAGS+=$(UUID_INCLUDE)
167
168 ifneq ($(findstring ENABLE_UPLOADS,$(MENUSELECT_CFLAGS)),)
169 http.o: _ASTCFLAGS+=$(GMIME_INCLUDE)
170 endif
171
172 stdtime/localtime.o: _ASTCFLAGS+=$(AST_NO_STRICT_OVERFLOW) -Wno-format-nonliteral
173
174 AST_EMBED_LDSCRIPTS:=$(sort $(EMBED_LDSCRIPTS))
175 AST_EMBED_LDFLAGS:=$(foreach dep,$(EMBED_LDFLAGS),$(value $(dep)))
176 AST_EMBED_LIBS:=$(foreach dep,$(EMBED_LIBS),$(value $(dep)))
177 OBJS:=$(sort $(OBJS))
178
179 ifneq ($(findstring $(OSARCH), mingw32 cygwin ),)
180 MAIN_TGT:=asterisk.dll
181 asterisk: cygload
182         mv cygload.exe asterisk.exe
183
184 cygload: asterisk.dll
185 else
186 MAIN_TGT:=asterisk
187 endif
188
189 ifneq ($(findstring ENABLE_UPLOADS,$(MENUSELECT_CFLAGS)),)
190 GMIMELDFLAGS+=$(GMIME_LIB)
191 endif
192
193 $(OBJS): _ASTCFLAGS+=-DAST_MODULE=\"core\" -DAST_IN_CORE
194
195 libasteriskssl.o: _ASTCFLAGS+=$(OPENSSL_INCLUDE)
196
197 ifeq ($(AST_ASTERISKSSL),yes)
198 # The ABI *version* of the asteriskssl library; don't change this unless there truly is a
199 # non-backwards-compatible ABI change in the library
200 ASTSSL_SO_VERSION=1
201
202 ASTSSL_LDLIBS=-L. -lasteriskssl
203
204 ifeq ($(findstring darwin,$(OSARCH)),) # not Darwin
205 ASTSSL_LIB:=libasteriskssl.so
206
207 $(ASTSSL_LIB).$(ASTSSL_SO_VERSION): _ASTLDFLAGS+=-Wl,-soname=$(ASTSSL_LIB)
208 $(ASTSSL_LIB).$(ASTSSL_SO_VERSION): _ASTCFLAGS+=-fPIC -DAST_MODULE=\"asteriskssl\" -DAST_NOT_MODULE
209 $(ASTSSL_LIB).$(ASTSSL_SO_VERSION): LIBS+=$(ASTSSL_LIBS)
210 ifeq ($(GNU_LD),1)
211     $(ASTSSL_LIB).$(ASTSSL_SO_VERSION): SO_SUPPRESS_SYMBOLS=-Wl,--version-script,libasteriskssl.exports,--warn-common
212 endif
213 $(ASTSSL_LIB).$(ASTSSL_SO_VERSION): SOLINK=$(DYLINK)
214
215 # These rules are duplicated from $(ASTTOPDIR)/Makefile.rules because the library name
216 # being built does not match the "%.so" pattern; there are also additional steps
217 # required to build a proper shared library (as opposed to the 'loadable module'
218 # type that are built by the standard rules)
219 $(ASTSSL_LIB).$(ASTSSL_SO_VERSION): libasteriskssl.o
220 ifeq ($(GNU_LD),1)
221         $(CMD_PREFIX) $(ASTTOPDIR)/build_tools/make_linker_version_script libasteriskssl "$(LINKER_SYMBOL_PREFIX)" "$(ASTTOPDIR)"
222 endif
223         $(ECHO_PREFIX) echo "   [LD] $^ -> $@"
224         $(CMD_PREFIX) $(CC) $(STATIC_BUILD) -o $@ $(CC_LDFLAGS_SO) $^ $(CC_LIBS)
225
226 $(ASTSSL_LIB): $(ASTSSL_LIB).$(ASTSSL_SO_VERSION)
227         $(ECHO_PREFIX) echo "   [LN] $< -> $@"
228 ifneq ($(LDCONFIG),)
229         $(CMD_PREFIX) $(LDCONFIG) $(LDCONFIG_FLAGS) . 2>/dev/null
230 else
231         $(CMD_PREFIX) $(LN) -sf $< $@
232 endif
233
234 else # Darwin
235 ASTSSL_LIB:=libasteriskssl.dylib
236
237 # -install_name allows library to be found if installed somewhere other than
238 # /lib or /usr/lib
239 $(ASTSSL_LIB): _ASTLDFLAGS+=-dynamiclib -install_name $(ASTLIBDIR)/$(ASTSSL_LIB)
240 $(ASTSSL_LIB): _ASTCFLAGS+=-fPIC -DAST_MODULE=\"asteriskssl\"
241 $(ASTSSL_LIB): LIBS+=$(ASTSSL_LIBS)
242 $(ASTSSL_LIB): SOLINK=$(DYLINK)
243
244 # Special rules for building a shared library (not a dynamically loadable module)
245 $(ASTSSL_LIB): libasteriskssl.o
246         $(ECHO_PREFIX) echo "   [LD] $^ -> $@"
247         $(CMD_PREFIX) $(CC) $(STATIC_BUILD) -o $@ $(CC_LDFLAGS_SO) $^ $(CC_LIBS)
248 endif
249
250 endif
251
252 libasteriskpj.o: _ASTCFLAGS+=$(PJPROJECT_INCLUDE)
253
254 ifeq ($(PJPROJECT_BUNDLED),yes)
255
256 ASTPJ_SO_VERSION=2
257 ASTPJ_LDLIBS=-L. -lasteriskpj
258
259 -include $(ASTTOPDIR)/$(PJPROJECT_DIR)/build.mak
260
261 PJPROJECT_LDLIBS := \
262 -Wl,--whole-archive \
263 $(PJSUA_LIB_LDLIB) \
264 $(PJSIP_UA_LDLIB) \
265 $(PJSIP_SIMPLE_LDLIB) \
266 $(PJSIP_LDLIB) \
267 $(PJNATH_LDLIB) \
268 $(PJMEDIA_CODEC_LDLIB) \
269 $(PJMEDIA_VIDEODEV_LDLIB) \
270 $(PJMEDIA_AUDIODEV_LDLIB) \
271 $(PJMEDIA_LDLIB) \
272 $(PJLIB_UTIL_LDLIB) \
273 $(PJLIB_LDLIB) \
274 -Wl,--no-whole-archive \
275 $(APP_THIRD_PARTY_LIBS) \
276 $(APP_THIRD_PARTY_EXT)
277
278 ifeq ($(findstring darwin,$(OSARCH)),) # not Darwin
279 ASTPJ_LIB:=libasteriskpj.so
280
281 libasteriskpj.exports: $(ASTTOPDIR)/$(PJPROJECT_DIR)/pjproject.symbols
282         $(ECHO_PREFIX) echo "   [GENERATE] libasteriskpj.exports"
283 ifeq ($(GNU_LD),1)
284         $(CMD_PREFIX) echo -e "{\n\tglobal:" > libasteriskpj.exports
285         $(CMD_PREFIX) sed -r -e "s/.*/\t\t$(LINKER_SYMBOL_PREFIX)&;/" $(ASTTOPDIR)/$(PJPROJECT_DIR)/pjproject.symbols >> libasteriskpj.exports
286         $(CMD_PREFIX) echo -e "\t\t$(LINKER_SYMBOL_PREFIX)ast_pj_init;\n" >> libasteriskpj.exports
287         $(CMD_PREFIX) echo -e "\tlocal:\n\t\t*;\n};" >> libasteriskpj.exports
288 endif
289
290 $(ASTPJ_LIB).$(ASTPJ_SO_VERSION): _ASTLDFLAGS+=-Wl,-soname=$(ASTPJ_LIB) $(PJ_LDFLAGS)
291 $(ASTPJ_LIB).$(ASTPJ_SO_VERSION): _ASTCFLAGS+=-fPIC -DAST_MODULE=\"asteriskpj\" $(PJ_CFLAGS)
292 $(ASTPJ_LIB).$(ASTPJ_SO_VERSION): LIBS+=$(PJPROJECT_LDLIBS) -lssl -lcrypto -luuid -lm -lrt -lpthread
293 ifeq ($(GNU_LD),1)
294     $(ASTPJ_LIB).$(ASTPJ_SO_VERSION): SO_SUPPRESS_SYMBOLS=-Wl,--version-script,libasteriskpj.exports,--warn-common
295 endif
296 $(ASTPJ_LIB).$(ASTPJ_SO_VERSION): SOLINK=$(DYLINK)
297
298 # These rules are duplicated from $(ASTTOPDIR)/Makefile.rules because the library name
299 # being built does not match the "%.so" pattern; there are also additional steps
300 # required to build a proper shared library (as opposed to the 'loadable module'
301 # type that are built by the standard rules)
302 $(ASTPJ_LIB).$(ASTPJ_SO_VERSION): libasteriskpj.o libasteriskpj.exports
303         $(ECHO_PREFIX) echo "   [LD] $< -> $@"
304         $(CMD_PREFIX) $(CC) $(STATIC_BUILD) -o $@ $(CC_LDFLAGS_SO) $< $(CC_LIBS)
305
306 $(ASTPJ_LIB): $(ASTPJ_LIB).$(ASTPJ_SO_VERSION)
307         $(ECHO_PREFIX) echo "   [LN] $< -> $@"
308 ifneq ($(LDCONFIG),)
309         $(CMD_PREFIX) $(LDCONFIG) $(LDCONFIG_FLAGS) . 2>/dev/null
310 else
311         $(CMD_PREFIX) $(LN) -sf $< $@
312 endif
313
314 else # Darwin
315 ASTPJ_LIB:=libasteriskpj.dylib
316
317 # -install_name allows library to be found if installed somewhere other than
318 # /lib or /usr/lib
319 $(ASTPJ_LIB): _ASTLDFLAGS+=-dynamiclib -install_name $(ASTLIBDIR)/$(ASTPJ_LIB) $(PJ_LDFLAGS)
320 $(ASTPJ_LIB): _ASTCFLAGS+=-fPIC -DAST_MODULE=\"asteriskpj\" $(PJ_CFLAGS)
321 $(ASTPJ_LIB): LIBS+=$(PJPROJECT_LIBS)  -lssl -lcrypto -luuid -lm -lrt -lpthread
322 $(ASTPJ_LIB): SOLINK=$(DYLINK)
323
324 # Special rules for building a shared library (not a dynamically loadable module)
325 $(ASTPJ_LIB): libasteriskpj.o
326         $(ECHO_PREFIX) echo "   [LD] $^ -> $@"
327         $(CMD_PREFIX) $(CC) $(STATIC_BUILD) -o $@ $(CC_LDFLAGS_SO) $^ $(CC_LIBS)
328 endif
329
330 endif
331
332 tcptls.o: _ASTCFLAGS+=$(OPENSSL_INCLUDE)
333
334 $(MAIN_TGT): $(OBJS) $(ASTSSL_LIB) $(ASTPJ_LIB) $(LIBEDIT_OBJ) $(AST_EMBED_LDSCRIPTS)
335         @$(CC) -c -o buildinfo.o $(_ASTCFLAGS) buildinfo.c $(ASTCFLAGS)
336         $(ECHO_PREFIX) echo "   [LD] $(OBJS) $(LIBEDIT_OBJ) $(AST_EMBED_LDSCRIPTS) -> $@"
337         $(CMD_PREFIX) $(CXX) $(STATIC_BUILD) -o $@ $(ASTLINK) $(AST_EMBED_LDFLAGS) $(_ASTLDFLAGS) $(ASTLDFLAGS) $(OBJS) $(ASTSSL_LDLIBS) $(ASTPJ_LDLIBS) $(LIBEDIT_OBJ) $(AST_EMBED_LDSCRIPTS) buildinfo.o $(AST_LIBS) $(AST_EMBED_LIBS) $(GMIMELDFLAGS) $(LIBEDIT_LIB)
338
339 ifeq ($(GNU_LD),1)
340 $(MAIN_TGT): asterisk.exports
341 asterisk.exports: asterisk.exports.in
342         $(CMD_PREFIX) $(ASTTOPDIR)/build_tools/make_linker_version_script asterisk $(LINKER_SYMBOL_PREFIX)
343 endif
344
345 bininstall:
346         $(INSTALL) -m 755 $(MAIN_TGT) "$(DESTDIR)$(ASTSBINDIR)/"
347 ifeq ($(AST_ASTERISKSSL),yes)
348 ifeq ($(findstring darwin,$(OSARCH)),) # not Darwin
349         $(INSTALL) -m 755 $(ASTSSL_LIB).$(ASTSSL_SO_VERSION) "$(DESTDIR)$(ASTLIBDIR)/"
350         $(LN) -sf $(ASTSSL_LIB).$(ASTSSL_SO_VERSION) "$(DESTDIR)$(ASTLIBDIR)/$(ASTSSL_LIB)"
351 else # Darwin
352         $(INSTALL) -m 755 $(ASTSSL_LIB) "$(DESTDIR)$(ASTLIBDIR)/"
353 endif
354 endif
355 ifeq ($(PJPROJECT_BUNDLED),yes)
356 ifeq ($(findstring darwin,$(OSARCH)),) # not Darwin
357         $(INSTALL) -m 755 $(ASTPJ_LIB).$(ASTPJ_SO_VERSION) "$(DESTDIR)$(ASTLIBDIR)/"
358         $(LN) -sf $(ASTPJ_LIB).$(ASTPJ_SO_VERSION) "$(DESTDIR)$(ASTLIBDIR)/$(ASTPJ_LIB)"
359 else # Darwin
360         $(INSTALL) -m 755 $(ASTPJ_LIB) "$(DESTDIR)$(ASTLIBDIR)/"
361 endif
362 endif
363 ifneq ($(LDCONFIG),)
364         $(LDCONFIG) $(LDCONFIG_FLAGS) "$(DESTDIR)$(ASTLIBDIR)/"
365 endif
366         $(LN) -sf asterisk "$(DESTDIR)$(ASTSBINDIR)/rasterisk"
367
368 binuninstall:
369         rm -f "$(DESTDIR)$(ASTSBINDIR)/$(MAIN_TGT)"
370         rm -f "$(DESTDIR)$(ASTSBINDIR)/rasterisk"
371 ifneq ($(ASTSSL_LIB).$(ASTSSL_SO_VERSION),.)
372         rm -f "$(DESTDIR)$(ASTLIBDIR)/$(ASTSSL_LIB).$(ASTSSL_SO_VERSION)"
373 endif
374 ifneq ($(ASTPJ_LIB).$(ASTPJ_SO_VERSION),.)
375         rm -f "$(DESTDIR)$(ASTLIBDIR)/$(ASTPJ_LIB).$(ASTPJ_SO_VERSION)"
376 endif
377 ifneq ($(LDCONFIG),)
378         $(LDCONFIG) $(LDCONFIG_FLAGS) "$(DESTDIR)$(ASTLIBDIR)/"
379 endif
380
381 clean::
382         rm -f asterisk libasteriskssl.o
383 ifeq ($(AST_ASTERISKSSL),yes)
384         rm -f $(ASTSSL_LIB) $(ASTSSL_LIB).*
385 endif
386         rm -f libasteriskpj.o
387         rm -f libasteriskpj.so* libasteriskpj.dynlib
388         rm -f .libasteriskpj*
389
390         rm -f asterisk.exports libasteriskssl.exports libasteriskpj.exports
391         @if [ -f editline/Makefile ]; then $(MAKE) -C editline distclean ; fi
392         @$(MAKE) -C stdtime clean
393         rm -f libresample/src/*.o
394         rm -f *.tmp