pjproject_bundled: Retry download if previously saved tarball is bad
[asterisk/asterisk.git] / third-party / pjproject / Makefile
1 .PHONY: _all all _install install clean distclean echo_cflags configure
2
3 include ../versions.mak
4 export PJDIR := $(shell pwd -P)/source
5
6 SPECIAL_TARGETS :=
7
8 ifneq ($(findstring configure,$(MAKECMDGOALS))$(findstring echo_cflags,$(MAKECMDGOALS)),)
9 # Run from $(ASTTOPDIR)/configure
10     SPECIAL_TARGETS += configure
11 endif
12
13 ifeq ($(findstring echo_cflags,$(MAKECMDGOALS)),echo_cflags)
14     -include build.mak
15     ECHO_PREFIX=@\#
16 endif
17
18 ifeq ($(findstring clean,$(MAKECMDGOALS)),clean)
19 # clean or distclean
20     SPECIAL_TARGETS += clean
21 endif
22
23 ifeq ($(findstring uninstall,$(MAKECMDGOALS)),uninstall)
24     SPECIAL_TARGETS += uninstall
25 endif
26
27
28 ifneq ($(wildcard ../../makeopts),)
29     include ../../makeopts
30 endif
31
32 TARGETS = build.mak
33
34 ifeq ($(SPECIAL_TARGETS),)
35 # Run locally or from $(ASTTOPDIR)/Makefile.  All include files should be present
36     ifeq ($(wildcard ../../makeopts),)
37         $(error ASTTOPDIR/configure hasn't been run)
38     endif
39
40     ifeq ($(PJPROJECT_BUNDLED),yes)
41         ifneq ($(wildcard ../../menuselect.makeopts),)
42             include ../../menuselect.makeopts
43         else
44             $(warning ASTTOPDIR/menuselect hasn't been run yet.  Can't find debug options.)
45         endif
46
47         all: _all
48         install: _install
49
50         include source/user.mak
51         include source/version.mak
52         include source/build.mak
53         CF := $(filter-out -W%,$(CC_CFLAGS))
54         CF := $(filter-out -I%,$(CF))
55         ifeq ($(findstring TEST_FRAMEWORK,$(MENUSELECT_CFLAGS)),TEST_FRAMEWORK)
56             apps := source/pjsip-apps/bin/pjsua-$(TARGET_NAME) source/pjsip-apps/bin/pjsystest-$(TARGET_NAME)
57             TARGETS += $(apps)
58             ifneq ($(PYTHONDEV_LIB),)
59                 TARGETS += source/pjsip-apps/src/python/_pjsua.so
60             endif
61         endif
62         ifeq ($(findstring MALLOC_DEBUG,$(MENUSELECT_CFLAGS)),MALLOC_DEBUG)
63             CF += -DMALLOC_DEBUG
64             MALLOC_DEBUG = yes
65             $(apps): export LDFLAGS += -L$(PJDIR)/pjsip-apps/lib -Wl,-whole-archive -lasterisk_malloc_debug -Wl,-no-whole-archive
66             $(apps): source/pjsip-apps/lib/libasterisk_malloc_debug.a
67             source/pjsip-apps/src/python/_pjsua.so: LDFLAGS += -L$(PJDIR)/pjsip-apps/lib -Wl,-whole-archive -lasterisk_malloc_debug -Wl,-no-whole-archive
68             source/pjsip-apps/src/python/_pjsua.so: source/pjsip-apps/lib/libasterisk_malloc_debug.a
69         endif
70         ifeq ($(findstring DONT_OPTIMIZE,$(MENUSELECT_CFLAGS)),)
71             CF += -O3
72         endif
73         export CFLAGS += $(CF) -g3
74         export LDFLAGS += $(CC_LDFLAGS)
75         TARGETS += pjproject.symbols
76     else
77         all install:
78     endif
79 endif
80
81 include ../../Makefile.rules
82 include ../Makefile.rules
83 include Makefile.rules
84
85 ECHO_PREFIX := $(ECHO_PREFIX) echo '[pjproject] '
86 SHELL_ECHO_PREFIX := echo '[pjproject] '
87
88 _all: $(TARGETS)
89
90 define verify_tarball
91         ($(SHELL_ECHO_PREFIX) Verifying $(TARBALL) &&\
92         tarball_sum=$$($(CAT) $(TARBALL) | $(MD5) | $(SED) -n -r -e "s/^([^ ]+)\s+.*/\1/gp") ;\
93         required_sum=$$($(SED) -n -r -e "s/^([^ ]+)\s+$(TARBALL_FILE)/\1/gp" $(PJMD5SUM)) ;\
94         if [ "$$tarball_sum" != "$$required_sum" ] ; then $(SHELL_ECHO_PREFIX) Verify failed ; exit 1 ;\
95         else $(SHELL_ECHO_PREFIX) Verify successful ; exit 0 ; fi; )
96 endef
97
98 define download_from_pjproject
99         ($(SHELL_ECHO_PREFIX) Downloading $(TARBALL_URL) to $(TARBALL) ;\
100         $(DOWNLOAD_TO_STDOUT) $(call DOWNLOAD_TIMEOUT,5,10) $(TARBALL_URL) > $(TARBALL) &&\
101         $(SHELL_ECHO_PREFIX) Downloading $(PJPROJECT_URL)/MD5SUM to $(PJMD5SUM) &&\
102         $(DOWNLOAD_TO_STDOUT) $(call DOWNLOAD_TIMEOUT,5,10) $(PJPROJECT_URL)/MD5SUM.TXT > $(PJMD5SUM) &&\
103         $(verify_tarball))
104 endef
105
106 .DELETE_ON_ERROR:
107
108 DOWNLOAD_DIR := $(or $(EXTERNALS_CACHE_DIR),$(TMPDIR),$(wildcard /tmp),.)
109 TARBALL_FILE = pjproject-$(PJPROJECT_VERSION).tar.bz2
110 TARBALL = $(DOWNLOAD_DIR)/$(TARBALL_FILE)
111 TARBALL_URL = $(PJPROJECT_URL)/$(TARBALL_FILE)
112 PJMD5SUM = $(patsubst %.tar.bz2,%.md5,$(TARBALL))
113
114 $(TARBALL): ../versions.mak
115         $(CMD_PREFIX) $(download_from_pjproject) || (rm -rf $@ ;\
116         $(SHELL_ECHO_PREFIX) Retrying download ; $(download_from_pjproject))
117
118 source/.unpacked: $(DOWNLOAD_DIR)/pjproject-$(PJPROJECT_VERSION).tar.bz2
119         ($(verify_tarball)) || (rm -rf $@ ;\
120         $(SHELL_ECHO_PREFIX) Retrying download ; $(download_from_pjproject))
121         $(ECHO_PREFIX) Unpacking $<
122         -@rm -rf source pjproject-* >/dev/null 2>&1
123         $(CMD_PREFIX) $(TAR) -xjf $<
124         @mv pjproject-$(PJPROJECT_VERSION) source
125         $(ECHO_PREFIX) Applying patches
126         $(CMD_PREFIX) ./apply_patches $(QUIET_CONFIGURE) patches source
127         -@touch source/.unpacked
128
129 source/version.mak: source/.unpacked
130
131 source/user.mak: source/.unpacked patches/user.mak
132         $(ECHO_PREFIX) Applying user.mak
133         $(CMD_PREFIX) cp -f patches/user.mak source/
134
135 source/pjlib/include/pj/%.h: patches/%.h
136         $(ECHO_PREFIX) Applying custom include file $<
137         $(CMD_PREFIX) cp -f $< source/pjlib/include/pj/
138
139 .rebuild_needed: $(wildcard ../../makeopts) $(wildcard ../../menuselect.makeopts)
140         $(ECHO_PREFIX) Rebuilding
141         $(CMD_PREFIX) $(MAKE) clean $(REALLY_QUIET)
142         @touch  .rebuild_needed
143
144 source/build.mak: Makefile.rules source/version.mak source/user.mak $(addprefix source/pjlib/include/pj/,$(notdir $(wildcard patches/*.h))) .rebuild_needed
145         $(ECHO_PREFIX) Configuring with $(PJPROJECT_CONFIG_OPTS)
146         $(CMD_PREFIX) (cd source ; ./aconfigure $(QUIET_CONFIGURE) $(PJPROJECT_CONFIG_OPTS))
147
148 build.mak: source/build.mak
149         $(CMD_PREFIX) $(SED) -r -e "/prefix|export PJ_SHARED_LIBRARIES|MACHINE_NAME|OS_NAME|HOST_NAME|CC_NAME|CROSS_COMPILE|LINUX_POLL/d" source/build.mak > build.mak
150
151 configure: source/build.mak
152
153 echo_cflags: source/build.mak
154         @echo $(PJ_CFLAGS)
155
156 libpj%.a: source/build.mak
157         $(ECHO_PREFIX) Compiling lib $(@F)
158         $(CMD_PREFIX) $(MAKE) -C $(dir $(shell dirname $@))/build $(@F) $(REALLY_QUIET)
159         -@rm -rf .rebuild_needed
160
161 # pjsua needs resample and g711 to successfully run the testsuite
162 libresample%.a: .rebuild_needed source/build.mak
163         $(ECHO_PREFIX) Compiling lib $(@F)
164         $(CMD_PREFIX) $(MAKE) -C $(dir $(shell dirname $@))/build/resample all $(REALLY_QUIET)
165         -@rm -rf .rebuild_needed
166
167 # We need to compile pjlib, then pjlib-util, then the rest
168 # so we separate them out and create the dependencies
169 PJLIB_LIB_FILES = $(foreach lib,$(PJ_LIB_FILES),$(if $(findstring libpj-,$(lib)),$(lib),))
170 PJLIB_UTIL_LIB_FILES = $(foreach lib,$(PJ_LIB_FILES),$(if $(findstring libpjlib-util,$(lib)),$(lib),))
171 PJSIP_LIB_FILES = $(filter-out $(PJLIB_LIB_FILES) $(PJLIB_UTIL_LIB_FILES) $(APP_THIRD_PARTY_LIB_FILES),$(PJ_LIB_FILES))
172 ALL_LIB_FILES = $(PJLIB_LIB_FILES) $(PJLIB_UTIL_LIB_FILES) $(PJSIP_LIB_FILES)
173
174 $(PJLIB_UTIL_LIB_FILES): $(PJLIB_LIB_FILES)
175 $(PJSIP_LIB_FILES): $(PJLIB_UTIL_LIB_FILES)
176
177 pjproject.symbols: $(ALL_LIB_FILES)
178         $(ECHO_PREFIX) Generating symbols
179         $(CMD_PREFIX) $(NM) -Pog $(ALL_LIB_FILES) | $(SED) -n -r -e "s/.+: ([pP][jJ][^ ]+) .+/\1/gp" | sort -u > pjproject.symbols
180
181 source/pjsip-apps/src/asterisk_malloc_debug.c: patches/asterisk_malloc_debug.c
182         $(ECHO_PREFIX) Copying $< to $@
183         $(CMD_PREFIX) cp -f $< $@
184
185 source/pjsip-apps/lib/asterisk_malloc_debug.o: source/pjsip-apps/src/asterisk_malloc_debug.c .rebuild_needed
186         $(ECHO_PREFIX) Compiling asterisk debug malloc stubs
187         $(CMD_PREFIX) $(CC) -fPIC  $(PJ_CFLAGS) -c $< -o $@
188
189 source/pjsip-apps/lib/libasterisk_malloc_debug.a: source/pjsip-apps/lib/asterisk_malloc_debug.o
190         $(ECHO_PREFIX) Creating archive $(@F)
191         $(CMD_PREFIX) ar qs $@ $< >/dev/null 2>&1
192
193 $(apps): APP = $(filter pj%,$(subst -, ,$(notdir $@)))
194 $(apps): pjproject.symbols $(APP_THIRD_PARTY_LIB_FILES)
195         $(ECHO_PREFIX) Compiling $(APP)
196         $(CMD_PREFIX) +$(MAKE) -C source/pjsip-apps/build $(filter pj%,$(subst -, ,$(notdir $@))) $(REALLY_QUIET)
197
198 source/pjsip-apps/src/python/_pjsua.o: source/pjsip-apps/src/python/_pjsua.c $(apps)
199         $(ECHO_PREFIX) Compiling python bindings
200         $(CMD_PREFIX) $(CC) -o $@ -c $< $(PYTHONDEV_INCLUDE) $(CFLAGS) $(PJ_CFLAGS)
201
202 source/pjsip-apps/src/python/_pjsua.so: source/pjsip-apps/src/python/_pjsua.o
203         $(ECHO_PREFIX) Linking python bindings $(@F)
204         $(CMD_PREFIX) gcc -shared -pthread -o $@ $< $(LDFLAGS) $(PJ_LDFLAGS) $(APP_LDLIBS) $(PYTHONDEV_LIB) $(REALLY_QUIET)
205
206 _install: _all
207         @if [ ! -d "$(DESTDIR)$(ASTDATADIR)/third-party/pjproject" ]; then \
208                 $(INSTALL) -d "$(DESTDIR)$(ASTDATADIR)/third-party/pjproject"; \
209         fi;
210 ifneq ($(findstring source/pjsip-apps/bin/pjsua-$(TARGET_NAME),$(TARGETS)),)
211         $(ECHO_PREFIX) Installing apps
212         $(CMD_PREFIX) $(INSTALL) -m 755 source/pjsip-apps/bin/pjsua-$(TARGET_NAME) "$(DESTDIR)$(ASTDATADIR)/third-party/pjproject/pjsua"
213         $(CMD_PREFIX) $(INSTALL) -m 755 source/pjsip-apps/bin/pjsystest-$(TARGET_NAME) "$(DESTDIR)$(ASTDATADIR)/third-party/pjproject/pjsystest"
214 endif
215 ifneq ($(findstring _pjsua.so,$(TARGETS)),)
216         $(ECHO_PREFIX) Installing python bindings
217         $(CMD_PREFIX) $(INSTALL) -m 755 source/pjsip-apps/src/python/_pjsua.so "$(DESTDIR)$(ASTDATADIR)/third-party/pjproject/"
218         $(CMD_PREFIX) $(INSTALL) -m 644 source/pjsip-apps/src/python/pjsua.py "$(DESTDIR)$(ASTDATADIR)/third-party/pjproject/"
219 endif
220
221 uninstall:
222         $(ECHO_PREFIX) Uninstalling apps and python bindings
223         $(CMD_PREFIX) rm -rf "$(DESTDIR)$(ASTDATADIR)/third-party/pjproject"
224
225 clean:
226         $(ECHO_PREFIX) Cleaning
227         +-$(CMD_PREFIX) test -d source && ($(SUBMAKE) -C source clean || : ;\
228                 rm -rf source/pjsip-apps/bin/* || : ;\
229                 find source -name *.a | xargs rm -rf  ;\
230                 find source -name *.o | xargs rm -rf  ;\
231                 find source -name *.so  | xargs rm -rf ; ) || :
232         -$(CMD_PREFIX) rm -rf pjproject.symbols
233
234 distclean:
235         $(ECHO_PREFIX) Distcleaning
236         -$(CMD_PREFIX) rm -rf source pjproject.symbols pjproject-*.tar.bz2 build.mak .rebuild_needed