Identify Cygwin, Linux, Windows using environment variables

一曲冷凌霜 提交于 2019-12-04 15:58:31
pkh

Cygwin and (tested on Ubuntu) Linux provide an $OSTYPE environment variable, set to cygwin for Cygwin and linux-gnu for (Ubuntu) Linux.

Windows does not have this variable, and so it appears to be the only one you'll need. I suppose it's possible that your Linux doesn't provide it, in which case you can use $OSTYPE to distinguish between Windows and Cygwin and then fall back to uname for Cygwin vs. Linux.

Distinguishing between Windows/not Windows using SHELL is not working for me as proposed by pkh. It's appeared that SHELL variable is defined in makefile running by gmake (mine is ver. 3.81) and it equals to "sh.exe". So, the current working solution for me is extend pkh's idea with distinguishing by .exe Windows executable file extension:

ifneq ($(findstring .exe,$(SHELL)),)
    $(warning In Windows)
else
    $(warning In Linux/Cygwin)
endif

Assuming you do have gcc available on all your machines (ie you are compiling something using your makefiles), you could use

gcc -dumpmachine

to find out the OS for which gcc builds.

You could use the output to set some variables like WINDOWS, LINUX or store it directly in order to use the information.

On Cygwin the OSTYPE environment variable needs to be exported for make to see it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!