Search code examples
recursiongnu-make

How do I get a list of folders using gnu make


I am trying to develop a recurrsive Makefile to run pandoc on all my markdown files in a multi-level folder structure. I want my top-level Makefile to be able to determine all folders contained in the root folder without having to hard-code the folders in the Makefile.

folder structure:

notes
----> topic1
      ----> sub-topic1
      ----> sub-topic2
----> topic2
      ----> sub-topic 2-1

...

In each "topic" folder I want a Makefile that will just determine the list of its sub-folders and call Make in the sub-folders.

In the leaf folders, the Makefiles will run pandoc to generate html files from markdown files.

I haven't seen any documentation on a command to list all sub-folders of the current folder, and that is what I need.


Solution

  • You can use the $(wildcard ...) function for this.

    For example:

    SUBDIRS := $(wildcard */.)
    
    .PHONY: $(SUBDIRS)
    
    all: $(SUBDIRS)
    
    $(SUBDIRS):
            $(MAKE) -C $@