1 # Copyright 1999-2018 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 # @SUPPORTED_EAPIS: 0 1 2 3 4 5 6
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
36 die "${ECLASS}: banned in EAPI=${EAPI}; use ver_* instead";;
41 # @FUNCTION: get_all_version_components
44 # Split up a version string into its component parts. If no parameter is
45 # supplied, defaults to $PV.
48 # 3.0_p2 -> 3 . 0 _ p2
49 # 20040905 -> 20040905
50 # 3.0c-r1 -> 3 . 0 c - r1
51 get_all_version_components() {
52 eshopts_push -s extglob
53 local ver_str=${1:-${PV}} result
56 # sneaky cache trick cache to avoid having to parse the same thing several
58 if [[ ${VERSIONATOR_CACHE_VER_STR} == ${ver_str} ]] ; then
59 echo ${VERSIONATOR_CACHE_RESULT}
63 export VERSIONATOR_CACHE_VER_STR=${ver_str}
65 while [[ -n $ver_str ]] ; do
66 case "${ver_str::1}" in
67 # number: parse whilst we have a number
69 result+=("${ver_str%%[^[:digit:]]*}")
70 ver_str=${ver_str##+([[:digit:]])}
73 # separator: single character
75 result+=("${ver_str::1}")
79 # letter: grab the letters plus any following numbers
81 local not_match=${ver_str##+([[:alpha:]])*([[:digit:]])}
82 # Can't say "${ver_str::-${#not_match}}" in Bash 3.2
83 result+=("${ver_str::${#ver_str} - ${#not_match}}")
89 result+=("${ver_str::1}")
95 export VERSIONATOR_CACHE_RESULT=${result[*]}
100 # @FUNCTION: get_version_components
103 # Get the important version components, excluding '.', '-' and '_'. Defaults to
104 # $PV if no parameter is supplied.
108 # 20040905 -> 20040905
109 # 3.0c-r1 -> 3 0 c r1
110 get_version_components() {
111 local c=$(get_all_version_components "${1:-${PV}}")
115 # @FUNCTION: get_major_version
118 # Get the major version of a value. Defaults to $PV if no parameter is supplied.
122 # 20040905 -> 20040905
124 get_major_version() {
125 local c=($(get_all_version_components "${1:-${PV}}"))
129 # @FUNCTION: get_version_component_range
130 # @USAGE: <range> [version]
132 # Get a particular component or range of components from the version. If no
133 # version parameter is supplied, defaults to $PV.
137 get_version_component_range() {
138 eshopts_push -s extglob
139 local c v="${2:-${PV}}" range="${1}" range_start range_end
141 c=($(get_all_version_components "${v}"))
142 range_start=${range%-*}; range_start=${range_start:-1}
143 range_end=${range#*-} ; range_end=${range_end:-${#c[@]}}
145 while ((j < range_start)); do
147 ((i > ${#c[@]})) && eshopts_pop && return
148 [[ -n "${c[i]//[-._]}" ]] && j+=1
151 while ((j <= range_end)); do
153 ((i > ${#c[@]})) && eshopts_pop && return
154 [[ -n "${c[i]//[-._]}" ]] && j+=1
160 # @FUNCTION: get_after_major_version
163 # Get everything after the major version and its separator (if present) of a
164 # value. Defaults to $PV if no parameter is supplied.
168 # 20040905 -> (empty string)
170 get_after_major_version() {
171 echo $(get_version_component_range 2- "${1:-${PV}}")
174 # @FUNCTION: replace_version_separator
175 # @USAGE: <search> <replacement> [subject]
177 # Replace the $1th separator with $2 in $3 (defaults to $PV if $3 is not
178 # supplied). If there are fewer than $1 separators, don't change anything.
179 # 1 '_' 1.2.3 -> 1_2.3
180 # 2 '_' 1.2.3 -> 1.2_3
181 # 1 '_' 1b-2.3 -> 1b_2.3
182 # Rather than being a number, $1 can be a separator character such as '-', '.'
183 # or '_'. In this case, the first separator of this kind is selected.
184 replace_version_separator() {
185 eshopts_push -s extglob
186 local w c v="${3:-${PV}}"
189 c=($(get_all_version_components ${v}))
190 if [[ ${w} != *[[:digit:]]* ]] ; then
191 # it's a character, not an index
192 for ((i = 0; i < ${#c[@]}; i++)); do
193 if [[ ${c[i]} == ${w} ]]; then
199 for ((i = 0; i < ${#c[@]}; i++)); do
200 if [[ -n "${c[i]//[^-._]}" ]]; then
202 if ((found == w)); then
214 # @FUNCTION: replace_all_version_separators
215 # @USAGE: <replacement> [subject]
217 # Replace all version separators in $2 (defaults to $PV) with $1.
218 # '_' 1b.2.3 -> 1b_2_3
219 replace_all_version_separators() {
220 local c=($(get_all_version_components "${2:-${PV}}"))
225 # @FUNCTION: delete_version_separator
226 # @USAGE: <search> [subject]
228 # Delete the $1th separator in $2 (defaults to $PV if $2 is not supplied). If
229 # there are fewer than $1 separators, don't change anything.
233 # Rather than being a number, $1 can be a separator character such as '-', '.'
234 # or '_'. In this case, the first separator of this kind is deleted.
235 delete_version_separator() {
236 replace_version_separator "${1}" "" "${2}"
239 # @FUNCTION: delete_all_version_separators
242 # Delete all version separators in $1 (defaults to $PV).
244 delete_all_version_separators() {
245 replace_all_version_separators "" "${1}"
248 # @FUNCTION: get_version_component_count
251 # How many version components are there in $1 (defaults to $PV)?
254 get_version_component_count() {
255 local a=($(get_version_components "${1:-${PV}}"))
259 # @FUNCTION: get_last_version_component_index
262 # What is the index of the last version component in $1 (defaults to $PV)?
263 # Equivalent to get_version_component_count - 1.
266 get_last_version_component_index() {
267 echo $(($(get_version_component_count "${1:-${PV}}" ) - 1))
270 # @FUNCTION: version_is_at_least
271 # @USAGE: <want> [have]
273 # Is $2 (defaults to $PVR) at least version $1? Intended for use in eclasses
274 # only. May not be reliable, be sure to do very careful testing before actually
276 version_is_at_least() {
277 local want_s="$1" have_s="${2:-${PVR}}" r
278 version_compare "${want_s}" "${have_s}"
288 die "versionator compare bug [atleast, ${want_s}, ${have_s}, ${r}]"
293 # @FUNCTION: version_compare
296 # Takes two parameters (A, B) which are versions. If A is an earlier version
297 # than B, returns 1. If A is identical to B, return 2. If A is later than B,
298 # return 3. You probably want version_is_at_least rather than this function.
299 # May not be very reliable. Test carefully before using this.
301 eshopts_push -s extglob
302 local ver_a=${1} ver_b=${2} parts_a parts_b
303 local cur_tok_a cur_tok_b num_part_a num_part_b
304 local -i cur_idx_a=0 cur_idx_b=0 prev_idx_a prev_idx_b
305 parts_a=( $(get_all_version_components "${ver_a}" ) )
306 parts_b=( $(get_all_version_components "${ver_b}" ) )
308 ### compare number parts.
312 ((inf_loop > 20)) && \
313 die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]"
315 # Store the current index to test later
319 # grab the current number components
320 cur_tok_a=${parts_a[cur_idx_a]}
321 cur_tok_b=${parts_b[cur_idx_b]}
324 if [[ -n ${cur_tok_a} ]] && [[ -z ${cur_tok_a//[[:digit:]]} ]] ; then
326 [[ ${parts_a[cur_idx_a]} == . ]] \
332 if [[ -n ${cur_tok_b} ]] && [[ -z ${cur_tok_b//[[:digit:]]} ]] ; then
334 [[ ${parts_b[cur_idx_b]} == . ]] \
340 # done with number components?
341 [[ -z ${cur_tok_a} && -z ${cur_tok_b} ]] && break
343 # if a component is blank, then it is the lesser value
344 [[ -z ${cur_tok_a} ]] && eshopts_pop && return 1
345 [[ -z ${cur_tok_b} ]] && eshopts_pop && return 3
347 # According to PMS, if we are *not* in the first number part, and either
348 # token begins with "0", then we use a different algorithm (that
349 # effectively does floating point comparison)
350 if (( prev_idx_a != 0 && prev_idx_b != 0 )) \
351 && [[ ${cur_tok_a} == 0* || ${cur_tok_b} == 0* ]] ; then
353 # strip trailing zeros
354 cur_tok_a=${cur_tok_a%%+(0)}
355 cur_tok_b=${cur_tok_b%%+(0)}
357 # do a *string* comparison of the resulting values: 2 > 11
358 [[ ${cur_tok_a} < ${cur_tok_b} ]] && eshopts_pop && return 1
359 [[ ${cur_tok_a} > ${cur_tok_b} ]] && eshopts_pop && return 3
361 # to avoid going into octal mode, strip any leading zeros. otherwise
362 # bash will throw a hissy fit on versions like 6.3.068.
363 cur_tok_a=${cur_tok_a##+(0)}
364 cur_tok_b=${cur_tok_b##+(0)}
366 # now if a component is blank, it was originally 0 -- make it so
371 ((cur_tok_a < cur_tok_b)) && eshopts_pop && return 1
372 ((cur_tok_a > cur_tok_b)) && eshopts_pop && return 3
376 ### number parts equal. compare letter parts.
378 letter_a=${parts_a[cur_idx_a]}
379 if [[ ${#letter_a} -eq 1 && -z ${letter_a/[a-z]} ]] ; then
386 letter_b=${parts_b[cur_idx_b]}
387 if [[ ${#letter_b} -eq 1 && -z ${letter_b/[a-z]} ]] ; then
394 [[ ${letter_a} < ${letter_b} ]] && eshopts_pop && return 1
395 [[ ${letter_a} > ${letter_b} ]] && eshopts_pop && return 3
397 ### letter parts equal. compare suffixes in order.
401 ((inf_loop > 20)) && \
402 die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]"
403 [[ ${parts_a[cur_idx_a]} == _ ]] && ((cur_idx_a++))
404 [[ ${parts_b[cur_idx_b]} == _ ]] && ((cur_idx_b++))
406 cur_tok_a=${parts_a[cur_idx_a]}
407 cur_tok_b=${parts_b[cur_idx_b]}
411 if has ${cur_tok_a%%+([0-9])} "alpha" "beta" "pre" "rc" "p"; then
413 num_part_a=${cur_tok_a##+([a-z])}
415 num_part_a=${num_part_a##+(0)}
417 cur_tok_a=${cur_tok_a%%+([0-9])}
422 if has ${cur_tok_b%%+([0-9])} alpha beta pre rc p; then
424 num_part_b=${cur_tok_b##+([a-z])}
425 # I still don't like octal
426 num_part_b=${num_part_b##+(0)}
428 cur_tok_b=${cur_tok_b%%+([0-9])}
433 if [[ ${cur_tok_a} != ${cur_tok_b} ]]; then
435 for suffix in alpha beta pre rc "" p; do
436 [[ ${cur_tok_a} == ${suffix} ]] && eshopts_pop && return 1
437 [[ ${cur_tok_b} == ${suffix} ]] && eshopts_pop && return 3
439 elif [[ -z ${cur_tok_a} && -z ${cur_tok_b} ]]; then
442 ((num_part_a < num_part_b)) && eshopts_pop && return 1
443 ((num_part_a > num_part_b)) && eshopts_pop && return 3
447 # At this point, the only thing that should be left is the -r# part
448 [[ ${parts_a[cur_idx_a]} == - ]] && ((cur_idx_a++))
449 [[ ${parts_b[cur_idx_b]} == - ]] && ((cur_idx_b++))
452 if [[ ${parts_a[cur_idx_a]/r+([0-9])} || ${parts_b[cur_idx_b]/r+([0-9])} ]]; then
453 die "versionator compare bug [revisions, ${ver_a}, ${ver_b}]"
456 num_part_a=${parts_a[cur_idx_a]#r}
457 num_part_a=${num_part_a##+(0)}
459 num_part_b=${parts_b[cur_idx_b]#r}
460 num_part_b=${num_part_b##+(0)}
463 ((num_part_a < num_part_b)) && eshopts_pop && return 1
464 ((num_part_a > num_part_b)) && eshopts_pop && return 3
471 # @FUNCTION: version_sort
472 # @USAGE: <version> [more versions...]
474 # Returns its parameters sorted, highest version last. We're using a quadratic
475 # algorithm for simplicity, so don't call it with more than a few dozen items.
476 # Uses version_compare, so be careful.
478 eshopts_push -s extglob
482 while ((left < ${#items[@]})); do
483 local -i lowest_idx=left
484 local -i idx=lowest_idx+1
485 while ((idx < ${#items[@]})); do
486 version_compare "${items[lowest_idx]}" "${items[idx]}"
487 [[ $? -eq 3 ]] && lowest_idx=idx
490 local tmp=${items[lowest_idx]}
491 items[lowest_idx]=${items[left]}
499 # @FUNCTION: version_format_string
500 # @USAGE: <format> [version]
502 # Reformat complicated version strings. The first argument is the string
503 # to reformat with while the rest of the args are passed on to the
504 # get_version_components function. You should make sure to single quote
505 # the first argument since it'll have variables that get delayed expansions.
507 # P="cow-hat-1.2.3_p4"
508 # MY_P=$(version_format_string '${PN}_source_$1_$2-$3_$4')
509 # Now MY_P will be: cow-hat_source_1_2-3_p4
510 version_format_string() {
513 set -- $(get_version_components "$@")