Make:通配符匹配完整路径?
Make: wildcard to match full path?
将 make 用于通用目的(不编译)
假设我有一组具有完整路径名的文件,我想对该文件做一些事情。
something --file a/b/c/X > $< # (for example)
并且我制定了一个规则:
something-%:
something --file $*
与 "something-foo" 匹配良好,但不匹配 "something-a/b/c/foo"。有没有办法为后一种情况编写通配符规则?
手册描述how patterns match:
When the target pattern does not contain a slash (and it usually does not), directory names in the file names are removed from the file name before it is compared with the target prefix and suffix.
在您的情况下,当以 make something-a/b/c/foo
调用时,something-a/b/c/
被视为目录并被删除,因此其余部分不符合您的规则。这可以很容易地检查:
$ cat Makefile
something-%:
echo something --file $<
f%o:
echo $*
输出:
$ make something-OtherDirectory/src/foo -dr
GNU Make 4.2.1
...
Considering target file 'something-OtherDirectory/src/foo'.
File 'something-OtherDirectory/src/foo' does not exist.
Looking for an implicit rule for 'something-OtherDirectory/src/foo'.
Trying pattern rule with stem 'o'.
Found an implicit rule for 'something-OtherDirectory/src/foo'.
Finished prerequisites of target file 'something-OtherDirectory/src/foo'.
Must remake target 'something-OtherDirectory/src/foo'.
echo something-OtherDirectory/src/o
...
请注意,它与其他模式规则匹配 o
。
如果您的模式 包含斜杠,您可以按照自己的方式使用。为了完整起见,如果您的规则基于文件,我还将定义一个先决条件,如果它不生成真实的输出文件,则将目标声明为虚假:
$ cat Makefile
.PHONY: something/%
something/%: %
echo something --file $<
输出:
$ make something/OtherDirectory/src/foo.c
echo something --file OtherDirectory/src/foo.c
something --file OtherDirectory/src/foo.c
将 make 用于通用目的(不编译)
假设我有一组具有完整路径名的文件,我想对该文件做一些事情。
something --file a/b/c/X > $< # (for example)
并且我制定了一个规则:
something-%:
something --file $*
与 "something-foo" 匹配良好,但不匹配 "something-a/b/c/foo"。有没有办法为后一种情况编写通配符规则?
手册描述how patterns match:
When the target pattern does not contain a slash (and it usually does not), directory names in the file names are removed from the file name before it is compared with the target prefix and suffix.
在您的情况下,当以 make something-a/b/c/foo
调用时,something-a/b/c/
被视为目录并被删除,因此其余部分不符合您的规则。这可以很容易地检查:
$ cat Makefile
something-%:
echo something --file $<
f%o:
echo $*
输出:
$ make something-OtherDirectory/src/foo -dr
GNU Make 4.2.1
...
Considering target file 'something-OtherDirectory/src/foo'.
File 'something-OtherDirectory/src/foo' does not exist.
Looking for an implicit rule for 'something-OtherDirectory/src/foo'.
Trying pattern rule with stem 'o'.
Found an implicit rule for 'something-OtherDirectory/src/foo'.
Finished prerequisites of target file 'something-OtherDirectory/src/foo'.
Must remake target 'something-OtherDirectory/src/foo'.
echo something-OtherDirectory/src/o
...
请注意,它与其他模式规则匹配 o
。
如果您的模式 包含斜杠,您可以按照自己的方式使用。为了完整起见,如果您的规则基于文件,我还将定义一个先决条件,如果它不生成真实的输出文件,则将目标声明为虚假:
$ cat Makefile
.PHONY: something/%
something/%: %
echo something --file $<
输出:
$ make something/OtherDirectory/src/foo.c
echo something --file OtherDirectory/src/foo.c
something --file OtherDirectory/src/foo.c