Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading