使用环境变量识别Cygwin,Linux,Windows [英] Identify Cygwin, Linux, Windows using environment variables

查看:236
本文介绍了使用环境变量识别Cygwin,Linux,Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当需要在不同的OS上运行makefile并根据OS正确设置各种设置(转义,路径分隔符等)时,就会出现问题. 第一种方法是使用Windows COMSPEC:

ifneq ($(COMSPEC)$(ComSpec),)
## in windows
else 
## in linux
endif

这对Cygwin来说是假阳性,因为它可以看到Windows的环境变量并将Cygwin检测为Windows. 然后我们尝试了Linux PWD:

ifeq ($(PWD),)
## in windows
else 
## in linux, cygwin
endif

但是,由于集成了非现场工具,因此我们在Windows(perl的模块之一)中设置了PWD.因此,检测再次失败.

我想知道,使用环境变量区分Cygwin,Linux和Windows的最佳方法是什么?

解决方案

Cygwin和(在Ubuntu上测试)Linux提供了$OSTYPE环境变量,对于Cygwin设置为cygwin,对于(Ubuntu)Linux设置为linux-gnu.

Windows没有此变量,因此它似乎是唯一需要的变量.我想您的 Linux可能不提供它,在这种情况下,您可以使用$OSTYPE来区分Windows和Cygwin,然后在Cygwin与Linux之间退回到uname. /p>

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.

这篇关于使用环境变量识别Cygwin,Linux,Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆