Recursive make in subdirectories

前端 未结 4 703
清歌不尽
清歌不尽 2021-02-04 01:37

How can I order make command in the Makefile to execute recursively in all subdirectories make commands (defined in the Makefile in the subdirectories)

相关标签:
4条回答
  • 2021-02-04 01:52

    The above answers work well when all of the subdirectories have Makefiles. It is not that difficult to only run make on the directories that contain Makefile, nor is it difficult to restrict the number of levels to recurse. In my sample code, I restrict the search for makefiles to the subdirectories immediately below the parent directory. The filter-out statement (line 2) prevents this Makefile from being included in the recursive makes.

    MAKEFILES = $(shell find . -maxdepth 2 -type f -name Makefile)
    SUBDIRS   = $(filter-out ./,$(dir $(MAKEFILES)))
    
    all:
        for dir in $(SUBDIRS); do \
            make -C $$dir all; \
        done
    
    0 讨论(0)
  • 2021-02-04 01:55

    I will submit here my particular solution. Suppose we have a directory with many sub-directories, all of them with its own makefile:

    root-dir\
        +----subdir1
        +----subdir2
        ...
        +----subdirn
    

    then, just copy in the root-dir this Makefile:

    SUBDIRS = $(shell ls -d */)
    all:
        for dir in $(SUBDIRS) ; do \
            make -C  $$dir ; \
        done
    
    0 讨论(0)
  • 2021-02-04 01:59
    1. Read Recursive Use of Make chapter of GNU Make manual.
    2. Learn Peter Miller's Recursive Make Considered Harmful article.
    3. ...
    4. PROFIT!!!

    P.S. A code snippet from my answer to a different yet related question could be used as a rough approximation.

    0 讨论(0)
  • 2021-02-04 02:02

    @eldar-abusalimov, the first link you posted assumes the makefile knows what are the subfolders. This is not always true, and I guess thats what @tyranitar would like to know. In that case, such a solution can do the job: (took me some time, but I needed it too)

    SHELL=/bin/bash
    
    all:
        @for a in $$(ls); do \
            if [ -d $$a ]; then \
                echo "processing folder $$a"; \
                $(MAKE) -C $$a; \
            fi; \
        done;
        @echo "Done!"
    
    0 讨论(0)
提交回复
热议问题