4
votes

I have the following code to untar all the files in a directory and move it to build directory. If I call make multiple times, it tries to execute "build" target everytime even if build directory already exists. Has anyone comes across this?

I found this question but it is not the same. Makefile always running target

OS: Ubuntu 12.04 Program: GNU Make 3.81

build: mkBuildDir untar 
    chmod 700 build

.PHONY: mkBuildDir untar 

mkBuildDir: 
    mkdir build 

untar: *.tar.gz
    for prefix in *.tar.gz; do \
        tar xvf $$prefix --directory=build; \
    done

clean: 
    rm -Rf build
1

1 Answers

7
votes

This is pretty much the same as the question you've linked. You never create a file called mkBuildDir, so it's always out-of-date, so build is always out of date.

Your mkBuildDir target isn't doing anything useful (though I presume this is a cut-down makefile). If instead you did

# it'd be better to list the TARFILES explicitly, though this will probably work
TARFILES=`ls *.tar.gz`

all: build untar

build: $(TARFILES)
    test -d build || mkdir build
    chmod 700 build
    for prefix in $(TARFILES); do \
        tar xvf $$prefix --directory=build; \
    done

clean: 
    rm -Rf build

that would probably accomplish what you're looking for.

Having too many phony targets in a Makefile is usually a makefile 'code smell'. They are rarely the best/idiomatic way of doing things.