Makefile set if variable is empty

前端 未结 6 474
暖寄归人
暖寄归人 2021-01-31 01:37

I want to set a variable if it is empty. I tried in this way:

....
TEST := $(something)
...
TEST ?= $(something else)

The first $(somethi

6条回答
  •  心在旅途
    2021-01-31 01:51

    Any elegant solution to set the variable if empty?

    GNU make is hardly known for elegant solutions. Unless you find trapdoors and minefields to be elegant. I know only of the two ways to accomplish what you want:

    1. The standard ifeq/endif solution:

      ifeq ($(TEST),)
      TEST := $(something else)
      endif
      
    2. Use the $(if) function:

      TEST := $(if $(TEST),$(TEST),$(something else))
      

      One can try to package that construct into a function too, but that is inadvisable. The function would have the hidden pitfall of occasionally breaking the $(something else) if it contains the , (for which there are only wayward workarounds). (The built-in functions like $(if) are immune to the , bug.)

    Elegance test is up to you.

提交回复
热议问题