Tejas Chopra
Tejas Chopra

Reputation: 133

dynamically setting a makefile variable using awk

I have a variable stoppoint defined in my makefile. I want to set this variable using the output of awk from withing my Makefile.

I want to see if the second argument is main or not

I have tried:

stoppoint = $(awk '$$2 ~ /main/ {print $$1}' file)  
stoppoint = "$(awk '$$2 ~ /main/ {print $$1}' file)"  
stoppoint = 'awk '$$2 ~ /main/ {print $$1}' file'
awk '$$2 ~ /main/ {print $$1}' file > stoppoint
awk '$$2 ~ /main/ {print $$1}' file > $(stoppoint)

However, I am unable to set this variable. Kindly tell me how one can set a variable using awk inside a Makefile

PS: On command line, the awk command gives me the output I desire...

Thanks, Tejas

Upvotes: 4

Views: 7733

Answers (6)

Amit Talmor
Amit Talmor

Reputation: 8184

Another option for using awk in a makefile, use of $$:

VERSION=`awk '/version:/{print $$NF}' charts/Chart.yaml`

Upvotes: 1

Doron Behar
Doron Behar

Reputation: 2878

What I needed to work around this problem was using the keyword shell when calling awk, like that:

VARIABLE="$(shell awk '__awk script__' file-awk-processes)"

Hope it helps!

Upvotes: 0

Tejas Chopra
Tejas Chopra

Reputation: 133

Right now the way I do it is:

@awk '$$2 ~ /main/ {print $$1}' inputfile > DUMP

It gives me a value 0x60000078 inside dump

And to use it:

@echo -e "set pc $(shell cat DUMP)" > testfile

This seems to solve my issue, But I hope someone can point me to a better solution.

Thanks,

Tejas

Upvotes: 1

Tejas Chopra
Tejas Chopra

Reputation: 133

The solution is simple: Makefile assumed the format

target: dependencies followed on the next line by actions after a TAB

In the above case, we cannot set a variable in the next line after a TAB because it assumes it to be an action. Just remove the TAB and set it. That would solve the issue

Upvotes: 1

JinnKo
JinnKo

Reputation: 973

You need to use an eval structure like this:

.PHONY: default
default: all

rule1:
    $(eval GLOBAL_VAR=`awk '$$2 ~ /main/ {print $$1}' file`)

all: rule1
    @echo "The global var is: $(GLOBAL_VAR)"

The eval evaluates the string as makefile syntax. Here, it sets the GLOBAL_VAR variable to the result of the shell function call, which in turn gets printed out in another rule to illustrate it's global.

Upvotes: 2

arutaku
arutaku

Reputation: 6107

You have to use french quotes (``)

Here is an example:

all:
    @a=`echo "hello world!!" | awk '{print $0;}' -`; echo $$a;

Upvotes: 5

Related Questions