Chris
Chris

Reputation: 31206

How to encode a string with a space at the end in a protected manner in a makefile?

Suppose I define a runners.mk file:

run=./
valgrind=valgrind --this --or --that-option 

And intentionally put a space at the end of the valgrind line.


The idea is to use any "runner" in the same way:

valgrind-foo-bar-executable: foo-bar-executable
  $(valgrind)$<

run-foo-bar-executable: foo-bar-executable
  $(run)$<

So that I can eventually abstract everything into a runner pattern with matching (once I get a few more examples).

However, this is a public repo and it is standard practice to clear out all blanks at the end of lines.

In order to do this pattern, it would be ideal to put some kind of protected blank at the end of the valgrind command. However, in a makefile quotes are interpreted literally.

Is there some way to protect this suffixed blank in the makefile?

Upvotes: 0

Views: 166

Answers (2)

MadScientist
MadScientist

Reputation: 100856

You don't even have to make a variable for this. In make, all trailing spaces are preserved automatically. So, just adding a comment is sufficient:

valgrind = valgrind --this --or --that-option # Leave one blank space

But, this is really kind asking for confusion IMO. There's no reason you can't include the space in the recipe like this:

run-it: foo-bar-executable
        $(valgrind) ./$<

then valgrind can be empty or not.

Upvotes: 1

Chris Dodd
Chris Dodd

Reputation: 126243

You can add an empty var on the end of the valgrind line to "protect" the space before it:

empty=
run=./
valgrind=valgrind --this --or --that-option $(empty)

Now the space between --that-option and $(empty) will be clearly visible and not removed as a trailing space.

Upvotes: 1

Related Questions