Ewan Delanoy
Ewan Delanoy

Reputation: 1282

Two assignments on same line in GNU makefile

A certain makefile (namely, the otherlibs/num/Makefile in the OCaml 4.2 distribution) has the following line :

EXTRACFLAGS=-DBNG_ARCH_$(BNG_ARCH) -DBNG_ASM_LEVEL=$(BNG_ASM_LEVEL)

I am surprised to see two assignments on the same line here. What does it mean ? Is it the same as

EXTRACFLAGS=-DBNG_ARCH_$(BNG_ARCH)
-DBNG_ASM_LEVEL=$(BNG_ASM_LEVEL)

I did not find anything about that in the GNU make manual, maybe I am unaware of the right keyword to search this.

Upvotes: 0

Views: 285

Answers (1)

Mike Kinghan
Mike Kinghan

Reputation: 61397

There are not two assignments here. The line assigns the definition:

-DBNG_ARCH_$(BNG_ARCH) -DBNG_ASM_LEVEL=$(BNG_ASM_LEVEL)

to the make variable EXTRACFLAGS. The value appears to consist of flags that are to be passed to some invocation of the C preprocessor to define macros BNG_ARCH_$(BNG_ARCH) and BNG_ASM_LEVEL. E.g. as in a make recipe:

    $(CPP) $(EXTRACFLAGS) ...

Which, supposing the make assignments:

BNG_ARCH = foo
BNG_ASM_LEVEL = bar

would expand to:

    cpp -DBNG_ARCH_foo -DBNG_ASM_LEVEL=bar ...

OP comments:

So you might say that = is "left-associative", the leftmost = sign is the one that really gets executed. Is this documented somewhere in the manual?

From my frequent, but far from exhaustive resorts to manual I can't say it is documented. Somebody may know better. There is no formal grammar there, but it is everywhere illustrated that the definition of a variable extends from the start of the non-whitespace text following =, := or ?= to the end of the (possibly \-extended) logical line. You may satisfied on the point by running the makefile:

.PHONY: all

x = X y = Y

all:
    @echo [$(x)]
    @echo [$(y)]

which prints:

[X y = Y]
[]

Assignment signs within a make-definition are not assignments, they're just characters in the definition.

Upvotes: 3

Related Questions