Home > database >  How to export key/value pair variables from a file inside makefile
How to export key/value pair variables from a file inside makefile

Time:11-18

How to set the variables - key/value pair from a text file before running any targets in a Makefile?

Let's say text .p4config file contains the following: P4PORT=ilp4.il.domain.com:1667 P4CLIENT=ws:domain.com::user:container.IP_V1:a0a12fc1

in makefile this wont work:

export $(cat .p4config | grep P4CLIENT | xargs) test: env > env.log

Expecting to see the variables P4PORT and P4CLIENT in the env.log

cat .p4config | grep P4CLIENT | xargs

results:

P4PORT=ilp4.il.domain.com:1667 P4CLIENT=ws:domain.com::user:container.IP_V1:a0a12fc1

but adding the "export" inside the makefile won't do the trick

CodePudding user response:

I'm not sure why you thought that would work. A makefile is not a shell script, so you can't just write a bunch of shell operations in it an expect to behave the same way. The syntax of a makefile is described in the manual.

A makefile can CONTAIN a shell script, but only inside a recipe. So you could put the same command you'd type at the command line, into a recipe:

env.log:
        cat .p4config | grep P4CLIENT | xargs > $@

Now if you type make env.log (or any other target that lists env.log as a prerequisite) make will create that file for you (of course, note the indentation here must be a real TAB character, not just spaces).

CodePudding user response:

I wonder if this can achieve what you expected :

.ONESHELL:

all:
    eval "$$(perl -ne 'if (/P4CLIENT/) { map {print "export $$_\n"} split }' .p4config)"
    env | grep P4

output of env | grep P4:

P4CLIENT=ws:domain.com::user:container.IP_V1:a0a12fc1
P4PORT=ilp4.il.domain.com:1667
  • Related