Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GNU Make. Variables with a scope limited to a single Makefile

I try to implement a non-recursive make build system in my current project. What I struggle with is variables scopes. Target specific variables don't fit my needs as often variables define targets and not prerequisites. What I need is:

Makefile1:

SOMEVAR := original_value
include Makefile2
$(warning $(SOMEVAR))

Makefile2:

#some magic here to do what I want and make me happy
SOMEVAR := included_value
#and maybe here

And the output I want is 'original_value'.

Are there any strategies to make it real?

EDIT: The only solution I came for the moment is to force and organize myself to place all inlcudes in the end of each particular Makefile and use immediate variable assignment :=

like image 349
Alexander Reshytko Avatar asked Oct 19 '12 09:10

Alexander Reshytko


People also ask

What is the meaning of $@ In a 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. $%

Can we use variables in makefile?

A variable is a name defined in a makefile to represent a string of text, called the variable's value. These values are substituted by explicit request into targets, prerequisites, commands, and other parts of the makefile. (In some other versions of make , variables are called macros.)

How do I override a makefile variable?

There is one way that the makefile can change a variable that you have overridden. This is to use the override directive, which is a line that looks like this: ' override variable = value ' (see The override Directive).

What is Patsubst in makefile?

$(patsubst pattern , replacement , text ) Finds whitespace-separated words in text that match pattern and replaces them with replacement . Here pattern may contain a ' % ' which acts as a wildcard, matching any number of any characters within a word.


1 Answers

One strategy is the old-fashioned solution to variable name collisions when all you have is global variables: add a prefix to your variable names in a form of poor-man's namespaces.

Makefile1:

Makefile1_SOMEVAR := original_value
include Makefile2
$(warning $(Makefile1_SOMEVAR))

Makefile2:

# no magic needed
Makefile2_SOMEVAR := included_value
# rest of Makefile2 uses $(Makefile2_SOMEVAR) of course

Hey presto, with a convention like this it's as if each makefile has its own local variables (or, at least, its own variables in a namespace that doesn't collide with any other makefiles).

like image 188
John Marshall Avatar answered Oct 17 '22 16:10

John Marshall