1if ( NOT CGAL_VERSION_UTILS_FILE_INCLUDED ) 2 set( CGAL_VERSION_UTILS_FILE_INCLUDED 1 ) 3 4# 5# Given a version string of the form "major.[minor.[patch.[tweak]]]" 6# decomposes it into components 7# 8macro( VERSION_DECOMPOSE v major minor patch tweak ) 9 10 string(REPLACE "." ";" VERSION_DECOMPOSE_LIST ${v} ) 11 12 list( LENGTH VERSION_DECOMPOSE_LIST VERSION_DECOMPOSE_LIST_LEN ) 13 14 if ( VERSION_DECOMPOSE_LIST_LEN GREATER 0 ) 15 list( GET VERSION_DECOMPOSE_LIST 0 ${major} ) 16 else() 17 set ( ${major} -1 ) 18 endif() 19 20 if ( VERSION_DECOMPOSE_LIST_LEN GREATER 1 ) 21 list( GET VERSION_DECOMPOSE_LIST 1 ${minor} ) 22 else() 23 set ( ${minor} -1 ) 24 endif() 25 26 if ( VERSION_DECOMPOSE_LIST_LEN GREATER 2 ) 27 list( GET VERSION_DECOMPOSE_LIST 2 ${patch} ) 28 else() 29 set ( ${patch} -1 ) 30 endif() 31 32 if ( VERSION_DECOMPOSE_LIST_LEN GREATER 3 ) 33 list( GET VERSION_DECOMPOSE_LIST 3 ${tweak} ) 34 else() 35 set ( ${tweak} -1 ) 36 endif() 37 38endmacro() 39 40# 41# Given two version string of the form "major.[minor.[patch.[tweak]]]" 42# returns TRUE if they are equal, FALSE otherwise. 43# 44macro( IS_VERSION_EQUAL a b r ) 45 46 VERSION_DECOMPOSE( ${a} _IVE_a_major _IVE_a_minor _IVE_a_patch _IVE_a_tweak ) 47 VERSION_DECOMPOSE( ${b} _IVE_b_major _IVE_b_minor _IVE_b_patch _IVE_b_tweak ) 48 49 set ( ${r} FALSE ) 50 51 if ( _IVE_a_major EQUAL ${_IVE_b_major} ) 52 if ( _IVE_a_minor EQUAL ${_IVE_b_minor} ) 53 if ( _IVE_a_patch EQUAL ${_IVE_b_patch} ) 54 if ( _IVE_a_tweak EQUAL ${_IVE_b_tweak} ) 55 set ( ${r} TRUE ) 56 endif() 57 endif() 58 endif() 59 endif() 60 61endmacro() 62 63# 64# Given two version string of the form "major.[minor.[patch.[tweak]]]" 65# returns TRUE if the first is smaller than the second, FALSE otherwise. 66# 67macro( IS_VERSION_LESS a b r ) 68 69 VERSION_DECOMPOSE( ${a} _IVL_a_major _IVL_a_minor _IVL_a_patch _IVL_a_tweak ) 70 VERSION_DECOMPOSE( ${b} _IVL_b_major _IVL_b_minor _IVL_b_patch _IVL_b_tweak ) 71 72 set ( ${r} FALSE ) 73 74 if ( _IVL_a_major LESS ${_IVL_b_major} ) 75 set ( ${r} TRUE ) 76 elseif( _IVL_a_major EQUAL ${_IVL_b_major}) 77 if ( _IVL_a_minor LESS ${_IVL_b_minor} ) 78 set ( ${r} TRUE ) 79 elseif( _IVL_a_minor EQUAL ${_IVL_b_minor} ) 80 if ( _IVL_a_patch LESS ${_IVL_b_patch} ) 81 set ( ${r} TRUE ) 82 elseif( _IVL_a_patch EQUAL ${_IVL_b_patch} ) 83 if ( _IVL_a_tweak LESS ${_IVL_b_tweak} ) 84 set ( ${r} TRUE ) 85 endif() 86 endif() 87 endif() 88 endif() 89 90endmacro() 91 92# 93# Given two version string of the form "major.[minor.[patch.[tweak]]]" 94# returns TRUE if the first is greater than the second, FALSE otherwise. 95# 96macro( IS_VERSION_GREATER a b r ) 97 98 IS_VERSION_LESS( ${a} ${b} _IVG_less ) 99 100 if ( _IVG_less ) 101 set( ${r} FALSE ) 102 else() 103 104 IS_VERSION_EQUAL( ${a} ${b} _IVG_eq ) 105 106 if ( _IVG_eq ) 107 set( ${r} FALSE ) 108 else() 109 set( ${r} TRUE ) 110 endif() 111 112 endif() 113 114endmacro() 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129# 130# -= TESTING =- 131# 132 133 134macro ( TEST_VERSION_DECOMPOSE v expected_major expected_minor expected_patch expected_tweak ) 135 136 VERSION_DECOMPOSE( ${v} major minor patch tweak ) 137 138 set ( OK 0 ) 139 140 if ( major EQUAL "${expected_major}" ) 141 if ( minor EQUAL "${expected_minor}" ) 142 if ( patch EQUAL "${expected_patch}" ) 143 if ( tweak EQUAL "${expected_tweak}" ) 144 set ( OK 1 ) 145 endif() 146 endif() 147 endif() 148 endif() 149 150 if ( OK ) 151 message( STATUS "correct - ${v} -> ${major}, ${minor}, ${patch}, ${tweak} " ) 152 else() 153 message( STATUS "FAILED - ${v} -> ${major}, ${minor}, ${patch}, ${tweak} " ) 154 endif() 155 156endmacro() 157 158macro ( TEST_VERSION_COMPARISON op v0 v1 expected ) 159 160 if ( "${op}" STREQUAL "<" ) 161 IS_VERSION_LESS( ${v0} ${v1} result ) 162 elseif ( "${op}" STREQUAL ">" ) 163 IS_VERSION_GREATER( ${v0} ${v1} result ) 164 else() 165 IS_VERSION_EQUAL( ${v0} ${v1} result ) 166 endif() 167 168 if ( result STREQUAL ${expected} ) 169 message( STATUS "correct - ${v0} ${op} ${v1} => ${result}" ) 170 else() 171 message( STATUS "FAILED - ${v0} ${op} ${v1} => ${result}" ) 172 endif() 173 174endmacro() 175 176if ( UNIT_TEST_VERSION_UTILS ) 177 178 TEST_VERSION_DECOMPOSE("1.2.3.4" 1 2 3 4 ) 179 TEST_VERSION_DECOMPOSE("1.2.3" 1 2 3 -1 ) 180 TEST_VERSION_DECOMPOSE("1.2" 1 2 -1 -1 ) 181 TEST_VERSION_DECOMPOSE("1" 1 -1 -1 -1 ) 182 183 TEST_VERSION_COMPARISON( "==" "1.2.3.4" "1.2.3.4" TRUE ) 184 TEST_VERSION_COMPARISON( "==" "1.2.3" "1.2.3" TRUE ) 185 TEST_VERSION_COMPARISON( "==" "1.2" "1.2" TRUE ) 186 TEST_VERSION_COMPARISON( "==" "1" "1" TRUE ) 187 188 TEST_VERSION_COMPARISON( "==" "1.2.3.4" "1.2.3" FALSE ) 189 TEST_VERSION_COMPARISON( "==" "1.2.3" "1.2" FALSE ) 190 TEST_VERSION_COMPARISON( "==" "1.2" "1" FALSE ) 191 192 TEST_VERSION_COMPARISON( "==" "1.2.3.4" "1.2.3.5" FALSE ) 193 TEST_VERSION_COMPARISON( "==" "1.2.3" "1.2.4" FALSE ) 194 TEST_VERSION_COMPARISON( "==" "1.2" "1.3" FALSE ) 195 TEST_VERSION_COMPARISON( "==" "1" "2" FALSE ) 196 197 TEST_VERSION_COMPARISON( "<" "1.2.3.4" "1.2.3.4" FALSE ) 198 TEST_VERSION_COMPARISON( "<" "1.2.3" "1.2.3" FALSE ) 199 TEST_VERSION_COMPARISON( "<" "1.2" "1.2" FALSE ) 200 TEST_VERSION_COMPARISON( "<" "1" "1" FALSE ) 201 202 TEST_VERSION_COMPARISON( "<" "1.2.3.4" "1.2.3.5" TRUE ) 203 TEST_VERSION_COMPARISON( "<" "1.2.3.4" "1.2.4.5" TRUE ) 204 TEST_VERSION_COMPARISON( "<" "1.2.3.4" "1.3.4.5" TRUE ) 205 TEST_VERSION_COMPARISON( "<" "1.2.3.4" "2.3.4.5" TRUE ) 206 207 TEST_VERSION_COMPARISON( "<" "1.2.3.4" "1.2.4" TRUE ) 208 TEST_VERSION_COMPARISON( "<" "1.2.3.4" "1.3" TRUE ) 209 TEST_VERSION_COMPARISON( "<" "1.2.3.4" "2" TRUE ) 210 211 TEST_VERSION_COMPARISON( "<" "1.2.3" "1.2.4" TRUE ) 212 TEST_VERSION_COMPARISON( "<" "1.2" "1.3" TRUE ) 213 TEST_VERSION_COMPARISON( "<" "1" "2" TRUE ) 214 215 TEST_VERSION_COMPARISON( "<" "1.2.3.6" "1.2.4.5" TRUE ) 216 TEST_VERSION_COMPARISON( "<" "1.2.5.6" "1.3.4.5" TRUE ) 217 TEST_VERSION_COMPARISON( "<" "1.4.5.6" "2.3.4.5" TRUE ) 218 219 TEST_VERSION_COMPARISON( ">" "1.2.3.4" "1.2.3.4" FALSE ) 220 TEST_VERSION_COMPARISON( ">" "1.2.3" "1.2.3" FALSE ) 221 TEST_VERSION_COMPARISON( ">" "1.2" "1.2" FALSE ) 222 TEST_VERSION_COMPARISON( ">" "1" "1" FALSE ) 223 224 TEST_VERSION_COMPARISON( ">" "1.2.3.5" "1.2.3.4" TRUE ) 225 226endif() 227 228endif() 229