#!/bin/bash
#
# This file is part of nzbget. See <http://nzbget.net>.
#
# Copyright (C) 2015-2017 Andrey Prygunkov <hugbug@users.sourceforge.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This script builds cross-compiling toolchain, which can compile NZBGet for FreeBSD.
# The toolchain itself runs on Linux.
# Setup strict bash error handling
set -o nounset
set -o errexit
# Uncomment next line for debuging
#set -x
TARGET="x86_64-pc-freebsd"
# FreeBSD
FREEBSDIMAGE_URL="https://download.freebsd.org/ftp/releases/amd64/10.1-RELEASE/base.txz"
# CLANG (must be already installed)
CLANG="clang-4.0"
# Libxml
LIBXML2_VERSION="2.9.2"
LIBXML2_ARCHIVE="libxml2-$LIBXML2_VERSION.tar.gz"
LIBXML2_URL="http://xmlsoft.org/sources/$LIBXML2_ARCHIVE"
# OpenSSL
OPENSSL_VERSION="1.0.2d"
OPENSSL_ARCHIVE="openssl-$OPENSSL_VERSION.tar.gz"
OPENSSL_URL="ftp://ftp.openssl.org/source/old/1.0.2/$OPENSSL_ARCHIVE"
### START OF THE SCRIPT
ROOTDIR=`pwd`
rm -rf output
rm -rf sysroot
# Download all required tools
mkdir -p downloads
cd downloads
if [ ! -f base.txz ]; then
wget $FREEBSDIMAGE_URL
fi
if [ ! -f $LIBXML2_ARCHIVE ]; then
wget $LIBXML2_URL
fi
if [ ! -f $OPENSSL_ARCHIVE ]; then
wget $OPENSSL_URL
fi
cd ..
# Creating sysroot for FreeBSD from official FreeBSD installation image.
# Our sysroot contains only a small set of files needed to compile NZBGet and dependent libraries
mkdir -p sysroot
cd sysroot
tar xf ../downloads/base.txz \
./lib \
./usr/include \
./usr/lib/crt1.o \
./usr/lib/crtbegin.o \
./usr/lib/crtbeginS.o \
./usr/lib/crtbeginT.o \
./usr/lib/crtend.o \
./usr/lib/crtendS.o \
./usr/lib/crti.o \
./usr/lib/crtn.o \
./usr/lib/libc++.a \
./usr/lib/libc.a \
./usr/lib/libcompiler_rt.a \
./usr/lib/libgcc.a \
./usr/lib/libgcc_eh.a \
./usr/lib/libgcc_s.so \
./usr/lib/libm.a \
./usr/lib/libncurses.a \
./usr/lib/libpthread.a \
./usr/lib/libthr.a \
./usr/lib/libz.a
# fix bad symlink
ln -s -f ../../lib/libgcc_s.so.1 usr/lib/libgcc_s.so
cd ..
mkdir -p output/build && cd output/build
# Unpack everything
tar xf ../../downloads/$LIBXML2_ARCHIVE
tar xf ../../downloads/$OPENSSL_ARCHIVE
# Build libxml2 (2 minutes)
cd libxml2-$LIBXML2_VERSION
CC="$CLANG" CFLAGS="--target=$TARGET --sysroot=$ROOTDIR/sysroot" LDFLAGS="-fuse-ld=lld -static --target=$TARGET --sysroot=$ROOTDIR/sysroot" ./configure --host=$TARGET --prefix=$ROOTDIR/output/staging/usr --disable-dependency-tracking --without-zlib --without-lzma --without-python --disable-shared
make -j2
make install
cd ..
# Build OpenSSL (5 minutes)
cd openssl-$OPENSSL_VERSION
CC="$CLANG --target=$TARGET --sysroot=$ROOTDIR/sysroot" LD="$CLANG -fuse-ld=lld --target=$TARGET --sysroot=$ROOTDIR/sysroot" ./Configure --prefix=$ROOTDIR/output/staging/usr no-shared no-dso no-hw no-zlib no-unit-test BSD-x86_64
make -j2
make install_sw
cd ..
cd ..
# Create missing package descriptions
echo "prefix=$ROOTDIR/output/staging/usr
Name: zlib
Description: zlib
Version: 1
Libs: -L\${prefix}/lib -lz
Cflags: -I\${prefix}/include
" > $ROOTDIR/output/staging/usr/lib/pkgconfig/zlib.pc
echo "prefix=$ROOTDIR/output/staging/usr
Name: ncurses
Description: ncurses
Version: 5
Libs: -L\${prefix}/lib -lncurses
Cflags: -I\${prefix}/include
" > $ROOTDIR/output/staging/usr/lib/pkgconfig/ncurses.pc
# Cleanup
rm -r staging/usr/bin
rm -r staging/usr/share
rm -r staging/usr/ssl