使虚拟目标检查现有文件的使用期限? [英] Make dummy target that checks the age of an existing file?

查看:76
本文介绍了使虚拟目标检查现有文件的使用期限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用make来控制统计分析中的数据流.如果我的原始数据在目录./data/raw_data_files中,并且我有一个数据操作脚本可以在./cache/clean_data上创建清除的数据缓存.生成规则类似于:

I'm using make to control the data flow in a statistical analysis. If have my raw data in a directory ./data/raw_data_files, and I've got a data manipulation script that creates cleaned data cache at ./cache/clean_data. The make rule is something like:

cache/clean_data:
  scripts/clean_data 

我不想使用make或任何我的数据处理脚本来触摸./data/中的数据. make是否有任何方法可以为cache/clean_data创建依赖项,从而仅检查./data/中的特定文件是否比上次make ran更新?

I do not want to touch the data in ./data/, either with make, or any of my data munging scripts. Is there any way in make to create a dependency for the cache/clean_data that just checks whether specific files in ./data/ are newer than last time make ran?

推荐答案

如果clean_data是单个文件,则让它依赖于所有数据文件:

If clean_data is a single file, just let it depend on all data files:

cache/clean_data: data/*
    scripts/clean_data

如果它是包含多个清除文件的目录,则最简单的方法是编写一个图章文件,并使其依赖于您的数据文件:

If it is a directory containing multiple cleaned files, the easiest way is to write a stamp file and have that depend on your data files:

cache/clean_data-stamp: data/*
    scripts/clean_data
    touch cache/clean_data-stamp

请注意,如果一个数据文件发生更改,这将重新生成所有clean_data文件.如果数据和清除的文件之间存在一对一的映射关系,则可以使用更精细的方法. 《 GNU Make手册》有一个此示例的体面.这是一个改编:

Note that this regenerates all clean_data files if one data file changes. A more elaborate approach is possible if you have a 1-to-1 mapping between data and cleaned files. The GNU Make Manual has a decent example of this. Here is an adaptation:

 DATAFILES:= $(wildcard data/*)
 CACHEFILES:= $(patsubst data/%,cache/clean_data/%,$(DATAFILES))

 cache/clean_data/% : data/%
         scripts/clean_data --input $< --output $@

 all: $(CACHEFILES)

在这里,我们使用通配符以获得data下所有文件的列表.然后,我们使用将数据路径替换为缓存路径patsubst .我们告诉make如何通过静态模式规则,最后,我们定义一个目标all,该目标将生成所有必需的缓存文件.

Here, we use wildcard to get a list of all files under data. Then we replace the data path with the cache path using patsubst. We tell make how to generate cache files via a static pattern rule, and finally, we define a target all which generates all the required cache files.

当然,您也可以在Makefile(CACHEFILES:= cache/clean_data/a cache/clean_data/b)中明确列出CACHEFILES,但是,如果可能的话,让make自动处理通常会更方便.

Of course you can also list your CACHEFILES explicitly in the Makefile (CACHEFILES:= cache/clean_data/a cache/clean_data/b), but it is typically more convenient to let make handle that automatically, if possible.

请注意,这个复杂的示例可能仅适用于GNU Make,不适用于Windows的nmake.有关更多信息,请查阅 GNU Make手册,它对您所有的人都是很好的资源Makefile需求.

Notice that this complex example probably only works with GNU Make, not in Windows' nmake. For further info, consult the GNU Make Manual, it is a great resource for all your Makefile needs.

这篇关于使虚拟目标检查现有文件的使用期限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆