2016-11-22 41 views
1

如何检测gnu make将使用的shell?我希望我的化妆要在以下平台上运行:如何检测GNU make中使用的shell?

  • Linux的
  • 窗口
  • 窗户cygwin的(请注意,在使用Cygwin的让Windows使用cygwin外壳)

然后我会喜欢在操作系统上独立检测gcc是否存在于系统中。

回答

1

到目前为止,我想出了这个解决方案:

# detect what shell is used 
ifeq ($(findstring cmd.exe,$(SHELL)),cmd.exe) 
$(info "shell Windows cmd.exe") 
DEVNUL := NUL 
WHICH := where 
else 
$(info "shell Bash") 
DEVNUL := /dev/null 
WHICH := which 
endif 

# detect platform independently if gcc is installed 
ifeq ($(shell ${WHICH} gcc 2>${DEVNUL}),) 
$(error "gcc is not in your system PATH") 
else 
$(info "gcc found") 
endif 

可选,当我需要检测更多的工具可以使用:

EXECUTABLES = ls dd 
K := $(foreach myTestCommand,$(EXECUTABLES),\ 
     $(if $(shell ${WHICH} $(myTestCommand) 2>${DEVNUL}),\ 
      $(myTestCommand) found,\ 
      $(error "No $(myTestCommand) in PATH))) 
$(info ${K})   
+0

只要是明确的,“没有cmd.exe的”不不暗示bash。这可能意味着POSIX sh,但这不是一回事。其次,如果你想知道GCC是否可用,为什么不运行它,看看它是否失败:'ifneq(,$(shell gcc --version))'? – MadScientist

+0

我知道可能有其他的shell。但是通常情况下,linux shell的行为非常相似。至少在支持我正在使用的'which'和'/ dev/null'方面。至于不只是做'ifneq(,$(shell gcc --version))',那是因为我确实想要疯狂地发布BFU。例如,我会得到类似于:'process_begin:CreateProcess(NULL,gccx --version,...)失败。 make:M:32:pipe:坏文件描述符。实际上,当我在使用一些废话自动消息给我留下一些简陋的语言时告诉我什么是错误的时候,我会变得过敏... –

+1

很好,但实际上bash有许多POSIX sh中没有的额外功能, make总是运行POSIX sh,绝不bash(除非你设置了SHELL变量)。人们使用bash扩展编写make食谱是非常普遍的,然后不能理解为什么他们不工作。所以,最好清楚这些事情。 – MadScientist