Sed One-Liner Confusion in Makefile Tutorial

Can someone explain this sed liner in English (the more details, the better)?

@sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' < $*.d > $@ ; \ rm -f $*.d; [ -s $@ ] || rm -f $@ 

This is part of this tutorial: http://mad-scientist.net/make/autodep.html

I have an inconsistent set of source files and you want to automatically generate a dependency tree based on the content (including) specified in my source files.

I carefully followed the textbook until ...

PS I have a basic understanding of sed select / replace, but I am confused by the matching line and all the redirection layers .... I also read the makefile tutorial once to get basic knowledge of standard makefiles ...

+6
source share
1 answer

The sed pattern will be processed by make first, so if the rule it applies to is trying to build foo.P , then $@ will be translated to foo.P and $* to foo . This means that the actual sed command will look something like this:

 sed 's/\(foo\)\.o[ :]*/\1.o foo.P : /g' < foo.d > foo.P 

\(foo\) exactly matches foo and sets up the first substitute for what matches (i.e. foo ) \. matches a literal period, and [ :]* matches any number of spaces and colons.

As you can see, replacing \1 bit redundant as the matched string is fixed. That would work too.

 sed 's/foo\.o[ :]*/foo.o foo.P : /g' < foo.d > foo.P 

which could be made from:

 sed 's/$*\.o[ :]*/$*.o $@ : /g' < $*.d > $@ 
+11
source

Source: https://habr.com/ru/post/887314/


All Articles