eplictical
eplictical

Reputation: 647

Multi-threaded make

I can multi-thread a make with make -jN

Can I dictate multi-threading within the Makefile so that just make from the command-line runs multiple threads. Here's my makefile:

BIN_OBJS = $(wildcard *.bin)
HEX_OBJS = $(subst .bin,.hex,$(BIN_OBJS))

all: $(HEX_OBJS)

$(HEX_OBJS): %.hex: %.bin
    python ../../tools/bin2h.py $< > $@

Upvotes: 19

Views: 62568

Answers (2)

Alec Keeler
Alec Keeler

Reputation: 51

Be careful using -j if the filesystem where the make is occurring is an nfs share. I have seen odd results and had it mentioned to me that nfs mounted directories operate differently (some sort of file lock issue?)

I ran my multi makes from a script and checked cpuinfo to find out how many processors the build box had (was running same script against multiple architectures/build machines)

CPUCOUNT=$(grep -c   "^processor" /proc/cpuinfo)

if [ ${CPUCOUNT} -lt 1 -o ${CPUCOUNT} -gt 4 ]
then
    echo "Unexpected value for number of cpus, ${CPUCOUNT}, exiting ..."
    exit 16
fi

echo "This machine has ${CPUCOUNT} cpus, will use make -j${CPUCOUNT} where possible"

Upvotes: 5

MadScientist
MadScientist

Reputation: 101081

First, to be clear, make is not multi-threaded. Using -j just tells make to run multiple commands at the same time (in the background, basically).

Second, no, it's not possible to enable multiple jobs from within the makefile. You don't want to do that, in general, anyway because other systems will have different numbers of cores and whatever value you choose won't work well on those systems.

You don't have to write multiple makefiles, though, you can just use:

BIN_OBJS = $(wildcard *.bin)
HEX_OBJS = $(subst .bin,.hex,$(BIN_OBJS))

.PHONY: all multi
multi:
        $(MAKE) -j8 all

all: $(HEX_OBJS)

$(HEX_OBJS): %.hex: %.bin
        python ../../tools/bin2h.py $< > $@

Upvotes: 26

Related Questions