简体   繁体   English

GNU make:如何确保模式只匹配没有目录的文件名

[英]GNU make: how to ensure pattern to match only filename without directory

I just want to copy files from other directories (before doing something).我只想从其他目录复制文件(在做某事之前)。 Because it's tedious to write the copy command for each file, I tried因为每个文件都写复制命令很繁琐,所以我试了

%: ../src1/%
    @echo cp $^ .

%: ../src2/%
    @echo cp $^ .

all: file1 file2 file3 file4
    # do something

but this doesn't work because make tries to look into ../src2/../src2/../src2/../. . . .但这不起作用,因为make试图查看../src2/../src2/../src2/../. . . . ../src2/../src2/../src2/../. . . . (I included echo for testing to prevent actual copying from happening. I keep forgetting what the "dry run" command line options is. . .) (我包括用于测试的echo以防止实际复制发生。我一直忘记“空运行”命令行选项是什么......)

I naïvely thought that there must be a way to force matches only to filenames that don't include directories.我天真地认为必须有一种方法可以强制只匹配不包含目录的文件名。

Is there a way?有办法吗?

You can mark the "make anything" rules terminal with a double colon:您可以用双冒号标记“make anything”规则终端

%:: ../src1/%
    @echo cp $^ .

%:: ../src2/%
    @echo cp $^ .

This does not answer your specific question of how to get Make to match only filenames without directories, but it does get Make to do what you want.这并没有回答你关于如何让 Make 只匹配没有目录的文件名的具体问题,但它确实让 Make 做你想做的事。

There is another approach that works and is closer to what you asked for: add pattern rules to satisfy Make:还有另一种方法可行并且更接近您的要求:添加模式规则以满足 Make:

../src1/%:
    @: # do nothing

%: ../src1/%
    @echo cp $^ .

EDIT: Or better still, us one dummy pattern rule to cover all source directories:编辑:或者更好的是,我们使用一个虚拟模式规则来覆盖所有源目录:

../%:
        @: # do nothing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM