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