Home > Blockchain >  How to assign a value to a variable in a Makefile?
How to assign a value to a variable in a Makefile?

Time:04-26

I was using this question: Assign a makefile variable value to a bash command result? to try and solve my problem, but it doesn't seem to work.

I have a one-liner to get the current port of a connected Arduino, that I need in my Makefile as a parameter:

[bf@localhost GameCode]$ arduino-cli board list | grep arduino:megaavr:nona4809 | awk '{ print $1; }'
/dev/ttyACM0

As you see this works fine. But now I want to use that in my Makefile:

BOARD=arduino:megaavr:nona4809
PORT=$(shell arduino-cli board list | grep $$BOARD | awk '{ print $$1; }')
OUTPUT=mycode.hex

program: $(OUTPUT)
     eeprom-program -f $(OUTPUT) -p $(PORT)

But, $(PORT) seems to be completely empty. I know I can fix this with a shell script, but as this is only a single line, I don't want to clutter my directory with small scripts.

How can I get that command in my Makefile?

CodePudding user response:

Here ...

BOARD=arduino:megaavr:nona4809
PORT=$(shell arduino-cli board list | grep $$BOARD | awk '{ print $$1; }')

... you correctly escape $1 as $$1 to pass the former through to the shell. But you do not want to pass $BOARD through to the shell as such. Rather, you want make to expand that one:

BOARD=arduino:megaavr:nona4809
PORT=$(shell arduino-cli board list | grep $(BOARD) | awk '{ print $$1; }')
  • Related