Merge "pjproject_bundled: Add MALLOC_DEBUG capability"
[asterisk/asterisk.git] / third-party / pjproject / Makefile
1 .PHONY: _all all _install install clean distclean echo_cflags configure
2
3 include ../versions.mak
4
5 SPECIAL_TARGETS :=
6
7 ifneq ($(findstring configure,$(MAKECMDGOALS))$(findstring echo_cflags,$(MAKECMDGOALS)),)
8 # Run from $(ASTTOPDIR)/configure
9     SPECIAL_TARGETS += configure
10     include ../Makefile.rules
11     include Makefile.rules
12 endif
13
14 ifeq ($(findstring echo_cflags,$(MAKECMDGOALS)),echo_cflags)
15     -include build.mak
16     ECHO_PREFIX=@\#
17 endif
18
19 ifeq ($(findstring clean,$(MAKECMDGOALS)),clean)
20 # clean or distclean
21     SPECIAL_TARGETS += clean
22     include ../Makefile.rules
23     include Makefile.rules
24 endif
25
26 ifeq ($(SPECIAL_TARGETS),)
27 # Run locally or from $(ASTTOPDIR)/Makefile.  All include files should be present
28     ifeq ($(wildcard ../../makeopts),)
29         $(error ASTTOPDIR/configure hasn't been run)
30     endif
31     include ../../makeopts
32
33     ifeq ($(PJPROJECT_BUNDLED),yes)
34         -include ../../menuselect.makeopts
35         include ../Makefile.rules
36
37         all: _all
38         install: _install
39
40         include ../../Makefile.rules
41         include Makefile.rules
42         include source/user.mak
43         include source/build.mak
44         CF := $(filter-out -W%,$(CC_CFLAGS))
45         CF := $(filter-out -I%,$(CF))
46         ifeq ($(findstring TEST_FRAMEWORK,$(MENUSELECT_CFLAGS)),TEST_FRAMEWORK)
47             apps := source/pjsip-apps/bin/pjsua-$(TARGET_NAME) source/pjsip-apps/bin/pjsystest-$(TARGET_NAME)
48             TARGETS += $(apps)
49             ifneq ($(PYTHONDEV_LIB),)
50                 TARGETS += source/pjsip-apps/src/python/_pjsua.so
51             endif
52         endif
53         ifeq ($(findstring MALLOC_DEBUG,$(MENUSELECT_CFLAGS)),MALLOC_DEBUG)
54             CF += -DMALLOC_DEBUG
55             MALLOC_DEBUG = yes
56             $(apps): export LDFLAGS += -L$(PJDIR)/pjsip-apps/lib -Wl,-whole-archive -lasterisk_malloc_debug -Wl,-no-whole-archive
57             $(apps): source/pjsip-apps/lib/libasterisk_malloc_debug.a
58             source/pjsip-apps/src/python/_pjsua.so: LDFLAGS += -L$(PJDIR)/pjsip-apps/lib -Wl,-whole-archive -lasterisk_malloc_debug -Wl,-no-whole-archive
59             source/pjsip-apps/src/python/_pjsua.so: source/pjsip-apps/lib/libasterisk_malloc_debug.a
60         endif
61         TARGETS += pjproject.symbols
62         export CFLAGS += $(CF)
63         export LDFLAGS += $(CC_LDFLAGS)
64     else
65         all install:
66     endif
67 endif
68
69 ECHO_PREFIX := $(ECHO_PREFIX) echo '[pjproject] '
70
71 ifndef $(TMPDIR)
72     ifneq ($(wildcard /tmp),)
73         TMPDIR=/tmp
74     else
75         TMPDIR=.
76     endif
77 endif
78
79 $(TMPDIR)/pjproject-$(PJPROJECT_VERSION).tar.bz2 : ../versions.mak
80         $(ECHO_PREFIX) Downloading $@ with $(DOWNLOAD)
81         $(CMD_PREFIX) $(DOWNLOAD) $(PJPROJECT_URL)/$(@F) > $@
82
83 source/.unpacked: $(TMPDIR)/pjproject-$(PJPROJECT_VERSION).tar.bz2
84         $(ECHO_PREFIX) Unpacking $<
85         -@rm -rf source >/dev/null 2>&1
86         -@mkdir source >/dev/null 2>&1
87         $(CMD_PREFIX) tar --strip-components=1 -C source -xjf $<
88         $(ECHO_PREFIX) Applying patches
89         $(CMD_PREFIX) ./apply_patches $(QUIET_CONFIGURE) ./patches ./source
90         -@touch source/.unpacked
91
92 source/user.mak: source/.unpacked ./patches/user.mak
93         $(ECHO_PREFIX) Applying user.mak
94         $(CMD_PREFIX) cp -f ./patches/user.mak ./source/
95
96 source/pjlib/include/pj/%.h : ./patches/%.h
97         $(ECHO_PREFIX) Applying custom include file $<
98         $(CMD_PREFIX) cp -f $< ./source/pjlib/include/pj/
99
100 build.mak: source/.unpacked $(addprefix source/pjlib/include/pj/,$(notdir $(wildcard ./patches/*.h))) source/user.mak Makefile.rules
101         $(ECHO_PREFIX) Configuring with $(PJPROJECT_CONFIG_OPTS)
102         $(CMD_PREFIX) (cd source ; autoconf aconfigure.ac > aconfigure && ./aconfigure $(QUIET_CONFIGURE) $(PJPROJECT_CONFIG_OPTS))
103         @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
104
105 configure: build.mak
106
107 echo_cflags: build.mak
108         @echo $(PJ_CFLAGS)
109
110 .rebuild_needed: ../../menuselect.makeopts
111         $(ECHO_PREFIX) Rebuilding
112         $(CMD_PREFIX)$(MAKE) clean $(REALLY_QUIET)
113         @touch  .rebuild_needed
114
115 libpj%.a:  .rebuild_needed  build.mak
116         $(ECHO_PREFIX) Compiling lib $(@F)
117         $(CMD_PREFIX)$(MAKE) -C $(dir $(shell dirname $@))/build $(@F) $(REALLY_QUIET)
118
119 # We need to compile pjlib, then pjlib-util, then the rest
120 # so we separate them out and create the dependencies
121 PJLIB_LIB_FILES = $(foreach lib,$(PJ_LIB_FILES),$(if $(findstring libpj-,$(lib)),$(lib),))
122 PJLIB_UTIL_LIB_FILES = $(foreach lib,$(PJ_LIB_FILES),$(if $(findstring libpjlib-util,$(lib)),$(lib),))
123 LIB_FILES = $(filter-out $(PJLIB_LIB_FILES) $(PJLIB_UTIL_LIB_FILES),$(PJ_LIB_FILES))
124 ALL_LIB_FILES = $(PJLIB_LIB_FILES) $(PJLIB_UTIL_LIB_FILES) $(LIB_FILES)
125
126 $(PJLIB_UTIL_LIB_FILES): $(PJLIB_LIB_FILES)
127 $(LIB_FILES): $(PJLIB_UTIL_LIB_FILES)
128
129 pjproject.symbols: $(ALL_LIB_FILES)
130         $(ECHO_PREFIX) Generating symbols
131         $(CMD_PREFIX) nm -Pog $(ALL_LIB_FILES) | sed -n -r -e "s/.+: ([pP][jJ][^ ]+) .+/\1/gp" | sort -u > pjproject.symbols
132
133 source/pjsip-apps/src/asterisk_malloc_debug.c: patches/asterisk_malloc_debug.c
134         $(ECHO_PREFIX) Copying $< to $@
135         $(CMD_PREFIX) cp -f $< $@
136
137 source/pjsip-apps/lib/asterisk_malloc_debug.o: source/pjsip-apps/src/asterisk_malloc_debug.c  .rebuild_needed
138         $(ECHO_PREFIX) Compiling asterisk debug malloc stubs
139         $(CMD_PREFIX) $(CC) -fPIC  $(PJ_CFLAGS) -c $< -o $@
140
141 source/pjsip-apps/lib/libasterisk_malloc_debug.a: source/pjsip-apps/lib/asterisk_malloc_debug.o
142         $(ECHO_PREFIX) Creating archive $(@F)
143         $(CMD_PREFIX) ar qs $@ $< >/dev/null 2>&1
144
145 $(apps): APP = $(filter pj%,$(subst -, ,$(notdir $@)))
146 $(apps): pjproject.symbols
147         $(ECHO_PREFIX) Compiling $(APP)
148         $(CMD_PREFIX) +$(MAKE) -C source/pjsip-apps/build $(filter pj%,$(subst -, ,$(notdir $@))) $(REALLY_QUIET)
149
150 source/pjsip-apps/src/python/_pjsua.o: source/pjsip-apps/src/python/_pjsua.c $(apps)
151         $(ECHO_PREFIX) Compiling python bindings
152         $(CMD_PREFIX) $(CC) -o $@ -c $< $(PYTHONDEV_INCLUDE) $(CFLAGS) $(PJ_CFLAGS)
153
154 source/pjsip-apps/src/python/_pjsua.so: source/pjsip-apps/src/python/_pjsua.o
155         $(ECHO_PREFIX) Linking python bindings $(@F)
156         $(CMD_PREFIX) gcc -shared -pthread -o $@ $< $(LDFLAGS) $(PJ_LDFLAGS) $(APP_LDLIBS) $(PYTHONDEV_LIB) $(REALLY_QUIET)
157
158 _all: $(TARGETS)
159
160 _install: _all
161         @if [ ! -d "$(DESTDIR)$(ASTDATADIR)/third-party/pjproject" ]; then \
162                 $(INSTALL) -d "$(DESTDIR)$(ASTDATADIR)/third-party/pjproject"; \
163         fi;
164 ifneq ($(findstring source/pjsip-apps/bin/pjsua-$(TARGET_NAME),$(TARGETS)),)
165         $(ECHO_PREFIX) Installing apps
166         $(CMD_PREFIX) $(INSTALL) -m 755 source/pjsip-apps/bin/pjsua-$(TARGET_NAME) "$(DESTDIR)$(ASTDATADIR)/third-party/pjproject/pjsua"
167         $(CMD_PREFIX) $(INSTALL) -m 755 source/pjsip-apps/bin/pjsystest-$(TARGET_NAME) "$(DESTDIR)$(ASTDATADIR)/third-party/pjproject/pjsystest"
168 endif
169 ifneq ($(findstring _pjsua.so,$(TARGETS)),)
170         $(ECHO_PREFIX) Installing python bindings
171         $(CMD_PREFIX) $(INSTALL) -m 755 source/pjsip-apps/src/python/_pjsua.so "$(DESTDIR)$(ASTDATADIR)/third-party/pjproject/"
172         $(CMD_PREFIX) $(INSTALL) -m 644 source/pjsip-apps/src/python/pjsua.py "$(DESTDIR)$(ASTDATADIR)/third-party/pjproject/"
173 endif
174
175 uninstall:
176         $(ECHO_PREFIX) Uninstalling apps and python bindings
177         $(CMD_PREFIX) rm -rf "$(DESTDIR)$(ASTDATADIR)/third-party/pjproject"
178
179 clean:
180         $(ECHO_PREFIX) Cleaning
181         +-$(CMD_PREFIX) test -d source && ($(SUBMAKE) -C source clean || : ;\
182                 rm -rf source/pjsip-apps/bin/* || : ;\
183                 find source -name *.a -delete ;\
184                 find source -name *.o -delete ;\
185                 find source -name *.so -delete ; ) || :
186         -$(CMD_PREFIX) rm -rf pjproject.symbols
187
188 distclean:
189         $(ECHO_PREFIX) Distcleaning
190         -$(CMD_PREFIX) rm -rf source pjproject.symbols pjproject-*.tar.bz2 build.mak .rebuild_needed
191