Create rule in makefile for just a set of files
Asked Answered
P

2

22

I am writing a Makefile, and I want to use a generic rule with wildcards, like

%: bkp/%
    cp $< $@

But I wanted this rule to be valid only for a few specific files. I wanted to define a variable with the list, for example

file_list = foo.c bar.c zzz.c

and configure the rule so it is only valid for files that are listed in this variable. How do I do that?

Pico answered 7/10, 2010 at 3:3 Comment(6)
You list just '.c' files; are there other '.c' files in the directory? Are there any other files that need to be backed up? If you add another '.c' file, will it need to saved too? And why aren't you using a version control system (VCS) to protect your work?Laurielaurier
@Jonathan: ...how do you infer that (s)he's not using a VCS?Acariasis
@Jonathan If you really need to know, this is what I am doing, and why I could not simply use a wildcard: I created a "myconfig" directory in my home directory that is on a VCS. This directory holds copies of files in my home directory, such as ~/.bashrc, ~/.emacs and others. I want the rule to copy the original file into this directory, but I want to make it safe, and avoid e.g. copying a Makefile from my home directory over the Makefile in this directory.Pico
@j_random: it was inference from the name of the sub-directory, bkp, which suggests backup, which in turn suggests not having a VCS. Apparently, that was a wrong inference - but the question was disguising the reality, and therefore gave false impressions.Laurielaurier
The way I handle the 'dot-files' in my home directory - not necessarily the best, and certainly not the only way to do it - is to store the files under a VCS in my $HOME/etc directory. As far as the VCS is concerned, the files in the etc directory are the master, and the makefile in the etc directory is responsible for copying the files up to $HOME/etc (actually, the makefile saves the old version as $HOME/.whatnot.old too). When the version in $HOME is modified, I copy the modified version from $HOME into $HOME/etc and update the VCS. Shout if you want more info (see my profile).Laurielaurier
Sorry for the confusing question, Jonathan. It actually makes no sense to manage a backup directory that way, I only realized it later. :) I was just trying to make the question more general, and less dependent on my specific purposes... I imagine someone might like for example, to have a set of .c files compiled with specific flags, if not a specific compiler, but having these files residing on the same directory. That would be another application. I am definitely interested in looking at this Makefile of yours!Pico
A
31

You want a static pattern rule:

file_list = foo.c bar.c zzz.c

$(file_list): %: bkp/%
        cp $< $@

The syntax is very similar to the implicit pattern rule you were using. And yes, it's generally safer (more predictable).

Acariasis answered 7/10, 2010 at 3:24 Comment(0)
P
5

Of course, 5 minutes later I found the answer myself... :)

What we need is a static pattern rule.

http://www.gnu.org/software/make/manual/make.html#Static-Pattern

So the example would be solved with

$(file_list) : % : bkp/%
    cp $< $@
Pico answered 7/10, 2010 at 3:25 Comment(1)
file_list -> $(file_list) and I'll +1.Acariasis

© 2022 - 2024 — McMap. All rights reserved.