使用环境变量识别 Cygwin、Linux、Windows

发布于 2024-09-07 06:46:06 字数 476 浏览 4 评论 0原文

当 makefile 需要在不同的操作系统上运行并且应根据操作系统正确设置各种设置(转义、路径分隔符等)时,就会出现问题。 第一种方法是使用 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 的最佳方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蛮可爱 2024-09-14 06:46:06

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

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

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.

似最初 2024-09-14 06:46:06

按照 pkh 的建议,使用 SHELL 区分 Windows/非 Windows 对我来说不起作用。看来 SHELL 变量是在 gmake 运行的 makefile 中定义的(我的版本是 3.81),它等于“sh.exe”。因此,我当前的工作解决方案是通过 .exe Windows 可执行文件扩展名来扩展 pkh 的想法:

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

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
梦里的微风 2024-09-14 06:46:06

在 Cygwin 上,需要导出 OSTYPE 环境变量才能让 make 看到它。

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

尐籹人 2024-09-14 06:46:06

假设您的所有机器上都有 gcc 可用(即您正在使用 makefile 编译某些内容),您可以使用

gcc -dumpmachine

它来找出 gcc 构建的操作系统。

您可以使用输出来设置一些变量,例如 WINDOWSLINUX 或直接存储它以便使用该信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文