Posted on

Automate building of dependent libs with rpath handling

Automating the building of 3rd party libraries involves fetching the source, extracting, optionally applying a patch, building and installing. On Mac and Linux you also have to take care of rpaths ensuring the rpath embedded in your target libs (and pulled in by referencing executables) are as you intend. It’s such a common task that I’ve a script that does this for me and decided to share it here in case someone else finds it useful for their projects (assuming the library being downloaded uses CMake). This task can also be performed now entirely within CMake using the ExternalProject command, the subject of a future post.

pkgBaseUrl=https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/bullet
pkgName=bullet
pkgVer=2.82-r2704
package_name=$pkgName-$pkgVer.tgz
installDir=../../../ThirdParty
pkgOptions="-DCMAKE_MACOSX_RPATH:BOOL=ON -DCMAKE_INSTALL_RPATH=@rpath/lib -DBUILD_DEMOS:BOOL=OFF -DINSTALL_LIBS=ON -DBUILD_SHARED_LIBS=ON"
src_dir="${package_name%.*}"
if [ -d "$src_dir" ]; then
	rm -rf $src_dir
fi
wget $pkgBaseUrl/$package_name
tar -xvf $package_name
if test -e ${pkgName}-${ver}.patch ; then
	patch -p0 < ${pkgName}-${ver}.patch
fi
cd $src_dir
mkdir $pkgName-build
cd $pkgName-build
install_dir=`pwd -P`/$installDir
cmake .. -G "Unix Makefiles" \
	-DCMAKE_OSX_ARCHITECTURES='i386;x86_64' -DCMAKE_BUILD_TYPE=Release \
	-DCMAKE_INSTALL_PREFIX=$install_dir \
	$pkgOptions
make -j4
make install