1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: versionator.eclass
6 # Jonathan Callen <jcallen@gentoo.org>
7 # base-system@gentoo.org
8 # @BLURB: functions which simplify manipulation of ${PV} and similar version strings
10 # This eclass provides functions which simplify manipulating $PV and similar
11 # variables. Most functions default to working with $PV, although other
14 # Simple Example 1: $PV is 1.2.3b, we want 1_2.3b:
15 # MY_PV=$(replace_version_separator 1 '_' )
17 # Simple Example 2: $PV is 1.4.5, we want 1:
18 # MY_MAJORV=$(get_major_version )
20 # Rather than being a number, the index parameter can be a separator character
21 # such as '-', '.' or '_'. In this case, the first separator of this kind is
25 # version_is_at_least want have
26 # which may be buggy, so use with caution.
28 if [[ -z ${_VERSIONATOR_ECLASS} ]]; then
33 # @FUNCTION: get_all_version_components
36 # Split up a version string into its component parts. If no parameter is
37 # supplied, defaults to $PV.
40 # 3.0_p2 -> 3 . 0 _ p2
41 # 20040905 -> 20040905
42 # 3.0c-r1 -> 3 . 0 c - r1
43 get_all_version_components() {
44 eshopts_push -s extglob
45 local ver_str=${1:-${PV}} result
48 # sneaky cache trick cache to avoid having to parse the same thing several
50 if [[ ${VERSIONATOR_CACHE_VER_STR} == ${ver_str} ]] ; then
51 echo ${VERSIONATOR_CACHE_RESULT}
55 export VERSIONATOR_CACHE_VER_STR=${ver_str}
57 while [[ -n $ver_str ]] ; do
58 case "${ver_str::1}" in
59 # number: parse whilst we have a number
61 result+=("${ver_str%%[^[:digit:]]*}")
62 ver_str=${ver_str##+([[:digit:]])}
65 # separator: single character
67 result+=("${ver_str::1}")
71 # letter: grab the letters plus any following numbers
73 local not_match=${ver_str##+([[:alpha:]])*([[:digit:]])}
74 # Can't say "${ver_str::-${#not_match}}" in Bash 3.2
75 result+=("${ver_str::${#ver_str} - ${#not_match}}")
81 result+=("${ver_str::1}")
87 export VERSIONATOR_CACHE_RESULT=${result[*]}
92 # @FUNCTION: get_version_components
95 # Get the important version components, excluding '.', '-' and '_'. Defaults to
96 # $PV if no parameter is supplied.
100 # 20040905 -> 20040905
101 # 3.0c-r1 -> 3 0 c r1
102 get_version_components() {
103 local c=$(get_all_version_components "${1:-${PV}}")
107 # @FUNCTION: get_major_version
110 # Get the major version of a value. Defaults to $PV if no parameter is supplied.
114 # 20040905 -> 20040905
116 get_major_version() {
117 local c=($(get_all_version_components "${1:-${PV}}"))
121 # @FUNCTION: get_version_component_range
122 # @USAGE: <range> [version]
124 # Get a particular component or range of components from the version. If no
125 # version parameter is supplied, defaults to $PV.
129 get_version_component_range() {
130 eshopts_push -s extglob
131 local c v="${2:-${PV}}" range="${1}" range_start range_end
133 c=($(get_all_version_components "${v}"))
134 range_start=${range%-*}; range_start=${range_start:-1}
135 range_end=${range#*-} ; range_end=${range_end:-${#c[@]}}
137 while ((j < range_start)); do
139 ((i > ${#c[@]})) && eshopts_pop && return
140 [[ -n "${c[i]//[-._]}" ]] && j+=1
143 while ((j <= range_end)); do
145 ((i > ${#c[@]})) && eshopts_pop && return
146 [[ -n "${c[i]//[-._]}" ]] && j+=1
152 # @FUNCTION: get_after_major_version
155 # Get everything after the major version and its separator (if present) of a
156 # value. Defaults to $PV if no parameter is supplied.
160 # 20040905 -> (empty string)
162 get_after_major_version() {
163 echo $(get_version_component_range 2- "${1:-${PV}}")
166 # @FUNCTION: replace_version_separator
167 # @USAGE: <search> <replacement> [subject]
169 # Replace the $1th separator with $2 in $3 (defaults to $PV if $3 is not
170 # supplied). If there are fewer than $1 separators, don't change anything.
171 # 1 '_' 1.2.3 -> 1_2.3
172 # 2 '_' 1.2.3 -> 1.2_3
173 # 1 '_' 1b-2.3 -> 1b_2.3
174 # Rather than being a number, $1 can be a separator character such as '-', '.'
175 # or '_'. In this case, the first separator of this kind is selected.
176 replace_version_separator() {
177 eshopts_push -s extglob
178 local w c v="${3:-${PV}}"
181 c=($(get_all_version_components ${v}))
182 if [[ ${w} != *[[:digit:]]* ]] ; then
183 # it's a character, not an index
184 for ((i = 0; i < ${#c[@]}; i++)); do
185 if [[ ${c[i]} == ${w} ]]; then
191 for ((i = 0; i < ${#c[@]}; i++)); do
192 if [[ -n "${c[i]//[^-._]}" ]]; then
194 if ((found == w)); then
206 # @FUNCTION: replace_all_version_separators
207 # @USAGE: <replacement> [subject]
209 # Replace all version separators in $2 (defaults to $PV) with $1.
210 # '_' 1b.2.3 -> 1b_2_3
211 replace_all_version_separators() {
212 local c=($(get_all_version_components "${2:-${PV}}"))
217 # @FUNCTION: delete_version_separator
218 # @USAGE: <search> [subject]
220 # Delete the $1th separator in $2 (defaults to $PV if $2 is not supplied). If
221 # there are fewer than $1 separators, don't change anything.
225 # Rather than being a number, $1 can be a separator character such as '-', '.'
226 # or '_'. In this case, the first separator of this kind is deleted.
227 delete_version_separator() {
228 replace_version_separator "${1}" "" "${2}"
231 # @FUNCTION: delete_all_version_separators
234 # Delete all version separators in $1 (defaults to $PV).
236 delete_all_version_separators() {
237 replace_all_version_separators "" "${1}"
240 # @FUNCTION: get_version_component_count
243 # How many version components are there in $1 (defaults to $PV)?
246 get_version_component_count() {
247 local a=($(get_version_components "${1:-${PV}}"))
251 # @FUNCTION: get_last_version_component_index
254 # What is the index of the last version component in $1 (defaults to $PV)?
255 # Equivalent to get_version_component_count - 1.
258 get_last_version_component_index() {
259 echo $(($(get_version_component_count "${1:-${PV}}" ) - 1))
262 # @FUNCTION: version_is_at_least
263 # @USAGE: <want> [have]
265 # Is $2 (defaults to $PVR) at least version $1? Intended for use in eclasses
266 # only. May not be reliable, be sure to do very careful testing before actually
268 version_is_at_least() {
269 local want_s="$1" have_s="${2:-${PVR}}" r
270 version_compare "${want_s}" "${have_s}"
280 die "versionator compare bug [atleast, ${want_s}, ${have_s}, ${r}]"
285 # @FUNCTION: version_compare
288 # Takes two parameters (A, B) which are versions. If A is an earlier version
289 # than B, returns 1. If A is identical to B, return 2. If A is later than B,
290 # return 3. You probably want version_is_at_least rather than this function.
291 # May not be very reliable. Test carefully before using this.
293 eshopts_push -s extglob
294 local ver_a=${1} ver_b=${2} parts_a parts_b
295 local cur_tok_a cur_tok_b num_part_a num_part_b
296 local -i cur_idx_a=0 cur_idx_b=0 prev_idx_a prev_idx_b
297 parts_a=( $(get_all_version_components "${ver_a}" ) )
298 parts_b=( $(get_all_version_components "${ver_b}" ) )
300 ### compare number parts.
304 ((inf_loop > 20)) && \
305 die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]"
307 # Store the current index to test later
311 # grab the current number components
312 cur_tok_a=${parts_a[cur_idx_a]}
313 cur_tok_b=${parts_b[cur_idx_b]}
316 if [[ -n ${cur_tok_a} ]] && [[ -z ${cur_tok_a//[[:digit:]]} ]] ; then
318 [[ ${parts_a[cur_idx_a]} == . ]] \
324 if [[ -n ${cur_tok_b} ]] && [[ -z ${cur_tok_b//[[:digit:]]} ]] ; then
326 [[ ${parts_b[cur_idx_b]} == . ]] \
332 # done with number components?
333 [[ -z ${cur_tok_a} && -z ${cur_tok_b} ]] && break
335 # if a component is blank, then it is the lesser value
336 [[ -z ${cur_tok_a} ]] && eshopts_pop && return 1
337 [[ -z ${cur_tok_b} ]] && eshopts_pop && return 3
339 # According to PMS, if we are *not* in the first number part, and either
340 # token begins with "0", then we use a different algorithm (that
341 # effectively does floating point comparison)
342 if (( prev_idx_a != 0 && prev_idx_b != 0 )) \
343 && [[ ${cur_tok_a} == 0* || ${cur_tok_b} == 0* ]] ; then
345 # strip trailing zeros
346 cur_tok_a=${cur_tok_a%%+(0)}
347 cur_tok_b=${cur_tok_b%%+(0)}
349 # do a *string* comparison of the resulting values: 2 > 11
350 [[ ${cur_tok_a} < ${cur_tok_b} ]] && eshopts_pop && return 1
351 [[ ${cur_tok_a} > ${cur_tok_b} ]] && eshopts_pop && return 3
353 # to avoid going into octal mode, strip any leading zeros. otherwise
354 # bash will throw a hissy fit on versions like 6.3.068.
355 cur_tok_a=${cur_tok_a##+(0)}
356 cur_tok_b=${cur_tok_b##+(0)}
358 # now if a component is blank, it was originally 0 -- make it so
363 ((cur_tok_a < cur_tok_b)) && eshopts_pop && return 1
364 ((cur_tok_a > cur_tok_b)) && eshopts_pop && return 3
368 ### number parts equal. compare letter parts.
370 letter_a=${parts_a[cur_idx_a]}
371 if [[ ${#letter_a} -eq 1 && -z ${letter_a/[a-z]} ]] ; then
378 letter_b=${parts_b[cur_idx_b]}
379 if [[ ${#letter_b} -eq 1 && -z ${letter_b/[a-z]} ]] ; then
386 [[ ${letter_a} < ${letter_b} ]] && eshopts_pop && return 1
387 [[ ${letter_a} > ${letter_b} ]] && eshopts_pop && return 3
389 ### letter parts equal. compare suffixes in order.
393 ((inf_loop > 20)) && \
394 die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]"
395 [[ ${parts_a[cur_idx_a]} == _ ]] && ((cur_idx_a++))
396 [[ ${parts_b[cur_idx_b]} == _ ]] && ((cur_idx_b++))
398 cur_tok_a=${parts_a[cur_idx_a]}
399 cur_tok_b=${parts_b[cur_idx_b]}
403 if has ${cur_tok_a%%+([0-9])} "alpha" "beta" "pre" "rc" "p"; then
405 num_part_a=${cur_tok_a##+([a-z])}
407 num_part_a=${num_part_a##+(0)}
409 cur_tok_a=${cur_tok_a%%+([0-9])}
414 if has ${cur_tok_b%%+([0-9])} alpha beta pre rc p; then
416 num_part_b=${cur_tok_b##+([a-z])}
417 # I still don't like octal
418 num_part_b=${num_part_b##+(0)}
420 cur_tok_b=${cur_tok_b%%+([0-9])}
425 if [[ ${cur_tok_a} != ${cur_tok_b} ]]; then
427 for suffix in alpha beta pre rc "" p; do
428 [[ ${cur_tok_a} == ${suffix} ]] && eshopts_pop && return 1
429 [[ ${cur_tok_b} == ${suffix} ]] && eshopts_pop && return 3
431 elif [[ -z ${cur_tok_a} && -z ${cur_tok_b} ]]; then
434 ((num_part_a < num_part_b)) && eshopts_pop && return 1
435 ((num_part_a > num_part_b)) && eshopts_pop && return 3
439 # At this point, the only thing that should be left is the -r# part
440 [[ ${parts_a[cur_idx_a]} == - ]] && ((cur_idx_a++))
441 [[ ${parts_b[cur_idx_b]} == - ]] && ((cur_idx_b++))
444 if [[ ${parts_a[cur_idx_a]/r+([0-9])} || ${parts_b[cur_idx_b]/r+([0-9])} ]]; then
445 die "versionator compare bug [revisions, ${ver_a}, ${ver_b}]"
448 num_part_a=${parts_a[cur_idx_a]#r}
449 num_part_a=${num_part_a##+(0)}
451 num_part_b=${parts_b[cur_idx_b]#r}
452 num_part_b=${num_part_b##+(0)}
455 ((num_part_a < num_part_b)) && eshopts_pop && return 1
456 ((num_part_a > num_part_b)) && eshopts_pop && return 3
463 # @FUNCTION: version_sort
464 # @USAGE: <version> [more versions...]
466 # Returns its parameters sorted, highest version last. We're using a quadratic
467 # algorithm for simplicity, so don't call it with more than a few dozen items.
468 # Uses version_compare, so be careful.
470 eshopts_push -s extglob
474 while ((left < ${#items[@]})); do
475 local -i lowest_idx=left
476 local -i idx=lowest_idx+1
477 while ((idx < ${#items[@]})); do
478 version_compare "${items[lowest_idx]}" "${items[idx]}"
479 [[ $? -eq 3 ]] && lowest_idx=idx
482 local tmp=${items[lowest_idx]}
483 items[lowest_idx]=${items[left]}
491 # @FUNCTION: version_format_string
492 # @USAGE: <format> [version]
494 # Reformat complicated version strings. The first argument is the string
495 # to reformat with while the rest of the args are passed on to the
496 # get_version_components function. You should make sure to single quote
497 # the first argument since it'll have variables that get delayed expansions.
499 # P="cow-hat-1.2.3_p4"
500 # MY_P=$(version_format_string '${PN}_source_$1_$2-$3_$4')
501 # Now MY_P will be: cow-hat_source_1_2-3_p4
502 version_format_string() {
505 set -- $(get_version_components "$@")