#!/bin/bash
# Copyright (c) 2000-2017 Synology Inc. All rights reserved.

function join_boost_component() {
	local COMPONENT=()
	for comp in $@; do
		COMPONENT+=" --with-${comp} "
	done

	echo ${COMPONENT[*]}
}

function build_boost() {
	case $ARCH in
		arm|arm64)
			abi="aapcs"
			architecture=arm
			;;
		x86_64|x86)
			abi="sysv"
			architecture=x86
			;;
		powerpc)
			abi="sysv"
			architecture=power
			;;
		*)
			;;
	esac

	_bindir="bin.linuxx86_64"
	_stagedir="${PWD}/stagedir"
	_jobs="$(sed -e 's/.*\(-j *[0-9]\+\).*/\1/' <<< ${MAKE_FLAGS})"

	PythonInclude=$(pkg-config --cflags python2)
	CFLAGS="${PythonInclude} ${CFLAGS}"
	LDFLAGS="${LDFLAGS} -lstdc++"

	./bootstrap.sh \
		--prefix=/usr \
		--exec-prefix=/usr \
		--with-toolset=gcc \
		--with-icu

	echo "using gcc : syno : ${CXX} ;" >> ./project-config.jam
	echo "using python : 2.7 : ${CC} : : : gcc-syno ;" >> ./project-config.jam

	./b2 headers

	components=$(join_boost_component $@)
	./b2 \
		variant=release \
		debug-symbols=off \
		threading=multi \
		runtime-link=shared \
		link=shared,static \
		toolset=gcc-syno \
		python=2.7 \
		architecture=$architecture \
		address-model=$BUILD_ARCH \
		abi=$abi \
		binary-format=elf \
		cflags="${CPPFLAGS} ${CFLAGS} -O3" linkflags="${LDFLAGS}" \
		${EXTRA_FLAGS} \
		--layout=system \
		--prefix="${_stagedir}" \
		${components} \
		$_jobs \
		install
}
