交叉编译 OpenOCD 到 Windows 使用

1. 准备工作

本文基于 openocd-0.12.0 进行撰写,在 Linux 中使用 mingw32OpenOCD 编译成 Win32 应用。

OpenOCD 依赖以下项目:

上述依赖下载源码后解压备用。

2. 编译

使用下方脚本进行编辑。
该脚本参考了 xpack 的编译方式。
OpenOCD 的编译选项可参考该项目下的 configure 的 help 。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash

BUILD_DIR=/path/to/openocd-build/build

# set source env
export OPENOCD_SRC=/path/to/openocd
export LIBUSB1_SRC=/path/to/libusb-1.0.26
export HIDAPI_SRC=/path/to/hidapi-0.13.1
export LIBFTDI_SRC=/path/to/libftdi1-1.5
export CAPSTONE_SRC=/path/to/capstone-4.0.2
export LIBJAYLINK_SRC=/path/to/libjaylink-0.3.1

# configure openocd
cd /path/to/openocd
./bootstrap

config_opts=()

config_opts+=("--disable-wextra")
config_opts+=("--disable-werror")
config_opts+=("--disable-gccwarnings")
config_opts+=("--disable-doxygen-html")
config_opts+=("--disable-doxygen-pdf")

config_opts+=("--enable-am335xgpio")
config_opts+=("--enable-amtjtagaccel")
config_opts+=("--enable-angie")
config_opts+=("--enable-armjtagew")
config_opts+=("--enable-at91rm9200")
config_opts+=("--enable-bcm2835gpio")
config_opts+=("--enable-cmsis-dap")
config_opts+=("--enable-cmsis-dap-v2")
config_opts+=("--enable-dummy")
config_opts+=("--enable-esp-usb-jtag")
config_opts+=("--enable-ft232r")
config_opts+=("--enable-ftdi")
config_opts+=("--enable-gw16012")
config_opts+=("--enable-imx_gpio")
config_opts+=("--enable-jlink")
config_opts+=("--enable-jtag_dpi")
config_opts+=("--enable-jtag_vpi")
config_opts+=("--enable-kitprog")
config_opts+=("--enable-nulink")
config_opts+=("--enable-opendous")
config_opts+=("--enable-openjtag")
config_opts+=("--enable-osbdm")
config_opts+=("--enable-parport")
config_opts+=("--enable-parport-giveio")
config_opts+=("--enable-presto")
config_opts+=("--enable-remote-bitbang")
config_opts+=("--enable-rlink")
config_opts+=("--enable-stlink")
config_opts+=("--enable-ti-icdi")
config_opts+=("--enable-ulink")
config_opts+=("--enable-usb-blaster")
config_opts+=("--enable-usb-blaster-2")
config_opts+=("--enable-usbprog")
config_opts+=("--enable-vdebug")
config_opts+=("--enable-vsllink")
config_opts+=("--enable-xds110")

config_opts+=("--disable-buspirate")           # not support by MinGW
config_opts+=("--disable-dmem")                # only available on linux
config_opts+=("--disable-parport-ppdev")       # only available on linux or freebsd
config_opts+=("--disable-rshim")               # only available on linux or freebsd
config_opts+=("--disable-sysfsgpio")           # only available on linux
config_opts+=("--disable-xlnx-pcie-xvc")       # only available on linux

# configure hidapi
cd $HIDAPI_SRC
./bootstrap

# configure libjaylink
cd $LIBJAYLINK_SRC
./autogen

# setup env
export MAKE_JOBS=16
export HOST=i686-w64-mingw32
export LIBUSB1_CONFIG="--enable-shared --disable-static"
export HIDAPI_CONFIG="--enable-shared --disable-static --disable-testgui"
export LIBFTDI_CONFIG="-DSTATICLIBS=OFF -DEXAMPLES=OFF -DFTDI_EEPROM=OFF"
export CAPSTONE_CONFIG="CAPSTONE_BUILD_CORE_ONLY=yes CAPSTONE_STATIC=yes CAPSTONE_SHARED=no"
export LIBJAYLINK_CONFIG="--enable-shared --disable-static"
export OPENOCD_CONFIG=${config_opts[@]}

cd $OPENOCD_SRC

# check if there is tag pointing at HEAD, otherwise take the HEAD SHA-1 as OPENOCD_TAG
OPENOCD_TAG="`git tag --points-at HEAD`"
[ -z $OPENOCD_TAG ] && OPENOCD_TAG="`git rev-parse --short HEAD`"
# check if there is tag pointing at HEAD, if so the release will have the same name as the tag,
# otherwise it will be named 'latest'
RELEASE_NAME="`git tag --points-at HEAD`"
[ -z $RELEASE_NAME ] && RELEASE_NAME="latest"
[[ $RELEASE_NAME = "latest" ]] && IS_PRE_RELEASE="true" || IS_PRE_RELEASE="false"

# set env and call cross-build.sh
export OPENOCD_TAG=$OPENOCD_TAG
mkdir -p $BUILD_DIR && cd $BUILD_DIR
bash $OPENOCD_SRC/contrib/cross-build.sh $HOST

[ $? -eq 0 ] || exit

# add missing dlls
cd $HOST-root/usr
cp `$HOST-gcc --print-file-name=libwinpthread-1.dll` ./bin/
# required by libftdi1.dll. For the gcc-mingw-10.3.x or later "libgcc_s_dw2-1.dll" will need to be copied.
cp `$HOST-gcc --print-file-name=libgcc_s_dw2-1.dll` ./bin/

# prepare the artifact
ARTIFACT="openocd-${OPENOCD_TAG}-${HOST}.tar.gz"
tar -czf $ARTIFACT *
echo "RELEASE_NAME=$RELEASE_NAME"
echo "IS_PRE_RELEASE=$IS_PRE_RELEASE"
echo "ARTIFACT_PATH=$PWD/$ARTIFACT

3. 使用

/path/to/openocd-build/build/i686-w64-mingw32-root/usr/ 目录下的文件,拷贝至 Windows (x64) 中即可使用。

本博客所有文章除特别声明外,均采用 BY-NC-SA 4.0 许可协议。转载请注明出处!
最后更新于 2024/09/02 18:30 CST
使用 Hugo 构建
主题 StackJimmy 设计