开发者

How can I check syntax for Make but be sure I am not executing?

We work with Make files and want to create a precommit check in HG to check Makefile syntax. Originally, our check was just going to be

make -n FOO.mk

However, we realized that if a Makefile were syntactically correct but required some environment variable to be set, the test could fail.

Any ideas? Our default is to resort to writing our own python scripts to check for a limited subset of common Makefile mistakes.

We are usi开发者_高级运维ng GNUmake.


$ make --dry-run > /dev/null
$ echo $?
0

The output is of no value to me so I always redirect to /dev/null (often stderr too) and rely on exit code. The man page https://linux.die.net/man/1/make explains:

-n, --just-print, --dry-run, --recon
    Print the commands that would be executed, but do not execute them.

A syntax error would result in the sample output:

$ make --dry-run > /dev/null
Makefile:11: *** unterminated variable reference.  Stop.


It is not a good idea to have makefiles depend on environment variables. Precisely because of the issue you mentioned.

Variables from the Environment:

... use of variables from the environment is not recommended. It is not wise for makefiles to depend for their functioning on environment variables set up outside their control, since this would cause different users to get different results from the same makefile. This is against the whole purpose of most makefiles.


References to an environment variable in the recipe need a $$ prefix so it is not that hard to find references to the pattern '[$][$][{] or the pattern [$][$][A-Z] which will find the direct references. A pretty simple perl filter (sed script) finds them all.

To find the indirect ones I would try the recipe with only PATH set and HOME set to /dev/null, and SHELL set to /bin/false. Make's macro SHELL is not the environment $SHELL, so you can get the recipes to run, you'll have to set SHELL=/bin/sh in the recipe file to run the command from the recipe. That should shake out enough data to help you find the depends.

What you do about the results is another issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜