depend.apache.eclass: Let APXS point to the new location of the binary.
[gentoo.git] / eclass / versionator.eclass
1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: versionator.eclass
6 # @MAINTAINER:
7 # Jonathan Callen <jcallen@gentoo.org>
8 # base-system@gentoo.org
9 # @BLURB: functions which simplify manipulation of ${PV} and similar version strings
10 # @DESCRIPTION:
11 # This eclass provides functions which simplify manipulating $PV and similar
12 # variables. Most functions default to working with $PV, although other
13 # values can be used.
14 # @EXAMPLE:
15 # Simple Example 1: $PV is 1.2.3b, we want 1_2.3b:
16 #     MY_PV=$(replace_version_separator 1 '_' )
17 #
18 # Simple Example 2: $PV is 1.4.5, we want 1:
19 #     MY_MAJORV=$(get_major_version )
20 #
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
23 # selected.
24 #
25 # There's also:
26 #     version_is_at_least             want      have
27 #  which may be buggy, so use with caution.
28
29 if [[ -z ${_VERSIONATOR_ECLASS} ]]; then
30 _VERSIONATOR_ECLASS=1
31
32 inherit eutils
33
34 # @FUNCTION: get_all_version_components
35 # @USAGE: [version]
36 # @DESCRIPTION:
37 # Split up a version string into its component parts. If no parameter is
38 # supplied, defaults to $PV.
39 #     0.8.3       ->  0 . 8 . 3
40 #     7c          ->  7 c
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
47         result=()
48
49         # sneaky cache trick cache to avoid having to parse the same thing several
50         # times.
51         if [[ ${VERSIONATOR_CACHE_VER_STR} == ${ver_str} ]] ; then
52                 echo ${VERSIONATOR_CACHE_RESULT}
53                 eshopts_pop
54                 return
55         fi
56         export VERSIONATOR_CACHE_VER_STR=${ver_str}
57
58         while [[ -n $ver_str ]] ; do
59                 case "${ver_str::1}" in
60                         # number: parse whilst we have a number
61                         [[:digit:]])
62                                 result+=("${ver_str%%[^[:digit:]]*}")
63                                 ver_str=${ver_str##+([[:digit:]])}
64                                 ;;
65
66                         # separator: single character
67                         [-_.])
68                                 result+=("${ver_str::1}")
69                                 ver_str=${ver_str:1}
70                                 ;;
71
72                         # letter: grab the letters plus any following numbers
73                         [[:alpha:]])
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}}")
77                                 ver_str=${not_match}
78                                 ;;
79
80                         # huh?
81                         *)
82                                 result+=("${ver_str::1}")
83                                 ver_str=${ver_str:1}
84                                 ;;
85                 esac
86         done
87
88         export VERSIONATOR_CACHE_RESULT=${result[*]}
89         echo ${result[@]}
90         eshopts_pop
91 }
92
93 # @FUNCTION: get_version_components
94 # @USAGE: [version]
95 # @DESCRIPTION:
96 # Get the important version components, excluding '.', '-' and '_'. Defaults to
97 # $PV if no parameter is supplied.
98 #     0.8.3       ->  0 8 3
99 #     7c          ->  7 c
100 #     3.0_p2      ->  3 0 p2
101 #     20040905    ->  20040905
102 #     3.0c-r1     ->  3 0 c r1
103 get_version_components() {
104         local c=$(get_all_version_components "${1:-${PV}}")
105         echo ${c//[-._]/ }
106 }
107
108 # @FUNCTION: get_major_version
109 # @USAGE: [version]
110 # @DESCRIPTION:
111 # Get the major version of a value. Defaults to $PV if no parameter is supplied.
112 #     0.8.3       ->  0
113 #     7c          ->  7
114 #     3.0_p2      ->  3
115 #     20040905    ->  20040905
116 #     3.0c-r1     ->  3
117 get_major_version() {
118         local c=($(get_all_version_components "${1:-${PV}}"))
119         echo ${c[0]}
120 }
121
122 # @FUNCTION: get_version_component_range
123 # @USAGE: <range> [version]
124 # @DESCRIPTION:
125 # Get a particular component or range of components from the version. If no
126 # version parameter is supplied, defaults to $PV.
127 #    1      1.2.3       -> 1
128 #    1-2    1.2.3       -> 1.2
129 #    2-     1.2.3       -> 2.3
130 get_version_component_range() {
131         eshopts_push -s extglob
132         local c v="${2:-${PV}}" range="${1}" range_start range_end
133         local -i i=-1 j=0
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[@]}}
137
138         while ((j < range_start)); do
139                 i+=1
140                 ((i > ${#c[@]})) && eshopts_pop && return
141                 [[ -n "${c[i]//[-._]}" ]] && j+=1
142         done
143
144         while ((j <= range_end)); do
145                 echo -n ${c[i]}
146                 ((i > ${#c[@]})) && eshopts_pop && return
147                 [[ -n "${c[i]//[-._]}" ]] && j+=1
148                 i+=1
149         done
150         eshopts_pop
151 }
152
153 # @FUNCTION: get_after_major_version
154 # @USAGE: [version]
155 # @DESCRIPTION:
156 # Get everything after the major version and its separator (if present) of a
157 # value. Defaults to $PV if no parameter is supplied.
158 #     0.8.3       ->  8.3
159 #     7c          ->  c
160 #     3.0_p2      ->  0_p2
161 #     20040905    ->  (empty string)
162 #     3.0c-r1     ->  0c-r1
163 get_after_major_version() {
164         echo $(get_version_component_range 2- "${1:-${PV}}")
165 }
166
167 # @FUNCTION: replace_version_separator
168 # @USAGE: <search> <replacement> [subject]
169 # @DESCRIPTION:
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}}"
180         declare -i i found=0
181         w=${1:-1}
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
187                                 c[i]=${2}
188                                 break
189                         fi
190                 done
191         else
192                 for ((i = 0; i < ${#c[@]}; i++)); do
193                         if [[ -n "${c[i]//[^-._]}" ]]; then
194                                 found+=1
195                                 if ((found == w)); then
196                                         c[i]=${2}
197                                         break
198                                 fi
199                         fi
200                 done
201         fi
202         c=${c[*]}
203         echo ${c// }
204         eshopts_pop
205 }
206
207 # @FUNCTION: replace_all_version_separators
208 # @USAGE: <replacement> [subject]
209 # @DESCRIPTION:
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}}"))
214         c=${c[@]//[-._]/$1}
215         echo ${c// }
216 }
217
218 # @FUNCTION: delete_version_separator
219 # @USAGE: <search> [subject]
220 # @DESCRIPTION:
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.
223 #     1 1.2.3       -> 12.3
224 #     2 1.2.3       -> 1.23
225 #     1 1b-2.3      -> 1b2.3
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}"
230 }
231
232 # @FUNCTION: delete_all_version_separators
233 # @USAGE: [subject]
234 # @DESCRIPTION:
235 # Delete all version separators in $1 (defaults to $PV).
236 #     1b.2.3        -> 1b23
237 delete_all_version_separators() {
238         replace_all_version_separators "" "${1}"
239 }
240
241 # @FUNCTION: get_version_component_count
242 # @USAGE: [version]
243 # @DESCRIPTION:
244 # How many version components are there in $1 (defaults to $PV)?
245 #     1.0.1       ->  3
246 #     3.0c-r1     ->  4
247 get_version_component_count() {
248         local a=($(get_version_components "${1:-${PV}}"))
249         echo ${#a[@]}
250 }
251
252 # @FUNCTION: get_last_version_component_index
253 # @USAGE: [version]
254 # @DESCRIPTION:
255 # What is the index of the last version component in $1 (defaults to $PV)?
256 # Equivalent to get_version_component_count - 1.
257 #     1.0.1       ->  2
258 #     3.0c-r1     ->  3
259 get_last_version_component_index() {
260         echo $(($(get_version_component_count "${1:-${PV}}" ) - 1))
261 }
262
263 # @FUNCTION: version_is_at_least
264 # @USAGE: <want> [have]
265 # @DESCRIPTION:
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
268 # using this.
269 version_is_at_least() {
270         local want_s="$1" have_s="${2:-${PVR}}" r
271         version_compare "${want_s}" "${have_s}"
272         r=$?
273         case $r in
274                 1|2)
275                         return 0
276                         ;;
277                 3)
278                         return 1
279                         ;;
280                 *)
281                         die "versionator compare bug [atleast, ${want_s}, ${have_s}, ${r}]"
282                         ;;
283         esac
284 }
285
286 # @FUNCTION: version_compare
287 # @USAGE: <A> <B>
288 # @DESCRIPTION:
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.
293 version_compare() {
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}" ) )
300
301         ### compare number parts.
302         local -i inf_loop=0
303         while true; do
304                 inf_loop+=1
305                 ((inf_loop > 20)) && \
306                         die "versionator compare bug [numbers, ${ver_a}, ${ver_b}]"
307
308                 # Store the current index to test later
309                 prev_idx_a=cur_idx_a
310                 prev_idx_b=cur_idx_b
311
312                 # grab the current number components
313                 cur_tok_a=${parts_a[cur_idx_a]}
314                 cur_tok_b=${parts_b[cur_idx_b]}
315
316                 # number?
317                 if [[ -n ${cur_tok_a} ]] && [[ -z ${cur_tok_a//[[:digit:]]} ]] ; then
318                         cur_idx_a+=1
319                         [[ ${parts_a[cur_idx_a]} == . ]] \
320                                 && cur_idx_a+=1
321                 else
322                         cur_tok_a=
323                 fi
324
325                 if [[ -n ${cur_tok_b} ]] && [[ -z ${cur_tok_b//[[:digit:]]} ]] ; then
326                         cur_idx_b+=1
327                         [[ ${parts_b[cur_idx_b]} == . ]] \
328                                 && cur_idx_b+=1
329                 else
330                         cur_tok_b=
331                 fi
332
333                 # done with number components?
334                 [[ -z ${cur_tok_a} && -z ${cur_tok_b} ]] && break
335
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
339
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
345
346                         # strip trailing zeros
347                         cur_tok_a=${cur_tok_a%%+(0)}
348                         cur_tok_b=${cur_tok_b%%+(0)}
349
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
353                 else
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)}
358
359                         # now if a component is blank, it was originally 0 -- make it so
360                         : ${cur_tok_a:=0}
361                         : ${cur_tok_b:=0}
362
363                         # compare
364                         ((cur_tok_a < cur_tok_b)) && eshopts_pop && return 1
365                         ((cur_tok_a > cur_tok_b)) && eshopts_pop && return 3
366                 fi
367         done
368
369         ### number parts equal. compare letter parts.
370         local letter_a=
371         letter_a=${parts_a[cur_idx_a]}
372         if [[ ${#letter_a} -eq 1 && -z ${letter_a/[a-z]} ]] ; then
373                 cur_idx_a+=1
374         else
375                 letter_a=@
376         fi
377
378         local letter_b=
379         letter_b=${parts_b[cur_idx_b]}
380         if [[ ${#letter_b} -eq 1 && -z ${letter_b/[a-z]} ]] ; then
381                 cur_idx_b+=1
382         else
383                 letter_b=@
384         fi
385
386         # compare
387         [[ ${letter_a} < ${letter_b} ]] && eshopts_pop && return 1
388         [[ ${letter_a} > ${letter_b} ]] && eshopts_pop && return 3
389
390         ### letter parts equal. compare suffixes in order.
391         inf_loop=0
392         while true ; do
393                 inf_loop+=1
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++))
398
399                 cur_tok_a=${parts_a[cur_idx_a]}
400                 cur_tok_b=${parts_b[cur_idx_b]}
401                 num_part_a=0
402                 num_part_b=0
403
404                 if has ${cur_tok_a%%+([0-9])} "alpha" "beta" "pre" "rc" "p"; then
405                         cur_idx_a+=1
406                         num_part_a=${cur_tok_a##+([a-z])}
407                         # I don't like octal
408                         num_part_a=${num_part_a##+(0)}
409                         : ${num_part_a:=0}
410                         cur_tok_a=${cur_tok_a%%+([0-9])}
411                 else
412                         cur_tok_a=
413                 fi
414
415                 if has ${cur_tok_b%%+([0-9])} alpha beta pre rc p; then
416                         cur_idx_b+=1
417                         num_part_b=${cur_tok_b##+([a-z])}
418                         # I still don't like octal
419                         num_part_b=${num_part_b##+(0)}
420                         : ${num_part_b:=0}
421                         cur_tok_b=${cur_tok_b%%+([0-9])}
422                 else
423                         cur_tok_b=
424                 fi
425
426                 if [[ ${cur_tok_a} != ${cur_tok_b} ]]; then
427                         local suffix
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
431                         done
432                 elif [[ -z ${cur_tok_a} && -z ${cur_tok_b} ]]; then
433                         break
434                 else
435                         ((num_part_a < num_part_b)) && eshopts_pop && return 1
436                         ((num_part_a > num_part_b)) && eshopts_pop && return 3
437                 fi
438         done
439
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++))
443
444         # Sanity check
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}]"
447         fi
448
449         num_part_a=${parts_a[cur_idx_a]#r}
450         num_part_a=${num_part_a##+(0)}
451         : ${num_part_a:=0}
452         num_part_b=${parts_b[cur_idx_b]#r}
453         num_part_b=${num_part_b##+(0)}
454         : ${num_part_b:=0}
455
456         ((num_part_a < num_part_b)) && eshopts_pop && return 1
457         ((num_part_a > num_part_b)) && eshopts_pop && return 3
458
459         ### no differences.
460         eshopts_pop
461         return 2
462 }
463
464 # @FUNCTION: version_sort
465 # @USAGE: <version> [more versions...]
466 # @DESCRIPTION:
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.
470 version_sort() {
471         eshopts_push -s extglob
472         local items=
473         local -i left=0
474         items=("$@")
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
481                         idx+=1
482                 done
483                 local tmp=${items[lowest_idx]}
484                 items[lowest_idx]=${items[left]}
485                 items[left]=${tmp}
486                 left+=1
487         done
488         echo ${items[@]}
489         eshopts_pop
490 }
491
492 # @FUNCTION: version_format_string
493 # @USAGE: <format> [version]
494 # @DESCRIPTION:
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.
499 # @EXAMPLE:
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() {
504         local fstr=$1
505         shift
506         set -- $(get_version_components "$@")
507         eval echo "${fstr}"
508 }
509
510 fi