Wildcard to obtain list of all directories

Haatschii picture Haatschii · Dec 16, 2012 · Viewed 33.3k times · Source

In my Makefile I need to get a list of all directories present in some other directory.

To get a list of all directories in the same folder as my Makefile I use:

DIRECTORIES = $(wildcard */)

all:
    echo $(DIRECTORIES)

which works fine, and gives me the desired list. However if I want to have a list of all directories in another directory using

DIRECTORIES = $(wildcard ../Test/*/)

all:
    echo $(DIRECTORIES)

I get a list of ALL files (with paths) in that directory, including .h and .cpp files.

Any suggestions why this happens and how to fix it? Other solutions to obtain the list are also welcome.

Answer

Lei Mou picture Lei Mou · Dec 16, 2012

Use sort and dir functions together with wildcard:

DIRECTORY = $(sort $(dir $(wildcard ../Test/*/)))

From GNU make manual:

$(dir names...) Extracts the directory-part of each file name in names. The directory-part of the file name is everything up through (and including) the last slash in it. If the file name contains no slash, the directory part is the string ‘./’.

$(sort list) Sorts the words of list in lexical order, removing duplicate words. The output is a list of words separated by single spaces.

Also look at the second and the third method in this article: Automatically Creating a List of Directories