Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a percent symbol do in a makefile?

Tags:

makefile

I have a makefile that looks like this :

include $(patsubst %,$(src)/%/Make.tests, $(TEST_SUBDIRS))  %-test:         Something here 

I understand what it is intended for in the target rule line. What is the % sign doing in the first line ? Does it have anything to do percent sign in the target rule line ?

When I write make sometarget, are the lines in the makefile that are not written as part of any rule (like the first line in this makefile) executed at all ? If yes, then what is the order of execution ?

like image 280
AnkurVj Avatar asked Sep 13 '11 15:09

AnkurVj


People also ask

What does %O %c mean in makefile?

The simple answer is that %.o is a target that matches any file ending in .o. "%.o: %. c" means that any file ending in .o depends on the same filename ending in . c to be present.

What does ?= Mean in makefile?

?= indicates to set the KDIR variable only if it's not set/doesn't have a value. For example: KDIR ?= "foo" KDIR ?= "bar" test: echo $(KDIR)

What does the special symbol mean in makefile $@ $<?

Inside actions we can use: $@ to represent the full target name of the current target $? returns the dependencies that are newer than the current target $* returns the text that corresponds to % in the target $< returns the name of the first dependency $^ returns the names of all the dependencies with space as the ...

What is symbol in make?

Lion is the symbol of Make in India. This logo was inspired by the Ashoka Chakra, to represent India's success in all spheres. You can read about the Make In India – Initiatives, Aims, Advantages & Challenges in the given link.


1 Answers

As you can read in the GNU make manual, the percent acts as a wildcard. The first argument of the patsubst function forms the pattern. Each item/word in the last argument is compared against this pattern, and if it matches, it is replaced with the second argument. If there is a wildcard symbol (%) in the pattern, this will match any number of characters, and these characters are copied into the replacement string at the place of the % in the second argument.

In your example the pattern is just the wildcard symbol, so it will match any word in the last argument to the function, and this word will be copied into the replacement string (the second argument) at the place of the %.

An example may make things more clear. Let's assume TEST_SUBDIRS contains two names.

TEST_SUBDIRS := test1 test2 include $(patsubst %,$(src)/%/Make.tests, $(TEST_SUBDIRS)) 

This is then equivalent to the following.

include $(src)/test1/Make.tests $(src)/test2/Make.tests 

A makefile is processed sequentially, line by line. Variable assignments are "internalized", and include statements cause the contents of other files to be inserted literally at that location after which that content is processed as part of the makefile.
A dependency graph is formed from the rules as they are being read in, and after the entire file is processed, the necessary recipes are executed to update the requested target.

like image 145
eriktous Avatar answered Oct 01 '22 12:10

eriktous