From 7dbe24e4e5fae61b23e677047b28e82ac49c3caf Mon Sep 17 00:00:00 2001
From: Guillem Jover <gjover@sipwise.com>
Date: Mon, 27 Jan 2025 20:23:43 +0100
Subject: [PATCH] MT#61993 build: Rework to use a config.mk generated during
 the build

These are potentially computed from inside each subdir, and in addition
due to what appears to be a regression in GNU make 4.4, where it is
reevaluating variables that contain $(shell) functions, many times (in
the order of thousands, this was slowing down the build, were on the
Debian amd64 build daemons it went from 5m with GNU make 4.3 to 2h40m
with GNU make 4.4. Although the bulk of the slow down has been fixed
with previous commits, the remaining optimizations are only to avoid
this potentially happening again in the future, and to reduce useless
duplicate work.

Instead of trying to cache the values from within make itself, where
programming this there is extremely painful, and does not seem to be
able to greatly reduce the number of calls, because the build system
is going to be called multiple times for different targets. Simply
externalize the generation into several shell scripts, that we call
to generate a make fragment that then we include from the various
Makefiles.

For a Debian build with GNU make 4.3, this reduces the amount of total
pkg-config calls from around ~1600 to 128, for dpkg-buildflags from
~1100 down to 6, and for dpkg-parsechangelog from ~56 to 17, but the
slow down is not as significant there anyway.

For a Debian build with GNU make 4.4, this reduces the amount of total
pkg-config calls from around ~2600 to 128, for dpkg-buildflags from
~2800 down to 6, and for dpkg-parsechangelog from ~350 to 21.

For a Debian build with GNU make 4.4, this reduces the build time
on this system from 2m10s to ~ 1m30s.

Change-Id: I427d0ea5106dc6ed1ff9e664ccdba2fa0725b7d0
---
 .gitignore                                |   1 +
 Makefile                                  |   7 +-
 daemon/Makefile                           | 109 ++++++++++----------
 debian/ngcp-rtpengine-kernel-dkms.install |   1 +
 kernel-module/.gitignore                  |   1 +
 kernel-module/Makefile                    |  38 +++----
 kernel-module/gen-rtpengine-kmod-flags    |  33 ++++++
 lib/codec-chain.Makefile                  |   7 --
 lib/deps.Makefile                         |  12 +++
 lib/g729.Makefile                         |  58 -----------
 lib/lib.Makefile                          |  62 ++---------
 lib/mqtt.Makefile                         |  10 --
 perf-tester/Makefile                      |  65 ++++++------
 recording-daemon/Makefile                 |  62 ++++++-----
 t/Makefile                                |  86 ++++++++--------
 utils/gen-bcg729-flags                    |  47 +++++++++
 utils/gen-codec-chain-flags               |   7 ++
 utils/gen-common-flags                    | 119 ++++++++++++++++++++++
 18 files changed, 416 insertions(+), 309 deletions(-)
 create mode 100755 kernel-module/gen-rtpengine-kmod-flags
 delete mode 100644 lib/codec-chain.Makefile
 create mode 100644 lib/deps.Makefile
 delete mode 100644 lib/g729.Makefile
 delete mode 100644 lib/mqtt.Makefile
 create mode 100755 utils/gen-bcg729-flags
 create mode 100755 utils/gen-codec-chain-flags
 create mode 100755 utils/gen-common-flags

diff --git a/.gitignore b/.gitignore
index 050e5cfbe..a256a9a19 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@
 *.strhash.c
 docs/_*
 bencode.c
+config.mk
diff --git a/Makefile b/Makefile
index aaef6083b..baf422e5c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,5 @@
 .DEFAULT_GOAL := all
 
-RTPENGINE_ROOT_DIR=.
 with_transcoding ?= yes
 
 ifeq ($(DO_ASAN_FLAGS),1)
@@ -20,6 +19,11 @@ export UBSAN_OPTIONS=print_stacktrace=1
 export G_SLICE=always-malloc
 endif
 
+export top_srcdir = $(CURDIR)
+
+# Initialize all flags, so that we only compute them once.
+include lib/deps.Makefile
+
 include lib/lib.Makefile
 
 .PHONY:	all distclean clean coverity
@@ -63,6 +67,7 @@ distclean clean:
 	$(MAKE) -C perf-tester clean
 	$(MAKE) -C kernel-module clean
 	$(MAKE) -C t clean
+	rm -f config.mk
 
 .DEFAULT:
 	$(MAKE) -C daemon $@
diff --git a/daemon/Makefile b/daemon/Makefile
index 51274b781..7a4293e6f 100644
--- a/daemon/Makefile
+++ b/daemon/Makefile
@@ -1,5 +1,9 @@
 TARGET=		rtpengine
 
+export top_srcdir = ..
+
+include ../lib/deps.Makefile
+
 with_iptables_option ?= yes
 with_transcoding ?= yes
 
@@ -11,36 +15,33 @@ CFLAGS:=	$(CFLAGS)
 endif
 CFLAGS+=	-pthread
 CFLAGS+=	-std=c11
-CFLAGS+=	$(shell pkg-config --cflags glib-2.0)
-CFLAGS+=	$(shell pkg-config --cflags gthread-2.0)
-CFLAGS+=	$(shell pkg-config --cflags zlib)
-CFLAGS+=	$(shell pkg-config --cflags openssl)
-CFLAGS+=	$(shell pkg-config --cflags libevent_pthreads)
-CFLAGS+=	-DPCRE2_CODE_UNIT_WIDTH=8
-CFLAGS+=	$(shell pkg-config --cflags libpcre2-8)
-CFLAGS+=	$(shell pkg-config xmlrpc_client --cflags 2> /dev/null || xmlrpc-c-config client --cflags)
-CFLAGS+=	$(shell pkg-config xmlrpc --cflags 2> /dev/null)
-CFLAGS+=	$(shell pkg-config xmlrpc_util --cflags 2> /dev/null)
-CFLAGS+=	$(shell pkg-config --cflags json-glib-1.0)
-CFLAGS+=	$(shell pkg-config --cflags libwebsockets)
-CFLAGS+=	$(shell pkg-config --cflags libnftnl)
-CFLAGS+=	$(shell pkg-config --cflags libmnl)
+CFLAGS+=	-I. -I../kernel-module/ -I../lib/ -I../include/
+CFLAGS+=	-D_GNU_SOURCE
+CFLAGS+=	$(CFLAGS_GLIB)
+CFLAGS+=	$(CFLAGS_GTHREAD)
+CFLAGS+=	$(CFLAGS_LIBZ)
+CFLAGS+=	$(CFLAGS_OPENSSL)
+CFLAGS+=	$(CFLAGS_LIBEVENT)
+CFLAGS+=	$(CFLAGS_LIBPCRE)
+CFLAGS+=	$(CFLAGS_XMLRPC)
+CFLAGS+=	$(CFLAGS_JSON_GLIB)
+CFLAGS+=	$(CFLAGS_LIBWEBSOCKETS)
+CFLAGS+=	$(CFLAGS_LIBNFTNL)
+CFLAGS+=	$(CFLAGS_LIBMNL)
 ifeq ($(with_iptables_option),yes)
-CFLAGS+=	$(shell pkg-config --cflags libiptc)
+CFLAGS+=	$(CFLAGS_LIBIPTC)
 CFLAGS+=	-DWITH_IPTABLES_OPTION
 endif
-CFLAGS+=	-I. -I../kernel-module/ -I../lib/ -I../include/
-CFLAGS+=	-D_GNU_SOURCE
 ifeq ($(with_transcoding),yes)
-CFLAGS+=	$(shell pkg-config --cflags libavcodec)
-CFLAGS+=	$(shell pkg-config --cflags libavformat)
-CFLAGS+=	$(shell pkg-config --cflags libavutil)
-CFLAGS+=	$(shell pkg-config --cflags libswresample)
-CFLAGS+=	$(shell pkg-config --cflags libavfilter)
-CFLAGS+=	$(shell pkg-config --cflags spandsp)
-CFLAGS+=	$(shell pkg-config --cflags opus)
+CFLAGS+=	$(CFLAGS_LIBAVCODEC)
+CFLAGS+=	$(CFLAGS_LIBAVFORMAT)
+CFLAGS+=	$(CFLAGS_LIBAVUTIL)
+CFLAGS+=	$(CFLAGS_LIBSWRESAMPLE)
+CFLAGS+=	$(CFLAGS_LIBAVFILTER)
+CFLAGS+=	$(CFLAGS_SPANDSP)
+CFLAGS+=	$(CFLAGS_OPUS)
 CFLAGS+=	-DWITH_TRANSCODING
-CFLAGS+=        $(shell mysql_config --cflags)
+CFLAGS+=	$(CFLAGS_MYSQL)
 else
 CFLAGS+=	-DWITHOUT_CODECLIB
 endif
@@ -53,42 +54,44 @@ endif
 #CFLAGS+=	-DSTRICT_SDES_KEY_LIFETIME
 
 LDLIBS:=	-lm -ldl
-LDLIBS+=	$(shell pkg-config --libs glib-2.0)
-LDLIBS+=	$(shell pkg-config --libs gthread-2.0)
-LDLIBS+=	$(shell pkg-config --libs zlib)
-LDLIBS+=	$(shell pkg-config --libs libpcre2-8)
-LDLIBS+=	$(shell pkg-config --libs libcrypto)
-LDLIBS+=	$(shell pkg-config --libs openssl)
-LDLIBS+=	$(shell pkg-config --libs libevent_pthreads)
-LDLIBS+=	-lpcap
-LDLIBS+=	$(shell pkg-config xmlrpc_client --libs 2> /dev/null || xmlrpc-c-config client --libs)
-LDLIBS+=	$(shell pkg-config xmlrpc --libs 2> /dev/null)
-LDLIBS+=	$(shell pkg-config xmlrpc_util --libs 2> /dev/null)
-LDLIBS+=	-lhiredis
-LDLIBS+=	$(shell pkg-config --libs json-glib-1.0)
-LDLIBS+=	$(shell pkg-config --libs libwebsockets)
-LDLIBS+=	$(shell pkg-config --libs libnftnl)
-LDLIBS+=	$(shell pkg-config --libs libmnl)
+LDLIBS+=	$(LDLIBS_GLIB)
+LDLIBS+=	$(LDLIBS_GTHREAD)
+LDLIBS+=	$(LDLIBS_LIBZ)
+LDLIBS+=	$(LDLIBS_LIBPCRE)
+LDLIBS+=	$(LDLIBS_LIBCRYPTO)
+LDLIBS+=	$(LDLIBS_OPENSSL)
+LDLIBS+=	$(LDLIBS_LIBEVENT)
+LDLIBS+=	$(LDLIBS_LIBPCAP)
+LDLIBS+=	$(LDLIBS_XMLRPC)
+LDLIBS+=	$(LDLIBS_LIBHIREDIS)
+LDLIBS+=	$(LDLIBS_JSON_GLIB)
+LDLIBS+=	$(LDLIBS_LIBWEBSOCKETS)
+LDLIBS+=	$(LDLIBS_LIBNFTNL)
+LDLIBS+=	$(LDLIBS_LIBMNL)
 ifeq ($(with_iptables_option),yes)
-LDLIBS+=	$(shell pkg-config --libs libiptc)
+LDLIBS+=	$(LDLIBS_LIBIPTC)
 endif
 ifeq ($(with_transcoding),yes)
-LDLIBS+=	$(shell pkg-config --libs libavcodec)
-LDLIBS+=	$(shell pkg-config --libs libavformat)
-LDLIBS+=	$(shell pkg-config --libs libavutil)
-LDLIBS+=	$(shell pkg-config --libs libswresample)
-LDLIBS+=	$(shell pkg-config --libs libavfilter)
-LDLIBS+=	$(shell pkg-config --libs spandsp)
-LDLIBS+=	$(shell pkg-config --libs opus)
-LDLIBS+=        $(shell mysql_config --libs)
+LDLIBS+=	$(LDLIBS_LIBAVCODEC)
+LDLIBS+=	$(LDLIBS_LIBAVFORMAT)
+LDLIBS+=	$(LDLIBS_LIBAVUTIL)
+LDLIBS+=	$(LDLIBS_LIBSWRESAMPLE)
+LDLIBS+=	$(LDLIBS_LIBAVFILTER)
+LDLIBS+=	$(LDLIBS_SPANDSP)
+LDLIBS+=	$(LDLIBS_OPUS)
+LDLIBS+=	$(LDLIBS_MYSQL)
 endif
 
 ifeq ($(with_transcoding),yes)
-include ../lib/g729.Makefile
-include ../lib/codec-chain.Makefile
+CFLAGS+=	$(CFLAGS_BCG729)
+LDLIBS+=	$(LDLIBS_BCG729)
+
+CFLAGS+=	$(CFLAGS_CODEC_CHAIN)
+LDLIBS+=	$(LDLIBS_CODEC_CHAIN)
 endif
 
-include ../lib/mqtt.Makefile
+CFLAGS+=	$(CFLAGS_MQTT)
+LDLIBS+=	$(LDLIBS_MQTT)
 
 SRCS=		main.c kernel.c helpers.c control_tcp.c call.c control_udp.c redis.c \
 		cookie_cache.c udp_listener.c control_ng_flags_parser.c control_ng.strhash.c sdp.strhash.c stun.c rtcp.c \
diff --git a/debian/ngcp-rtpengine-kernel-dkms.install b/debian/ngcp-rtpengine-kernel-dkms.install
index 1b046efbc..aa79ef266 100644
--- a/debian/ngcp-rtpengine-kernel-dkms.install
+++ b/debian/ngcp-rtpengine-kernel-dkms.install
@@ -2,3 +2,4 @@ kernel-module/*.c	usr/src/${env:DEB_SOURCE}-${env:DEB_VERSION_UPSTREAM}
 kernel-module/*.h	usr/src/${env:DEB_SOURCE}-${env:DEB_VERSION_UPSTREAM}
 kernel-module/*.inc	usr/src/${env:DEB_SOURCE}-${env:DEB_VERSION_UPSTREAM}
 kernel-module/Makefile	usr/src/${env:DEB_SOURCE}-${env:DEB_VERSION_UPSTREAM}
+kernel-module/gen-*	usr/src/${env:DEB_SOURCE}-${env:DEB_VERSION_UPSTREAM}
diff --git a/kernel-module/.gitignore b/kernel-module/.gitignore
index fc5e8de35..f3e89fc5e 100644
--- a/kernel-module/.gitignore
+++ b/kernel-module/.gitignore
@@ -4,5 +4,6 @@
 *.mod.c
 modules.order
 Module.symvers
+rtpengine.mk
 .*.cmd
 xt_RTPENGINE.mod
diff --git a/kernel-module/Makefile b/kernel-module/Makefile
index 70d0a5599..161c4513e 100644
--- a/kernel-module/Makefile
+++ b/kernel-module/Makefile
@@ -1,27 +1,11 @@
-PWD    := $(shell pwd)
 KSRC   ?= /lib/modules/$(shell uname -r)/build
 KBUILD := $(KSRC)
-M      ?= $(PWD)
-
-ifeq ($(RTPENGINE_VERSION),)
-  DPKG_PRSCHNGLG := $(shell which dpkg-parsechangelog 2>/dev/null)
-  DEB_CHANGELOG := $(shell test -f $(M)/../debian/changelog && echo $(M)/../debian/changelog || echo $(M)/debian/changelog)
-  ifneq ($(DPKG_PRSCHNGLG),)
-    DPKG_PRSCHNGLG := $(shell dpkg-parsechangelog -l$(DEB_CHANGELOG) | awk '/^Version: / {print $$2}')
-  endif
-  GIT_BR_COMMIT := git-$(shell cd $(M) && git rev-parse --abbrev-ref --symbolic-full-name HEAD 2> /dev/null)-$(shell cd $(M) && git rev-parse --short HEAD 2> /dev/null)
-
-  ifneq ($(DPKG_PRSCHNGLG),)
-    RTPENGINE_VERSION+=$(DPKG_PRSCHNGLG)
-  endif
-  ifneq ($(GIT_BR_COMMIT),git--)
-    RTPENGINE_VERSION+=$(GIT_BR_COMMIT)
-  endif
-
-  ifeq ($(RTPENGINE_VERSION),)
-    RTPENGINE_VERSION+=undefined
-  endif
-endif
+M      ?= $(CURDIR)
+
+export M
+
+include rtpengine-kmod.mk
+
 EXTRA_CFLAGS+=	-DRTPENGINE_VERSION="\"$(RTPENGINE_VERSION)\""
 
 obj-m        += xt_RTPENGINE.o
@@ -29,14 +13,18 @@ obj-m        += xt_RTPENGINE.o
 .PHONY:		modules clean patch install
 
 modules:
-		$(MAKE) -C $(KBUILD) M=$(PWD) O=$(KBUILD) modules
+		$(MAKE) -C $(KBUILD) M=$(CURDIR) O=$(KBUILD) modules
 
 clean:
-		$(MAKE) -C $(KBUILD) M=$(PWD) clean || true
+		$(MAKE) -C $(KBUILD) M=$(CURDIR) clean || true
+		rm -f rtpengine-kmod.mk
 
 patch:
-		../utils/patch-kernel magic "$(PWD)" "$(KERNEL)" "$(RTPENGINE_VERSION)"
+		../utils/patch-kernel magic "$(CURDIR)" "$(KERNEL)" "$(RTPENGINE_VERSION)"
 
 install:
 	install -D xt_RTPENGINE.ko $(DESTDIR)/lib/modules/$(shell uname -r)/updates/xt_RTPENGINE.ko
 	depmod -a
+
+rtpengine-kmod.mk:
+	./gen-rtpengine-kmod-flags >$@
diff --git a/kernel-module/gen-rtpengine-kmod-flags b/kernel-module/gen-rtpengine-kmod-flags
new file mode 100755
index 000000000..dda476e1b
--- /dev/null
+++ b/kernel-module/gen-rtpengine-kmod-flags
@@ -0,0 +1,33 @@
+#!/bin/bash
+
+if [ -z "${M}" ]; then
+  M=$(pwd)
+fi
+
+if [ -z "${RTPENGINE_VERSION}" ]; then
+  have_dpkg_parsechangelog=no
+  if command -v dpkg-parsechangelog >/dev/null; then
+    have_dpkg_parsechangelog=yes
+  fi
+  if [ -f "${M}/../debian/changelog" ]; then
+    deb_changelog="${M}/../debian/changelog"
+  else
+    deb_changelog="${M}/debian/changelog"
+  fi
+  if [ "${have_dpkg_parsechangelog}" = yes ]; then
+    deb_version="$(dpkg-parsechangelog -l${deb_changelog} | awk '/^Version: / {print $$2}')"
+  fi
+  git_br_commit="git-$(cd ${M} && git rev-parse --abbrev-ref --symbolic-full-name HEAD 2> /dev/null)-$(cd ${M} && git rev-parse --short HEAD 2> /dev/null)"
+
+  if [ "${have_dpkg_parsechangelog}" = yes ]; then
+    RTPENGINE_VERSION+=" ${deb_version}"
+  fi
+  if [ "${git_br_commit}" != "git--" ]; then
+    RTPENGINE_VERSION+=" ${git_br_commit}"
+  fi
+
+  if [ -z "${RTPENGINE_VERSION}" ]; then
+    RTPENGINE_VERSION="undefined"
+  fi
+  echo "RTPENGINE_VERSION := ${RTPENGINE_VERSION}"
+fi
diff --git a/lib/codec-chain.Makefile b/lib/codec-chain.Makefile
deleted file mode 100644
index 6cf796d5e..000000000
--- a/lib/codec-chain.Makefile
+++ /dev/null
@@ -1,7 +0,0 @@
-ifneq (,$(filter pkg.ngcp-rtpengine.codec-chain,${DEB_BUILD_PROFILES}))
-ifneq (,$(wildcard $(CODEC_CHAIN_HOME)/usr/include/codec-chain/client.h))
-CFLAGS+=	-DHAVE_CODEC_CHAIN -I$(CODEC_CHAIN_HOME)/usr/include
-else ifneq (,$(wildcard /usr/include/codec-chain/client.h))
-CFLAGS+=	-DHAVE_CODEC_CHAIN
-endif
-endif
diff --git a/lib/deps.Makefile b/lib/deps.Makefile
new file mode 100644
index 000000000..d3564146c
--- /dev/null
+++ b/lib/deps.Makefile
@@ -0,0 +1,12 @@
+# Define build flags for used dependencies.
+
+$(top_srcdir)/config.mk:
+	$(top_srcdir)/utils/gen-common-flags >$@
+ifeq (,$(filter pkg.ngcp-rtpengine.nobcg729,${DEB_BUILD_PROFILES}))
+	$(top_srcdir)/utils/gen-bcg729-flags >>$@
+endif
+ifneq (,$(filter pkg.ngcp-rtpengine.codec-chain,${DEB_BUILD_PROFILES}))
+	$(top_srcdir)/utils/gen-codec-chain-flags >>$@
+endif
+
+include $(top_srcdir)/config.mk
diff --git a/lib/g729.Makefile b/lib/g729.Makefile
deleted file mode 100644
index abeef264a..000000000
--- a/lib/g729.Makefile
+++ /dev/null
@@ -1,58 +0,0 @@
-ifeq (,$(filter pkg.ngcp-rtpengine.nobcg729,${DEB_BUILD_PROFILES}))
-# look for bcg729
-# system pkg-config
-have_bcg729 := $(shell pkg-config --exists libbcg729 && echo yes)
-ifeq ($(have_bcg729),yes)
-bcg729_inc := $(shell pkg-config --cflags libbcg729)
-bcg729_lib := $(shell pkg-config --libs libbcg729)
-else
-# system generic
-ifneq (,$(wildcard /usr/include/bcg729/decoder.h))
-have_bcg729 := yes
-bcg729_lib := -lbcg729
-else
-# /usr/src
-ifneq (,$(wildcard /usr/src/bcg729/include/bcg729/decoder.h))
-have_bcg729 := yes
-bcg729_inc := -I/usr/src/bcg729/include/
-bcg729_lib := -L/usr/src/bcg729/src/ -lbcg729
-else
-# rfuchs dev
-ifneq (,$(wildcard $(HOME)/src/bcg729/include/bcg729/decoder.h))
-have_bcg729 := yes
-bcg729_inc := -I$(HOME)/src/bcg729/include/
-bcg729_lib := -L$(HOME)/src/bcg729/src/ -lbcg729
-else
-# home directory
-ifneq (,$(wildcard $(HOME)/bcg729/include/bcg729/decoder.h))
-have_bcg729 := yes
-bcg729_inc := -I$(HOME)/bcg729/include/
-bcg729_lib := -L$(HOME)/bcg729/src/ -lbcg729
-else
-# included toplevel
-ifneq (,$(wildcard ../bcg729/include/bcg729/decoder.h))
-have_bcg729 := yes
-bcg729_inc := -I../bcg729/include/
-bcg729_lib := -L../bcg729/src/ -lbcg729
-else
-# /usr/local/include when installing from git
-ifneq (,$(wildcard /usr/local/include/bcg729/decoder.h))
-have_bcg729 := yes
-bcg729_inc := -I/usr/local/include/
-bcg729_lib := -L/usr/local/lib64/ -lbcg729
-endif
-endif
-endif
-endif
-endif
-endif
-endif
-endif
-
-ifeq ($(have_bcg729),yes)
-CFLAGS+=	-DHAVE_BCG729
-CFLAGS+=	$(bcg729_inc)
-endif
-ifeq ($(have_bcg729),yes)
-LDLIBS+=	$(bcg729_lib)
-endif
diff --git a/lib/lib.Makefile b/lib/lib.Makefile
index ddd2bb61d..83df96cac 100644
--- a/lib/lib.Makefile
+++ b/lib/lib.Makefile
@@ -1,56 +1,14 @@
 CC ?= gcc
 
-
-ifeq ($(RTPENGINE_ROOT_DIR),)
-	RTPENGINE_ROOT_DIR=..
-endif
-
-HAVE_DPKG_PARSECHANGELOG := $(shell which dpkg-parsechangelog 2>/dev/null)
-
-ifeq ($(RELEASE_DATE),)
-  ifneq ($(HAVE_DPKG_PARSECHANGELOG),)
-    RELEASE_DATE := $(shell date -u -d "@$$(dpkg-parsechangelog -l$(RTPENGINE_ROOT_DIR)/debian/changelog -STimestamp)" '+%F')
-  endif
-  ifeq ($(RELEASE_DATE),)
-    RELEASE_DATE := undefined
-  endif
-endif
-
-ifeq ($(RTPENGINE_VERSION),)
-  ifneq ($(HAVE_DPKG_PARSECHANGELOG),)
-    DPKG_PRSCHNGLG := $(shell dpkg-parsechangelog -l$(RTPENGINE_ROOT_DIR)/debian/changelog | awk '/^Version: / {print $$2}')
-  endif
-  GIT_BR_COMMIT := git-$(shell git rev-parse --abbrev-ref --symbolic-full-name HEAD 2> /dev/null)-$(shell git rev-parse --short HEAD 2> /dev/null)
-
-  ifneq ($(DPKG_PRSCHNGLG),)
-    RTPENGINE_VERSION+=$(DPKG_PRSCHNGLG)
-  endif
-  ifneq ($(GIT_BR_COMMIT),git--)
-    RTPENGINE_VERSION+=$(GIT_BR_COMMIT)
-  endif
-
-  ifeq ($(RTPENGINE_VERSION),)
-    RTPENGINE_VERSION+=undefined
-  endif
-endif
 CFLAGS+=	-DRTPENGINE_VERSION="\"$(RTPENGINE_VERSION)\""
 
-# look for libsystemd
-have_libsystemd := $(shell pkg-config --exists libsystemd && echo yes)
-ifeq ($(have_libsystemd),yes)
-CFLAGS+=	$(shell pkg-config --cflags libsystemd)
-CFLAGS+=	-DHAVE_LIBSYSTEMD
-LDLIBS+=	$(shell pkg-config --libs libsystemd)
-endif
+CFLAGS+=	$(CFLAGS_LIBSYSTEMD)
+LDLIBS+=	$(LDLIBS_LIBSYSTEMD)
 
 # look for liburing
 ifeq (,$(filter pkg.ngcp-rtpengine.nouring,${DEB_BUILD_PROFILES}))
-have_liburing := $(shell pkg-config --atleast-version=2.3 liburing && echo yes)
-ifeq ($(have_liburing),yes)
-CFLAGS+=	$(shell pkg-config --cflags liburing)
-CFLAGS+=	-DHAVE_LIBURING
-LDLIBS+=	$(shell pkg-config --libs liburing)
-endif
+CFLAGS+=	$(CFLAGS_LIBURING)
+LDLIBS+=	$(LDLIBS_LIBURING)
 endif
 
 ifeq ($(DBG),yes)
@@ -62,15 +20,9 @@ LDFLAGS += -rdynamic
 
 ifneq ($(DBG),yes)
   ifeq (,$(filter $(CFLAGS),-O0))
-    DPKG_BLDFLGS := $(shell which dpkg-buildflags 2>/dev/null)
-    ifneq ($(DPKG_BLDFLGS),)
-      # support http://wiki.debian.org/Hardening for >=wheezy
-      CFLAGS+=	$(shell dpkg-buildflags --get CFLAGS)
-      CPPFLAGS+=	$(shell dpkg-buildflags --get CPPFLAGS)
-      LDFLAGS+=	$(shell dpkg-buildflags --get LDFLAGS)
-    endif
-    CFLAGS+=-O3 -flto=auto -ffat-lto-objects
-    LDFLAGS+=-flto=auto
+    CFLAGS+=	$(CFLAGS_DEFAULT)
+    CPPFLAGS+=	$(CPPFLAGS_DEFAULT)
+    LDFLAGS+=	$(LDFLAGS_DEFAULT)
   endif
 endif
 
diff --git a/lib/mqtt.Makefile b/lib/mqtt.Makefile
deleted file mode 100644
index 37d687442..000000000
--- a/lib/mqtt.Makefile
+++ /dev/null
@@ -1,10 +0,0 @@
-have_mqtt := $(shell pkg-config --exists libmosquitto && echo yes)
-ifeq ($(have_mqtt),yes)
-mqtt_inc := $(shell pkg-config --cflags libmosquitto)
-mqtt_lib := $(shell pkg-config --libs libmosquitto)
-CFLAGS+=	-DHAVE_MQTT
-CFLAGS+=	$(mqtt_inc)
-endif
-ifeq ($(have_mqtt),yes)
-LDLIBS+=	$(mqtt_lib)
-endif
diff --git a/perf-tester/Makefile b/perf-tester/Makefile
index fa7a50c7c..e97a017b0 100644
--- a/perf-tester/Makefile
+++ b/perf-tester/Makefile
@@ -1,5 +1,9 @@
 TARGET = rtpengine-perftest
 
+export top_srcdir = ..
+
+include ../lib/deps.Makefile
+
 FIXTURES_PATH ?= ../fixtures
 
 ifeq ($(origin CFLAGS),undefined)
@@ -12,39 +16,42 @@ endif
 CFLAGS += -pthread
 CFLAGS += -std=c11
 CFLAGS += -I. -I../kernel-module/ -I../lib/
-CFLAGS += -DPCRE2_CODE_UNIT_WIDTH=8
-CFLAGS += -DFIXTURES_PATH="\"$(FIXTURES_PATH)\""
-CFLAGS += $(shell pkg-config --cflags glib-2.0)
-CFLAGS += $(shell pkg-config --cflags json-glib-1.0)
-CFLAGS += $(shell pkg-config --cflags gthread-2.0)
 CFLAGS += -D_GNU_SOURCE
-CFLAGS += $(shell pkg-config --cflags libavcodec)
-CFLAGS += $(shell pkg-config --cflags libavformat)
-CFLAGS += $(shell pkg-config --cflags libavutil)
-CFLAGS += $(shell pkg-config --cflags libswresample)
-CFLAGS += $(shell pkg-config --cflags libavfilter)
-CFLAGS += $(shell pkg-config --cflags spandsp)
-CFLAGS += $(shell pkg-config --cflags opus)
-CFLAGS += $(shell pkg-config --cflags ncursesw)
+CFLAGS += -DFIXTURES_PATH="\"$(FIXTURES_PATH)\""
 CFLAGS += -DWITH_TRANSCODING
-CFLAGS += $(shell pkg-config --cflags openssl)
+CFLAGS += $(CFLAGS_LIBPCRE)
+CFLAGS += $(CFLAGS_GLIB)
+CFLAGS += $(CFLAGS_JSON_GLIB)
+CFLAGS += $(CFLAGS_GTHREAD)
+CFLAGS += $(CFLAGS_LIBAVCODEC)
+CFLAGS += $(CFLAGS_LIBAVFORMAT)
+CFLAGS += $(CFLAGS_LIBAVUTIL)
+CFLAGS += $(CFLAGS_LIBSWRESAMPLE)
+CFLAGS += $(CFLAGS_LIBAVFILTER)
+CFLAGS += $(CFLAGS_SPANDSP)
+CFLAGS += $(CFLAGS_OPUS)
+CFLAGS += $(CFLAGS_NCURSESW)
+CFLAGS += $(CFLAGS_OPENSSL)
 
 LDLIBS := -lm -ldl
-LDLIBS += $(shell pkg-config --libs glib-2.0)
-LDLIBS += $(shell pkg-config --libs json-glib-1.0)
-LDLIBS += $(shell pkg-config --libs gthread-2.0)
-LDLIBS += $(shell pkg-config --libs libavcodec)
-LDLIBS += $(shell pkg-config --libs libavformat)
-LDLIBS += $(shell pkg-config --libs libavutil)
-LDLIBS += $(shell pkg-config --libs libswresample)
-LDLIBS += $(shell pkg-config --libs libavfilter)
-LDLIBS += $(shell pkg-config --libs spandsp)
-LDLIBS += $(shell pkg-config --libs opus)
-LDLIBS += $(shell pkg-config --libs ncursesw)
-LDLIBS += $(shell pkg-config --libs openssl)
-
-include ../lib/g729.Makefile
-include ../lib/codec-chain.Makefile
+LDLIBS += $(LDLIBS_GLIB)
+LDLIBS += $(LDLIBS_JSON_GLIB)
+LDLIBS += $(LDLIBS_GTHREAD)
+LDLIBS += $(LDLIBS_LIBAVCODEC)
+LDLIBS += $(LDLIBS_LIBAVFORMAT)
+LDLIBS += $(LDLIBS_LIBAVUTIL)
+LDLIBS += $(LDLIBS_LIBSWRESAMPLE)
+LDLIBS += $(LDLIBS_LIBAVFILTER)
+LDLIBS += $(LDLIBS_SPANDSP)
+LDLIBS += $(LDLIBS_OPUS)
+LDLIBS += $(LDLIBS_NCURSESW)
+LDLIBS += $(LDLIBS_OPENSSL)
+
+CFLAGS += $(CFLAGS_BCG729)
+LDLIBS += $(LDLIBS_BCG729)
+
+CFLAGS += $(CFLAGS_CODEC_CHAIN)
+LDLIBS += $(LDLIBS_CODEC_CHAIN)
 
 SRCS = main.c log.c
 LIBSRCS = codeclib.strhash.c loglib.c auxlib.c resample.c str.c dtmflib.c rtplib.c poller.c ssllib.c bufferpool.c \
diff --git a/recording-daemon/Makefile b/recording-daemon/Makefile
index 8fe8f75e4..42917ea0a 100644
--- a/recording-daemon/Makefile
+++ b/recording-daemon/Makefile
@@ -1,43 +1,49 @@
 TARGET=		rtpengine-recording
 
+export top_srcdir = ..
+
+include ../lib/deps.Makefile
+
 ifeq ($(origin CFLAGS),undefined)
 CFLAGS?=	-g -Wall -Wextra -Wno-sign-compare -Wno-unused-parameter -Wstrict-prototypes -Werror=return-type \
 		-Wshadow
 else
 CFLAGS:=	$(CFLAGS)
 endif
-CFLAGS+=	-pthread -I. -I../lib/ -I../kernel-module/
+CFLAGS+=	-pthread
 CFLAGS+=	-std=c11
+CFLAGS+=	-I. -I../lib/ -I../kernel-module/
 CFLAGS+=	-D_GNU_SOURCE -D_POSIX_SOURCE -D_POSIX_C_SOURCE
-CFLAGS+=	-DPCRE2_CODE_UNIT_WIDTH=8
-CFLAGS+=	$(shell pkg-config --cflags glib-2.0)
-CFLAGS+=	$(shell pkg-config --cflags json-glib-1.0)
-CFLAGS+=	$(shell pkg-config --cflags gthread-2.0)
-CFLAGS+=	$(shell pkg-config --cflags libavcodec)
-CFLAGS+=	$(shell pkg-config --cflags libavformat)
-CFLAGS+=	$(shell pkg-config --cflags libavutil)
-CFLAGS+=	$(shell pkg-config --cflags libswresample)
-CFLAGS+=	$(shell pkg-config --cflags libavfilter)
-CFLAGS+=	$(shell pkg-config --cflags opus)
-CFLAGS+=	$(shell mysql_config --cflags)
-CFLAGS+=	$(shell pkg-config --cflags openssl)
-CFLAGS+=	$(shell pkg-config --cflags libcurl)
+CFLAGS+=	$(CFLAGS_LIBPCRE)
+CFLAGS+=	$(CFLAGS_GLIB)
+CFLAGS+=	$(CFLAGS_JSON_GLIB)
+CFLAGS+=	$(CFLAGS_GTHREAD)
+CFLAGS+=	$(CFLAGS_LIBAVCODEC)
+CFLAGS+=	$(CFLAGS_LIBAVFORMAT)
+CFLAGS+=	$(CFLAGS_LIBAVUTIL)
+CFLAGS+=	$(CFLAGS_LIBSWRESAMPLE)
+CFLAGS+=	$(CFLAGS_LIBAVFILTER)
+CFLAGS+=	$(CFLAGS_OPUS)
+CFLAGS+=	$(CFLAGS_MYSQL)
+CFLAGS+=	$(CFLAGS_OPENSSL)
+CFLAGS+=	$(CFLAGS_LIBCURL)
 
 LDLIBS:=	-lm -ldl
-LDLIBS+=	$(shell pkg-config --libs glib-2.0)
-LDLIBS+=	$(shell pkg-config --libs json-glib-1.0)
-LDLIBS+=	$(shell pkg-config --libs gthread-2.0)
-LDLIBS+=	$(shell pkg-config --libs libavcodec)
-LDLIBS+=	$(shell pkg-config --libs libavformat)
-LDLIBS+=	$(shell pkg-config --libs libavutil)
-LDLIBS+=	$(shell pkg-config --libs libswresample)
-LDLIBS+=	$(shell pkg-config --libs libavfilter)
-LDLIBS+=	$(shell pkg-config --libs opus)
-LDLIBS+=	$(shell mysql_config --libs)
-LDLIBS+=	$(shell pkg-config --libs openssl)
-LDLIBS+=	$(shell pkg-config --libs libcurl)
-
-include ../lib/g729.Makefile
+LDLIBS+=	$(LDLIBS_GLIB)
+LDLIBS+=	$(LDLIBS_JSON_GLIB)
+LDLIBS+=	$(LDLIBS_GTHREAD)
+LDLIBS+=	$(LDLIBS_LIBAVCODEC)
+LDLIBS+=	$(LDLIBS_LIBAVFORMAT)
+LDLIBS+=	$(LDLIBS_LIBAVUTIL)
+LDLIBS+=	$(LDLIBS_LIBSWRESAMPLE)
+LDLIBS+=	$(LDLIBS_LIBAVFILTER)
+LDLIBS+=	$(LDLIBS_OPUS)
+LDLIBS+=	$(LDLIBS_MYSQL)
+LDLIBS+=	$(LDLIBS_OPENSSL)
+LDLIBS+=	$(LDLIBS_LIBCURL)
+
+CFLAGS+=	$(CFLAGS_BCG729)
+LDLIBS+=	$(LDLIBS_BCG729)
 
 SRCS=		epoll.c garbage.c inotify.c main.c metafile.c stream.c recaux.c packet.c \
 		decoder.c output.c mix.c db.c log.c forward.c tag.c poller.c notify.c
diff --git a/t/Makefile b/t/Makefile
index cc0315ea0..d82c86436 100644
--- a/t/Makefile
+++ b/t/Makefile
@@ -1,5 +1,9 @@
 TARGET=		all-tests
 
+export top_srcdir = ..
+
+include ../lib/deps.Makefile
+
 with_transcoding ?= yes
 
 ifeq ($(origin CFLAGS),undefined)
@@ -11,64 +15,60 @@ endif
 PRELOAD_CFLAGS := $(CFLAGS)
 CFLAGS+=	-pthread
 CFLAGS+=	-std=c11
-CFLAGS+=	$(shell pkg-config --cflags glib-2.0)
-CFLAGS+=	$(shell pkg-config --cflags gthread-2.0)
-CFLAGS+=	$(shell pkg-config --cflags openssl)
 CFLAGS+=	-I. -I../lib/ -I../kernel-module/ -I../include/
 CFLAGS+=	-D_GNU_SOURCE
-CFLAGS+=	-DPCRE2_CODE_UNIT_WIDTH=8
-CFLAGS+=	$(shell pkg-config --cflags libpcre2-8)
-CFLAGS+=	$(shell pkg-config --cflags json-glib-1.0)
+CFLAGS+=	$(CFLAGS_GLIB)
+CFLAGS+=	$(CFLAGS_GTHREAD)
+CFLAGS+=	$(CFLAGS_OPENSSL)
+CFLAGS+=	$(CFLAGS_LIBPCRE)
+CFLAGS+=	$(CFLAGS_JSON_GLIB)
 ifeq ($(with_transcoding),yes)
-CFLAGS+=	$(shell pkg-config --cflags libavcodec)
-CFLAGS+=	$(shell pkg-config --cflags libavformat)
-CFLAGS+=	$(shell pkg-config --cflags libavutil)
-CFLAGS+=	$(shell pkg-config --cflags libswresample)
-CFLAGS+=	$(shell pkg-config --cflags libavfilter)
-CFLAGS+=	$(shell pkg-config --cflags spandsp)
-CFLAGS+=	$(shell pkg-config --cflags opus)
+CFLAGS+=	$(CFLAGS_LIBAVCODEC)
+CFLAGS+=	$(CFLAGS_LIBAVFORMAT)
+CFLAGS+=	$(CFLAGS_LIBAVUTIL)
+CFLAGS+=	$(CFLAGS_LIBSWRESAMPLE)
+CFLAGS+=	$(CFLAGS_LIBAVFILTER)
+CFLAGS+=	$(CFLAGS_SPANDSP)
+CFLAGS+=	$(CFLAGS_OPUS)
+CFLAGS+=	$(CFLAGS_LIBZ)
+CFLAGS+=	$(CFLAGS_LIBWEBSOCKETS)
+CFLAGS+=	$(CFLAGS_LIBEVENT)
+CFLAGS+=	$(CFLAGS_XMLRPC)
 CFLAGS+=	-DWITH_TRANSCODING
-CFLAGS+=	$(shell pkg-config --cflags zlib)
-CFLAGS+=	$(shell pkg-config --cflags libwebsockets)
-CFLAGS+=	$(shell pkg-config --cflags libevent_pthreads)
-CFLAGS+=	$(shell pkg-config xmlrpc_client --cflags 2> /dev/null || xmlrpc-c-config client --cflags)
-CFLAGS+=	$(shell pkg-config xmlrpc --cflags 2> /dev/null)
-CFLAGS+=	$(shell pkg-config xmlrpc_util --cflags 2> /dev/null)
 ifeq ($(RTPENGINE_EXTENDED_TESTS),1)
 CFLAGS+=	-DWITH_AMR_TESTS
 endif
-CFLAGS+=	$(shell mysql_config --cflags)
+CFLAGS+=	$(CFLAGS_MYSQL)
 else
 CFLAGS+=	-DWITHOUT_CODECLIB
 endif
 
 LDLIBS:=	-lm -ldl
-LDLIBS+=	$(shell pkg-config --libs glib-2.0)
-LDLIBS+=	$(shell pkg-config --libs gthread-2.0)
-LDLIBS+=	$(shell pkg-config --libs libcrypto)
-LDLIBS+=	$(shell pkg-config --libs openssl)
-LDLIBS+=	$(shell pkg-config --libs libpcre2-8)
-LDLIBS+=	$(shell pkg-config --libs json-glib-1.0)
+LDLIBS+=	$(LDLIBS_GLIB)
+LDLIBS+=	$(LDLIBS_GTHREAD)
+LDLIBS+=	$(LDLIBS_LIBCRYPTO)
+LDLIBS+=	$(LDLIBS_OPENSSL)
+LDLIBS+=	$(LDLIBS_LIBPCRE)
+LDLIBS+=	$(LDLIBS_JSON_GLIB)
 ifeq ($(with_transcoding),yes)
-LDLIBS+=	$(shell pkg-config --libs libavcodec)
-LDLIBS+=	$(shell pkg-config --libs libavformat)
-LDLIBS+=	$(shell pkg-config --libs libavutil)
-LDLIBS+=	$(shell pkg-config --libs libswresample)
-LDLIBS+=	$(shell pkg-config --libs libavfilter)
-LDLIBS+=	$(shell pkg-config --libs spandsp)
-LDLIBS+=	$(shell pkg-config --libs opus)
-LDLIBS+=	$(shell pkg-config --libs zlib)
-LDLIBS+=	$(shell pkg-config --libs libwebsockets)
-LDLIBS+=	-lpcap
-LDLIBS+=	$(shell pkg-config --libs libevent_pthreads)
-LDLIBS+=	$(shell pkg-config xmlrpc_client --libs 2> /dev/null || xmlrpc-c-config client --libs)
-LDLIBS+=	$(shell pkg-config xmlrpc --libs 2> /dev/null)
-LDLIBS+=	$(shell pkg-config xmlrpc_util --libs 2> /dev/null)
-LDLIBS+=	-lhiredis
-LDLIBS+=	$(shell mysql_config --libs)
+LDLIBS+=	$(LDLIBS_LIBAVCODEC)
+LDLIBS+=	$(LDLIBS_LIBAVFORMAT)
+LDLIBS+=	$(LDLIBS_LIBAVUTIL)
+LDLIBS+=	$(LDLIBS_LIBSWRESAMPLE)
+LDLIBS+=	$(LDLIBS_LIBAVFILTER)
+LDLIBS+=	$(LDLIBS_SPANDSP)
+LDLIBS+=	$(LDLIBS_OPUS)
+LDLIBS+=	$(LDLIBS_LIBZ)
+LDLIBS+=	$(LDLIBS_LIBWEBSOCKETS)
+LDLIBS+=	$(LDLIBS_LIBPCAP)
+LDLIBS+=	$(LDLIBS_LIBEVENT)
+LDLIBS+=	$(LDLIBS_XMLRPC)
+LDLIBS+=	$(LDLIBS_LIBHIREDIS)
+LDLIBS+=	$(LDLIBS_MYSQL)
 endif
 
-include ../lib/codec-chain.Makefile
+CFLAGS+=	$(CFLAGS_CODEC_CHAIN)
+LDLIBS+=	$(LDLIBS_CODEC_CHAIN)
 
 SRCS=		test-bitstr.c aes-crypt.c aead-aes-crypt.c test-const_str_hash.strhash.c aead-decrypt.c
 LIBSRCS=	loglib.c auxlib.c str.c rtplib.c ssllib.c mix_buffer.c bufferpool.c bencode.c
diff --git a/utils/gen-bcg729-flags b/utils/gen-bcg729-flags
new file mode 100755
index 000000000..dc8db3c59
--- /dev/null
+++ b/utils/gen-bcg729-flags
@@ -0,0 +1,47 @@
+#!/bin/bash
+
+have_bcg729="no"
+
+# look for bcg729
+if pkg-config --exists libbcg729; then
+  # system pkg-config
+  have_bcg729="yes"
+
+  bcg729_inc="$(pkg-config --cflags libbcg729)"
+  bcg729_lib="$(pkg-config --libs libbcg729)"
+elif [ -e /usr/include/bcg729/decoder.h ]; then
+  # system generic
+  have_bcg729="yes"
+  bcg729_lib="-lbcg729"
+elif [ -e /usr/src/bcg729/include/bcg729/decoder.h ]; then
+  # /usr/src
+  have_bcg729=yes
+  bcg729_inc="-I/usr/src/bcg729/include/"
+  bcg729_lib="-L/usr/src/bcg729/src/ -lbcg729"
+elif [ -e "${HOME}/src/bcg729/include/bcg729/decoder.h" ]; then
+  # rfuchs dev
+  have_bcg729="yes"
+  bcg729_inc="-I${HOME}/src/bcg729/include/"
+  bcg729_lib="-L${HOME}/src/bcg729/src/ -lbcg729"
+elif [ -e "${HOME}/bcg729/include/bcg729/decoder.h" ]; then
+  # home directory
+  have_bcg729="yes"
+  bcg729_inc="-I$(HOME)/bcg729/include/"
+  bcg729_lib="-L$(HOME)/bcg729/src/ -lbcg729"
+elif [ -e "../bcg729/include/bcg729/decoder.h" ]; then
+  # included toplevel
+  have_bcg729="yes"
+  bcg729_inc="-I../bcg729/include/"
+  bcg729_lib="-L../bcg729/src/ -lbcg729"
+elif [ -e /usr/local/include/bcg729/decoder.h ]; then
+  # /usr/local/include when installing from git
+  have_bcg729="yes"
+  bcg729_inc="-I/usr/local/include/"
+  bcg729_lib="-L/usr/local/lib64/ -lbcg729"
+fi
+
+if [ "${have_bcg729}" = "yes" ]; then
+  echo "CFLAGS_BCG729 := -DHAVE_BCG729"
+  echo "CFLAGS_BCG729 += ${bcg729_inc}"
+  echo "LDLIBS_BCG729 := ${bcg729_lib}"
+fi
diff --git a/utils/gen-codec-chain-flags b/utils/gen-codec-chain-flags
new file mode 100755
index 000000000..151f0aa70
--- /dev/null
+++ b/utils/gen-codec-chain-flags
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+if [ -e "${CODEC_CHAIN_HOME}/usr/include/codec-chain/client.h" ]; then
+  echo "CFLAGS_CODEC_CHAIN := -DHAVE_CODEC_CHAIN -I${CODEC_CHAIN_HOME}/usr/include"
+elif [ -e /usr/include/codec-chain/client.h ]; then
+  echo "CFLAGS_CODEC_CHAIN := -DHAVE_CODEC_CHAIN"
+fi
diff --git a/utils/gen-common-flags b/utils/gen-common-flags
new file mode 100755
index 000000000..63c447486
--- /dev/null
+++ b/utils/gen-common-flags
@@ -0,0 +1,119 @@
+#!/bin/bash
+
+gen-pkgconf-flags()
+{
+  local var="$1"
+  local pc="$2"
+
+  echo "export CFLAGS_${var} LDLIBS_${var}"
+  echo "CFLAGS_${var} := $(pkg-config --cflags "${pc}")"
+  echo "LDLIBS_${var} := $(pkg-config --libs "${pc}")"
+}
+
+if [ -z "${top_srcdir}" ]; then
+  top_srcdir=.
+fi
+
+have_dpkg_parsechangelog=no
+if command -v dpkg-parsechangelog >/dev/null; then
+  have_dpkg_parsechangelog=yes
+fi
+
+if [ -z "${RELEASE_DATE}" ]; then
+  if [ "${have_dpkg_parsechangelog}" = yes ]; then
+    RELEASE_DATE="$(date -u -d "@$(dpkg-parsechangelog -l${top_srcdir}/debian/changelog -STimestamp)" '+%F')"
+  fi
+  if [ -z "${RELEASE_DATE}" ]; then
+    RELEASE_DATE="undefined"
+  fi
+fi
+echo "RELEASE_DATE = ${RELEASE_DATE}"
+
+if [ -z "${RTPENGINE_VERSION}" ]; then
+  if [ "${have_dpkg_parsechangelog}" = yes ]; then
+    deb_version="$(dpkg-parsechangelog -l${top_srcdir}/debian/changelog | awk '/^Version: / { print $2 }')"
+  fi
+  git_br_commit="git-$(git rev-parse --abbrev-ref --symbolic-full-name HEAD 2> /dev/null)-$(git rev-parse --short HEAD 2> /dev/null)"
+
+  if [ "${have_dpkg_parsechangelog}" = yes ]; then
+    RTPENGINE_VERSION+=" ${deb_version}"
+  fi
+  if [ "${git_br_commit}" != "git--" ]; then
+    RTPENGINE_VERSION+=" ${git_br_commit}"
+  fi
+
+  if [ -z "${RTPENGINE_VERSION}" ]; then
+    RTPENGINE_VERSION=" undefined"
+  fi
+fi
+echo "RTPENGINE_VERSION := ${RTPENGINE_VERSION}"
+
+CFLAGS_DEFAULT=
+CPPFLAGS_DEFAULT=
+LDFLAGS_DEFAULT=
+LDLIBS_DEFAULT=
+if command -v dpkg-buildflags >/dev/null; then
+  # support http://wiki.debian.org/Hardening for >=wheezy
+  CFLAGS_DEFAULT=$(dpkg-buildflags --get CFLAGS)
+  CPPFLAGS_DEFAULT=$(dpkg-buildflags --get CPPFLAGS)
+  LDFLAGS=$(dpkg-buildflags --get LDFLAGS)
+fi
+CFLAGS_DEFAULT+=" -O3 -flto=auto -ffat-lto-objects"
+LDFLAGS_DEFAULT+=" -flto=auto"
+
+echo "export CFLAGS_DEFAULT := ${CFLAGS_DEFAULT}"
+echo "export CPPFLAGS_DEFAULT := ${CPPFLAGS_DEFAULT}"
+echo "export LDFLAGS_DEFAULT := ${LDFLAGS_DEFAULT}"
+
+gen-pkgconf-flags GLIB                glib-2.0
+gen-pkgconf-flags GTHREAD             gthread-2.0
+gen-pkgconf-flags JSON_GLIB           json-glib-1.0
+gen-pkgconf-flags LIBAVCODEC          libavcodec
+gen-pkgconf-flags LIBAVFILTER         libavfilter
+gen-pkgconf-flags LIBAVFORMAT         libavformat
+gen-pkgconf-flags LIBAVUTIL           libavutil
+gen-pkgconf-flags LIBCURL             libcurl
+gen-pkgconf-flags LIBCRYPTO           libcrypto
+gen-pkgconf-flags LIBEVENT            libevent_pthreads
+gen-pkgconf-flags LIBIPTC             libiptc
+gen-pkgconf-flags LIBMNL              libmnl
+gen-pkgconf-flags LIBNFTNL            libnftnl
+gen-pkgconf-flags LIBPCRE             libpcre2-8
+echo "CFLAGS_LIBPCRE += -DPCRE2_CODE_UNIT_WIDTH=8"
+gen-pkgconf-flags LIBSWRESAMPLE       libswresample
+gen-pkgconf-flags LIBWEBSOCKETS       libwebsockets
+gen-pkgconf-flags LIBZ                zlib
+gen-pkgconf-flags NCURSESW            ncursesw
+gen-pkgconf-flags OPENSSL             openssl
+gen-pkgconf-flags OPUS                opus
+gen-pkgconf-flags SPANDSP             spandsp
+
+echo "export CFLAGS_XMLRPC := $(pkg-config xmlrpc_client --cflags 2> /dev/null || xmlrpc-c-config client --cflags)"
+echo "CFLAGS_XMLRPC += $(pkg-config xmlrpc --cflags 2> /dev/null)"
+echo "CFLAGS_XMLRPC += $(pkg-config xmlrpc_util --cflags 2> /dev/null)"
+echo "export LDLIBS_XMLRPC := $(pkg-config xmlrpc_client --libs 2> /dev/null || xmlrpc-c-config client --libs)"
+echo "LDLIBS_XMLRPC += $(pkg-config xmlrpc --libs 2> /dev/null)"
+echo "LDLIBS_XMLRPC += $(pkg-config xmlrpc_util --libs 2> /dev/null)"
+
+echo "export CFLAGS_MYSQL := $(mysql_config --cflags)"
+echo "export LDLIBS_MYSQL := $(mysql_config --libs)"
+
+echo "export LDLIBS_LIBHIREDIS := -lhiredis"
+echo "export LDLIBS_LIBPCAP := -lpcap"
+
+if pkg-config --exists libmosquitto; then
+  gen-pkgconf-flags MQTT              libmosquitto
+  echo "CFLAGS_MQTT += -DHAVE_MQTT"
+fi
+
+# look for libsystemd
+if pkg-config --exists libsystemd; then
+  gen-pkgconf-flags LIBSYSTEMD        libsystemd
+  echo "CFLAGS_LIBSYSTEMD += -DHAVE_LIBSYSTEMD"
+fi
+
+# look for liburing
+if pkg-config --atleast-version=2.3 liburing; then
+  gen-pkgconf-flags LIBURING liburing
+  echo "CFLAGS_LIBURING += -DHAVE_LIBURING"
+fi