Compare if folder is older than zip file

时间:2016-10-20 12:46:09

标签: makefile

I am making a makefile that has to unrar a certain .zip file. I want to compare if it is newer than a specific folder.

Since the folder might be in a different place based on every system this is what I do:

.PHONY : install

install : recipe1 ${COMPONENTS_ROOT}/Model/temp recipe2
     @echo done

recipe1:
     @echo recipe1

${COMPONENTS_ROOT}/Model/temp : ${COMPONENTS_ROOT}/model/model.zip
     @echo temp1
     commands...
     @echo temp2

recipe2:
     @echo recipe2

Output:

recipe1
recipe2
done

Even if there is no temp folder i never reach "temp1" and "temp2"

1 个答案:

答案 0 :(得分:0)

So this does work on my system (see below). You may want to check that COMPONENTS_ROOT expands to what you think it does. Also, if you're on windows, the only difference between your directory names is case, so that might cause you problems.

Having said that, what you're doing is somewhat error prone. First, the date of a directory changes as its contents do, so the date does not reflect when the directory was built. The second thing is that if a user presses ctrl-c while the untar is going on, you could have half an untarred directory which can cause problems down the road.

A better convention is to do something as follows:

${untar_dir}/.untar_done: ${zip_dir}/file.zip
     [ ${untar_dir} ] // just to be safe
     rm -rf ${untar_dir}/*
     cd ${untar_dir} && tar -xf $^
     touch $@

This will create a temporary phony file which will mark when the tar was last untarred. It will only exist if the untar was complete.

As for this working for me, I have the following logs:

~/tmp/tst1/tst2> more Makefile
PHONY : install

COMPONENTS_ROOT=$(shell pwd)

install : recipe1 ${COMPONENTS_ROOT}/Model/temp recipe2
        @echo done

recipe1:
        @echo recipe1

${COMPONENTS_ROOT}/Model/temp : ${COMPONENTS_ROOT}/model/model.zip
        @echo temp1
        mkdir $@
        @echo temp2

recipe2:
        @echo recipe2
~/tmp/tst1/tst2> rmdir Model/temp/
~/tmp/tst1/tst2> make
recipe1
temp1
mkdir /home/julvr/tmp/tst1/tst2/Model/temp
temp2
recipe2
done
~/tmp/tst1/tst2>