有没有办法定义自定义隐式GNU制定规则?

发布于 2025-01-27 12:32:08 字数 504 浏览 2 评论 0原文

我经常从dot(GraphViz格式)文件中创建png文件。这样做的命令如下:

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

但是,我希望能够像$ make my_graph.dot.dot一样,能够自动生成我的png文件。

目前,我正在使用一个我定义以下规则的makefile,但是该配方仅在包含makefile

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

的目录中可用,是否可以定义自定义隐式gnu制作配方?哪个将允许上述食谱在系统范围内可用

如果不是,您可以使用哪种解决方案来解决这些问题?

设置:

  • 带有ZSH/bash的Fedora Linux

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 
lt;  -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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゞ花落谁相伴 2025-02-03 12:32:08

您可以在Shell的启动文件中定义shell函数,例如

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

,可以称呼此功能,例如

dotpng my_graph.dot

dotpng my_graph

代码$ {1%.dot} .dot strips .dot。在文件名中,如果存在并将其附加(再次)允许my_graph.dotmy_graph作为函数参数。

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.

椵侞 2025-02-03 12:32:08

是否可以定义自定义隐式gnu制作食谱?

并非不需要修改GNU制造的源代码。

如果不是,您使用什么解决方案来解决这些问题?

我不会在全球范围内对系统进行Modyfyfy,但是您可以这样做:

  • 使用内容

    创建一个文件

    创建一个文件

     %.eps:%.dot
          点$&lt; -o $@ -Teps
     
  • Use include /usr/local/lib/make/myimplicitrules.make in您的makefile。

我宁愿使用git subsodule或类似的项目之间共享项目之间的共同配置,而不是根据全局配置。取决于全球环境,您的计划难以测试和不可支配。

我宁愿使用外壳功能,还有一些事情:

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

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 
    lt;  -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 
lt;  -o $@ -Teps
EOF
   ) "$@"
}
mymake my_graph.dot
靑春怀旧 2025-02-03 12:32:08

GNU使您可以使用makefiles指定额外的makefiles读取
环境变量。引用info'(make)makefiles变量'

默认目标永远不会从这些makefiles之一(或任何一个
其中包括makefile),如果列出的文件,这不是错误
在“ makefiles”中找不到

如果您在没有特定的makefile的情况下运行“制造”,则是makefile
在“ makefiles”中可以做有用的事情来帮助内置的隐式
规则更好

例如,当前目录中没有makefile,
以下.mk make s include路径(例如,通过
makeflags = - include-dir =“ $ home”/。local/lib/make/)您可以创建
subdir gen/和convert my_graph.dotdot/my_graph.dot by by
运行:

MAKEFILES=dot.mk make gen/my_graph.png

为了进一步保存一些打字,它很容易添加makefiles = dot.mk
在会话环境中,但在启动文件中定义makefiles
可以使事情完全不透明。因此,我更喜欢
在命令行上查看makefiles =…

文件:dot.mk

include common.mk

genDir ?= gen/
dotDir ?= dot/
dotFlags ?= $(if $(DEBUG),-v)
Tvariant ?= :cairo:cairo

vpath %.dot $(dotDir)

$(genDir)%.png $(genDir)%.svg $(genDir)%.eps : %.dot | $(genDir).
    dot $(dotFlags) 
lt; -o $@ -T'$(patsubst .%,%,$(suffix $@))$(Tvariant)'

包括common.mk是您将常规定义存储到的位置
管理目录创建,诊断等,例如

.PRECIOUS: %/. ## preempt 'unlink: ...: Is a directory'
%/. : ; $(if $(wildcard $@),,mkdir -p -- $(@D))

参考:

  • ?= = :=… - info'(make)读取makefiles'
  • vpath - info'(make)选择性搜索'
  • 仅订单前提条件(例如| $(gendir)。) - <代码> info'(make)先决条件类型'
  • .precious - info'(make)链式规则'

GNU Make lets you specify extra makefiles to read using the MAKEFILES
environment variable. Quoting from info '(make)MAKEFILES Variable':

the default goal is never taken from one of these makefiles (or any
makefile included by them) and it is not an error if the files listed
in 'MAKEFILES' are not found

if you are running 'make' without a specific makefile, a makefile
in 'MAKEFILES' can do useful things to help the built-in implicit
rules work better

As an example, with no makefile in the current directory and the
following .mk files in make's include path (e.g. via
MAKEFLAGS=--include-dir="$HOME"/.local/lib/make/) you can create
subdir gen/ and convert my_graph.dot or dot/my_graph.dot by
running:

MAKEFILES=dot.mk make gen/my_graph.png

To further save some typing it's tempting to add MAKEFILES=dot.mk
to a session environment but defining MAKEFILES in startup files
can make things completely nontransparent. For that reason I prefer
seeing MAKEFILES=… on the command line.

File: dot.mk

include common.mk

genDir ?= gen/
dotDir ?= dot/
dotFlags ?= $(if $(DEBUG),-v)
Tvariant ?= :cairo:cairo

vpath %.dot $(dotDir)

$(genDir)%.png $(genDir)%.svg $(genDir)%.eps : %.dot | $(genDir).
    dot $(dotFlags) 
lt; -o $@ -T'$(patsubst .%,%,$(suffix $@))$(Tvariant)'

The included common.mk is where you'd store general definitions to
manage directory creation, diagnostics etc., e.g.

.PRECIOUS: %/. ## preempt 'unlink: ...: Is a directory'
%/. : ; $(if $(wildcard $@),,mkdir -p -- $(@D))

References:

  • ?= = := … - info '(make)Reading Makefiles'
  • vpath - info '(make)Selective Search'
  • order-only prerequisites (e.g. | $(genDir).) - info '(make)Prerequisite Types'
  • .PRECIOUS - info '(make)Chained Rules'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文