Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/tutorial2/cmake/tools/CompareVersionStrings.cmake @ 11629

Last change on this file since 11629 was 7163, checked in by dafrick, 14 years ago

Merged presentation3 branch into trunk.

  • Property svn:eol-style set to native
File size: 2.6 KB
Line 
1# Computes the relationship between two version strings.  A version
2# string is a number delineated by '.'s such as 1.3.2 and 0.99.9.1.
3# You can feed version strings with different number of dot versions,
4# and the shorter version number will be padded with zeros: 9.2 <
5# 9.2.1 will actually compare 9.2.0 < 9.2.1.
6#
7# Input: a_in - value, not variable
8#        b_in - value, not variable
9#        result_out - variable with value:
10#                         -1 : a_in <  b_in
11#                          0 : a_in == b_in
12#                          1 : a_in >  b_in
13#        optional argument - TRUE or default FALSE:
14#              When passing 1.2.3 and 1.2 and this switch is on
15#              the function will actually compare 1.2 < 1.2
16#              Caution: Only the first string is adjusted!
17#
18# Written by James Bigler.
19# Extended with an optional argument by Reto Grieder
20
21FUNCTION(COMPARE_VERSION_STRINGS a_in b_in result_out)
22  # Additional argument can be a switch to change compare behaviour
23  SET(cut_first ${ARGN})
24
25  # Replace '.' with ';' to allow easy tokenization of the string.
26  STRING(REPLACE "." ";" a ${a_in})
27  STRING(REPLACE "." ";" b ${b_in})
28
29  # Check the size of each list to see if they are equal.
30  LIST(LENGTH a a_length)
31  LIST(LENGTH b b_length)
32
33  # Pad the shorter list with zeros.
34
35  IF(a_length LESS b_length)
36    # a is shorter
37    SET(shorter a)
38    MATH(EXPR pad_range "${b_length} - ${a_length} - 1")
39  ELSE(a_length LESS b_length)
40    # b is shorter
41    SET(shorter b)
42    SET(first_longer a)
43    MATH(EXPR pad_range "${a_length} - ${b_length} - 1")
44  ENDIF(a_length LESS b_length)
45
46  # PAD out if we need to
47  IF(NOT pad_range LESS 0)
48    FOREACH(pad RANGE ${pad_range})
49      # Since shorter is an alias for b, we need to get to it by dereferencing shorter.
50      IF(cut_first AND first_longer)
51        LIST(REMOVE_AT a -1) # remove last element
52      ELSE(cut_first AND first_longer)
53        LIST(APPEND ${shorter} 0)
54      ENDIF(cut_first AND first_longer)
55    ENDFOREACH(pad)
56  ENDIF(NOT pad_range LESS 0)
57
58  SET(result 0)
59  SET(index 0)
60  FOREACH(a_version ${a})
61    IF(result EQUAL 0)
62      # Only continue to compare things as long as they are equal
63      LIST(GET b ${index} b_version)
64      # LESS
65      IF(a_version LESS b_version)
66        SET(result -1)
67      ENDIF(a_version LESS b_version)
68      # GREATER
69      IF(a_version GREATER b_version)
70        SET(result 1)
71      ENDIF(a_version GREATER b_version)
72    ENDIF(result EQUAL 0)
73    MATH(EXPR index "${index} + 1")
74  ENDFOREACH(a_version)
75
76  # Copy out the return result
77  SET(${result_out} ${result} PARENT_SCOPE)
78ENDFUNCTION(COMPARE_VERSION_STRINGS)
Note: See TracBrowser for help on using the repository browser.