Parallel make: set -j8 as the default option

后端 未结 3 1063
既然无缘
既然无缘 2021-02-05 01:10

I can set number of threads for the build process using -j argument. For example, I have 4 cores +4 virtual. When I write: make -j8 the speed increases

3条回答
  •  执念已碎
    2021-02-05 01:24

    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)
    …
    

提交回复
热议问题