How can I write a makefile to auto-detect and parallelize the build with GNU Make?

前端 未结 7 1550
悲&欢浪女
悲&欢浪女 2021-02-01 03:37

Not sure if this is possible in one Makefile alone, but I was hoping to write a Makefile in a way such that trying to build any target in the file auto-magically detects the num

相关标签:
7条回答
  • 2021-02-01 04:30

    Here's what I went with:

    ifeq ($(OS),Linux)
            NUMPROC := $(shell grep -c ^processor /proc/cpuinfo)
    else ifeq ($(OS),Darwin)
            NUMPROC := $(shell sysctl hw.ncpu | awk '{print $$2}')
    endif
    
    # Only take half as many processors as available
    NUMPROC := $(shell echo "$(NUMPROC)/2"|bc)
    
    ifeq ($(NUMPROC),0)
            NUMPROC = 1
    endif 
    
    0 讨论(0)
提交回复
热议问题