How can I write a makefile to auto-detect and parallelize the build with GNU Make?

Posted by xyld on Stack Overflow See other posts from Stack Overflow or by xyld
Published on 2010-03-26T23:42:38Z Indexed on 2010/03/27 0:23 UTC
Read the original article Hit count: 700

Filed under:
|

Not sure if this is possible in one Makefile alone, but I was hoping to write a Makefile in a way such that trying to build any target in the file auto-magically detects the number of processors on the current system and builds the target in parallel for the number of processors.

Something like the below "pseudo-code" examples, but much cleaner?

all:
    @make -j$(NUM_PROCESSORS) all

Or:

all: .inparallel
    ... build all here ...

.inparallel:
    @make -j$(NUM_PROCESSORS) $(ORIGINAL_TARGET)

In both cases, all you would have to type is:

% make all

Hopefully that makes sense.

© Stack Overflow or respective owner

Related posts about makefile

Related posts about parallel