How to detect if the CMake script is being run in MSYS2 environment?
Asked Answered
C

5

6

I want to detect if the CMakeLists.txt is being summoned inside an MSYS2 environment/shell:

$ uname
MSYS_NT-10.0-18363

from here I have tried:

cmake_minimum_required(VERSION 3.10)

project(test_cmake)

if(MSYS) # yes I have also tried MSYS2 with the same result!
    message("This is MSYS")
else()
    message("This is not MSYS")
endif()

which returns the same result This is not MSYS both in Windows cmd and MSYS2 shell. I'm not actually surprised because MSYS2 and MSYS are two different beasts! Also from here, I tried:

message(${CMAKE_SYSTEM_NAME})

which both in cmd and MSYS2 returns Windows! I would appreciate it if you could help me know how I can detect if the cmake command is being run from inside an MSYS2 shell. Thanks for your support in advance.

P.S.1. As a personal note for myself, this and this seem like good sources to look into.

P.S.2 Strangely enough the command

message(${CMAKE_HOST_SYSTEM_NAME})

also returns Windows while on my MSYS2, according to the docuemntation, it must return the result of uname -s!

Complexioned answered 21/3, 2020 at 23:49 Comment(0)
C
2

OK, I think I know where the problem comes from. As I also had other issues earlier, the mingw64/mingw-w64-x86_64-cmake caused this problem. So I uninstalled it and installed the msys/cmake package. Now:

  • if(MSYS) retruns TRUE
  • message(${CMAKE_SYSTEM_NAME}) --> MSYS
  • message(${CMAKE_SYSTEM}) --> MSYS-3.0.7-338.x86_64
  • message(${CMAKE_HOST_SYSTEM_NAME}) --> MSYS

one caveat is that now you have to specify the path to the make tool by -D CMAKE_MAKE_PROGRAM:path=mingw32-make

Complexioned answered 22/3, 2020 at 11:54 Comment(0)
K
1

I think there should be a better answer, but you can use the output from uname you presented:

execute_process(COMMAND uname OUTPUT_VARIABLE uname)
if (uname MATCHES "^MSYS")
Kyle answered 22/3, 2020 at 0:41 Comment(4)
still not detecting as uname MATCHES "^MSYS" seem to return false. would you please expand your if-statement? I have a suspicion that I don't know how to write that part.Complexioned
I'm also concerned if we can distinguish between MSYS and MSYS2 given that the uname results don't say anything about being MSYS2. We might need to parse the whole return value.Complexioned
It works for me, as in uname outputs Linux and I substitute "^MSYS" for "^Lin" and it matches successfully. So parse the whole output. How would you detect if you are on MSYS and on MSYS2?Kyle
uname on MSYS2 actually gives MINGW64_NT-10.0-19043!Punjab
V
1

What I found most reliable, considering all answers and comments up to now, and building upon them, is using

execute_process(COMMAND uname OUTPUT_VARIABLE uname)
if (uname MATCHES "^MSYS" OR uname MATCHES "^MINGW")
    message("This is MSYS")
else()
    message("This is not MSYS")
endif()

In my msys2, this is what I see in cmake

message("a ${MSYS}")                    --> prints a, MSYS does not exist 
message("a ${CMAKE_SYSTEM_NAME}")       --> prints a, CMAKE_SYSTEM_NAME does not exist 
message("a ${CMAKE_SYSTEM}")            --> prints a, CMAKE_SYSTEM does not exist 
message("a ${CMAKE_HOST_SYSTEM_NAME}")  --> prints a MSYS_NT-10.0-19044-WOW64, could be used

and

$ uname
MSYS_NT-10.0-19044-WOW64
Voroshilov answered 9/2, 2022 at 11:2 Comment(0)
A
1

With MSYS2 default installation, when running MSYS2 MinGW x64, I have:

$ uname
MINGW64_NT-10.0-19042

So we can simply use https://cmake.org/cmake/help/latest/variable/MINGW.html

if (MINGW)
   message("This is MSYS2")
else()
    message("This is not MSYS2")
endif()
Amoakuh answered 9/3, 2022 at 16:7 Comment(0)
A
1

in msys2/msys (pacman -S cmake) these cmake variables are True:

MSYS
CYGWIN
UNIX

${CMAKE_SYSTEM_NAME} is MSYS.


in msys2/ucrt64 (pacman -S mingw-w64-ucrt-x86_64-cmake) these cmake variables are True:

MINGW
WIN32

${CMAKE_SYSTEM_NAME} is Windows.

Alleged answered 27/5 at 8:23 Comment(2)
How about the mingw ones?Complexioned
@FoadS.Farimani I took ucrt64 as an example of the mingw ones. I didn't test all of them, but at least, in ucrt64, the variable of MINGW is true, and I think it's good enough to detect the environment.Alleged

© 2022 - 2024 — McMap. All rights reserved.