Wednesday, April 15, 2015

Another way to find the value of GNU make's -j parameter

The other day I wrote a blog post about finding the value of -j in GNU make and it received a lot of commentary. Daniel Frey wrote in with a different solution that goes like this. It's worth studying if you are interested in GNU make wrangling.

.PHONY: default FORCE token recursive
default: foo.out
    @$(eval JOB_COUNT := $(shell cat $<))
    @echo Job count: $(JOB_COUNT)

foo.out: FORCE
    @timeout 1 $(MAKE) --no-print-directory token recursive 2>/dev/null | wc -l >$@


    @echo X && sleep 2 && false

    @$(MAKE) --no-print-directory token recursive

1 comment:

Ralph Corderoy said...

Has the same issues as with the previous solution. From my comment on that earlier post:

"Two problems that I can see. If -k is in use then the false doesn't stop make creating more children up to the 32 explicitly targeted. And it assumes that all the first round of children will be running before the first child finishes with failure after a second's sleep. On a busy machine, this might not happen."

Incrementing sleep's second to two seconds isn't a solution. :-)