sclarke81
sclarke81

Reputation: 1769

Read a value from a file into a variable

I'm using make in Windows and I'd like to read a couple of values from a file to use as variables in the makefile.

I have a file that includes the following lines:

/* A comment */
#define SOME_STRING "ABCDE"

/* A comment         "     " */
#define BLANK_STRING "     "

/* A comment */
#define SOME_VERSION 01.01a01

I'd like to end up with two variables in my makefile. One set to ABCDE and the other set to 01.01a01.

This could also be done using some bash commands called from within the makefile. I'm new to makefiles, bash and stackoverflow so please bear with me!

Thanks

Stephen

Upvotes: 2

Views: 235

Answers (2)

Beta
Beta

Reputation: 99094

VARS := $(shell  sed -n "s/^\#define .* //p" filename)

A = $(word 1, $(VARS))
B = $(word 2, $(VARS))

Upvotes: 0

perreal
perreal

Reputation: 97938

all:
  echo $(SOME_VERSION)

imports: input_file
  sed -n 's/^#define \([^ ]*\) \("*[^"]*"*\)/\1=\2/p' input_file > imports

include imports

Upvotes: 1

Related Questions