Colen
Colen

Reputation: 13908

How do I create a directory in a makefile

I'm using Visual Studio 2005 nmake, and I have a test makefile like this:

sometarget:
    -mkdir c:\testdir

I want to always create the directory, without having to specify 'sometarget'. For example, I can do this:

!if [if not exist c:\testdir\$(null) mkdir c:\testdir]
!endif

But that requires two lines, where I really only want to do the "-mkdir c:\testdir". If I just replace it with "-mkdir c:\testdir" I get an error from nmake - "fatal error U1034: syntax error : separator missing".

How can I always execute the mkdir, without messing about with !if [] stuff?

Upvotes: 4

Views: 10680

Answers (5)

Bisc8
Bisc8

Reputation: 9

Try using this:

-mkdir -p c:\testdir

Upvotes: -2

Dominic
Dominic

Reputation: 31

I'm not sure if there is an equivalent in Windows with nmake, but I managed to create a directory without using targets on Linux. I used the make function "shell". For example:

# Find where we are
TOPDIR := $(shell pwd)
# Define destination directory
ROOTFS := $(TOPDIR)/rootfs
# Make sure destination directory exists before invoking any tags
$(shell [ -d "$(ROOTFS)" ] || mkdir -p $(ROOTFS))

all:
    @if [ -d "$(ROOTFS)" ]; then echo "Cool!"; else echo "Darn!"; fi

I hope Windows has the equivalent.

Upvotes: 3

Sagar
Sagar

Reputation: 9503

$(DIRNAME): @[ -d $@ ] || mkdir -p $@

Upvotes: 0

Aaron Saarela
Aaron Saarela

Reputation: 4036

I think this will work:

 -@ if NOT EXIST "dir" mkdir "dir"

Upvotes: 9

Conor OG
Conor OG

Reputation: 543

Make always wants to do things based on targets. It's not a general scripting tool. It looks at the targets and checks to see if they exist. If the target does not exist it executes the commands for that target.

The usual way to do this is to have a dummy target that is never going to be generated by the make scripts, so every time make runs it has to execute the relevant commands.

Or, you could add the command to a batch file that then calls your make file.

Upvotes: 8

Related Questions