Search code examples
makefilegnu-make

Makefile: What does $(VARIABLE:/text=) mean?


Can someone explain to me what the following variable declaration (for MY_DIR) means please?

MY_CLOUD = $(shell .......)
MY_DIR ?= $(MY_CLOUD:/cloud=)

Specifically after the ?=, which I understand to mean "set MY_DIR if it does not already hold a value".

I cannot seem to find the documentation on this, but I assumed it to mean "set to the value of MY_CLOUD if it not empty, otherwise set to /cloud", but this doesn't seem to ring true in practice. I don't get the =.


Solution

  • MY_DIR ?= $(MY_CLOUD:/cloud=)
    

    There are two parts here:

    • $(MY_CLOUD:/cloud=) is a substitution. This is taking the variable $(MY_CLOUD) and replacing /cloud with nothing.

    • MY_DIR ?= expr is a conditional assignment. It is defined as:

      ifeq ($(origin MY_DIR), undefined)
        MY_DIR = expr
      endif
      

      but obviously much shorter.

    Put together, if MY_DIR wasn't already defined, this assigns it to the value of MY_CLOUD after removing all instances of /cloud