superb owl
superb owl

Reputation: 185

How can I run two makefile targets such that the second uses the first's variables?

I am writing a makefile that has multiple targets for architectures and build types. When I call make I would like to be able to pass it two targets such as:

make x86 debug

Where the architecture target sets variables to be used by the build target. An architecture target might look like this:

.PHONY: x86
x86: NAME_PREFIX = x86_64_Linux
x86: TARGET_TRIPLE = x86_64-pc-linux-elf
x86: CC = gcc
x86: CFLAGS += -fomit-frame-pointer

First issue: If I run the two targets, make skips the first target since it does nothing but set variables.

$ make x86 debug
make: Nothing to be done for `x86'.

Second issue: Even if it would run the first target, my understanding is that the values won't be retained when make starts running the second target anyway.

For now, I'm using targets that set variables and add the build type target as a prerequisite at the end of the target body, but I would end up needing a target for every combination of architecture and build type.

.PHONY: x86_debug
x86_debug: NAME_PREFIX = x86_64_Linux
x86_debug: TARGET_TRIPLE = x86_64-pc-linux-elf
x86_debug: CC = gcc
x86_debug: CFLAGS += -fomit-frame-pointer
x86_debug: debug

Upvotes: 0

Views: 152

Answers (1)

Toby Speight
Toby Speight

Reputation: 30709

Your second approach is better. But you don't need a target for every combination, if you use a consistent naming scheme, and use wildcard targets:

.PHONY: x86_% arm_% # etc
.PHONY: %_debug %_optimised # etc

x86_%: NAME_PREFIX = x86_64_Linux
x86_%: TARGET_TRIPLE = x86_64-pc-linux-elf
x86_%: CC = gcc
x86_%: CFLAGS += -fomit-frame-pointer

arm_%: NAME_PREFIX = ARM_LINUX
arm_%: TARGET_TRIPLE = arm-linux-eabi
arm_%: CC = arm-linux-gcc


%_debug: CFLAGS += -g

%_optimised: CFLAGS += -O3

Upvotes: 1

Related Questions