views:

48

answers:

3

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?

+1  A: 

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.

pkh
Unfortunately, SHELL is defined in makefile. See my answer.
Pmod
+1 since your solution is the best for the present
Pmod
Updated with what appears to be an answer that suits your needs fully.
pkh
This solution seems the most suitable according to the question - accept it.PSSeems to be Windows can be distinguished by presence of $OS environment variable.
Pmod
A: 

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
Pmod
A: 

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.

Veger
I use another compiler (propitiatory one), checked its help and didn't find any similar option. Actually, the question is about using environment variables and make operations. Anyway, thank you, it may help in case we switch to gcc.
Pmod
I've just had a thought, that even we do not use gcc as a compiler, we may use it only for this reason - "identifying OS". Doesn't it seem weird to use a compiler to detect OS. For Windows it shows "mingw32" and for Cygwin "i686-pc-cygwin" on my site. So, I wonder how vary this ouput could be and would it be problematic to distinguish by this output?
Pmod
Well, you could (un)set variables like `WINDOWS` or `LINUX` for known outcomes of the `gcc -dumpmachine` command, ie `mingw32` and `i686-pc-cygwin` would set the `WINDOWS` variable and something like `x86_64-linux-gnu` sets the `LINUX` variable. Of course you could look for sub-strings to ease this process. After that you could use these variables in the rest of your makefile(s).
Veger