Search code examples
mingwvisual-studio-2019msys2

How to get Visual Studio to execute build commands in a msys2 shell?


I managed to build and debug my C++ (mingw) project with Visual Studio Code. I want to be able to do it with Visual Studio 2019 (as discussed here). To do this, it seems I need to run the build commands in a different shell. This is the config I'm using for VS Code:

in .vscode/settings.json:

{
    "terminal.integrated.shell.windows": "C:\\tools\\msys64\\usr\\bin\\bash.exe",
    "terminal.integrated.shellArgs.windows": [
        "--login",
        "-i"
    ],
    "terminal.integrated.env.windows": {
        "MSYSTEM": "MINGW64",
        "CHERE_INVOKING":"1"
    }
}

in .vscode/tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "build",
            "command": "sh",
            "args": [
                "build.sh"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

This works really well. My build.sh script executes various commands to compile my project.

But I want to be able to develop my project in Visual Studio 2019 (not Visual Studio Code), since VS Code is still lacking in a couple of "must-have" C++ development features.

But my problem is that I don't understand how I can make Visual Studio execute the build commands in a different shell like VS Code does. So the build commands don't work, since they are not executed in the msys2, mingw64 environment.

This i my .vs/tasks.vs.json file:

{
  "version": "0.2.1",
  "tasks": [
    {
      "taskLabel": "build",
      "appliesTo": "/",
      "type": "launch",
      "command": "sh",
      "args": ["build.sh"],
      "env": "Mingw64",
      "customLaunchCommand": "C:\\tools\\msys64\\usr\\bin\\bash.exe",
      "customLaunchCommandArgs": [
        "--login",
        "-i"
      ]
    }
  ]
}

This config file was my best bet on how to make the sh build.sh command be executed in the shell opened with the command C:\tools\msys64\usr\bin\bash.exe --login -i.

Can someone tell me how to make Visual Studio 2019 execute the build commands in a msys2 shell, like VS Code does?

EDIT:

So maybe I'm getting closer with this, and maybe not. Following the examples in the Visual Studio 2019 docs, my .vs/CppProperties.json now looks like this:

{
  "configurations": [
    {
      "inheritEnvironments": [
        "mingw_64"
      ],
      "name": "Mingw64",
      "includePath": [
        "${workspaceRoot}\\**"
      ],
      "intelliSenseMode": "linux-gcc-x64",
      "environments": [
        {
          "MINGW_PREFIX": "C:\\tools\\msys64",
          "MINGW_CHOST ": "x86_64-w64-mingw32",
          "MINGW_PACKAGE_PREFIX": "mingw-w64-x86_64",
          "MSYSTEM": "MINGW64",
          "MSYSTEM_CARCH": "x64_64",
          "MSYSTEM_PREFIX": "${env.MINGW_PREFIX}",
          "SHELL": "${env.MINGW_PREFIX}\\usr\\bin\\bash.exe --login -i",
          "TEMP": "${env.MINGW_PREFIX}/../tmp",
          "TMP": "${env.TEMP}"
        }
      ]
    }
  ]
}

And my .vs/tasks.vs.json:

{
  "version": "0.2.1",
  "tasks": [
    {
      "taskLabel": "build",
      "appliesTo": "build.sh",
      "contextType": "build",
      "type": "default",
      "command": "sh",
      "args": [
        "build.sh"
      ],
      "inheritEnvironments": [ "Mingw64" ]
    }
  ]
}

But still, it appears Visual Studio still does not use the msys2 shell to execute the build task. How to actually use a "configuration" defined in the CppProperties.json file is a mystery to me...


Solution

  • This is one way to do it:

    // .vs/tasks.vs.json
    {
      "version": "0.2.1",
      "tasks": [
        {
          "taskLabel": "build",
          "appliesTo": "build.sh",
          "contextType": "build",
          "type": "launch",
          "command": "bash.exe",
          "args": [
            "--login",
            "-c",
            "\"sh build.sh\""
          ],
          "env": {
            "PATH": "C:\\tools\\msys64\\usr\\bin",
            "CHERE_INVOKING": "1",
            "MSYSTEM": "MINGW64"
          }
        }
      ]
    }