How to check that the current compiler is GCC/MSYS when using Ninja generator?

@brad.king

Based on what you said in this issue:

If you want to build for the cygwin- or msys2-runtime libraries, instead of MinGW, you need to use a CMake that’s built for them too. MSYS2 and Cygwin both have packages for that.

And it actually worked. The CMAKE_SYSTEM_NAME will be evaluated to “MSYS” when using the CMake (/c/msys64/usr/bin/cmake.exe) installed on MSYS.

However, I wondered how to judge the GCC currently used is MSYS2-based when using the CMake (C:\Program Files\CMake\bin\cmake.exe) installed on Windows?

Prerequisites

pacman -S base-devel
pacman -S gcc
pacman -S cmake
pacman -S ninja

Demonstration

  • Example CMakeLists.txt

    cmake_minimum_required(VERSION 3.20)
    project(cmake-windows LANGUAGES C CXX)
    message("CMAKE_COMMAND = ${CMAKE_COMMAND}")
    message("CMAKE_SYSTEM_NAME = ${CMAKE_SYSTEM_NAME}")
    
  • Example CMakePresets.json

    Click to expand
    {
      "version": 4,
      "configurePresets": [
        {
          "name": "msys2-gcc-x64-ninja-debug",
          "displayName": "MSYS2 GCC x64 (Ninja) Debug",
          "description": "Using GCC x64 compiler with \"Ninja\" geneartor on MSYS2 - Debug",
          "generator": "Ninja",
          "binaryDir": "${sourceDir}/build/${presetName}",
          "installDir": "${sourceDir}/install/${presetName}",
          "environment": {
            "PATH": "/c/msys64/usr/bin:$penv{PATH}"
          },
          "cacheVariables": {
            "CMAKE_C_COMPILER": "gcc.exe",
            "CMAKE_CXX_COMPILER": "g++.exe",
            "CMAKE_BUILD_TYPE": "Debug"
          }
        }
      ]
    }
    
  • Example Terminal Log on MSYS shell

    Click to expand
    hwhsu1231@vvb-windows MSYS /f/GitRepo/cmake-windows-preset
    $ cmake --preset msys2-gcc-x64-ninja-debug
    Preset CMake variables:
    
      CMAKE_BUILD_TYPE="Debug"
      CMAKE_CXX_COMPILER="g++.exe"
      CMAKE_C_COMPILER="gcc.exe"
      CMAKE_INSTALL_PREFIX:PATH="/f/GitRepo/cmake-windows-preset/install/msys2-gcc-x64-ninja-debug"
    
    Preset environment variables:
    
      PATH="/c/msys64/usr/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl"
    
    -- The C compiler identification is GNU 11.3.0
    -- The CXX compiler identification is GNU 11.3.0
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working C compiler: /c/msys64/usr/bin/gcc.exe - skipped
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Check for working CXX compiler: /c/msys64/usr/bin/g++.exe - skipped
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    CMAKE_COMMAND = /usr/bin/cmake.exe
    CMAKE_SYSTEM_NAME = MSYS
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /f/GitRepo/cmake-windows-preset/build/msys2-gcc-x64-ninja-debug