【发布时间】:2020-08-20 23:45:31
【问题描述】:
我有一个模式规则,可以将打字稿文件中的类型定义转换为 JSON 模式文件。进行这种转换的程序需要两个参数:
-
source file的名称 - 要从该文件中提取的
typename
我决定将所需的类型名编码为目标文件的名称。
<--------- Typename ---------> <------- source file ------->
BlockLanguageGeneratorDocument.json : block-language.description.ts
我定义了这个模式规则来进行转换:
%.json : %.ts
# $^ is the name of the input file
# $(notdir $(basename $@)) is the filename of the target file (without the .json suffix)
$(TYPESCRIPT_JSON_SCHEMA_BIN) --path $^ --type $(notdir $(basename $@)) > "$@.json"
遗憾的是,与模式规则相比,我设置为依赖项的<typename>.json: <sourcefile> 规则更具体,因此模式规则永远不会执行。因此,我决定将转换封装在 define CONVERT_COMMAND 中,并在上述每个定义中简单地使用它:
BlockLanguageGeneratorDocument.json : block-language.description.ts
$(CONVERT_COMMAND)
虽然这确实有效,但重复让我觉得很难看。有没有办法声明从一个文件到另一个文件的依赖关系,同时仍然更喜欢模式规则?
最小重现:使用make BlockLanguageGeneratorDocument.json Unrelated.json 运行此程序,观察echo 永远不会执行。
block-language.description.ts :
touch $@
another.description.ts :
touch $@
%.json : %.ts
echo "generic target"
BlockLanguageGeneratorDocument.json : block-language.description.ts
Unrelated.json : another.description.ts
如果有帮助:调试输出如下。
❯❯❯ make --debug=verbose BlockLanguageGeneratorDocument.json
GNU Make 4.3
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Reading makefiles...
Reading makefile 'Makefile'...
Reading makefile 'Makefile.json' (search path) (no ~ expansion)...
Reading makefile '../../Makefile.common' (search path) (no ~ expansion)...
Updating makefiles....
Updating goal targets....
Considering target file 'BlockLanguageGeneratorDocument.json'.
Considering target file 'block-language.description.ts'.
Finished prerequisites of target file 'block-language.description.ts'.
No need to remake target 'block-language.description.ts'.
Finished prerequisites of target file 'BlockLanguageGeneratorDocument.json'.
Prerequisite 'block-language.description.ts' is newer than target 'BlockLanguageGeneratorDocument.json'.
Must remake target 'BlockLanguageGeneratorDocument.json'.
Successfully remade target file 'BlockLanguageGeneratorDocument.json'.
make: 'BlockLanguageGeneratorDocument.json' is up to date.
【问题讨论】:
-
我不明白。以
BlockLanguageGeneratorDocument.json为目标,您有多少条规则(模式与否)?如果你有不止一个,为什么会这样?注意:在您的模式规则的配方中,您可能应该重定向到"$@"而不是"$@.json"。