1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: versionator.eclass
7 # Jonathan Callen <jcallen@gentoo.org>
8 # base-system@gentoo.org
9 # @BLURB: functions which simplify manipulation of ${PV} and similar version strings
11 # This eclass provides functions which simplify manipulating $PV and similar
12 # variables. Most functions default to working with $PV, although other
15 # Simple Example 1: $PV is 1.2.3b, we want 1_2.3b:
16 # MY_PV=$(replace_version_separator 1 '_' )
18 # Simple Example 2: $PV is 1.4.5, we want 1:
19 # MY_MAJORV=$(get_major_version )
21 # Rather than being a number, the index parameter can be a separator character
22 # such as '-', '.' or '_'. In this case, the first separator of this kind is
26 # version_is_at_least want have
27 # which may be buggy, so use with caution.
29 if [[ -z ${_VERSIONATOR_ECLASS} ]]; then
34 # @FUNCTION: get_all_version_components
37 # Split up a version string into its component parts. If no parameter is
38 # supplied, defaults to $PV.
41 # 3.0_p2 -> 3 . 0 _ p2
42 # 20040905 -> 20040905
43 # 3.0c-r1 -> 3 . 0 c - r1
44 get_all_version_components() {
45 eshopts_push -s extglob
46 local ver_str=${1:-${PV}} result
49 # sneaky cache trick cache to avoid having to parse the same thing several
51 if [[ ${VERSIONATOR_CACHE_VER_STR} == ${ver_str} ]] ; then
52 echo ${VERSIONATOR_CACHE_RESULT}
56 export VERSIONATOR_CACHE_VER_STR=${ver_str}
58 while [[ -n $ver_str ]] ; do
59 case "${ver_str::1}" in
60 # number: parse whilst we have a number
62 result+=("${ver_str%%[^[:digit:]]*}")
63 ver_str=${ver_str##+([[:digit:]])}
66 # separator: single character
68 result+=("${ver_str::1}")
72 # letter: grab the letters plus any following numbers
74 local not_match=${ver_str##+([[:alpha:]])*([[:digit:]])}
75 # Can't say "${ver_str::-${#not_match}}" in Bash 3.2
76 result+=("${ver_str::${#ver_str} - ${#not_match}}")
82 result+=("${ver_str::1}")
88 export VERSIONATOR_CACHE_RESULT=${result[*]}
93 # @FUNCTION: get_version_components
96 # Get the important version components, excluding '.', '-' and '_'. Defaults to
97 # $PV if no parameter is supplied.
101 # 20040905 -> 20040905
102 # 3.0c-r1 -> 3 0 c r1
103 get_version_components() {
104 local c=$(get_all_version_components "${1:-${PV}}")
108 # @FUNCTION: get_major_version
111 # Get the major version of a value. Defaults to $PV if no parameter is supplied.
115 # 20040905 -> 20040905
117 get_major_version() {
118 local c=($(get_all_version_components "${1:-${PV}}"))
122 # @FUNCTION: get_version_component_range
123 # @USAGE: <range> [version]
125 # Get a particular component or range of components from the version. If no
126 # version parameter is supplied, defaults to $PV.
130 get_version_component_range() {
131 eshopts_push -s extglob
132 local c v="${2:-${PV}}" range="${1}" range_start range_end
134 c=($(get_all_version_components "${v}"))
135 range_start=${range%-*}; range_start=${range_start:-1}
136 range_end=${range#*-} ; range_end=${range_end:-${#c[@]}}
138 while ((j < range_start)); do
140 ((i > ${#c[@]})) && eshopts_pop && return
141 [[ -n "${c[i]//[-._]}" ]] && j+=1
144 while ((j <= range_end)); do
146 ((i > ${#c[@]})) && eshopts_pop && return
147 [[ -n "${c[i]//[-._]}" ]] && j+=1
153 # @FUNCTION: get_after_major_version
156 # Get everything after the major version and its separator (if present) of a
157 # value. Defaults to $PV if no parameter is supplied.
161 # 20040905 -> (empty string)
163 get_after_major_version() {
164 echo $(get_version_component_range 2- "${1:-${PV}}")
167 # @FUNCTION: replace_version_separator
168 # @USAGE: <search> <replacement> [subject]
170 # Replace the $1th separator with $2 in $3 (defaults to $PV if $3 is not
171 # supplied). If there are fewer than $1 separators, don't change anything.
172 # 1 '_' 1.2.3 -> 1_2.3
173 # 2 '_' 1.2.3 -> 1.2_3
174 # 1 '_' 1b-2.3 -> 1b_2.3
175 # Rather than being a number, $1 can be a separator character such as '-', '.'
176 # or '_'. In this case, the first separator of this kind is selected.
177 replace_version_separator() {
178 eshopts_push -s extglob
179 local w c v="${3:-${PV}}"
182 c=($(get_all_version_components ${v}))
183 if [[ ${w} != *[[:digit:]]* ]] ; then
184 # it's a character, not an index
185 for ((i = 0; i < ${#c[@]}; i++)); do
186 if [[ ${c[i]} == ${w} ]]; then
192 for ((i = 0; i < ${#c[@]}; i++)); do
193 if [[ -n "${c[i]//[^-._]}" ]]; then
195 if ((found == w)); then
207 # @FUNCTION: replace_all_version_separators
208 # @USAGE: <replacement> [subject]
210 # Replace all version separators in $2 (defaults to $PV) with $1.
211 # '_' 1b.2.3 -> 1b_2_3
212 replace_all_version_separators() {
213 local c=($(get_all_version_components "${2:-${PV}}"))
218 # @FUNCTION: delete_version_separator
219 # @USAGE: <search> [subject]
221 # Delete the $1th separator in $2 (defaults to $PV if $2 is not supplied). If
222 # there are fewer than $1 separators, don't change anything.
226 # Rather than being a number, $1 can be a separator character such as '-', '.'
227 # or '_'. In this case, the first separator of this kind is deleted.
228 delete_version_separator() {
229 replace_version_separator "${1}" "" "${2}"
232 # @FUNCTION: delete_all_version_separators
235 # Delete all version separators in $1 (defaults to $PV).
237 delete_all_version_separators() {
238 replace_all_version_separators "" "${1}"
241 # @FUNCTION: get_version_component_count
244 # How many version components are there in $1 (defaults to $PV)?
247 get_version_component_count() {
248 local a=($(get_version_components "${1:-${PV}}"))
252 # @FUNCTION: get_last_version_component_index
255 # What is the index of the last version component in $1 (defaults to $PV)?
256 # Equivalent to get_version_component_count - 1.
259 get_last_version_component_index() {
260 echo $(($(get_version_component_count "${1:-${PV}}" ) - 1))
263 # @FUNCTION: version_is_at_least
264 # @USAGE: <want> [have]
266 # Is $2 (defaults to $PVR) at least version $1? Intended for use in eclasses
267 # only. May not be reliable, be sure to do very careful testing before actually
269 version_is_at_least() {
270 local want_s="$1" have_s="${2:-${PVR}}" r
271 version_compare "${want_s}" "${have_s}"
281 die "versionator compare bug [atleast, ${want_s}, ${have_s}, ${r}]"
286 # @FUNCTION: version_compare
289 # Takes two parameters (A, B) which are versions. If A is an earlier version
290 # than B, returns 1. If A is identical to B, return 2. If A is later than B,
291 # return 3. You probably want version_is_at_least rather than this function.
292 # May not be very reliable. Test carefully before using this.
294 eshopts_push -s extglob
295 local ver_a=${1} ver_b=${2} parts_a parts_b
296 local cur_tok_a cur_tok_b num_part_a num_part_b
297 local -i cur_idx_a=0 cur_idx_b=0 prev_idx_a prev_idx_b
298 parts_a=( $(get_all_version_components "${ver_a}" ) )
299 parts_b=( $(get_all_version_components "${ver_b}" ) )
301 ### compare number parts.
305 ((inf_loop > 20)) && \
306 die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]"
308 # Store the current index to test later
312 # grab the current number components
313 cur_tok_a=${parts_a[cur_idx_a]}
314 cur_tok_b=${parts_b[cur_idx_b]}
317 if [[ -n ${cur_tok_a} ]] && [[ -z ${cur_tok_a//[[:digit:]]} ]] ; then
319 [[ ${parts_a[cur_idx_a]} == . ]] \
325 if [[ -n ${cur_tok_b} ]] && [[ -z ${cur_tok_b//[[:digit:]]} ]] ; then
327 [[ ${parts_b[cur_idx_b]} == . ]] \
333 # done with number components?
334 [[ -z ${cur_tok_a} && -z ${cur_tok_b} ]] && break
336 # if a component is blank, then it is the lesser value
337 [[ -z ${cur_tok_a} ]] && eshopts_pop && return 1
338 [[ -z ${cur_tok_b} ]] && eshopts_pop && return 3
340 # According to PMS, if we are *not* in the first number part, and either
341 # token begins with "0", then we use a different algorithm (that
342 # effectively does floating point comparison)
343 if (( prev_idx_a != 0 && prev_idx_b != 0 )) \
344 && [[ ${cur_tok_a} == 0* || ${cur_tok_b} == 0* ]] ; then
346 # strip trailing zeros
347 cur_tok_a=${cur_tok_a%%+(0)}
348 cur_tok_b=${cur_tok_b%%+(0)}
350 # do a *string* comparison of the resulting values: 2 > 11
351 [[ ${cur_tok_a} < ${cur_tok_b} ]] && eshopts_pop && return 1
352 [[ ${cur_tok_a} > ${cur_tok_b} ]] && eshopts_pop && return 3
354 # to avoid going into octal mode, strip any leading zeros. otherwise
355 # bash will throw a hissy fit on versions like 6.3.068.
356 cur_tok_a=${cur_tok_a##+(0)}
357 cur_tok_b=${cur_tok_b##+(0)}
359 # now if a component is blank, it was originally 0 -- make it so
364 ((cur_tok_a < cur_tok_b)) && eshopts_pop && return 1
365 ((cur_tok_a > cur_tok_b)) && eshopts_pop && return 3
369 ### number parts equal. compare letter parts.
371 letter_a=${parts_a[cur_idx_a]}
372 if [[ ${#letter_a} -eq 1 && -z ${letter_a/[a-z]} ]] ; then
379 letter_b=${parts_b[cur_idx_b]}
380 if [[ ${#letter_b} -eq 1 && -z ${letter_b/[a-z]} ]] ; then
387 [[ ${letter_a} < ${letter_b} ]] && eshopts_pop && return 1
388 [[ ${letter_a} > ${letter_b} ]] && eshopts_pop && return 3
390 ### letter parts equal. compare suffixes in order.
394 ((inf_loop > 20)) && \
395 die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]"
396 [[ ${parts_a[cur_idx_a]} == _ ]] && ((cur_idx_a++))
397 [[ ${parts_b[cur_idx_b]} == _ ]] && ((cur_idx_b++))
399 cur_tok_a=${parts_a[cur_idx_a]}
400 cur_tok_b=${parts_b[cur_idx_b]}
404 if has ${cur_tok_a%%+([0-9])} "alpha" "beta" "pre" "rc" "p"; then
406 num_part_a=${cur_tok_a##+([a-z])}
408 num_part_a=${num_part_a##+(0)}
410 cur_tok_a=${cur_tok_a%%+([0-9])}
415 if has ${cur_tok_b%%+([0-9])} alpha beta pre rc p; then
417 num_part_b=${cur_tok_b##+([a-z])}
418 # I still don't like octal
419 num_part_b=${num_part_b##+(0)}
421 cur_tok_b=${cur_tok_b%%+([0-9])}
426 if [[ ${cur_tok_a} != ${cur_tok_b} ]]; then
428 for suffix in alpha beta pre rc "" p; do
429 [[ ${cur_tok_a} == ${suffix} ]] && eshopts_pop && return 1
430 [[ ${cur_tok_b} == ${suffix} ]] && eshopts_pop && return 3
432 elif [[ -z ${cur_tok_a} && -z ${cur_tok_b} ]]; then
435 ((num_part_a < num_part_b)) && eshopts_pop && return 1
436 ((num_part_a > num_part_b)) && eshopts_pop && return 3
440 # At this point, the only thing that should be left is the -r# part
441 [[ ${parts_a[cur_idx_a]} == - ]] && ((cur_idx_a++))
442 [[ ${parts_b[cur_idx_b]} == - ]] && ((cur_idx_b++))
445 if [[ ${parts_a[cur_idx_a]/r+([0-9])} || ${parts_b[cur_idx_b]/r+([0-9])} ]]; then
446 die "versionator compare bug [revisions, ${ver_a}, ${ver_b}]"
449 num_part_a=${parts_a[cur_idx_a]#r}
450 num_part_a=${num_part_a##+(0)}
452 num_part_b=${parts_b[cur_idx_b]#r}
453 num_part_b=${num_part_b##+(0)}
456 ((num_part_a < num_part_b)) && eshopts_pop && return 1
457 ((num_part_a > num_part_b)) && eshopts_pop && return 3
464 # @FUNCTION: version_sort
465 # @USAGE: <version> [more versions...]
467 # Returns its parameters sorted, highest version last. We're using a quadratic
468 # algorithm for simplicity, so don't call it with more than a few dozen items.
469 # Uses version_compare, so be careful.
471 eshopts_push -s extglob
475 while ((left < ${#items[@]})); do
476 local -i lowest_idx=left
477 local -i idx=lowest_idx+1
478 while ((idx < ${#items[@]})); do
479 version_compare "${items[lowest_idx]}" "${items[idx]}"
480 [[ $? -eq 3 ]] && lowest_idx=idx
483 local tmp=${items[lowest_idx]}
484 items[lowest_idx]=${items[left]}
492 # @FUNCTION: version_format_string
493 # @USAGE: <format> [version]
495 # Reformat complicated version strings. The first argument is the string
496 # to reformat with while the rest of the args are passed on to the
497 # get_version_components function. You should make sure to single quote
498 # the first argument since it'll have variables that get delayed expansions.
500 # P="cow-hat-1.2.3_p4"
501 # MY_P=$(version_format_string '${PN}_source_$1_$2-$3_$4')
502 # Now MY_P will be: cow-hat_source_1_2-3_p4
503 version_format_string() {
506 set -- $(get_version_components "$@")