30

From the docs:

$(patsubst PATTERN,REPLACEMENT,TEXT)

Finds whitespace-separated words in TEXT that match PATTERN and replaces them with REPLACEMENT. Here PATTERN may contain a % which acts as a wildcard, matching any number of any characters within a word.
...
Whitespace between words is folded into single space characters; leading and trailing whitespace is discarded.



Now, given a makefile, is:

# The pattern for patsubst, does NOT contain '%'
foo := $(patsubst  x,y,x    x    x)
# The pattern for patsubst, does contain '%'
bar := $(patsubst x%,y,x    x    x)


# The variable 'foo', is a result from a patsubst-pattern, that did NOT contain a '%'
# The variable 'bar', is a result from a patsubst-pattern, that did contain a '%'
all ::
    @echo 'foo is: "$(foo)"'
    @echo 'bar is: "$(bar)"'



Executing, we get:

foo is: "y    y    y"
bar is: "y y y"



So, it is obvious, that Make, may or may not "fold" all whitespace into one and single whitespace.

Or, did I do something wrong.

1 Answer 1

30

In fact all is explained in the doc:

Finds whitespace-separated words in TEXT ...

means that one or more spaces have to separate the words.

... that match PATTERN ...

means that it select only words that match a pattern (which can include some spaces).

... and replaces them with REPLACEMENT.

means that the selected patterns will be replace by a replacement.


A picture is worth a thousand words.

For PATTERN = X:

           +----  SEPARATORS  ----+
           |                      |
   +-------+-------+     +--------+------+
   |               |     |               | 
X  space space space  X  space space space  x
|                     |                     |
+---------------------+---------------------+
                      |
                   PATTERNS

For PATTERN = X%:

                 +----  SEPARATORS  ---+
                 |                     |
               +-+-+                 +-+-+
               |   |                 |   | 
X  space space space  X  space space space  x
|            |        |            |        |
+------+-----+        +------+-----+        |
       |                     |              |
       +---  PATTERNS  ------+--------------+

Interesting thing:

When you use the % character in your pattern, you can re-use it in the replacement, like this:

$(patsubst x%,y%,xa xb xc)
# Will be "ya yb yc"

But when you have space character in the % variable, make will strip them in the replacement.

$(patsubst x%,y%,xa   xb   xc)
# Will also be "ya yb yc"

EDIT: After reading the source code, the interesting things are:

So here is the behavior:

  1. If no % in the pattern, this is a simple substitution (which keep the spaces).
  2. Else it split the text by words and get rid of all spaces (using the isblank function).
  3. Finally, it does the replacement
4
  • 1
    Yes...Your final paragraph. It's easy to show, that contrary to pattern-rules and pattern-specific variables, Make accepts an empty string, as a match for %. Yes, %-matching rules, are not consistent across features of Make. And, can we both agree, that % matched (in my example) the empty-string, and only it. You sort of proved it :)
    – Ji Cha
    Aug 24, 2015 at 10:18
  • @JiCha To be clear, in your example % matched space space in the input string. And will effectively by replaced by the empty-string in the output string. Aug 24, 2015 at 11:21
  • I strongly disagree. The rules for $(patsubst pattern,replacement,text) when there is a % in the pattern, are: 1)If text is empty OR rendered empty after variable-expansion, then, the function expands - always - to an empty string. 2) If, text is non-empty, then if it contains WS, then first split up words, effectively getting rid of all white-space, and then do - per each argument - the pattern-matching, where the matching rules do not require the % to match a non-empty string. Yes, it semantics, but it matters, as proven in last paragraph of your post.
    – Ji Cha
    Aug 24, 2015 at 12:07
  • 1
    @JiCha See my edit at the end. You are right, it effectively getting rid of spaces characters before to do the replacement when the pattern have a %. Aug 24, 2015 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.