openocd|openocd 0.6.0/0.10.0( windows版) 在debian6/debian10系统上的交叉编译

前言 之前使用openocd软件连同busblaster B3C硬件对hg255d路由器进行JTAG调试时,一直只能下载网络上编译好的openocd windows版软件,如果自己需要对openocd进一步了解和学习,不可避免需要从源码进行编译。编译linux版的openocd一般比较顺利,在ubuntu系统下,一般通过apt命令安装必要的依赖库就可以了。但是编译window版的openocd则比较麻烦,一般有两个方向,一个是在windows系统下编译,通过cygwin或者msys,mingw等软件环境,安装libusb等库,另一个方向是在linux下通过mingw进行交叉编译。第一个方向尝试了下,一直不成功,后来就根据网络上有个成功的范例,改为在linux下进行交叉编译了。
linux系统的选择 【openocd|openocd 0.6.0/0.10.0( windows版) 在debian6/debian10系统上的交叉编译】以前用linux都是用ubuntu的,方便联网下载安装软件。刚开始在ubuntu上尝试编译,一直失败。后来看到这个项目https://github.com/caspencer/openocd-mingw32-build-scripts。这上面说在debian下编译成功了。于是也下载了debian进行编译。
mingw工具的选择 debian系统最新的稳定版本为10,在debian10下一开始并没有成功,因为无法按照那个项目中介绍的安装gcc-mingw32软件,也就是没有安装脚本中使用的i586-mingw32msvc工具。在debian10中只能安装i686-w64-mingw32工具。后来查了下,i686-w64-mingw32替代了i586-mingw32msvc。通过尝试发现,在debian6.0.10中才能安装i586-mingw32msvc。
debian 6.0.10下的编译 于是在debian 6.0.10系统下对项目https://github.com/caspencer/openocd-mingw32-build-scripts中的脚本进行编译,发现这个脚本都是临时在线下载libusb,libftdi和openocd的源码再进行编译,而在下载libusb代码时失败了,所以干脆手动到各个网址下载软件包,libusb-win32-src-1.2.5.0.zip,libftdi-0.19.tar.gz,openocd-0.6.0.tar.gz(这里openocd脚本中没有明确说版本,刚开始用最新版0.10.0编译失败,后来在build.sh文件中看到“OUTPUT_ZIP= C W D / o p e n o c d ? 0.6.0 ? d e v ? m i n g w 32 ? CWD/openocd-0.6.0-dev-mingw32- CWD/openocd?0.6.0?dev?mingw32?OUTPUT_ZIP_DATE.zip”这么一行,于是猜测作者用的openocd是0.6.0版本),修改脚本中关于在线下载软件的代码,改为直接使用压缩包。成功编译出windows版的openocd。
build.sh

#!/bin/bash# install required packages sudo apt-get install build-essential gcc-mingw32 cmake automake zip libtool# apply winbase.h patch sudo patch -N -u -s /usr/i586-mingw32msvc/include/winbase.h winbase.h.patchCWD=`pwd` TMP_CROSS_COMPILE_DIR=/tmp/cross-compile OUTPUT_ZIP_DATE=`date +%Y-%m-%d_%H%M` OUTPUT_ZIP=$CWD/openocd-0.6.0-dev-mingw32-$OUTPUT_ZIP_DATE.zipif [ -d "$TMP_CROSS_COMPILE_DIR" ]; then rm -rf $TMP_CROSS_COMPILE_DIR fimkdir -p $TMP_CROSS_COMPILE_DIR./cross-compile-libusb-win32.sh ./cross-compile-libftdi.sh ./cross-compile-openocd.shif [ -f "$OUTPUT_ZIP" ]; then rm $OUTPUT_ZIP ficd $TMP_CROSS_COMPILE_DIR/output zip -q -r $OUTPUT_ZIP . && cd $CWD echo '*** DONE! ***'

cross-compile-libusb-win32.sh
#!/bin/sh# requires gcc-mingw32 package # apt-get install gcc-mingw32 # NOTE: must patch /usr/i586-mingw32msvc/include/winbase.h to include CheckTokenMembership declaration because packaged version of winbase.h is lame apparentlyBUILD_DIR=/tmp/cross-compile/libusb-win32 OUTPUT_DIR=/tmp/cross-compile/output RELEASE_NAME=libusb-win32-src-1.2.5.0 RELEASE_VERSION=1.2.5.0if [ -d $BUILD_DIR ]; then rm -rf $BUILD_DIR fiif [ ! -d "$OUTPUT_DIR/include" ]; then mkdir -p $OUTPUT_DIR/include else rm $OUTPUT_DIR/include/*usb.h fiif [ ! -d "$OUTPUT_DIR/lib" ]; then mkdir -p $OUTPUT_DIR/lib else rm $OUTPUT_DIR/lib/libusb* fiif [ ! -d "$OUTPUT_DIR/bin" ]; then mkdir -p $OUTPUT_DIR/bin else rm $OUTPUT_DIR/bin/libusb* fimkdir -p $BUILD_DIR cd $BUILD_DIR #wget http://sourceforge.net/projects/libusb-win32/files/libusb-win32-releases/$RELEASE_VERSION/$RELEASE_NAME.zip cp /home/zouyulong/openocd-mingw32-build-scripts-master/openocd-mingw32-build-scripts-master/$RELEASE_NAME.zip ./ unzip -q $RELEASE_NAME.zipcd $RELEASE_NAME make host_prefix=i586-mingw32msvc allcp src/lusb0_usb.h $OUTPUT_DIR/include ln -s $OUTPUT_DIR/include/lusb0_usb.h $OUTPUT_DIR/include/usb.h cp *.a $OUTPUT_DIR/lib cp *.dll $OUTPUT_DIR/bin

cross-compile-libftdi.sh
#!/bin/sh# requires gcc-mingw32 package # apt-get install gcc-mingw32OUTPUT_DIR=/tmp/cross-compile/output BUILD_DIR=/tmp/cross-compile/libftdi LIBFTDI_VERSION=libftdi-0.19if [ -d $BUILD_DIR ]; then rm -rf $BUILD_DIR fiif [ ! -d "$OUTPUT_DIR/include" ]; then mkdir -p $OUTPUT_DIR/include else rm $OUTPUT_DIR/include/ftdi* fiif [ ! -d "$OUTPUT_DIR/lib" ]; then mkdir -p $OUTPUT_DIR/lib else rm $OUTPUT_DIR/lib/libftdi* fiif [ ! -d "$OUTPUT_DIR/bin" ]; then mkdir -p $OUTPUT_DIR/bin else rm $OUTPUT_DIR/bin/libftdi* fimkdir -p $BUILD_DIRcd $BUILD_DIR#wget http://www.intra2net.com/en/developer/libftdi/download/$LIBFTDI_VERSION.tar.gz cp /home/zzz/openocd-mingw32-build-scripts-master/openocd-mingw32-build-scripts-master/$LIBFTDI_VERSION.tar.gz ./tar zxf $LIBFTDI_VERSION.tar.gzcd $LIBFTDI_VERSIONmkdir build-win32# modify Toolchain-mingw32.cmake to use i586-mingw32msvc cross compiler sed -i 's/i386/i586/g' Toolchain-mingw32.cmake #sed -i 's/i386/amd64/g' Toolchain-mingw32.cmake sed -i 's/opt\/cross/usr/g' Toolchain-mingw32.cmakecd build-win32#cmake -DCMAKE_TOOLCHAIN_FILE=../Toolchain-mingw32.cmake -DCMAKE_BUILD_TYPE=MinSizeRel .. cmake -DCMAKE_TOOLCHAIN_FILE=../Toolchain-mingw32.cmake -DCMAKE_BUILD_TYPE=MinSizeRel \ -DLIBUSB_INCLUDE_DIR=$OUTPUT_DIR/include \ -DLIBUSB_LIBRARIES=$OUTPUT_DIR/lib/libusb.a \ ..make all cp ../src/ftdi.h $OUTPUT_DIR/include cp src/libftdi.a $OUTPUT_DIR/lib cp src/libftdi.dll $OUTPUT_DIR/bin

cross-compile-openocd.sh
#!/bin/bashOUTPUT_DIR=/tmp/cross-compile/output BUILD_DIR=/tmp/cross-compile/openocd-git OPENOCD_VERSION=openocd-0.6.0if [ -d $BUILD_DIR ]; then rm -rf $BUILD_DIR fimkdir -p $BUILD_DIRcd $BUILD_DIR#git clone git://openocd.git.sourceforge.net/gitroot/openocd/openocd $OPENOCD_VERSION cp/home/zzz/openocd-mingw32-build-scripts-master/openocd-mingw32-build-scripts-master/openocd-0.6.0.tar.gz./ $BUILD_DIR tar xzvf $OPENOCD_VERSION.tar.gz cd $OPENOCD_VERSION./bootstrap# cross compile with libftdi support ./configure --build=i686-pc-linux-gnu --host=i586-mingw32msvc \ --enable-maintainer-mode \ --prefix=$OUTPUT_DIR \ --disable-werror \ --disable-shared \ --enable-ft2232_libftdi \ --enable-usb_blaster_libftdimake && make install

openocd|openocd 0.6.0/0.10.0( windows版) 在debian6/debian10系统上的交叉编译
文章图片

debian 10.0.0 下的编译 因为openocd0.6.0已经很旧了,如何编译openocd0.10.0呢,并且使用i686-w64-mingw32编译是不是可以呢。发现是可以的。在debian10中编译openocd参考了https://blog.csdn.net/lshjn/article/details/82528076 一文。
首先安装一些依赖包
sudo apt-get install build-essentialcmake automake zip libtool sudo apt-getinstalli686-w64-mingw32-gcc sudo apt-getinstalli686-w64-mingw32-g++ sudo apt-get install libboost-all-dev sudo apt-get install libtool sudo apt-get install libudev-dev sudo apt-get install flex Sudo apt-get install cmake

然后交叉编译libusb,libconfuse,libftdi,openocd。
libusb
git clone https://github.com/libusb/libusbautogen.sh./configure --build=i686-linux --host=i686-w64-mingw32 --target=arm-linux-gnueabihf --prefix=/home/zzz/openocd-mingw32-build-scripts/libusb/output CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ --disable-udevmake make install

libconfuse
git clone https://github.com/martinh/libconfuse.gitautogen.sh./configure --build=i686-linux --host=i686-w64-mingw32 --target=arm-linux-gnueabihf --prefix=/home/zzz/openocd-mingw32-build-scripts/libconfuse/output CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ --disable-udevmake make install

libftdi
cmake/Toolchain-i686-w64-mingw32.cmake
# the name of the target operating system SET(CMAKE_SYSTEM_NAME Windows)# which compilers to use for C and C++ SET(CMAKE_C_COMPILER i686-w64-mingw32-gcc) SET(CMAKE_CXX_COMPILER i686-w64-mingw32-g++)# here is the target environment located SET(CMAKE_FIND_ROOT_PATH/usr/i686-w64-mingw32 )# adjust the default behaviour of the FIND_XXX() commands: # search headers and libraries in the target environment, search # programs in the host environment set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set (CMAKE_RC_COMPILER i686-w64-mingw32-windres)SET(LIBUSB_LIBRARIES /home/zzz/openocd-mingw32-build-scripts/libusb/output/lib/libusb-1.0.a) SET(LIBUSB_INCLUDE_DIR /home/zzz/openocd-mingw32-build-scripts/libusb/output/include/libusb-1.0) SET(CMAKE_INSTALL_PREFIX /home/zzz/openocd-mingw32-build-scripts/libftdi-output) SET(CONFUSE_LIBRARY /home/zzz/openocd-mingw32-build-scripts/libconfuse/output/lib/libconfuse.a) SET(CONFUSE_INCLUDE_DIR /home/zzz/openocd-mingw32-build-scripts/libconfuse/output/include/)

cmake -DCMAKE_TOOLCHAIN_FILE=/home/zzz/openocd-mingw32-build-scripts/libftdi1-1.4/cmake/Toolchain-i686-w64-mingw32.cmake ../makemake install

openocd 0.10.0
./configure --build=i686-pc-linux-gnu --host=i686-w64-mingw32 --enable-maintainer-mode --prefix=/home/zzz/openocd-mingw32-build-scripts/openocd-output --disable-werror --disable-shared --enable-ft2232_libftdi --enable-usb_blaster_libftdi make make install

openocd|openocd 0.6.0/0.10.0( windows版) 在debian6/debian10系统上的交叉编译
文章图片

    推荐阅读