Force Makefile to execute script before building targets

anon picture anon · Jan 23, 2010 · Viewed 19.3k times · Source

I am using Makefiles.

However, there is a command (zsh script) I want executed before any targets is executed. How do I do this?

Thanks!

Answer

JesperE picture JesperE · Jan 23, 2010

There are several techniques to have code executed before targets are built. Which one you should choose depends a little on exactly what you want to do, and why you want to do it. (What does the zsh script do? Why do you have to execute it?)

You can either do like @John suggests; placing the zsh script as the first dependency. You should then mark the zsh target as .PHONY unless it actually generates a file named zsh.

Another solution (in GNU make, at least) is to invoke the $(shell ...) function as part of a variable assignment:

ZSH_RESULT:=$(shell zsh myscript.zsh)

This will execute the script as soon as the makefile is parsed, and before any targets are executed. It will also execute the script if you invoke the makefile recursively.