get a default value when variable is unset in make

后端 未结 4 1886
面向向阳花
面向向阳花 2020-12-31 09:10

(edit: question more accurate based on @Michael feedback)

In bash, I often use parameter expansion: the following commands print \"default value\" when

4条回答
  •  自闭症患者
    2020-12-31 09:38

    I have a similar case where the result of filtering a shell command could be a single word or empty string. When empty, it should fallback to the default word. In the example below APPLE_LINUX will be 'apple' on macOS or 'linux' on other platforms. MSG will be set to the message for the appropriate platform. The example intentionality avoids using ifeq.

    MACHINE         := $(shell $(COMPILE.cpp) -dumpmachine)
    MACHINE_APPLE   := $(findstring apple,$(MACHINE))
    APPLE_LINUX     := $(firstword $(MACHINE_APPLE) linux)
    apple.MSG       := You are building on macOS
    linux.MSG       := You are building on Linux or another OS
    MSG             := $($(APPLE_LINUX).MSG)
    

提交回复
热议问题