gpt4 book ai didi

c - 如何删除 Makefile 中的重复项?

转载 作者:行者123 更新时间:2023-12-01 23:53:03 25 4
gpt4 key购买 nike

有没有办法在 Makefile 中简化这种重复?

duo = ./node_modules/.bin/duo

build: lib/background/build lib/page/build lib/popup/build

lib/background/build: lib/background/build/build.js lib/background/build/build.css
lib/page/build: lib/page/build/build.js lib/page/build/build.css
lib/popup/build: lib/popup/build/build.js lib/popup/build/build.css

lib/background/build/build.js: lib/background/index.js node_modules component.json
@mkdir -p lib/background/build
@$(duo) lib/background/index.js > lib/background/build/build.js

lib/page/build/build.js: lib/page/index.js node_modules component.json
@mkdir -p lib/page/build
@$(duo) lib/page/index.js > lib/page/build/build.js

lib/popup/build/build.js: lib/popup/index.js node_modules component.json
@mkdir -p lib/popup/build
@$(duo) lib/popup/index.js > lib/popup/build/build.js

lib/background/build/build.css: lib/background/index.css node_modules component.json
@mkdir -p lib/background/build
@$(duo) lib/background/index.css | $(myth) > lib/background/build/build.css

lib/page/build/build.css: lib/page/index.css node_modules component.json
@mkdir -p lib/page/build
@$(duo) lib/page/index.css | $(myth) > lib/page/build/build.css

lib/popup/build/build.css: lib/popup/index.css node_modules component.json
@mkdir -p lib/popup/build
@$(duo) lib/popup/index.css | $(myth) > lib/popup/build/build.css

基本上,我想从顶层运行一个简单的 make build 命令,它只在必要时重建这些子项目。我不想为每个子项目都使用 Makefile,因为这也是重复的。我尝试过的与通配符路径相关的一切都没有成功,所以想知道是否有办法做到这一点。例如,我尝试做这样的事情(对于 js 和 css 都类似)但没有成功:

js = $(shell find lib test -type f -name '*.js' ! -path "*build.js")

$(js)/build/build.js: node_modules component.json
# somehow get the directory such as lib/background based on the make command?
local dir=$(shell dirname $(shell dirname $@))
@mkdir -p $(dir)/build
@$(duo) $(dir)/index.js > $(dir)/build/build.js

有什么想法可以让这个变干吗?

最佳答案

一个好的开始是停止重复目标等。在规则主体本身中,并为它们使用自动变量。所以 '$@' 为目标文件名,'$(@D)' 为目标文件名的目录路径(如 dirname)等

哪个让你:

duo = ./node_modules/.bin/duo

build: lib/background/build lib/page/build lib/popup/build

lib/background/build: lib/background/build/build.js lib/background/build/build.css
lib/page/build: lib/page/build/build.js lib/page/build/build.css
lib/popup/build: lib/popup/build/build.js lib/popup/build/build.css

lib/background/build/build.js: lib/background/index.js node_modules component.json
@mkdir -p '$(@D)'
@$(duo) lib/background/index.js > '$@'

lib/page/build/build.js: lib/page/index.js node_modules component.json
@mkdir -p '$(@D)'
@$(duo) lib/page/index.js > '$@'

lib/popup/build/build.js: lib/popup/index.js node_modules component.json
@mkdir -p '$(@D)'
@$(duo) lib/popup/index.js > '$@'

lib/background/build/build.css: lib/background/index.css node_modules component.json
@mkdir -p '$(@D)'
@$(duo) lib/background/index.css | $(myth) > '$@'

lib/page/build/build.css: lib/page/index.css node_modules component.json
@mkdir -p '$(@D)'
@$(duo) lib/page/index.css | $(myth) > '$@'

lib/popup/build/build.css: lib/popup/index.css node_modules component.json
@mkdir -p '$(@D)'
@$(duo) lib/popup/index.css | $(myth) > '$@'

然后意识到 pattern rules当您拥有共享文件名模式和类似规则主体的目标和先决条件时,它们会很有帮助,您也可以开始使用它们。他们还给你一个额外的 automatic variable .

你得到这个(中间)阶段:

duo = ./node_modules/.bin/duo

build: lib/background/build lib/page/build lib/popup/build

lib/background/build: lib/background/build/build.js lib/background/build/build.css
lib/page/build: lib/page/build/build.js lib/page/build/build.css
lib/popup/build: lib/popup/build/build.js lib/popup/build/build.css

%/build/build.js: %/index.js node_modules component.json
@mkdir -p '$(@D)'
@$(duo) '$*'/index.js > '$@'

%/build/build.js: %/index.js node_modules component.json
@mkdir -p '$(@D)'
@$(duo) '$*'/index.js > '$@'

%/build/build.js: %/index.js node_modules component.json
@mkdir -p '$(@D)'
@$(duo) '$*'/index.js > '$@'

%/build/build.css: %/index.css node_modules component.json
@mkdir -p '$(@D)'
@$(duo) '$*'/index.css | $(myth) > '$@'

%/build/build.css: %/index.css node_modules component.json
@mkdir -p '$(@D)'
@$(duo) '$*'/index.css | $(myth) > '$@'

%/build/build.css: %/index.css node_modules component.json
@mkdir -p '$(@D)'
@$(duo) '$*'/index.css | $(myth) > '$@'

然后你会看到你实际上只有两个重复的规则,所以你将它们组合起来。

duo = ./node_modules/.bin/duo

build: lib/background/build lib/page/build lib/popup/build

lib/background/build: lib/background/build/build.js lib/background/build/build.css
lib/page/build: lib/page/build/build.js lib/page/build/build.css
lib/popup/build: lib/popup/build/build.js lib/popup/build/build.css

%/build/build.js: %/index.js node_modules component.json
@mkdir -p '$(@D)'
@$(duo) '$*'/index.js > '$@'

%/build/build.css: %/index.css node_modules component.json
@mkdir -p '$(@D)'
@$(duo) '$*'/index.css | $(myth) > '$@'

然后,由于 make 无法像人们希望的那样处理目录目标/先决条件,您可以删除 lib/background/buildlib/page/buildlib/popup/build 中间目标,仅将实际文件列为 build 的先决条件。

duo = ./node_modules/.bin/duo

build: lib/background/build/build.js lib/background/build/build.css \
lib/page/build/build.js lib/page/build/build.css \
lib/popup/build/build.js lib/popup/build/build.css

%/build/build.js: %/index.js node_modules component.json
@mkdir -p '$(@D)'
@$(duo) '$*'/index.js > '$@'

%/build/build.css: %/index.css node_modules component.json
@mkdir -p '$(@D)'
@$(duo) '$*'/index.css | $(myth) > '$@'

我可能应该提一下,我没有对此进行测试(因为不想模拟目录布局等),但转换是直接的,概念也相当简单,所以它应该可以正常工作。但一切皆有可能。

要清理 build 先决条件,您可以使用类似的东西:

build: $(foreach d,background page popup,$(addprefix lib/$d/build/,build.js build.css))

关于c - 如何删除 Makefile 中的重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25536683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com