OpenOCDをwslでWindows用にクロスコンパイルする

# OpenOCD for windows
ソースコードからビルドできたので、記録を残しておきます。

# 環境
- Windows 10 Home 1809
- WSL上のLinux環境
 - Ubuntu 18.04.2 LTS

# 前準備
WSL上のmingwでクロスコンパイルできるようにする。足りないパッケージがあるかも。

```
$ sudo apt install mingw-w64 autoconf texinfo libtool-bin autopoint flex cmake
```

# 環境変数の設定
ビルドに使う環境変数を設定。ライブラリはすべてstatic linkする。

```
$ BUILD_DIR=${HOME}/openocd_build
$ LIBUSB1_BUILD_DIR=${BUILD_DIR}/libusb
$ LIBUSB0_BUILD_DIR=${BUILD_DIR}/libusb-compat
$ LIBCONFUSE_BUILD_DIR=${BUILD_DIR}/libconfuse
$ HIDAPI_BUILD_DIR=${BUILD_DIR}/hidapi
$ LIBFTDI_BUILD_DIR=${BUILD_DIR}/libftdi
$ mkdir -p ${BUILD_DIR}
$ cd ${BUILD_DIR}
```

# libusb
```
$ git clone https://github.com/libusb/libusb.git libusb
$ cd libusb
$ ./bootstrap.sh
$ ./configure --host=x86_64-w64-mingw32 --enable-static --disable-shared
$ make
$ cd ..
```

#libusb-compat
```
$ git clone https://github.com/libusb/libusb-compat-0.1.git libusb-compat
$ cd libusb-compat
$ ./autogen.sh
$ LIBUSB_1_0_CFLAGS="-I${LIBUSB1_BUILD_DIR}/libusb" \
  LIBUSB_1_0_LIBS="-L${LIBUSB1_BUILD_DIR}/libusb/.libs -lusb-1.0" \
  PKG_CONFIG_PATH=${LIBUSB1_BUILD_DIR} \
  ./configure --host=x86_64-w64-mingw32 --enable-static --disable-shared
$ make
$ cd ..
```

#libconfuse
```
$ git clone https://github.com/martinh/libconfuse.git libconfuse
$ cd libconfuse
$ ./autogen.sh
$ ./configure --host=x86_64-w64-mingw32 --enable-static --disable-shared
$ make
$ cd ..
```

#libftdi
```
$ git clone git://developer.intra2net.com/libftdi libftdi
$ cd libftdi && mkdir build && cd build
$ cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/Toolchain-x86_64-w64-mingw32.cmake \
   -DLIBUSB_LIBRARIES=${LIBUSB1_BUILD_DIR}/libusb/.libs/ \
   -DLIBUSB_INCLUDE_DIR=${LIBUSB1_BUILD_DIR}/libusb \
   -DCONFUSE_LIBRARY=${LIBCONFUSE_BUILD_DIR}/src/.libs/ \
   -DCONFUSE_INCLUDE_DIR=${LIBCONFUSE_BUILD_DIR}/src/ ../
$ make ftdi1-static
$ cd ../..
```

#hidapi
```
$ git clone https://github.com/signal11/hidapi.git hidapi
$ cd hidapi
$ ./bootstrap
$ ./configure --host=x86_64-w64-mingw32 --enable-static --disable-shared
$ make
$ cd ..
```

#openocd
```
$ git clone https://git.code.sf.net/p/openocd/code openocd-code
$ cd openocd-code
$ patch -p1 << EOF
diff --git a/src/jtag/drivers/openjtag.c b/src/jtag/drivers/openjtag.c
index 7a3aa233..cd8d6260 100644
--- a/src/jtag/drivers/openjtag.c
+++ b/src/jtag/drivers/openjtag.c
@@ -438,7 +438,7 @@ static int openjtag_init_standard(void)
                return ERROR_JTAG_DEVICE_ERROR;
        }

-       if (ftdi_usb_purge_buffers(&ftdic) < 0) {
+       if (ftdi_tcioflush(&ftdic) < 0) {
                LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
                return ERROR_JTAG_INIT_FAILED;
        }
diff --git a/src/jtag/drivers/presto.c b/src/jtag/drivers/presto.c
index 2a94d061..1db41b8e 100644
--- a/src/jtag/drivers/presto.c
+++ b/src/jtag/drivers/presto.c
@@ -160,7 +160,7 @@ static int presto_open_libftdi(char *req_serial)
                return ERROR_JTAG_DEVICE_ERROR;
        }

-       if (ftdi_usb_purge_buffers(&presto->ftdic) < 0) {
+       if (ftdi_tcioflush(&presto->ftdic) < 0) {
                LOG_ERROR("unable to purge PRESTO buffers");
                return ERROR_JTAG_DEVICE_ERROR;
        }
@@ -174,7 +174,7 @@ static int presto_open_libftdi(char *req_serial)
        if (presto_read(&presto_data, 1) != ERROR_OK) {
                LOG_DEBUG("no response from PRESTO, retrying");

-               if (ftdi_usb_purge_buffers(&presto->ftdic) < 0)
+               if (ftdi_tcioflush(&presto->ftdic) < 0)
                        return ERROR_JTAG_DEVICE_ERROR;

                presto_data = 0xD0;
EOF
$ ./bootstrap
$ HIDAPI_CFLAGS="-I${HIDAPI_BUILD_DIR}/hidapi" \
  HIDAPI_LIBS="-L${HIDAPI_BUILD_DIR}/windows/.libs -lhidapi" \
  LIBUSB0_CFLAGS="-I${LIBUSB0_BUILD_DIR}/libusb" \
  LIBUSB0_LIBS="-L${LIBUSB0_BUILD_DIR}/libusb/.libs -lusb" \
  LIBUSB1_CFLAGS="-I${LIBUSB1_BUILD_DIR}/libusb" \
  LIBUSB1_LIBS="-L${LIBUSB1_BUILD_DIR}/libusb/.libs -lusb-1.0" \
  LIBFTDI_CFLAGS="-I${LIBFTDI_BUILD_DIR}/src " \
  LIBFTDI_LIBS="-L${LIBFTDI_BUILD_DIR}/build/src -lftdi1" \
  PKG_CONFIG_PATH=${HIDAPI_BUILD_DIR}/pc:${LIBUSB1_BUILD_DIR}:${LIBUSB0_BUILD_DIR}:${LIBFTDI_BUILD_DIR}/build \
  libusb_CFLAGS="-I${LIBUSB1_BUILD_DIR}/libusb" \
  libusb_LIBS="-L${LIBUSB1_BUILD_DIR}/libusb/.libs/ -lusb-1.0" \
  ./configure --host=x86_64-w64-mingw32 --prefix=/mnt/c/OpenOCD
$ make
$ sed -i -e 's|-lftdi1|'${LIBFTDI_BUILD_DIR}'/build/src/libftdi1.a|' src/libopenocd.la
$ make
$ make install
$ cd ..
```

#動作確認
ターゲットデバイスをつなげてPowerShellで実行

```
> C:\OpenOCD\bin\openocd.exe -f interface/cmsis-dap.cfg -f target/lpc8xx.cfg
Open On-Chip Debugger 0.10.0+dev-00920-g6ea43726-dirty (2019-06-24-10:02)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "swd". To override use 'transport select <transport>'.
cortex_m reset_config sysresetreq

Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : CMSIS-DAP: SWD  Supported
Info : CMSIS-DAP: FW Version = 1.0
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 0 TDO = 0 nTRST = 0 nRESET = 1
Info : CMSIS-DAP: Interface ready
Info : clock speed 10 kHz
Info : SWD DPIDR 0x0bc11477
Info : lpc8xx.cpu: hardware has 4 breakpoints, 2 watchpoints
Info : lpc8xx.cpu: external reset detected
Info : Listening on port 3333 for gdb connections
shutdown command invoked
```
# 補足:LPC Link2(CMSIS-DAP)
LPC Link2をデバッガ(CMSIS-DAP)にする場合、新しいファームウェア(Scrypt_installer_2.1.0_842.exe)だと下記エラーで動きませんでした

```
Info : auto-selecting first available session transport "swd". To override use 'transport select <transport>'.
cortex_m reset_config sysresetreq

Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Error: CMSIS-DAP command CMD_INFO failed.
```

~~古いファームウェア(lct_installer_1.exe)だとうまく動きました~~動かないです…