Multi-threaded design

I can multithreaded make with make -jN

Can I detect multithreading in the Makefile, so that only make from the command line starts multiple threads. Here is my makefile:

 BIN_OBJS = $(wildcard *.bin) HEX_OBJS = $(subst .bin,.hex,$(BIN_OBJS)) all: $(HEX_OBJS) $(HEX_OBJS): %.hex: %.bin python ../../tools/bin2h.py $< > $@ 
+6
source share
2 answers

First, to be clear, make is not multithreaded. Using -j just makes make run several commands at the same time (mostly in the background).

Secondly, no, it is impossible to include several jobs from the makefile. In any case, you do not want to do this, because other systems will have a different number of cores and no matter what value you choose, it will not work well in these systems.

You do not need to write multiple makefiles, but you can simply 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 $< > $@ 
+12
source

Be careful using -j if the file system where make is run is a nfs share. I saw odd results, and he mentioned to me that the installed nfs directories work differently (is there a file lock problem?)

I ran my multi-commands from a script and checked cpuinfo to find out how many processors were in the build box (there was the same script for several build architectures / 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" 
+2
source

Source: https://habr.com/ru/post/969717/


All Articles