rodee
rodee

Reputation: 3161

Split a string in gnu make with a separator

I have makefile with below code:

 .PHONY: $(PROJECTDIR)/projectroot
 $(PROJECTDIR)/projectroot:
if [ -f $(@)/etc/release ]; then \
         #$(RELEASE_VERSION) will have value something like 00.01.02.03
     IFS=. read major minor micro build <<<"${RELEASE_VERSION}" \
         echo major=${major} > $(@)/etc/release \
         echo major=${minor} >> $(@)/etc/release; \
         echo major=${micro} >> $(@)/etc/release; \
         echo major=${build} >> $(@)/etc/release; \
fi

I am expecting the $(@)/etc/release to get content like below:

 major=00
 minor=01
 micro=02
 build=03

I need to replace IFS=. with some equivalent command in gnu make, can you please help me with this? thanks.

Upvotes: 1

Views: 3524

Answers (2)

None
None

Reputation: 2594

.PHONY: $(PROJECTDIR)/projectroot
$(PROJECTDIR)/projectroot: $(PROJECTDIR)/projectroot/etc/release
$(PROJECTDIR)/projectroot/etc/release:
    echo "${RELEASE_VERSION}" | \
    sed 's/^/major=/; s/\./\nminor=/; s/\./\nmicro=/; s/\./\nbuild=/' >$@

Add dependencies to the release file.

Upvotes: 1

Jonathan Wakely
Jonathan Wakely

Reputation: 171303

The problem is that you're readin the values into shell variables called major, minor etc. but then you are printing Make variables with the same names. The shell cannot set the Make variables.

To fix your existing code you need to repeat the $ characters as $$ so Make outputs a $ to the shell, which will interpret them (also you're saying "major" every time, and are missing some semi-colons):

     IFS=. read major minor micro build <<<"${RELEASE_VERSION}" ; \
     echo major=$${major} > $(@)/etc/release ;\
     echo minor=$${minor} >> $(@)/etc/release; \
     echo micro=$${micro} >> $(@)/etc/release; \
     echo build=$${build} >> $(@)/etc/release; \

But to do it within make you can use the subst and word functions:

REL_WORDS := $(subst ., ,${RELEASE_VERSION})
...
     echo major=$(word 1,${REL_WORDS}) >  $(@)/etc/release ;\
     echo minor=$(word 2,${REL_WORDS}) >> $(@)/etc/release ;\
     echo micro=$(word 3,${REL_WORDS}) >> $(@)/etc/release ;\
     echo build=$(word 4,${REL_WORDS}) >> $(@)/etc/release ;\

Upvotes: 2

Related Questions