使用递归 make 时如何避免这些重复?

发布于 2024-12-01 20:57:36 字数 416 浏览 1 评论 0原文

假设我有一个包含两个或多个子文件夹 foobar 等的项目。我在项目的根目录下有一个 Makefile,而且在每个子目录中。

我希望某些目标(例如 allclean 等)在每个子目录中递归运行。我的顶级 Makefile 看起来像这样:

all:
    $(MAKE) -C foo all
    $(MAKE) -C bar all

clean:
    $(MAKE) -C foo clean
    $(MAKE) -C bar clean

在我看来,这里有很多重复。 有没有办法可以避免 Makefile 中如此繁琐的重复?

Suppose I have a project with two or more subfolders foo, bar, etc. I have a Makefile at the root of the project, and also in each subdirectory.

I would like to have certain targets (e.g. all, clean, etc) to run recursively in each subdirectory. My top-level Makefile looks like this:

all:
    $(MAKE) -C foo all
    $(MAKE) -C bar all

clean:
    $(MAKE) -C foo clean
    $(MAKE) -C bar clean

Seems to me there's a lot of duplication going on here. Is there a way I can avoid such tedious duplication in my Makefiles?

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

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

发布评论

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

评论(3

故笙诉离歌 2024-12-08 20:57:36

这个怎么样:

SUBDIRS=foo bar
all clean:
        for dir in $(SUBDIRS) ; do \
            $(MAKE) -C $dir $@ ; \
        done

How about this:

SUBDIRS=foo bar
all clean:
        for dir in $(SUBDIRS) ; do \
            $(MAKE) -C $dir $@ ; \
        done
许仙没带伞 2024-12-08 20:57:36

有点吓人:

SUBDIRS=foo bar
SUBDIR_TARGETS=all clean

define subdir_rule
$(2): $(1)-$(2)
$(1)-$(2):
    make -C $(1) $(2)
endef

$(foreach targ,$(SUBDIR_TARGETS),\
    $(foreach dir,$(SUBDIRS),\
        $(eval $(call subdir_rule,$(dir),$(targ)))))

A bit scary:

SUBDIRS=foo bar
SUBDIR_TARGETS=all clean

define subdir_rule
$(2): $(1)-$(2)
$(1)-$(2):
    make -C $(1) $(2)
endef

$(foreach targ,$(SUBDIR_TARGETS),\
    $(foreach dir,$(SUBDIRS),\
        $(eval $(call subdir_rule,$(dir),$(targ)))))
复古式 2024-12-08 20:57:36

我是这样做的:

SUBDIRS=foo bar baz

TARGETS = clean all whatever
.PHONY:$(TARGETS)

# There really should be a way to do this as "$(TARGETS):%:TARG=%" or something...
all: TARG=all
clean: TARG=clean
whatever: TARG=whatever

$(TARGETS): $(SUBDIRS)
    @echo $@ done

.PHONY: $(SUBDIRS)
$(SUBDIRS):
    @$(MAKE) -s -C $@ $(TARG)

Here's how I'd do it:

SUBDIRS=foo bar baz

TARGETS = clean all whatever
.PHONY:$(TARGETS)

# There really should be a way to do this as "$(TARGETS):%:TARG=%" or something...
all: TARG=all
clean: TARG=clean
whatever: TARG=whatever

$(TARGETS): $(SUBDIRS)
    @echo $@ done

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