huynle
huynle

Reputation: 53

cannot create multiple directories with makefile

What is wrong with this statement in my makefile?

folder: @mkdir -vp ".cache/vim/"{backup,swap}

I can run mkdir -vp ".cache/vim/"{backup,swap} on the commandline and it does exactly what it is supposed to do, ie. creating two folders, backup and swap.

But for the when i run make folder with the above code, it literally create a folder name {backup,swap}

Upvotes: 2

Views: 2728

Answers (2)

Dario
Dario

Reputation: 2723

By default, all versions of make use /bin/sh, not bash, for shell commands, while in most situations bash is the interactive shell you use from the command line. This explains why brace expansion, a bash feature absent in sh, works in the latter case but not in the former.

If you want to keep your makefiles portable to the highest degree, your only option is to write the command in full:

folder:
        @mkdir -vp '.cache/vim/backup' '.cache/vim/swap'

However, if you want to use features of GNU make (available for every platform in existence, but not necessarily installed by default) you can use the addprefix function in a way similar to this:

# Quotes aren’t necessary in this case, this is just to show how to use them
BASEDIR = './cache/vim'
FILES   = 'backup' 'swap'

folder:
        @mkdir -vp $(addprefix $(BASEDIR)/,$(FILES))

Or you can instruct GNU make to use /bin/bash and use the same command you use on the command line:

SHELL   = /bin/bash
BASEDIR = './cache/vim'

folder:
        @mkdir -vp $(BASEDIR)/{backup,swap}

A discussion on how GNU make uses the SHELL variable can be found here.

Upvotes: 4

Gilles Quénot
Gilles Quénot

Reputation: 185831

is not , so brace expansions will not work, so :

folder:
  @mkdir -vp '.cache/vim/backup' '.cache/vim/backupswap'

Upvotes: 2

Related Questions