Reputation: 24812
I'm trying to set up a Makefile so I can work the following rules out:
make board1 flash
or
make board2 flash
Where board1
compiles a firmware for a given board and board2
compiles a firmware for another one.
I know I could define a flash
rule for each board, but I'm supporting a lot of boards, and I'd like to add support of a new board as simple as possible, using variables defined in rules.
So I wrote the following rule:
board1: MCU=atmega2560
board1: F_CPU=16000000
board1: build/main-board1.o
and I can compile everything perfectly well. Then I got the flash
command to work:
flash:
$(AVRDUDE) -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) -U flash:w:build/*.hex
so when I execute make board1 flash
, I'd like to have $(MCU)
set to the atmega2560
value.
N.B.: though I think the previous question is a possible one, I got another one, but I think that one is not possible:
board1: TARGET=board1
board1: build/main-$(TARGET).o
flash: build/main-$(TARGET).o
$(AVRDUDE) ...
what I mean, is if there would be a way to define a variable from within the rule to be used in another rule?
Upvotes: 0
Views: 454
Reputation: 99084
You can use "make board1 flash" and "make board2 flash", but you shouldn't. It goes against the way Make is supposed to work. The arguments listed in the call to Make are supposed to be independent targets, not commands that modify each other.
A better way is like this:
make BOARD=board1 flash
and
make BOARD=board2 flash
(or equivalently make flash BOARD=board1
and make flash BOARD=board2
, the order doesn't matter), using this makefile:
ifeq ($(BOARD), board1)
MCU=atmega2560
F_CPU=16000000
endif
ifeq ($(BOARD), board2)
MCU=otherMCU
F_CPU=33333
endif
flash: build/main-$(BOARD).o
$(AVRDUDE) -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) -U flash:w:build/main-$(TARGET).hex
(Notice that TARGET is redundant, it's the same as BOARD.)
Upvotes: 2
Reputation: 24812
ahah! Sometimes one just need to ask a question to find the answer to it the following second!
here's the answer to my own question: the $(eval VAR=VAL)
magic does the trick.
board1: $(eval MCU=atmega2560)
board1: $(eval F_CPU=16000000)
board1: $(eval TARGET=board1)
board1: build/main-$(TARGET).o
flash: build/main-$(TARGET).o
$(AVRDUDE) -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) -U flash:w:build/main-$(TARGET).hex
I'm still open to suggestions and for more elegant ways to achieve the same thing, though!
Upvotes: 0