Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automate the correctness-checking of makefiles? [closed]

I know there are many linters for programming languages, like pep8 for python, but I have never come across one for a makefile. Are there any such linters for makefiles?

Any other ways to programmatically detect errors or problems in makefiles without actually running them?

As I have grown into using a makefile, it keeps on getting more complicated and long, and for me it would make sense to have a linter to keep the makefile more readable.

like image 860
msakya Avatar asked Apr 03 '14 15:04

msakya


People also ask

What does $@ mean in makefiles?

The variable $@ represents the name of the target and $< represents the first prerequisite required to create the output file.

What is lint in makefile?

As with the clean target, lint is a target name used by convention; it is usually a good practice to include it in makefiles that build C programs. lint produces output files that have been preprocessed through cpp and its own first (parsing) pass.

Where are makefiles stored?

some projects put their makefile in src/ subdirectory of the root directories of the projects, some projects put their makefiles in the root directory of the project.


3 Answers

Things have apparently changed. I found the following:

  • Checkmake
  • Mint

Of the two, Checkmake has (as of 2018/11) more recent development, but I haven't tried either.

like image 63
pjz Avatar answered Oct 18 '22 22:10

pjz


The only thing resembling lint behaviour is the command-line option --warn-undefined-variables

like image 44
Mark Galeck Avatar answered Oct 18 '22 22:10

Mark Galeck


I also do not know where to find make file lint (web search for "make file lint" got me here), but here is a incomplete list of shallow idea fragments for implementing a make file lint utility...

  1. White spaces are one aspect of the readability, as tabs and spaces have distinct semantics within make file. Emacs makefile-mode by default warns you of "suspicious" lines when you try to save make file with badly spaced tabs. Maybe it would be feasible to run emacs in batch mode and invoke the parsing and verification functions from that mode. If somebody were to start implementing such a make file lint utility, the emacs lisp mode could be an interesting to check.

  2. About checking for correctness, @Mark Galeck in his answer already mentioned --warn-undefined-variables. The problem is that there is a lot of output from undefined but standardized variables. To improve on this idea, a simple wrapper could be added to filter out messages about those variables so that the real typos would be spotted. In this case make could be run with option --just-print (aka -n or --dry-run) so as to not run the actual commands to build the targets.

  3. It is not good idea to perform any changes when make is run with --just-print option. It would be useful to grep for $(shell ...) function calls and try to do ensure nothing is changed from within them. First iteration of what we could check for: $(shell pwd) and some other common non-destructive uses are okay, anything else should invoke a warning for manual check.

  4. We could grep for $ not followed by ( (maybe something like [$][^$(][[:space:]] expressed with POSIX regular expressions) to catch cases like $VARIABLE which parses as $(V)ARIABLE and is possibly not what the author intended and also is not good style.

The problem with make files is that they are so complex with all the nested constructs like $(shell), $(call), $(eval) and rule evaluations; the results can change from input from environment or command line or calling make invocations; also there are many implicit rules or other definitions that make any deeper semantic analysis problematic. I think all-compassing make lint utility is not feasible (except perhaps built-in within make utility itself), but certain codified guidelines and heuristic checks would already prove useful indeed.

like image 6
FooF Avatar answered Oct 18 '22 22:10

FooF