makefile - 如何执行所有匹配通配符的 makefile 目标

标签 makefile

给定一个 Makefile:

all: build/a build/b build/c    # need to change this to    all: build/*

build/a:
   ...

build/b:
   ...

build/c:
   ...

我想更改 all 目标以自动构建所有匹配 build/* 的目标。 This question看起来非常相似,除了它打印结果而不是对其进行操作。另外,我不确定这个答案是否适用于 Linux 和 Mac。

最佳答案

Make 没有内置这样的功能。而且,事实上,手动更新目标列表比任何替代解决方案都更简洁、更容易。就个人而言,我可能会从这样的事情开始:

# second expansion is needed to get the value of
# $(targets) after the whole file was preprocessed
.SECONDEXPANSION:
all: $$(targets)

targets += build/a
build/a:
    ...
targets += build/b
build/b:
    ...
targets += build/c
build/c:
    ...

我认为为每个目标添加一行 (targets+=xxx) 的要求不会让您感到厌烦。

但是,这是我们自己“预处理”Makefile 的方式:

# assume all targets are explicitly defined in Makefile
targets != grep -o '^ *build/\w* *:' Makefile | sed 's/^ *//;s/ *:$$//'
all: $(targets)

build/a:
    ...

build/b:
    ...

build/c:
    ...

当然,如果目标在包含的文件中或包含替换等,这将失败。但是,它适用于“简单情况”。

关于makefile - 如何执行所有匹配通配符的 makefile 目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55349222/

相关文章:

linux - 未通过目标时打印默认目标

unix - Makefile $@ 变量-它是做什么用的?

linux - 如何理解Linux内核模块的 "Building Separate Files"?

c - make 出了什么问题?

haskell - 如何为 Haskell 语言程序制作 Makefile?

build - 哪种构建系统对跨平台驱动程序、库和 GUI 构建的支持最好?

c - Makefile:使所有 '.c' 文件依赖于同名头文件的有效方法?

makefile - make : missing separator 的错误消息

c++ - 如何创建库?

c++ - Makefile 错误 - 链接器输入文件未使用