Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reasoning behind the Makefile whitespace syntax?

Tags:

I'm revisiting Python after Michael Sparks's excellent walk through of Peter Norvig's Python spell checker at the SO DevDay in London.

One of the points he highlighted was how clean Python is to look at. Not cluttered with braces for scopes but using white space to indicate block scope instead.

This got me thinking. I wonder if that is the reason behind the TAB indentations that are prepended to the commands needed to build a make target.

Was it the same clarity aspect? To readily distinguish between a target and the commands needed to build the target?

like image 220
Rob Wells Avatar asked Nov 18 '09 12:11

Rob Wells


People also ask

Does whitespace matter Makefile?

Whitespace and indentation in makefiles are more problematic than for other languages. Even while most of the time whitespace is not significant, sometimes it is. This might be considered a design flaw in make, but that's not something we can do anything about.

Why do makefiles use tabs?

In order for make to tell the difference between a recipe and things that are not a recipe, it uses TAB characters.

What is $@ in Makefile?

The file name of the target of the rule. If the target is an archive member, then ' $@ ' is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ' $@ ' is the name of whichever target caused the rule's recipe to be run.

What is filter in Makefile?

The filter function can be used to separate out different types of strings (such as file names) in a variable. For example: sources := foo.c bar.c baz.s ugh.h foo: $(sources) cc $(filter %.c %.s,$(sources)) -o foo. says that foo depends of foo. c , bar.


2 Answers

From "The Art of Unix Programming" comes this quote:

Why the tab in column 1? Yacc was new, Lex was brand new. I hadn't tried either, so I figured this would be a good excuse to learn. After getting myself snarled up with my first stab at Lex, I just did something simple with the pattern newline-tab. It worked, it stayed. And then a few weeks later I had a user population of about a dozen, most of them friends, and I didn't want to screw up my embedded base. The rest, sadly, is history.
-- Stuart Feldman
like image 126
PP. Avatar answered Oct 11 '22 19:10

PP.


I think Makefiles make the mistake of insisting on precisely one tab character for indentation. What's the harm in allowing spaces? There's no loss of precision, since shell commands can't usefully begin with spaces anyway, and there's no confusion between target lines and command lines.

like image 22
Ned Batchelder Avatar answered Oct 11 '22 17:10

Ned Batchelder