我有一个模式规则,可以将typescript文件中的类型定义转换为JSON模式文件。进行此转换的程序需要两个参数:
source file
的名称- 要从该文件中提取的
typename
我决定将所需的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.
如果没有看到一个最小的完整示例,很难回答这个问题,但我认为我看到了问题。此模式规则:
%.json : %.ts
...whatever...
使用CCD_ 7来构建CCD_。Make不会尝试使用它来构建BlockLanguageGeneratorDocument.json
,因为没有BlockLanguageGeneratorDocument.ts
。
我想你要找的是:
%.json :
@echo do various things with $@ and $^
BlockLanguageGeneratorDocument.json : block-language.description.ts
请注意,第二条规则没有配方,它只是提供了先决条件。
假设从依赖项到目标名称的映射定义良好,则可以编写该部分的脚本,将输出发送到生成文件(比如deps.mk
(中,并将其包含在主生成文件中。
名称映射似乎有点像。。。
<text-with-dashes>.description.ts --> <Capitalized text without dashes>.GeneratorDocument.json
下面的perl脚本从参数列表中读取每个.ts
文件名,并生成一个合适的依赖项。。。
#!/usr/bin/perl -w
# -*- perl -*-
use strict;
while (defined(my $file = shift(@ARGV))) {
my($d) = ($file =~ m,^(.*).description.ts,);
my @d = split("-",$d);
s/(.)/uL$1/ for @d;
$d = join("",@d);
print "nall: $d"."GeneratorDocument.jsonn$d"."GeneratorDocument.json: $filent$(CONVERT_COMMAND)n";
}
如果我们将上面的脚本命名为mapname.pl
,那么。。。
./mapname.pl block-language.description.ts
应该给出输出。。。
all: BlockLanguageGeneratorDocument.json
BlockLanguageGeneratorDocument.json: block-language.description.ts
$(CONVERT_COMMAND)
现在你的主要makefile是…
ALL_TS_FILES := $(wildcard *.description.ts)
MAPNAME := ./mapname.pl
all:
include deps.mk
deps.mk: $(ALL_TS_FILES) $(MAPNAME)
$(MAPNAME) $(ALL_TS_FILES) > $@.tmp
diff -q $@ $@.tmp || mv $@.tmp $@