Ubuntu 16.04上使用crosstool-ng编译Nvidia TX2上使用的boost库

目前在Nvidia TX2上尝试安装ROS Kinetic的时候发现依赖libboost-all-dev,但是自带的源http://ports.ubuntu.com下面没有合适的源,尽管在universe源下面包含libboost-all-dev ,但是实际安装的时候发现安装不上。

因此尝试手工编译最新的boost安装包,建立自己的源。

首先,编译安装最新的crosstool-ng,如下:

$ sudo apt-get install git

$ sudo apt-get install gperf

$ sudo apt-get install bison

$ sudo apt-get install flex

$ sudo apt-get install texinfo

$ sudo apt-get install help2man

$ sudo apt-get install gawk

$ sudo apt-get install libncurses5-dev

$ cd ~

$ git clone https://github.com/crosstool-ng/crosstool-ng.git

$ cd crosstool-ng

$ git checkout crosstool-ng-1.23.0

$ ./bootstrap

$ ./configure --prefix=/usr/local

$ make

$ sudo make install

$ cd ~

$ mkdir tx2

$ cd tx2

#用树莓派的模板作为参考模板即可
$ cp ~/crosstool-ng/samples/aarch64-rpi3-linux-gnueabi/crosstool.config .config

$ ct-ng oldconfig

#默认一路回车即可,如果使用图形界面配置执行 ct-ng menuconfig
 
$ unset LD_LIBRARY_PATH

$ ct-ng build

生成的工具链在~/x-tools/aarch64-rpi3-linux-gnueabi目录下,编译的时候,指定编译工具即可。

安装编译依赖

$ sudo apt-get install devscripts

$ sudo apt-get install cdbs

$ sudo apt-get install python-dev

$ sudo apt-get install libbz2-dev

接着,下载并编译最新的代码

#!/bin/bash
export DEBVERSION=1.58.0-1
if [ ! -d "boost_1_58_0" ]; then
    wget "http://nchc.dl.sourceforge.net/project/boost/boost/1.58.0/boost_1_58_0.tar.bz2" -O boost_1_58_0.tar.bz2
    tar xjvf boost_1_58_0.tar.bz2
fi
cd boost_1_58_0

:<<BLOCK
'export DEBVERSION=1.60.0-1
if [ ! -d "boost_1_60_0" ]; then
 wget "http://downloads.sourceforge.net/project/boost/boost/1.60.0/boost_1_60_0.tar.bz2" -O boost-all_1.60.0.orig.tar.bz2
 tar xjvf boost-all_1.60.0.orig.tar.bz2
fi
cd boost_1_60_0
'
BLOCK

#Build DEB
rm -rf debian
mkdir -p debian
#Use the LICENSE file from nodejs as copying file
touch debian/copying
#Create the changelog (no messages needed)
dch --create -v $DEBVERSION --package libboost-all ""
#Create copyright file
touch debian
#Create control file
cat > debian/control <<EOF
Source: libboost-all
Maintainer: LongSky <wangqiang1588@sina.com>
Section: misc
Priority: optional
Standards-Version: 3.9.2
Build-Depends: debhelper (>= 8), cdbs, libbz2-dev, zlib1g-dev

Package: libboost-all
Architecture: arm64
Depends: \${shlibs:Depends}, \${misc:Depends}, libboost-all (= $DEBVERSION)
Description: Boost library, version $DEBVERSION (shared libraries)

Package: libboost-all-dev
Architecture: any
Depends: libboost-all (= $DEBVERSION)
Description: Boost library, version $DEBVERSION (development files)

Package: libboost-build
Architecture: any
Depends: \${misc:Depends}
Description: Boost Build v2 executable
EOF
#Create rules file
cat > debian/rules <<EOF 
#!/usr/bin/make -f
%:
	dh \$@
override_dh_auto_configure:
	./bootstrap.sh
override_dh_auto_build:
	./b2 link=static,shared -j 1 --prefix=`pwd`/debian/libboost-all/usr/
override_dh_auto_test:
override_dh_auto_install:
	mkdir -p debian/libboost-all/usr debian/libboost-all-dev/usr debian/libboost-build/usr/bin
	./b2 link=static,shared --prefix=`pwd`/debian/libboost-all/usr/ install
	mv debian/libboost-all/usr/include debian/libboost-all-dev/usr
	cp b2 debian/libboost-build/usr/bin
	./b2 install --prefix=`pwd`/debian/libboost-build/usr/ install
EOF
#Create some misc files
echo "8" > debian/compat
mkdir -p debian/source
echo "3.0 (quilt)" > debian/source/format
#Build the package
nice -n19 ionice -c3 debuild -b -d -uc -us

注意,对于boost-1.58来说,当时的代码是没有检测ARM64的功能的,当年也没有ARM64的芯片。因此需要修改{boost_root}/libs/config/checks/architecture/arm.cpp里面的代码,增加ARM64的判断

#if !defined(__arm__) && !defined(__thumb__) && \
    !defined(__TARGET_ARCH_ARM) && !defined(__TARGET_ARCH_THUMB) && \
    !defined(_ARM) && !defined(_M_ARM)
#error "Not ARM"
#endif

调整为

#if !defined(__arm__) && !defined(__thumb__) && \
    !defined(__TARGET_ARCH_ARM) && !defined(__TARGET_ARCH_THUMB) && \
    !defined(_ARM) && !defined(_M_ARM) && !defined(__aarch64__)
#error "Not ARM"
#endif

参考链接