Makefile - Make dependency only if file doesn't exist

社会主义新天地 提交于 2019-12-03 15:26:27

问题


Like the title says, I would like to make a dependency only if a certain file does not exist, NOT every time it updates.

I have a root directory (the one with the makefile) and in it a sub-directory called "example". In my root directory are four .h files (functions.h, parser.h, node.h, and exception.h) which I would like to copy to the "example" sub-directory if those .h files do not already exist in "examples".

Unfortunately I can not just make a standard dependency to check for the header files in "example" because each time I copy the header files from root to "example", the header files in "example" will be considered updated and will trigger that dependency each time I run make. I would like for a way to have my makefile copy the header files from the root directory to "example" only if they do not exist in "example".


回答1:


This is what order-only prerequisites/dependencies are for:

Occasionally, however, you have a situation where you want to impose a specific ordering on the rules to be invoked without forcing the target to be updated if one of those rules is executed. In that case, you want to define order-only prerequisites. Order-only prerequisites can be specified by placing a pipe symbol (|) in the prerequisites list: any prerequisites to the left of the pipe symbol are normal; any prerequisites to the right are order-only.

In your case:

examples/%.h : | %.h
    cp $| $@

See also: Order-only prerequisites do not show up in $^ or $+.




回答2:


You must express the dependencies in terms of relative file age.

A commonly used workaround is to use a dummy ("sentinel") file that flags the operation's completion:

examples/%.h.hasbeencopied: %.h
    if [ -f example/$@.h ] || cp -p $< examples/$@.h; \
    then touch $@; \
    else rm -f $@; \
    fi

and make whatever rule depends on example/foo.h also depend on example/foo.h.hasbeencopied.



来源:https://stackoverflow.com/questions/21745816/makefile-make-dependency-only-if-file-doesnt-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!