Here's how I've done it:
CORES ?= $(shell sysctl -n hw.ncpu || echo 1)
all:; @$(MAKE) _all -j$(CORES)
_all: install lint test
.PHONY: all _all
…
I've basically "aliased" my default target all
to a "private" _all
. The command to figure out the number of cores is OSX specific, AFAIK, so you could just improve it to be more cross platform if you will. And because of the ?=
assignment, we can just override it with and env variable if/when needed.
EDIT:
You can also append to your MAKEFLAGS
from within the makefile itself, like so:
CPUS ?= $(shell sysctl -n hw.ncpu || echo 1)
MAKEFLAGS += --jobs=$(CPUS)
…
EDIT 2:
You may also use the following, ff you want it to be more cross-platform:
CPUS ?= $(shell (nproc --all || sysctl -n hw.ncpu) 2>/dev/null || echo 1)
make
once more makes everything right while still having built most of the stuff much faster. – Fassold