Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force gnu make to not build recipe in parallel?

How can I tell gnu make not to build some recipe in parallel. Let's say I have the following makefile :

sources = a.xxx b.xxx c.xxx
target  = program

all : $(target)

$(target) : $(patsubst %.xxx,%.o,$(sources))
    $(CXX) -o $@ $<

%.o : %.cpp
    $(CXX) -c -o $@ $<

%.cpp : %.xxx
    my-pre-processor -o $@ $<

However, the my-pre-processor command create temporary files with fixed name (I cannot change this). This is working fine if I just use make without the -j parameter. However, if the -j option is used, the build sometimes fails because two concurrent invocation of my-pre-processor overwrite their temporary files.

I'd like to know if there is a way to tell make that it must not build the try to parallelize the execution of the %.cpp : %.xxx recipes.

like image 269
Sylvain Defresne Avatar asked Dec 03 '10 14:12

Sylvain Defresne


People also ask

Is make parallel?

Normally, the make command runs commands sequentially for only one target at a time, waiting for the command to finish before running the next. However, the make command can also run in a parallel run mode, where it can run many concurrent jobs to build independent targets.

What is makefile phony?

PHONY: allows to declare phony targets, so that make will not check them as actual file names: it will work all the time even if such files still exist. You can put several . PHONY: in your Makefile : .

What is Gmake in Linux?

GNU Make is a tool which controls the generation of executables and other non-source files of a program from the program's source files. Make gets its knowledge of how to build your program from a file called the makefile, which lists each of the non-source files and how to compute it from other files.

How does make J work?

-j [jobs], --jobs[=jobs] Specifies the number of jobs (commands) to run simultaneously. If there is more than one -j option, the last one is effective. If the -j option is given without an argument, make will not limit the number of jobs that can run simultaneously.


1 Answers

Solution 1

GNU Make contains the special built-in pseudo-target .NOTPARALLEL

Example:

.PHONY: all clean

.NOTPARALLEL:

anotherTarget: dependency1

Solution 2

You can also use the -j <n>,--jobs[=<n>] flag on the command line where n is the number of recipies allowed to run in parallel.

Usage: make -j <n> or make --jobs=<n>

Example: make -j 1 or make --jobs=1

note: omitting <n> will allow an arbitrary number of recipes to be executed, only limited by your system's available resources


Solution 3

Finally, you can assign the command line flag in solution 2 to the MAKEFLAGS variable from within your Makefile

Example: MAKEFLAGS := -j 1 or MAKEFLAGS := --jobs=1

like image 161
Shammel Lee Avatar answered Sep 19 '22 09:09

Shammel Lee