从另一个 makefile 中仅包括特定目标?

huangapple go评论57阅读模式
英文:

Include only specific targets from another makefile?

问题

假设我有两个相邻的目录 AB
在目录 A 中有 makefile_a,而在目录 B 中有 makefile_b

makefile_b 进行一些预处理,其输出稍后被makefile_a中的目标使用。因此,我想在 makefile_a 中定义某些目标,这些目标依赖于 makefile_b 中的目标。

通常,我会在 makefile_a 中使用 include ../B/makefile_b 这行,如在 make 文档中的 include 部分所描述。
然而,我不想在 makefile_a 的命名空间中引入 makefile_b 中定义的所有目标;我只想导入特定的目标。例如,在 makefile_amakefile_b 中都定义了具有相同名称的目标,这可能会冲突。出于这个原因和其他原因,我只想导入特定的目标,而不是所有目标。

如何才能做到这一点呢?我在文档中没有看到任何解释这个的方法。我考虑在 B 中创建第二个 makefile,类似于 makefile_b_export,被同时包括在 makefile_amakefile_b 中,只包含我希望包括在 makefile_b 中的目标,但这将是一种妥协,会带来自己的一系列问题,比如导出的目标如何依赖于 makefile_b 中定义的目标,而不会创建导入循环。

有没有一种适当的方式来处理这种情况,使用 make?你建议如何以最干净的方式处理这种情况?

英文:

Suppose I have two adjacent directories A and B.
In A is makefile_a, and in B is makefile_b.

makefile_b does some preprocessing whose output is later used by the targets of makefile_a. So I would like to define certain targets in makefile_a to depend on targets in makefile_b.

Normally I would do this with, in makefile_a, the line include ../B/makefile_b, as described in make's documentation on include.
However, I don't want to pollute the namespace of makefile_a with all the targets defined in makefile_b; I only want to import the one target in question. For example, there are targets with identical names defined in both makefile_a and makefile_b which would clash. For that and other reasons I only want to import the one target in question, not all of them.

How can this be done? I don't see any way to do that explained in the documentation. I considered making a second makefile in B, something like makefile_b_export, to be included by both makefile_a and makefile_b, and contain only the targets that I wish to include in makefile_b, but that would be a hack and presents its own set of issues, like how do the exported targets depend on those defined in makefile_b without creating an import cycle.

Is there a proper way to handle this scenario using make? What do you suggest would be the cleanest way to handle this situation?

答案1

得分: 1

只有 part 文件,或只有 some 目标是无法包含的。

您可以将在其中共享的内容放入单独的文件,如 shared_b.mk,然后将 include shared_b.mk 添加到两个 makefile 中。

或者,您可以使用 ifdef 来跳过包含非共享内容,如果某个变量已设置,然后在包含 makefile_b 之前设置该变量。

或者,您可以不包含 makefile,而是让 makefile_a 通过在子目录中递归调用 make 的规则来构建 makefile_b 中的特定目标:

.PHONY: targ_b1 targ_b2
targ_b1 targ_b2:
        $(MAKE) -C ../b $@

或其他方法。

英文:

There is no way to include just "part" of a file, or just "some" targets.

You can either take the content that's shared in put it into a separate file such as shared_b.mk and then add include shared_b.mk to both the makefiles.

Or, you can use ifdef to skip including the non-shared content if a variable is set, then before you include makefile_b you can set that variable.

Or, instead of including the makefiles you can have makefile_a build the specific target(s) in makefile_b by having a rule that invokes make recursively in that subdirectory:

.PHONY: targ_b1 targ_b2
targ_b1 targ_b2:
        $(MAKE) -C ../b $@

or whatever.

huangapple
  • 本文由 发表于 2023年3月4日 04:29:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定