如何访问 Makefile 中目标的相对路径?

How can I access to the relative path of the target in Makefile?

我有一个包含与以下规则非常相似的规则的 Makefile

data/processed/21.12.2021/experiment6/averaged_flow_profile.csv: data/processed/21.12.2021/experiment6/PIVlab_output.mat \
    data/processed/21.12.2021/experiment6/parameters.csv \
    data/processed/21.12.2021/experiment6/PIVlab_set_10.01.2022.mat \
    data/processed/21.12.2021/experiment6/piv21122021.005.exp6.roimask.tif \
    data/processed/21.12.2021/experiment6/written_piv21122021.005.exp6.mp4 \
    data/processed/21.12.2021/experiment6/PIVlab_output.mat \
    code/piv/get_flowProfileFromPIV.R
    code/piv/get_flowProfileFromPIV.R data/processed/21.12.2021/experiment6/PIVlab_output.mat \
    data/processed/21.12.2021/experiment6/parameters.csv \
    data/processed/21.12.2021/experiment6/PIVlab_set_10.01.2022.mat \
    data/processed/21.12.2021/experiment6/piv21122021.005.exp6.roimask.tif \
    data/processed/21.12.2021/experiment6/averaged_flow_profile.csv

其中每个目标文件,即 *.csv,取决于与目标文件位于同一目录中的数据文件和元数据文件。

如何访问目标的相对路径,即“data/processed/21.12.2021/experiment6”,以便对这些相似的规则进行模式匹配?

How can I access to the relative path of the target, namely "data/processed/21.12.2021/experiment6", so that I can pattern match these similar rules?

您可以简单地使用模式来匹配路径。这对您来说一点也不困难,因为这是您的任何先决条件名称中唯一与目标名称一开始对应的部分。 (我不考虑目标名称本身在先决条件列表中的出现,因为这是无用的。)因此,您的模式规则可能具有以下形式:

%/averaged_flow_profile.csv: \
    %/PIVlab_output.mat \
    %/parameters.csv \
    %/PIVlab_set_10.01.2022.mat \
    %/piv21122021.005.exp6.roimask.tif \
    %/written_piv21122021.005.exp6.mp4 \
    code/piv/get_flowProfileFromPIV.R
        # Recipe ...

此外,如果您随后想要轻松访问规则配方中的路径,您可以通过 $* 自动变量访问它。对于与此类模式规则匹配的名为 data/processed/21.12.2021/experiment6/averaged_flow_profile.csv 的目标,$* 将扩展为 data/processed/21.12.2021/experiment6.