【问题标题】:Make execute commands on folder context multiple times在文件夹上下文中多次执行命令
【发布时间】:2023-03-06 19:35:01
【问题描述】:

我在proj 根目录下有一个makefile

文件夹proj是主文件夹,其下有ws-ledtools-ext等文件夹,其中包含docker文件。 另外,还有Makefile在root下,需要运行所有的命令。

这是文件夹结构

proj
 - ws-led
  — Dockerfile
 - tools-ext
  — Dockerfile    
- Makefile

我需要的是 cd 到 rot 下的每个文件夹(我们还有更多)并运行:

docker build <folder name> .

示例:(与手动运行以下命令完全一样)

cd ws-led
docker build -t ws-led .

cd tools-ext
docker build -t tools-ext .

我尝试以下方法(也许我在 Makefile 同一级别的所有文件夹上运行而不是 repo 参数)

喜欢(CURDIR)……

all: pre docker-build
.PHONY: pre docker-build

repos := ws-led tools-ext

pre:
    $(patsubst %,docker-build,$(repos))

docker-build:pre
    cd $*; docker build -t $* . >&2 | tee docker-build

使用此即时消息时出现错误:

invalid argument "." for "-t, --tag" flag: invalid reference format

知道这里有什么问题吗?或者我可以做得更好?

由于我有很多存储库/文件夹,我想使用 make 来处理它

【问题讨论】:

  • 在执行 bash 子 shell 之前,您需要注意 make 如何使用引号和特殊字符。我忘记了所有的规则.....
  • $s 是魔法;所以$?可能需要 $$?.

标签: linux bash shell makefile gnu-make


【解决方案1】:

有不止一种方法可以做到这一点。

您可以使用 bash for 循环:

docker-build:
    for dir in $(repos); do cd $$dir; docker build -t $$dir . >&2 | tee docker-build; done

或使用模式规则(或在本例中为静态模式规则):

REPO_BUILDS := $(addsuffix -build, $(repos))

docker-build: $(REPO_BUILDS)

.PHONY: $(REPO_BUILDS)
$(REPO_BUILDS): %-build:
    cd $*; docker build -t $* . >&2 | tee docker-build

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多