SUMMARY="A deep learning framework"
DESCRIPTION="Caffe is a deep learning framework made with expression, speed, \
and modularity in mind. It is developed by Berkeley AI Research (BAIR)/The \
Berkeley Vision and Learning Center (BVLC) and community contributors."
HOMEPAGE="http://caffe.berkeleyvision.org/"
COPYRIGHT="2014 Randolph Voorhies, Shane Grant"
LICENSE="BSD (2-clause)"
REVISION="9"
SOURCE_URI="https://github.com/BVLC/caffe/archive/$portVersion.tar.gz"
CHECKSUM_SHA256="71d3c9eb8a183150f965a465824d01fe82826c22505f7aa314f700ace03fa77f"
PATCHES="caffe-$portVersion.patchset"

ARCHITECTURES="all !x86_gcc2 ?x86"
SECONDARY_ARCHITECTURES="?x86"

# On x86_gcc2 we don't want to install the commands in bin/<arch>/, but in bin/.
commandSuffix=$secondaryArchSuffix
if [ "$targetArchitecture" = x86_gcc2 ]; then
	commandSuffix=
fi

PROVIDES="
	caffe$secondaryArchSuffix = $portVersion
	cmd:caffe$commandSuffix
	cmd:classification$commandSuffix
	cmd:compute_image_mean$commandSuffix
	cmd:convert_cifar_data$commandSuffix
	cmd:convert_imageset$commandSuffix
	cmd:convert_mnist_data$commandSuffix
	cmd:convert_mnist_siamese_data$commandSuffix
	cmd:device_query$commandSuffix
	cmd:extract_features$commandSuffix
	cmd:finetune_net$commandSuffix
	cmd:net_speed_benchmark$commandSuffix
	cmd:test_net$commandSuffix
	cmd:train_net$commandSuffix
	cmd:upgrade_net_proto_binary$commandSuffix
	cmd:upgrade_net_proto_text$commandSuffix
	cmd:upgrade_solver_proto_text$commandSuffix
	lib:libcaffe$secondaryArchSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libboost_atomic$secondaryArchSuffix
	lib:libboost_chrono$secondaryArchSuffix
	lib:libboost_date_time$secondaryArchSuffix
	lib:libboost_filesystem$secondaryArchSuffix
	lib:libboost_system$secondaryArchSuffix
	lib:libboost_thread$secondaryArchSuffix
	lib:libgflags$secondaryArchSuffix
	lib:libglog$secondaryArchSuffix
	lib:libhdf5$secondaryArchSuffix
	lib:liblmdb$secondaryArchSuffix
	lib:libopenblas$secondaryArchSuffix
	lib:libopencv_core$secondaryArchSuffix
	lib:libopencv_highgui$secondaryArchSuffix
	lib:libopencv_imgcodecs$secondaryArchSuffix
	lib:libopencv_imgproc$secondaryArchSuffix
	lib:libprotobuf$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

PROVIDES_devel="
	caffe${secondaryArchSuffix}_devel = $portVersion
	devel:libcaffe$secondaryArchSuffix
	devel:libproto$secondaryArchSuffix
	"
REQUIRES_devel="
	caffe$secondaryArchSuffix == $portVersion base
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libboost_atomic$secondaryArchSuffix >= 1.69.0
	devel:libboost_chrono$secondaryArchSuffix >= 1.69.0
	devel:libboost_date_time$secondaryArchSuffix >= 1.69.0
	devel:libboost_filesystem$secondaryArchSuffix >= 1.69.0
	devel:libboost_system$secondaryArchSuffix >= 1.69.0
	devel:libboost_thread$secondaryArchSuffix >= 1.69.0
	devel:libgflags$secondaryArchSuffix
	devel:libglog$secondaryArchSuffix
	devel:libhdf5$secondaryArchSuffix
	devel:liblmdb$secondaryArchSuffix
	devel:libopenblas$secondaryArchSuffix
	devel:libopencv_core$secondaryArchSuffix
	devel:libopencv_highgui$secondaryArchSuffix
	devel:libopencv_imgcodecs$secondaryArchSuffix
	devel:libopencv_imgproc$secondaryArchSuffix
	devel:libprotobuf$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:cmake
	cmd:gcc$secondaryArchSuffix
#	cmd:f2py2
#	cmd:f2py3
	cmd:ld$secondaryArchSuffix
	cmd:make
#	cmd:python2
#	cmd:python3
	"

BUILD()
{
#Main settings
cat > Makefile.config << EOF
CPU_ONLY := 1
BLAS := open
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
USE_PKG_CONFIG := 1
EOF

	mkdir -p build && cd build

	cmake -DCMAKE_INSTALL_PREFIX=$prefix \
		-DCMAKE_BUILD_TYPE='Release' \
		-DUSE_LEVELDB:BOOL=OFF \
		-DUSE_OPENCV:BOOL=ON \
		-DCPU_ONLY:BOOL=ON \
		-DBUILD_python:BOOL=OFF \
		-DBUILD_SHARED_LIBS:BOOL=ON \
		..
	make $jobArgs
}

INSTALL()
{
	cd build
	make install

	mkdir -p $includeDir
	mv $prefix/include/* $includeDir
	rmdir $prefix/include/

	mkdir -p $libDir/cmake
	mv $prefix/share/* $libDir/cmake
	rmdir $prefix/share/

	# Python supporte disabled for now.
	rm -rf $prefix/python

	prepareInstalledDevelLibs libcaffe libproto

	# devel package
	packageEntries devel \
		$developDir \
		$libDir/cmake
}

TEST()
{
	cd build
	make test
	make runtest
}
