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.
UPDATE: Still hoping for an example Makefile for the above. Not really interested in finding the number of processes, but interested in how to write a makefile to build in parallel without the -j command line option.
After poking around the LDD3 chapter 2 a bit and reading dmckee’s answer, I came up with this not so great answer of using two makefiles (I would prefer just one).
What do you think about this solution?
Benefits I see is that people still type
maketo build. So there isn’t some “driver” script that does theNPROCSandmake -j$(NPROCS)work which people will have to know instead of typing make.Downside is that you’ll have to explicitly use
make -f Makefile.goalsin order to do a serial build. And I’m not sure how to solve this problem…UPDATED: added $J to above code segment. Seems work work quite well. Even though its two makefiles instead of one, its still quite seamless and useful.