Home > Net >  Is there a way to define custom implicit GNU Make rules?
Is there a way to define custom implicit GNU Make rules?

Time:05-10

I'm often creating png files out of dot (graphviz format) files. The command to do so is the following:

$ dot my_graph.dot -o my_graph.png -Tpng

However, I would like to be able to have a shorter command format like $ make my_graph.dot to automatically generate my png file.

For the moment, I'm using a Makefile in which I've defined the following rule, but the recipe is only available in the directory containing the Makefile

%.eps: %.dot
    dot $<  -o $@ -Teps

Is it possible to define custom implicit GNU Make recipes ? Which would allow the above recipe to be available system-wide

If not, what solution do you use to solve those kind of problem ?

Setup:

  • Fedora Linux with ZSH/Bash

CodePudding user response:

You could define shell functions in your shell's startup files, e.g.

dotpng()
{
    echo dot ${1%.dot}.dot -o ${1%.dot}.png -Tpng;
}

This function can be called like

dotpng my_graph.dot

or

dotpng my_graph

The code ${1%.dot}.dot strips .dot from the file name if present and appends it (again) to allow both my_graph.dot and my_graph as function argument.

CodePudding user response:

Is it possible to define custom implicit GNU Make recipes ?

Not without modifying the source code of GNU Make.

If not, what solution do you use to solve those kind of problem ?

I wouldn't be a fan o modyfying the system globally, but you could do:

  • Create a file /usr/local/lib/make/myimplicitrules.make with the content

      %.eps: %.dot
          dot $<  -o $@ -Teps
    
  • Use include /usr/local/lib/make/myimplicitrules.make in your Makefile.

I would rather use a git submodule or similar to share common configuration between projects, rather than depending on global configuration. Depending on global environment will make your program hard to test and non-portable.

I would rather go with a shell function, something along:

mymake() {
   make -f <(cat <<'EOF'
%.eps: %.dot
    dot $<  -o $@ -Teps
EOF
   ) "$@"
}
mymake my_graph.dot
  • Related