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

前端 未结 7 1548
悲&欢浪女
悲&欢浪女 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:09

    I'll skip over the $(NPROCS) detection stuff, but here's how you could do this in a single Makefile (this is probably GNU Make specific, but that looks like the variant you're running):

    ifeq ($(NPROCS),)
    # Code to set NPROCS...
    %:
        $(MAKE) -j$(NPROCS) NPROCS=$(NPROCS)
    else
    # All of your targets...
    endif
    

    See Defining Last-Resort Default Rules and Overriding Part of Another Makefile in the GNU Make Manual.

提交回复
热议问题