The problem arises when makefile needs be run on different OS and various setting should be properly set up (escaping, path separator, etc) depending on OS. The first approach was to use Windows COMSPEC:
ifneq ($(COMSPEC)$(ComSpec),)
## in windows
else
## in linux
endif
This is false positive for Cygwin, because it sees Windows' environment variables and detects Cygwin as Windows. Then we tried Linux PWD:
ifeq ($(PWD),)
## in windows
else
## in linux, cygwin
endif
However, as a result of integration of off-site tool we have PWD set in windows (one of the perl's modules). So, the detection fails again.
I wonder, what is the best approach to differentiate between Cygwin, Linux, Windows using environment variables?
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.
来源:https://stackoverflow.com/questions/3104410/identify-cygwin-linux-windows-using-environment-variables