ariya.io About Talks Articles

On GitHub Actions with MSYS2

3 min read

Thanks to the complete GitHub Actions for MSYS2, it is easier than ever to construct a continuous integration setup for building with compilers and toolchains which can run on MSYS2.

The details are available on the official page, github.com/marketplace/actions/setup-msys2. However, perhaps it is best illustrated with a simple but concrete example. As usual, for this illustration, you will see the use of this simplistic Hello, world program in ANSI C. To follow along, check out its repository at github.com/ariya/hello-c90.

Let us take a look at this workflow setup to build this C program with GCC on MSYS2 (on Windows, obviously):

name: amd64_windows_gcc
jobs:
  amd64_windows_gcc:
    runs-on: windows-2019
    defaults:
      run:
        shell: msys2 {0}
    steps:
    - uses: actions/checkout@v2
    - uses: msys2/setup-msys2@v2
      with:
        install: gcc make
    - run: gcc -v
    - run: make CC=gcc
    - run: file ./hello.exe
    - run: ./hello

The important lines are for the setup-msys2 section. The install value allows an easy selection of various packages which shall be installed before proceeding to the next step. For this purpose, it is sufficient to install gcc and make, but YMMV.

The rest is self-explanatory. Please note also the defaults section earlier, this is convenient to set the default shell so that we do not need to explicitly call it for every single run thereafter.

Now let us come up with another variant, this time for Clang instead of GCC (read also my previous post: Clang for Windows)

name: amd64_windows_clang
jobs:
  amd64_windows_clang:
    runs-on: windows-2019
    defaults:
      run:
        shell: msys2 {0}
    steps:
    - uses: actions/checkout@v2
    - uses: msys2/setup-msys2@v2
      with:
        install: make mingw-w64-x86_64-clang
    - run: clang --version
    - run: make CC=clang
    - run: file ./hello.exe
    - run: ./hello

Pretty straightforward, isn’t it? We just change the package to be installed and the compiler to be used. Since the two YAML files are very similar, to avoid a lot of repeated steps, we can parametrize it as follows. This is basically taking advantage of the matrix strategy feature of GitHub Actions.

  amd64_windows:
    runs-on: windows-2019
    strategy:
      matrix:
        compiler: [gcc, clang]
    defaults:
      run:
        shell: msys2 {0}
    steps:
    - uses: actions/checkout@v2
    - uses: msys2/setup-msys2@v2
    - run: pacman --noconfirm -S make gcc
      if: ${{ matrix.compiler == 'gcc' }}
    - run: pacman --noconfirm -S make mingw-w64-x86_64-clang
      if: ${{ matrix.compiler == 'clang' }}
    - run: ${{ matrix.compiler }} --version
    - run: make CC=${{ matrix.compiler }}
    - run: file ./hello.exe
    - run: ./hello

To take it one step further, we can also support both i686 and AMD64 platforms in the same YML, again by parametrizing the architecture. Here is how it looks:

name: windows
jobs:
  windows:
    runs-on: windows-2019
    strategy:
      matrix:
        compiler: [gcc, clang]
        msystem: [MINGW32, MINGW64]
    defaults:
      run:
        shell: msys2 {0}
    steps:
    - uses: actions/checkout@v2
    - uses: msys2/setup-msys2@v2
      with:
        msystem: ${{ matrix.msystem }}
        install: make
    - run: pacman --noconfirm -S gcc
      if: ${{ matrix.compiler == 'gcc' }}
    - run: pacman --noconfirm -S mingw-w64-x86_64-clang
      if: ${{ (matrix.msystem == 'MINGW64') && (matrix.compiler == 'clang') }}
    - run: pacman --noconfirm -S mingw-w64-i686-clang
      if: ${{ (matrix.msystem == 'MINGW32') && (matrix.compiler == 'clang') }}
    - run: ${{ matrix.compiler }} --version
    - run: make CC=${{ matrix.compiler }}
    - run: file ./hello.exe
    - run: ./hello

That’s all 4 combinations, 32-bit and 64-bit, for each GCC and Clang, in a simple configuration!

Related posts:

♡ this article? Explore more articles and follow me Twitter.

Share this on Twitter Facebook

comments powered by Disqus