Building OpenOCD under Windows using MSYS2

About

In past, building OpenOCD under Windows was really complicated since it has a lot of dependencies and not was easily available under windows. With MSYS2 this task has become very simple and, if you need to use the latest stable version of OpenOCD in MSYS2, it is available as binary and could be installed with a single command.

In some cases, you need to build the development version of OpenOCD or, maybe, to apply some patches from OpenOCD Gerrit repository. Using MSYS2 and some PKGBUILD from the upstream repository, this operation is easy to accomplish.

Introducing MSYS2

MSYS2, which stand for Minimal SYStem 2, is a compact application based on Cygwin and MinGW-w64 which runs under Windows. It allow to use autotools, revision control systems and build native Windows applications using MINGW-w64 toolchains. The installation of packages is simplified since creators have ported Arch Linux’s Pacman. This brings many powerful features such as dependency resolution and simple complete system upgrades. When launched MSYS2 appears to be a simple terminal.

MSYS2 consists of three subsystems (here and out MSYSTEM) and their corresponding package repositories. They are msys2, mingw32, and mingw64.

  • The mingw subsystems provide native Windows programs and are the main focus of the project. These programs are built to cooperate well with other Windows programs, independently of the other subsystems.
  • The msys2 subsystem provides an emulated mostly-POSIX-compliant environment for building software, package management, and shell scripting. These programs live in a virtual single-root filesystem (the root is the MSYS2 installation directory). Some effort is made to have the programs work well with native Windows programs, but it’s not seamless.

Preliminary notes

First time I wrote this article I was looking for a simple recipe to build OpenOCD under Windows and create a standalone suitable for ChibiStudio. The initial purpose has been extended and this article now is addressed to the reader who wants:

  • install OpenOCD in MSYS2 from binary;
  • compile and install OpenOCD in MSYS2 from development repository;
  • export OpenOCD as stand alone.

Setup of MSYS2

Setup of MSYS2
One of the prompt windows during MSYS2 setup

MSYS2 is available as executable on SourceForge. Setup is straightforward and creates a folder usually under C: named msys64 (or msys32 in case of 32-bit system).

After the installation, the shell could be launched through one of the three .exe which are available in the installation folder. Each exe is related to one of the three MSYSTEM.

Usually we are interested to build software which cooperates with other Windows programs so we should use mingw32 or mingw64.

To be compatible with both 32-bit and 64-bit operating systems, the best choice is to use mingw32. In this case we should launch mingw32.exe.

It is possible to check current MSYSTEM through this command:

To avoid any problem related to low permission levels, it is a good idea to launch the .exe right-clicking and choosing Run as Administrator.

Note that, here classical copy and paste hotkeys don’t work. It is possible to use it right clicking on terminal or using CTRL+Ins and Shift+Ins instead of CTRL+C and CTRL+V.

Install OpenOCD from binary

This procedure is quite simple. We can install the 32-bit version using the command

or the 64-bit version using

After the setup it is possible to use OpenOCD in MSYS2 like a native command. As example to launch openocd for a STM32 Nucleo-64 F401 we can just use the command

It is simple and it is suitable for most of the scenarios. It is also possible to create a standalone version extracting OpenOCD and its dependencies from the folder C:\msys64\mingw32 (or C:\msys64\mingw64 if we are using the 64-bit version).

The only restriction is we are using the last built by MSYS2 maintainers. In certain cases we want to use the current development version and hence we need to build it from source code.

Building OpenOCD from repository

Configuring MSYS2

To compile packages requires additional configuration in MSYS2. First of all we need to update it

repeating this step until it says there are no packages to update.

Note that, every time we updating it is required to restart MSYS2. It is also a good idea to do that when installing big packages like base-devel or mingw-toolchain

We also need to install the base development kit

And the mingw toolchain. If we want to install the 32-bit version the command is

the 64-bit version requires this command

We need to clone the repository of the packages so we need also to install git:

Note that at first use git requires to configure our identity. To do this we must use these commands (obviously using your identity and your email):

As suggested by Kristof (see comments below), the following commands enlarge the memory allocated to git, preventing crashes when cloning large repositories:

Cloning the upstream packages repository

As said, each MSYSTEM has its own package repository. We need to clone the MINGW-packages repository which contains packages for a big number of tools including OpenOCD and its dependencies.

In what follow I will use my home folder (C:/msys64/home/RoccoMarco) as workspace, so I will clone the repository here using the git clone command

If git clone ends successfully, into our home folder we will find a new folder named MINGW-packages. This folder contains a big number of sub-folders. Every subfolder contains a PKGBUILD which is a set of instruction required to build a tool resolving its dependencies.

Building OpenOCD

Now we are able to build OpenOCD from its repository. If we want to build the 32-bit version we have to issue

Otherwise

The procedure would required some time (depending on your PC). When ends in the folder mingw-w64-openocd-git there would be:

  • a folder named pkg containing the built package;
  • a folder named src containing the source code;
  • a tar containing the built package and the source code ready to be installed;

Installing the fresh built OpenOCD

We are now able to install it in MSYS2 using the command

which install OpenOCD from the tar just created. Like for installation from binary this allow to use OpenOCD in MSYS2 as a native command. The difference is we are now using the latest development version of OpenOCD.

Exporting the fresh built OpenOCD as stand alone

If we want to use OpenOCD as standalone, we need to build also its dependencies and copy them in a separate folder with the binary of OpenOCD. If we have installed OpenOCD, these dependencies are already available in the folder C:\msys64\mingw32 (or C:\msys64\mingw64 if we are using the 64-bit version).

By the way, it is quite simple to build them using the same procedure adopted for OpenOCD. We can do this using these commands

Now we have all the pieces to create the stand alone folder. The next commands will copy these pieces into a unique folder rearranging it to obtain something similar to that one released by the OpenOCD team through the Freddie Chopin website.

This will create the standalone folder named openocd in home (in my case C:\msys64\home\RoccoMarco).

Additional notes

How to patch OpenOCD

If we also want to patch the repository a good way could be duplicate the folder /mingw-packages/mingw-w64-openocd-git and edit its PKGBUILD adding some git commands.

As example some time ago I had to add two patches by Rémi PRUD’HOMME to support STM32F746G Discovery and this is how I edited the PKGBUILD

What I have done is more add a printf() in the function pkgver() containing additional git commands.

Read OpenOCD documentation from MSYS2

If we have installed OpenOCD in MSYS2, it is possible to browse the documentation just using the command:

Replies to Building OpenOCD under Windows using MSYS2

  • Thanks for this great article!!! It is so helpful.

    One item of errata: this command:
    >>pacman -S ${MINGW_PACKAGE_PREFIX}-ftdi
    didn’t work for me: package not found error.

    I suppose you instead meant:
    >>pacman -S ${MINGW_PACKAGE_PREFIX}-libftdi

  • I am having difficulty building the first mingw package:

    $ cd ~/mingw-packages/mingw-w64-libusb
    $ makepkg –config ${MAKEPKG_CONFIG} -i
    ==> ERROR: Cannot find the strip binary required for object file stripping.

    I’m running windows 7 here. Don’t know if that would mess it up. I followed all your other instructions carefully.

  • I found a solution to the “missing strip binary” problem. Just run:
    >> export PATH=${PATH}:/mingw64/bin
    The “strip.exe” binary was sitting in /mingw64/bin

  • Hi Rocco,
    I have the following error at the end:

    $ makepkg –config ${MAKEPKG_CONFIG}
    ==> ERROR: conflicts should be an array
    ==> ERROR: provides should be an array

    What should I do?
    Thanks so much for your help

    • Dear, as you can see on top of this article, I have updated it yesterday since I had to rebuilt openocd and I experimented some problems.

      My suggestion is to delete the old files and restart the whole procedure. By the way, if you want to skip the procedure, I have a copy of OpenOCD. I can add it in the download section.

  • Hi Rocco,
    That would be awesome!
    Will your copy work on a 64-bit Windows 10 machine?

    PS: I just did the whole procedure today. So probably your update yesterday didn’t work for my specific case. Anyway, I will be extremely happy to get your executable 🙂

    • Yes, I am using it in ChibiStudio and I have tested it on Win7 32bit and Win7 64bit. I have shared it to the ChibiOS community and I haven’t received any report ’til now. It is now available on the download page in the last section.

  • Hi Rocco.
    Sadly it doesn’t work. I get an error when starting up “openocd.exe”:
    ” The program cannot get started because libgcc_s_dw2-1.dll is missing on your computer. You can possibly solve this issue by reinstalling this program on your computer”.

  • This is how I open it:
    >”C:\Apps\OpenOCD-0.9.0-Win32\bin\openocd” -f “C:\Apps\OpenOCD-0.9.0-Win32\share\openocd\scripts\interface\stlink-v2.cfg” -f “C:\Apps\OpenOCD-0.9.0-Win32\share\openocd\scripts\target\stm32f7x.cfg” -s “C:\Apps\OpenOCD-0.9.0-Win32\share\openocd\scripts”

    I’ve been reading a bit on StackOverflow about this missing “libgcc_s_dw2-1.dll” file. Apparently this issue arises if you’ve built the executable without certain compiler flags. The quickest fix is to find that dll file inside your compiler (I cannot find it in mine), and put it also in the download section. I just need to put that dll in the same folder as the “openocd.exe”, and then it should work 🙂

    Here is the StackOverflow question related to the issue:
    http://stackoverflow.com/questions/4702732/the-program-cant-start-because-libgcc-s-dw2-1-dll-is-missing

    Thank you so much for your help 🙂

    • Take a look to chapter 2.6 of this article. There are commands I use to launch it from bash. Note that according to your environment variables you should or should not use the whole path to launch openocd.exe.

  • Note: I have deleted the original content of my folder “OpenOCD-0.9.0-Win32”, and replaced it with the content from your downloaded zip folder. So I have sort of ‘updated’ my OpenOCD version in a manual way 🙂

  • Although OpenOCD now starts up without errors, and even connects to the chip, I still cannot flash my STM32F7 microcontroller. The error I got initially (with the ‘older’ version of OpenOCD) still persists. I have explained everything in detail on this StackOverflow post: http://stackoverflow.com/questions/38033130/how-to-use-the-gdb-gnu-debugger-and-openocd-for-microcontroller-debugging-fr

    Carmine Noviello, the writer of this book: https://leanpub.com/mastering-stm32
    told me that he got my “.elf” file flashed through GDB – OpenOCD without problem to the STM32F7 microcontroller. He compiled the newest OpenOCD source code on his Macintosh.

    Sadly Windows 10 seems to have some more troubles..

    • Kristof the problem is informations are too… fragmentary. I cannot help you in this way.

      Are you using a toolchain? The toolchain usually provides a gdb server? The toolchain usually uses openocd? What compiler are you using?

      I have always used ChibiStudio (eclipse, arm-none-eabi toolchain, openocd, gcc). I have just used my version on OpenOCD to flash and run a demo on STM32F7 using a pc with Win 10 64bit.

  • Hi Rocco,
    I took on a personal mission to learn the whole embedded chain (compile – link – flash – debug) manually, without any IDE involved. So I’m using the windows cmd terminal to go through every step.
    I have dedicated a detailed StackOverflow question to this procedure. You can find it here:

    http://stackoverflow.com/questions/38033130/how-to-use-the-gdb-gnu-debugger-and-openocd-for-microcontroller-debugging-fr

    Thank you so much for your help. You are really a kind guy!
    I appreciate it so much. I’m sure that the community is grateful to you, for all the interesting articles on your website!

    • While I appreciate a lot your effort, I have a couple of tips if you are resolute to proceed in this way:
      1. Uninstall winzozz!
      2. Destroy every trace of winzozz using fire. Be careful. Use gloves and goggles: safety first!

      3. If you are to ignoring first 2 tips, install an eclipse based toolchain like chibistudio in order to understand if compiler, cross compiler, gdb server and debugger work fine
      4. Try to remove, automatisms one at a time.

      Good luck 🙂

  • Hi, I am trying to follow the steps above, but failed at the following step. Any idea? Please help. Thanks.

    $ makepkg-mingw -sLf
    ==> Making package: mingw-w64-openocd-git 0.10.0.rc1.r1.gc404ff5-1 (15 Nov 2017 23:04:57)
    ==> Checking runtime dependencies...
    ==> Checking buildtime dependencies...
    ==> Retrieving sources...
    -> Updating openocd git repo...
    Fetching origin
    ==> Validating source files with sha256sums...
    openocd ... Skipped
    ==> Extracting sources...
    -> Creating working copy of code git repo...
    Reset branch 'makepkg'
    ==> Starting prepare()...
    which: no libtoolize in (/mingw32/bin:/mingw32/bin:/usr/local/bin:/usr/bin:/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/)
    which: no glibtoolize in (/mingw32/bin:/mingw32/bin:/usr/local/bin:/usr/bin:/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/)
    ./bootstrap: Error: libtool is required
    ==> ERROR: A failure occurred in prepare().
    Aborting...

    • Hi,
      usually I follow this procedure from time to time in order to build a fresh OpenOCD for ChibiStudio. Everytime I usually update this guide which actually is a reminder for my self. I run this procedure last time few month ago and it worked. I am not an expert but I think that maybe the problem is that you do not have installed the base development packages

      pacman -S base-devel

      Let me know if you succeed or we will find a way out together and I will update this guide. Thanks for your feedback.

  • I just used this page again to make a fresh OpenOCD installation. Thanks!
    Please consider adding the following commands at the end of paragraph 4.1:

    # git config –global pack.windowMemory “100m”
    # git config –global pack.packSizeLimit “100m”
    # git config –global pack.threads “1”

    These commands enlarge the memory allocated to git, preventing crashes when cloning large repositories.

Leave a Reply