Makefile automatic variable @F doesn't get expanded in dependencies

Viewed 80

In the following Makefile $F doesn't get expanded and it's just empty. I am wondering what is an elegant solution to use @F instead of writing dependencies explicitly?

nested-cycles.scala first.scala follow.scala : ../$(basename $(@F)).aps ${APS2SCALA}
    ${APS2SCALA} ${APS2SCALAFLAGS} -S $(basename $(@F))
1 Answers

You're right. See the GNU make manual:

It’s very important that you recognize the limited scope in which automatic variable values are available: they only have values within the recipe. In particular, you cannot use them anywhere within the target list of a rule; they have no value there and will expand to the empty string. Also, they cannot be accessed directly within the prerequisite list of a rule. A common mistake is attempting to use $@ within the prerequisites list; this will not work.

The usual way this is handled is to write a pattern rule telling make how to build a target matching a given pattern, based on a prerequisite using that pattern. For your example it would be something like this:

%.scala : ../%.aps ${APS2SCALA}
        ${APS2SCALA} ${APS2SCALAFLAGS} -S $*
Related