Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to assign a value to a variable in a Makefile?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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; }')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading