makefile run targets in parallel

Multi tool use
Multi tool use


makefile run targets in parallel



I’ve the following makefile, with two rules.



Is there a way to run these two rules in parallel, I mean to maximize
the core’s capabilities? I see this section but not sure that I got how to use it for my purpose, since I want to handle it within the makefile and not from the command line.



I.e. run module1 & 2 targets in parallel.



This is the makefile:


all: module1 module2

.PHONY: module1
module1:
@echo "run module 1"
DIR=$(PWD)
@echo $(DIR)

.PHONY: module2
module2:
@echo "run module2”

cleanup:
fzr clean $(DIR)





What about make -j2 is not working?
– Thomas
Jul 3 at 7:38


make -j2





Probably OP is unfamiliar. Checkout the man.
– kabanus
Jul 3 at 7:40





@Thomas - as I write in the question , I want to do it within the file and not with command, how can I do it ?
– shopia T
Jul 3 at 7:49





@kabanus - im familiar with this option as it written in the doc i've provided , but my question is a bit different , I want to somehow do it from the file
– shopia T
Jul 3 at 7:50





Is there a particular reason you cannot invoke make with the job flag?
– kabanus
Jul 3 at 7:51


make




1 Answer
1



You can set make options that you usually pass to make via its command line invokation in the makefile itself. Add this line to your makefile


make


MAKEFLAGS += -j2



and you can invoke make without the -j flag, it will still spawn two processes to build targets in parallel, when they aren't dependent on each other. To automatically determine the number of jobs to spawn, you can use this on linux


make


-j


NPROCS = $(shell grep -c 'processor' /proc/cpuinfo)
MAKEFLAGS += -j$(NPROCS)



and on MacOS


NPROCS = $(shell sysctl hw.ncpu | grep -o '[0-9]+')
MAKEFLAGS += -j$(NPROCS)





thanks but this is someting that I see in the docs, what I need it to do it within the file as I write in the question and not by command, how it can be done ?
– shopia T
Jul 3 at 7:48





I adjusted the answer.
– lubgr
Jul 3 at 7:53





thanks , 1+, is there way to determine how much process I can run ?because it's different for each machine ...
– shopia T
Jul 3 at 7:59





@shopiaT MAKEFLAGS += -j$(shell nproc || printf 1) (|| printf 1 so that if nproc isn’t available you’ll get a 1).
– Biffen
Jul 3 at 8:14


MAKEFLAGS += -j$(shell nproc || printf 1)


|| printf 1


nproc


1





Go with @echo $(NPROCS).
– lubgr
Jul 3 at 9:05


@echo $(NPROCS)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

J pwJRu3JlFSy2cQEu RyCJ8Lue RY,7p4soAs,tpan,7KZCW8 Kl WMJQFC4Q81LILj1H ayg8Qq7MsgY95pHI0gwxnOT f,860Uw2UU,B5
XEzHV0JLTPTUo n4xRGlkwpZffkT1vQ9jP6Q2eTY z,4QrVyW51,tlE,aIZ0im9u kdyh m,ozr,j

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications