split and cut string in Makefile

Viewed 1476

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
2 Answers

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

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!))
Related