How to use "Open folder" tasks to run custom mingw build / cmake script?

CR 136 Reputation points
2020-10-28T23:41:06.603+00:00

Hello,

I just started looking into Visual Studio, since there will be C++20 support soon and my current IDE (Eclipse) seems to have no plans to update anytime soon .. even some C++17 features are still broken. I have a few related questions so I'll use bold to mark them.

I usually do not like the IDE handling my build tools for me. Instead, I handle my build tools in scripts completely outside the IDE to not depend on IDE-specific solutions. Then at most, the scripts are platform dependent. However, for ease of use I would still like to be able to run the scripts from withing the IDE to avoid many console windows etc. My current IDE handles this by running the toolchain shell internally and taking any custom build command that the user supplies, which is then run inside that shell.

  • To my understanding so far, this is not possible in Visual Studio? At best, it seems I can use custom tasks to run the full shell command from a Windows shell environment.

To be more specific, one environment I use is MinGW with MSYS2 so I am trying to create an "Open folder" setup in Visual Studio for this. I added a CppProperties.json as explained in the reference and so far I think IntelliSense works. However I am having trouble running my build script with custom tasks. My tasks.vs.json file is:

{
  "version": "0.2.1",
  "tasks": [
    {
      "inheritEnvironments": [
        "mingw_64"
      ],
      "taskLabel": "Create Release build",
      "appliesTo": "mk",
      "type": "launch",
      "workingDirectory": "C:\\msys64\\usr\\bin",
      "command": "\"bash.exe\"",
      "args": [
        "--login -c \"",
        "cd '${projectRoot}'; ",
        "./mk build",
        "\""
      ]
    }
  ]

I had to do some trial and error here ..

  • My ${projectRoot} path has spaces in it. Visual Studio does not seem to be able to handle this, giving an error when trying to run the command. For that reason, I changed the workingDirectory here..
  • For some reason unclear to me, I have to add additional literal quotes around the command..
  • Since I cannot run the command from inside the custom shell (? see my question above), escaping special characters and quoting becomes very cumbersome..

If I run the command I defined here inside powershell (i.e. from outside VS), my script (which internally calls cmake) runs correctly. However, when I try to run this task inside VS, CMake gives an error

 -- The CXX compiler identification is unknown

  CMake Error at CMakeLists.txt:2 (project):
    No CMAKE_CXX_COMPILER could be found.

Looking at the ErrorLog:

ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\CL.exe /c /nologo /W0 /WX- ....

I think it finds the MSVC compiler first, instead of gcc, which fails to compile the test in mingw. I'm not sure how mingw finds this compiler, my PATH in mingw does not include any VS or MSVC paths. It also does not inherit Windows environment variables .. like I already noted, the same command works when called from Powershell directly..

So I guess my main question is: How do I "unset" VS PATHs when running custom commands, so that I have a clean environment in my custom toolchain, just as if I were to run it from inside the given shell?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,683 questions
{count} votes

Accepted answer
  1. CR 136 Reputation points
    2020-10-29T17:22:12.587+00:00

    First, thanks for the pointer. However after looking through these CMakeSettings references, it did not look like what I wanted. This seems to be more about having VS run cmake with the correct settings. However, I do not want to have VS run cmake. Cmake is only run from within my bash script, which is in the MinGW environment. It also does other things beside running cmake.

    If I were to only use CMake directly without bash script helpers, then maybe this would be the right approach, though it looks a bit like doing everything twice to me. If I wanted to e.g. also compile outside VS from say windows shell. Why write the settings again in a CMakeSettings.json specifically for VS, when I already have all settings outside VS? On the other hand, if I were to do the settings only in VS, then compiling outside VS would not be easily possible .. which I very much want to avoid. Imho code should be independent from IDE tools, including the compilation process. Having to deal with possibly different build systems is already enough of a nightmare :)

    Second, I'm sorry - it seems something in my question is not correct, as today the command did not run correctly in Powershell anymore. I'm not sure what is different now, as I made sure it ran while typing the post .. maybe I forgot to hit save somewhere or similar ..

    But this helped me identify the issue, which was to simply set CMAKE_GENERATOR to "MSYS Makefiles". It seems cmake will automatically check for MSVC first if VS is installed, which is probably the reason why I did not need to set this before I installed VS. Then again it somehow worked without setting it yesterday, so this is only a guess .. For now everything works.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.