Robert Hu
Robert Hu

Reputation: 171

split and cut string in Makefile

I'm working on a Makefile.

version := v39.0.12.8    // format rules: va.b.c.d

I want to get "version_a" based on the format, which is "39" in this case. How to do this? Maybe via "sed/cut/awk but I'm not familar with these shell command.

e.g.

version         version_a  version_b   version_c  version_d
v39.0.12.8      39         0            12        8
v87.2.9.17      87         2            9         17
v142.98.77.68   142        98           77        68

Upvotes: 5

Views: 4331

Answers (3)

Chiel
Chiel

Reputation: 2169

As an alternative to the other great answers, here is my preferred solution.

Makefile:

split-dot = $(word $2,$(subst ., ,$1))

version := v39.0.12.8
version_a := $(call split-dot,$(version:v%=%),1)
version_b := $(call split-dot,$(version),2)
version_c := $(call split-dot,$(version),3)
version_d := $(call split-dot,$(version),4)

all:
    $(info version_a=$(version_a))
    $(info version_b=$(version_b))
    $(info version_b=$(version_c))
    $(info version_b=$(version_d))

output:

$ make -s
version_a=39
version_b=0
version_b=12
version_b=8

Upvotes: 0

Vroomfondel
Vroomfondel

Reputation: 2898

With gmtt you can execute a glob match on your version number:

include gmtt/gmtt.mk

version := v39.0.12.8

matchresult := $(call glob-match,$(version),v*.*.*.*)

$(info [$(matchresult)])

major := $(word 2,$(matchresult))
minor := $(word 4,$(matchresult))
bugfix := $(word 6,$(matchresult))
buildcnt := $(word 8,$(matchresult))

$(info Major = $(major))
$(info Minor = $(minor))
$(info Bugfix = $(bugfix))
$(info Buildcnt = $(buildcnt))

$(if $(call int-ge,$(major),39),$(info Major is 39 or higher!))

Upvotes: 2

raspy
raspy

Reputation: 4261

This can be achieved with make functionality, like so:

$ cat Makefile
version := v39.0.12.8

version_tuple := $(subst ., ,$(version:v%=%))

version_a := $(word 1,$(version_tuple))
version_b := $(word 2,$(version_tuple))
version_c := $(word 3,$(version_tuple))
version_d := $(word 4,$(version_tuple))

all:
        echo version_tuple = $(version_tuple)
        echo version_a = $(version_a)
        echo version_b = $(version_b)
        echo version_c = $(version_c)
        echo version_d = $(version_d)

Output:

$ make -s
version_tuple = 39 0 12 8
version_a = 39
version_b = 0
version_c = 12
version_d = 8

Upvotes: 9

Related Questions