Debian Codegraph @

mklove/modules/configure.parseversion

raw
1 #!/bin/bash
2 #
3 # Parses the provided version string and creates variables accordingly.
4 # [ "hex2str" <fmt> ] -- version-string is in hex (e.g., 0x00080300)
5 # version-string
6 # STR_VERSION_VARIABLE_NAME
7 # [ HEX_VERSION_VARIABLE_NAME ]
8 #
9 # Note: The version will also be set in MKL_APP_VERSION
10 #
11 # Example: Set string version in variable named "MYVERSION_STR" and
12 # the hex representation in "MYVERSION"
13 # mkl_require parseversion "$(head -1 VERSION.txt)" MYVERSION_STR MYVERSION
14
15 if [[ $1 == "hex2str" ]]; then
16 parseversion_type="hex"
17 parseversion_fmt="${2}:END:%d%d%d%d"
18 shift
19 shift
20 else
21 parseversion_type=""
22 parseversion_fmt="%d.%d.%d.%d"
23 fi
24
25 if [[ -z "$2" ]]; then
26 mkl_fail "parseversion" "none" "fail" "Missing argument(s)"
27 return 0
28 fi
29
30 parseversion_orig="$1"
31 parseversion_strvar="$2"
32 parseversion_hexvar="$3"
33
34 function checks {
35 mkl_check_begin --verb "parsing" "parseversion" "" "no-cache" \
36 "version '$parseversion_orig'"
37
38 # Strip v prefix if any
39 orig=${parseversion_orig#v}
40
41 if [[ $orig == 0x* ]]; then
42 parseversion_type="hex"
43 orig=${orig#0x}
44 fi
45
46 if [[ -z $orig ]]; then
47 mkl_check_failed "parseversion" "" "fail" "Version string is empty"
48 return 1
49 fi
50
51 # If orig is in hex we construct a string format instead.
52 if [[ $parseversion_type == "hex" ]]; then
53 local s=$orig
54 local str=""
55 local vals=""
56 while [[ ! -z $s ]]; do
57 local n=${s:0:2}
58 s=${s:${#n}}
59 vals="${vals}$(printf %d 0x$n) "
60 done
61 str=$(printf "$parseversion_fmt" $vals)
62 orig=${str%:END:*}
63 fi
64
65
66 # Try to decode version string into hex
67 # Supported format is "[v]NN.NN.NN[.NN]"
68 if [[ ! -z $parseversion_hexvar ]]; then
69 local hex=""
70 local s=$orig
71 local ncnt=0
72 local n=
73 for n in ${s//./ } ; do
74 if [[ ! ( "$n" =~ ^[0-9][0-9]?$ ) ]]; then
75 mkl_check_failed "parseversion" "" "fail" \
76 "Could not decode '$parseversion_orig' into hex version, expecting format 'NN.NN.NN[.NN]'"
77 return 1
78 fi
79 hex="$hex$(printf %02x $n)"
80 ncnt=$(expr $ncnt + 1)
81 done
82
83 if [[ ! -z $hex ]]; then
84 # Finish all four bytess
85 while [[ ${#hex} -lt 8 ]]; do
86 hex="$hex$(printf %02x 0)"
87 done
88 mkl_allvar_set "parseversion" "$parseversion_hexvar" "0x$hex"
89 fi
90 fi
91
92 mkl_allvar_set "parseversion" "$parseversion_strvar" "$orig"
93 mkl_allvar_set "parseversion" MKL_APP_VERSION "$orig"
94 mkl_check_done "parseversion" "" "cont" "ok" "${!parseversion_strvar}"
95 }