Automatically setting jobs (-j) flag for a multicore machine?

前端 未结 10 948
伪装坚强ぢ
伪装坚强ぢ 2020-12-12 22:23

I have a Makefile on a machine that has a ton of cores in it, but I always seem to forget to write -jX when compiling my project and it takes way longer than it

10条回答
  •  借酒劲吻你
    2020-12-12 23:13

    Until sometime in 2016, you could put this in your makefile: (GNU make tested)

    MAKEFLAGS += "-j$(NUM_CORES) -l$(NUM_CORES)
    

    (where NUM_PPROCS is calculated or set according to one of many of the other answers here) And, bam! you have multi-process building going on.

    Given that this has stopped working, the best thing that I could come up with is this, where the makefile calls itself, but with -jX and -lX.

    ifeq ($(PARALELL_WRAPPER_ABXCOEOEKCOEBMQJKHTOEUB),done)
    
    all: ...
       ...
    
    other_target: ...
        ...
    
    else
    # add parallelism equal to number of cores every time.
    # "random" strings are to ensure uniqueness
    NUM_CORES ?= $(shell grep -c "vendor_id" /proc/cpuinfo)
    MAKEFLAGS +=" -j$(NUM_CORES) -l$(NUM_CORES) "
    
    # for the default target case
    parallel_wrapper_default_target_anthsqjkshbeohcbmeuthnoethoaeou:
        $(MAKE) PARALELL_WRAPPER_ABXCOEOEKCOEBMQJKHTOEUB=done
    
    # catches everything else
    % :
        $(MAKE) $@ PARALELL_WRAPPER_ABXCOEOEKCOEBMQJKHTOEUB=done
    
    endif
    

提交回复
热议问题