24

I have a bunch of applications that are built with the same type of make rule:

apps = foo bar baz

all: $(apps)

foo: foo.o $(objects)
    $(link)

bar: bar.o $(objects)
    $(link)

baz: baz.o $(objects)
    $(link)

If they had an extension (for example .x) I could make a pattern rule like:

%.x: %.o $(objects)
    $(link)

and I wouldn't have to write out a new rule for each app.

But they don't have an extension, and I'm pretty sure that:

%: %.o $(objects)
    $(link)

won't work (because it specifies that to build any file you can use this rule).

Is there anyway to specify one rule that will cover all the $(apps) build rules?

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319

3 Answers3

29

This looks like a job for a static pattern rule:

$(apps) : % : %.o $(objects)
    $(link)
Beta
  • 96,650
  • 16
  • 149
  • 150
8
%: %.o $(objects)
    $(link)

The above should work.

You can limit the scope of the rule by converting it into a static pattern rule, so that it is only considered for your list of targets:

$(apps) : % : %.o $(objects) # only consider this for $(apps) targets
    $(link)
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
0

not an answer to what you are looking for , but a reason that might explain why such level of generic code might not yield good results. ....

static patterns rely on the presence of a stem to match and build a dependency chain. pretty much in the same manner as the implicit rules (which are used for targets that dont have any recipie.)

i see what you were trying to achive , making a generic rule that will satisfy all the target checks for object and link in your code.

something like this ::

 % : % : $(rule1)
         echo / generic code ;

so that it gets invoked for all apps in differnt scenarios

since you dont want to add a extension (this becomes the root of some issues ) the issue with this is that the target will get reflected in the dependency as well since there will be no way of differentiating the dependencies form the targets.

hence if you did try that i think you will get here ...

 $ make -nf mk.t
   mk.t:18: *** mixed implicit and static pattern rules.  Stop.

:) , i will give this a try again tomorrow to see if i can get this working in a real generic way. Nice question though.

Nitin4873
  • 16,804
  • 1
  • 13
  • 15
  • I am not sure I understand, are you saying that the answer from [Beta](http://stackoverflow.com/a/15718701/1131467) is incorrect? – Andrew Tomazos Mar 31 '13 at 04:16
  • nope , i didnt say that! , both the answers posted are correct in theory - in fact i was trying the same thing. Making static pattern rules are obvious choice for these scenarios ..... but trying to make them too generic didn't work. I just posted my observation which seemed logical to me, just to let you know. – Nitin4873 Mar 31 '13 at 11:56