1# Copyright 2006-2008 The FLWOR Foundation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15# Computes the realtionship between two version strings.  A version
16# string is a number delineated by '.'s such as 1.3.2 and 0.99.9.1.
17# You can feed version strings with different number of dot versions,
18# and the shorter version number will be padded with zeros: 9.2 <
19# 9.2.1 will actually compare 9.2.0 < 9.2.1.
20#
21# Input: a_in - value, not variable
22#        b_in - value, not variable
23#        result_out - variable with value:
24#                         -1 : a_in <  b_in
25#                          0 : a_in == b_in
26#                          1 : a_in >  b_in
27#
28# Written by James Bigler.
29MACRO(COMPARE_VERSION_STRINGS a_in b_in result_out)
30  # Since SEPARATE_ARGUMENTS using ' ' as the separation token,
31  # replace '.' with ' ' to allow easy tokenization of the string.
32  STRING(REPLACE "." " " a "${a_in}")
33  STRING(REPLACE "." " " b "${b_in}")
34  SEPARATE_ARGUMENTS(a)
35  SEPARATE_ARGUMENTS(b)
36
37  # Check the size of each list to see if they are equal.
38  LIST(LENGTH a a_length)
39  LIST(LENGTH b b_length)
40
41  # Pad the shorter list with zeros.
42
43  # Note that range needs to be one less than the length as the for
44  # loop is inclusive (silly CMake).
45  IF(a_length LESS b_length)
46    # a is shorter
47    SET(shorter a)
48    MATH(EXPR range "${b_length} - 1")
49    MATH(EXPR pad_range "${b_length} - ${a_length} - 1")
50  ELSE(a_length LESS b_length)
51    # b is shorter
52    SET(shorter b)
53    MATH(EXPR range "${a_length} - 1")
54    MATH(EXPR pad_range "${a_length} - ${b_length} - 1")
55  ENDIF(a_length LESS b_length)
56
57  # PAD out if we need to
58  IF(NOT pad_range LESS 0)
59    FOREACH(pad RANGE ${pad_range})
60      # Since shorter is an alias for b, we need to get to it by by dereferencing shorter.
61      LIST(APPEND ${shorter} 0)
62    ENDFOREACH(pad RANGE ${pad_range})
63  ENDIF(NOT pad_range LESS 0)
64
65  SET(result 0)
66  FOREACH(index RANGE ${range})
67    IF(result EQUAL 0)
68      # Only continue to compare things as long as they are equal
69      LIST(GET a ${index} a_version)
70      LIST(GET b ${index} b_version)
71      # LESS
72      IF(a_version LESS b_version)
73        SET(result -1)
74      ENDIF(a_version LESS b_version)
75      # GREATER
76      IF(a_version GREATER b_version)
77        SET(result 1)
78      ENDIF(a_version GREATER b_version)
79    ENDIF(result EQUAL 0)
80  ENDFOREACH(index)
81
82  # Copy out the return result
83  SET(${result_out} ${result})
84ENDMACRO(COMPARE_VERSION_STRINGS)
85