Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of processing components in makefile

In a makefile, the dependency line is of the form -

abc: x y z

All three of the components (x,y,z) are themselves targets in dependency lines further down in the makefile.

If make abc is invoked, in what order will the three targets x,y,z be executed?

like image 370
Shailesh Tainwala Avatar asked Feb 06 '12 12:02

Shailesh Tainwala


People also ask

Does the order of files in a makefile matter?

The order of rules is not significant, except for determining the default goal: the target for make to consider, if you do not otherwise specify one. The default goal is the target of the first rule in the first makefile. If the first rule has multiple targets, only the first target is taken as the default.

How is a makefile structured?

A makefile consists of three sections: target, dependencies, and rules. The target is normally either an executable or object file name. The dependencies are source code or other things needed to make the target. The rules are the commands needed to make the target.

What are dependencies in makefile?

A dependency is a file that is used as input to create the target. A target often depends on several files. A command is an action that make carries out. A rule may have more than one command, each on its own line.

What is Makecmdgoals?

MAKECMDGOALS. The targets given to make on the command line. Setting this variable has no effect on the operation of make. See Arguments to Specify the Goals.


3 Answers

By default, the order of execution is the same as specified in the prerequisites list, unless there are any dependencies defined between these prerequisites.

abc: x y z

The order is x y z.

abc: x y z
y : z

The order would be x z y.

But ideally, you should design your Makefiles so that it wouldn't rely on the order in which prerequisites are specified. That is, if y should be executed after z, there must be a y : z dependence.

And keep in mind that GNU Make can execute some recipes in parallel, see Mat's answer.

like image 193
Eldar Abusalimov Avatar answered Oct 17 '22 11:10

Eldar Abusalimov


You really shouldn't depend on the order in which they are executed - all else being equal, all three recipes for those prerequisites could run in parallel.

The only hard rule is that all prerequisites must be met before the target recipe is run.

If there are no dependencies between x, y and z, and no parallel execution, GNU make appears to run them in the order you specified them, but this is not guaranteed in the docs.

like image 53
Mat Avatar answered Oct 17 '22 09:10

Mat


The POSIX description of make includes a rationale which says:

The make utilities in most historical implementations process the prerequisites of a target in left-to-right order, and the makefile format requires this. It supports the standard idiom used in many makefiles that produce yacc programs; for example:

foo: y.tab.o lex.o main.o
     $(CC) $(CFLAGS) -o $@ t.tab.o lex.o main.o

In this example, if make chose any arbitrary order, the lex.o might not be made with the correct y.tab.h. Although there may be better ways to express this relationship, it is widely used historically. Implementations that desire to update prerequisites in parallel should require an explicit extension to make or the makefile format to accomplish it, as described previously.

(I believe the t.tab.o in the $(CC) line is a typo for y.tab.o, but that is what the rationale actually says.)

Thus, the observed behaviour that pre-requisites are processed from left to right has validation here, though it is only in the Rationale section, not in the main description. The Rationale also mentions issues with parallel make etc.

like image 20
Jonathan Leffler Avatar answered Oct 17 '22 10:10

Jonathan Leffler