Makefile:在先决条件中使用目标文件路径

发布于 2024-12-11 02:04:05 字数 1020 浏览 0 评论 0原文

我有以下项目结构:

+-Makefile
+-src/
  +-a/
  | +-foo.py
  +-b/
  | +-foo.py
  +-c/
  | +-foo.py

每个 foo.py 文件都是一个具有完全相同名称的不同文件(即它们具有不同的索引节点,但字面上都称为“foo.py” - 当然实际上是这样)名称不是 foo.py,这只是一个示例)。

我希望创建一个 GNU Makefile 规则,该规则在运行时会创建以下结构:

+-Makefile
+-src/
  +-a/
  | +-foo.py
  | +-a.zip
  +-b/
  | +-foo.py
  | +-b.zip
  +-c/
  | +-foo.py
  | +-c.zip

这是我能够弄清楚的最接近的结构,尽管它当然会由于在似乎不允许的先决条件:

SRCDIR = src/
PRJ_DIRS = a b c
SRC_FILE = foo.py

# This next rather nasty line turns PRJ_DIRS in to, e.g., src/a/a.zip etc.
ZIP_FILES = $(addprefix $(SRCDIR),$(join $(PRJ_DIRS),$(PRJ_DIRS:%=/%.zip)))'

build: $(ZIP_FILES)

# Next line crashes because we can't use $@ in the prereq
$(ZIP_FILES): $(addprefix $(dir $@), $(SRC_FILE))
        touch $@

所以提出问题的一种方法是: 如何编写一条规则,使用每个相应的 foo.py 文件作为构建适当的 .zip 文件的先决条件?

或者,一个可能实际上正在寻找的更直接的问题从错误的角度来看: 如何引用先决条件中构建的特定目标?

I have the following project structure:

+-Makefile
+-src/
  +-a/
  | +-foo.py
  +-b/
  | +-foo.py
  +-c/
  | +-foo.py

Each foo.py file is a different file with exactly the same name (ie they have different inodes but are all literally called 'foo.py' - although of course in reality the name is not foo.py, which is just an example).

I wish to create a GNU Makefile rule which, when run, creates the following structure:

+-Makefile
+-src/
  +-a/
  | +-foo.py
  | +-a.zip
  +-b/
  | +-foo.py
  | +-b.zip
  +-c/
  | +-foo.py
  | +-c.zip

This is the closest I have been able to figure out, although of course it fails due to using a target variable in the prereqs which doesn't seem to be allowed:

SRCDIR = src/
PRJ_DIRS = a b c
SRC_FILE = foo.py

# This next rather nasty line turns PRJ_DIRS in to, e.g., src/a/a.zip etc.
ZIP_FILES = $(addprefix $(SRCDIR),$(join $(PRJ_DIRS),$(PRJ_DIRS:%=/%.zip)))'

build: $(ZIP_FILES)

# Next line crashes because we can't use $@ in the prereq
$(ZIP_FILES): $(addprefix $(dir $@), $(SRC_FILE))
        touch $@

So one way to put the question:
How can I write a rule the uses each corresponding foo.py file as a prereq to build the appropriate .zip file?

Or, a more direct question that might actually be looking at this from the wrong angle:
How can I refer to the specific target that's being built in the prereqs?

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

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

发布评论

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

评论(1

も星光 2024-12-18 02:04:05

好吧,既然你认为二次扩展是一种拼凑,那么 $(foreach ...)$(eval ...) 再次可以拯救你(一个非常强大的组合) 。

将您的规则替换为以下内容,您应该得到您想要的。

define rule
$(SRCDIR)$(1)/$(1).zip: $(SRCDIR)$(1)/$(SRC_FILE)
    touch $@
endef

$(foreach dir, $(PRJ_DIRS), $(eval $(call rule,$(dir))))

Okay, since you consider secondary expansion a kludge, it's $(foreach ...) and $(eval ...) to the rescue again (a very powerful combination).

Replace your rule with the following and you should get what you want.

define rule
$(SRCDIR)$(1)/$(1).zip: $(SRCDIR)$(1)/$(SRC_FILE)
    touch $@
endef

$(foreach dir, $(PRJ_DIRS), $(eval $(call rule,$(dir))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文