#***PRELIMINARY VERSION***
# Compiling Asterisk on Alpine
This is currently a quick and very dirty way document on compiling Asterisk on Alpine.
This is not intended to be a Tutorial or HOWTO. I am not holding hands.
I *HAVE NOT* re-verified this document with a second build. In fact, I haven't even verified
the current build works. It simply doesn't fail at any point in the process.
This is based heavily off Alpine's own buildlog and uses some of their scripts from aports.
Install the dependencies (as a virtual package) - CHANGE linux-virt-dev TO MATCH YOUR KERNEL!
```
apk add linux-virt-dev bsd-compat-headers linux-headers dahdi-linux-dev wget git nano build-base alsa-lib-dev alpine-sdk bluez-dev bsd-compat-headers curl-dev dahdi-tools-dev findutils freetds-dev imap-dev jansson-dev libcap-dev libedit-dev libogg-dev libpri-dev libresample libsrtp-dev libtool libxml2-dev lua-dev mariadb-connector-c-dev ncurses-dev subversion newt-dev openssl1.1-compat-dev opus-dev opusfile-dev pjproject-dev popt-dev libpq-dev spandsp-dev speexdsp-dev speex-dev sqlite-dev tar tiff-dev unbound-dev unixodbc-dev util-linux-devperl file autoconf automake pkgconfig zlib-dev -t builddep.asterisk
```
Clone the repos:
```
git clone https://github.com/asterisk/asterisk.git
git clone https://github.com/asterisk/dahdi-linux.git
git clone https://github.com/asterisk/dahdi-tools.git
git clone https://github.com/asterisk/libpri.git
```
cd in to dhadi-linux and build it:
```
make -j4
make install
```
Okay. dahdi-tools has been a royal PITA. But I think we need to do this:
```
cd dahdi-tools
wget https://git.alpinelinux.org/aports/plain/main/dahdi-tools/fix-musl.patch?h=3.16-stable
patch -ruN fix-musl.patch?h=3.16-stable
```
The directories are different..plus I never got the APKBUILD patches to work automatically. I couldn't even get my own to work automatically. This is going to happen for the rest of our patches...so pay attention.
```
|+++ dahdi-tools-2.11.1/xpp/hexfile.h 2017-05-29 09:30:50.034438806 +0300
```
The file is `xpp/hexfile.h` if running the patch inside the dahdi-tools. You have to go through this similar punching in of files for all the patches. For pretty much all of these, you ignore the first folder since we're running this relative to the local repo.
```
autoreconf -fi
./configure
make -j4
make install
make install-config
dahdi_genconf modules
rm -rf /usr/lib/dracut
```
Holy cow. It worked. This is the first I've gotten this far with it. Still using ISDN? Of course not. We'll build it anyway.
```
cd libpri
sed -e 's|-Werror|-Wno-error|g' -i Makefile
sed -e 's,^#include < sys / signal . h > $,#include < signal.h > ,g' -i pritest.c testprilib.c
make -j4
make install
```
Wow. This is looking promising. Let's do Asterisk.
Grab the Opus codec and copy the file in to the repository before cd'ing in to it.
```
wget https://distfiles.alpinelinux.org/distfiles/v3.16/asterisk-13.7-90e8780faccc79e68c07775c6ab2fe1ffaccfa08.tar.gz
tar -zxvf asterisk-13.7-90e8780faccc79e68c07775c6ab2fe1ffaccfa08.tar.gz
cp asterisk-opus-90e8780faccc79e68c07775c6ab2fe1ffaccfa08/codecs/codec_opus_open_source.c asterisk/codecs/codec_opus_open_source.c
cp asterisk-opus-90e8780faccc79e68c07775c6ab2fe1ffaccfa08/codecs/ex_opus.h asterisk/codecs/ex_opus.h
```
Browse to the repository folder and grab the following files:
```
wget https://git.alpinelinux.org/aports/plain/main/asterisk/10-musl-mutex-init.patch
wget https://git.alpinelinux.org/aports/plain/main/asterisk/20-musl-astmm-fix.patch
wget https://git.alpinelinux.org/aports/plain/main/asterisk/40-asterisk-cdefs.patch
```
Grab Asterisk's MP3
```
./contrib/scripts/get_mp3_source.sh
```
res_crypto.c requires some attention. Open the file and add these lines with the other definitions:
```
#if !defined(ALLPERMS)
# define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO) /* 07777 */
#endif
```
All patched. Now configure.
```
./configure --build=$CBUILD --host=$CHOST --prefix=/usr --sysconfdir=/etc --mandir=/usr/share/man --infodir=/usr/share/info --libdir=/usr/lib --localstatedir=/var --disable-xmldoc --enable-permanent-dlopen --with-gsm=internal --with-popt --with-z --with-newt --with-unixodbc --with-postgres --with-tds --with-dahdi --with-pri --with-tonezone --with-resample --with-sqlite3 --with-speex --with-asound --without-x11 --without-pjproject-bundled --with-spandsp --with-bluetooth --with-libcurl --with-libedit --with-srtp --with-imap=system --with-opus --with-opusfile
```
And OMG, yes. Now do the menuselect thing:
```
make menuselect
```
Compile using all the cores you have
```
make -j4
make install
```
At this point you can `make samples` , `make basic-pbx` , or whatever else.
We have some openrc/init.d stuff to setup.
```
https://git.alpinelinux.org/aports/plain/main/dahdi-tools/dahdi-tools.initd?h=3.16-stable
mv dahdi-tools.initd?h=3.16-stable /etc/init.d/dahdi-tools
chmod 755 /etc/init.d/dahdi
rc-update add dahdi
```
That's dahdi. Now Asterisk.
```
addgroup -S asterisk 2>/dev/null
adduser -S -D -h /var/lib/asterisk -s /sbin/nologin -G asterisk -g asterisk asterisk 2>/dev/null
addgroup -S dialout 2>/dev/null
addgroup asterisk dialout 2>/dev/null
wget https://git.alpinelinux.org/aports/plain/main/asterisk/asterisk.confd?h=3.16-stable
mv asterisk.confd\?h=3.16-stable /etc/conf.d/asterisk
chmod 644 /etc/conf.d/asterisk
wget https://git.alpinelinux.org/aports/plain/main/asterisk/asterisk.initd?h=3.16-stable
mv asterisk.initd\?h=3.16-stable /etc/init.d/asterisk
chmod 644 /etc/init.d/asterisk
wget https://git.alpinelinux.org/aports/plain/main/asterisk/asterisk.logrotate?h=3.16-stable
mv asterisk.logrotate?h=3.16-stable /etc/logrotate.d/asterisk
chmod 644 /etc/logrotate.d/asterisk
chown -R asterisk: /var/{lib,log,run,spool}/asterisk /usr/lib/asterisk /etc/asterisk
chmod -R 750 /var/{lib,log,run,spool}/asterisk /usr/lib/asterisk /etc/asterisk
rc-update add asterisk
```
That's it. It's all on you to write your configurations.