Our
Our

Reputation: 1035

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

I have a Makefile that contains rules that are very similar to the following

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

where each target file, namely *.csv, depends on the data file and metadata files that are in the same directory as the target file.

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?

Upvotes: 0

Views: 150

Answers (1)

John Bollinger
John Bollinger

Reputation: 180508

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?

You can simply use the pattern to match the path. This presents no hardship for you at all, because that's the only part of any of your prerequisite names that corresponds to the target name in the first place. (I discount the appearance of the target name itself in the prerequisite list, as this is useless.) Thus, your pattern rule might have this form:

%/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 ...

Furthermore, if you then want easy access to the path inside the rule's recipe, you can access it via the $* automatic variable. For a target named data/processed/21.12.2021/experiment6/averaged_flow_profile.csv that is matched by such a pattern rule, $* would expand to data/processed/21.12.2021/experiment6.

Upvotes: 1

Related Questions