tags:

views:

17

answers:

1

From the command line you can make multiple directories like so:

mkdir -p build/linux/{src,test,debug}

How can the same thing be achieved from a Makefile? I've come up with this:

# The architecture/OS to compile for
ARCH        := linux

# Object files go here, under the architecture/OS directory
BUILD_DIR   := build/$(ARCH)

# List of source code directories
SOURCES     := src test debug

# e.g. The object files for 
# src/foo.c  test/bar.c  debug/bort.c
# will be in
# build/$(ARCH)/src/foo.o
# build/$(ARCH)/test/bar.o
# build/$(ARCH)/debug/bort.o

# Make a comma separated list of the source directories
# Is there a more concise way of doing this?
comma       :=,
empty       :=
space       := $(empty) $(empty)
DIRECTORIES := $(subst $(space),$(comma),$(SOURCES))

default:
    mkdir -p $(BUILD_DIR)/{$(DIRECTORIES)}

clean:
    rm -fr $(BUILD_DIR)

Which just makes one directory called '{src,test,debug}' instead of three distinct directories.

$ make
mkdir -p build/linux/{src,test,debug}
$ ls build/linux/
{src,test,debug}
$ make clean
rm -fr build/linux
$ mkdir -p build/linux/{src,test,debug}
$ ls build/linux/
debug  src  test

I suspect the problem might be with/around the curly brackets... What am I doing wrong?

Edit: It does seem to be the shell make uses. The example at the top was in bash, but sh fails:

$ sh
$ mkdir -p build/linux/{src,test,debug}
$ ls build/linux
{src,test,debug}
+4  A: 

I believe the problem is that make is invoking a shell that does not support the {} syntax. You can replace your rule with a simpler, more portable rule:

        for p in $(SOURCES); do mkdir -p $(BUILD_DIR)/$$p; done
William Pursell
Thank you. Much simpler than my bizarre attempt!
DrTwox