Meson with MSVC on GitHub Actions
The Meson Build System provides support for running on Microsoft Windows, including support for Microsoft Visual Studio C++. GitHub Actions provides public access to CI machines running Microsoft Windows. But trying to tie both together is not as straightforward as it sounds.
Sometimes you stumble over a task you never thought you have to deal with. This story is about one of those times. In particular, I was faced with running CI tests for a simple C library on Microsoft Visual Studio C++ (MSVC). Gladly, GitHub already provides simple access to machines running Microsoft Windows Server 2016 and 2019, so this sounded like a straightforward task. Unfortunately, my infinite ignorance of anything Windows made this harder than it should have been.
The root of this problem is that the Meson Build System needs to run in the MSVC Developer Shell. This shell has all the necessary environment variables prepared for a particular install of MSVC. Since you can have multiple versions installed in parallel, Meson cannot know which install to use if run outside of such a shell. Unfortunately, GitHub Actions has no simple way to enter this shell. Therefore, running Meson on GitHub Actions will end up using GCC rather than MSVC, since this is what it detects by default in the GitHub Actions Environment. This is not what we wanted, so adjustments are needed.
Luckily, Microsoft provides a tool called
which finds MSVC installs on a Windows system. We can use this to find the
required setup scripts and then import the environment variables into our
GitHub Actions setup. This tool is pre-deployed on GitHub Actions, so we can
simply invoke it to find a suitable MSVC install. From there on, we look for
DevShell.dll, which provides the required integration. We load it into
PowerShell and invoke the provided
Enter-VsDevShell function. By comparing
our own environment variables before and after that call, we can extract the
changes and export them into the GitHub Actions environment. Thus,
the following workflow-steps will have access to those variables as well.
I plugged this into a re-usable GitHub Action using the new composite type. To use it in a GitHub Actions workflow, simply use:
- name: Prepare MSVC uses: bus1/cabuild/action/msdevshell@v1 with: architecture: x64
This queries the MSVC environment and exports it to your GitHub Actions job. Following steps will thus run as if in an MSVC Developer Shell. A full example is appended at the bottom, which shows how to get Meson to compile and test a project on MSVC for both Windows Server 2016 and 2019.
If you rather import the code into your own project, you can find it on GitHub. Note that this uses PowerShell syntax, so it might look alien to linux developers.
While this is only roughly 50 lines of PowerShell scripting, it still feels a bit too hacky. The Meson developers are aware of this, but so far no patches have found their way upstream. Lets hope that this workaround will one day be obsolete and Meson invokes vswhere itself.
Following a full example workflow:
name: Continuous Integration on: [push, pull_request] jobs: ci-msvc: name: CI with MSVC runs-on: $ strategy: matrix: os: [windows-2016, windows-latest] steps: - name: Fetch Sources uses: actions/checkout@v2 - name: Setup Python uses: actions/setup-python@v2 with: python-version: '3.x' - name: Install Python Dependencies run: pip install meson ninja - name: Prepare MSVC uses: bus1/cabuild/action/msdevshell@v1 with: architecture: x64 - name: Prepare Build run: meson setup build - name: Run Build run: meson compile -v -C build - name: Run Test Suite run: meson test -v -C build