Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make: Using target specific variables in prerequisites

I'm trying to write a Makefile where prerequisites using target specific variables

version=

target1: override version=1
target1: package

target2: override version=2
target2: package

package: dir=package-${version}\
package: source

source: src/${version}.c

When i run make the version variable is in target package and source empty.

What I'm doing wrong?

like image 512
user1213840 Avatar asked Feb 16 '12 13:02

user1213840


People also ask

What is $@ in makefile?

The file name of the target of the rule. If the target is an archive member, then ' $@ ' is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ' $@ ' is the name of whichever target caused the rule's recipe to be run.

What is a specific variable value?

An attribute is a specific value on a variable. For instance, the variable Student grade has two attributes: pass and fail . Or, the variable agreement might be defined as having five attributes: 1 = strongly disagree.

What is a make target?

A 'make target' is basically a file that you want rebuilt. Make can't divine what you want built, so you have to tell it, implicitly or explicitly, what it should build.


1 Answers

Use Secondary Expansion:

.SECONDEXPANSION:

package: dir=package-$${version}
package: source

source: src/$${version}.c

UPD.

This answer is wrong, the suggested code won't work because of the reasons explained in the answer to a similar question.

TL;DR: Target-specific variables take their effect based on the target that make is currently building [1]. Second expansion, in turn, takes place right at the end of the read-in phase [2], before building anything.

Thanks to @koniiiik for pointing out.

like image 168
Eldar Abusalimov Avatar answered Oct 29 '22 06:10

Eldar Abusalimov