[Cmake-commits] [cmake-commits] king committed cmIfCommand.cxx 1.88 1.89 cmIfCommand.h 1.49 1.50

cmake-commits at cmake.org cmake-commits at cmake.org
Wed Sep 10 11:58:42 EDT 2008


Update of /cvsroot/CMake/CMake/Source
In directory public:/mounts/ram/cvs-serv22756/Source

Modified Files:
	cmIfCommand.cxx cmIfCommand.h 
Log Message:
ENH: Add version comparison to if() command

Provide VERSION_LESS, VERSION_EQUAL, and VERSION_GREATER operators in
the if() command.  This simplifies component-wise comparison of version
numbers in the form "major[.minor[.patch[.tweak]]]".


Index: cmIfCommand.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmIfCommand.cxx,v
retrieving revision 1.88
retrieving revision 1.89
diff -C 2 -d -r1.88 -r1.89
*** cmIfCommand.cxx	20 Aug 2008 15:45:16 -0000	1.88
--- cmIfCommand.cxx	10 Sep 2008 15:58:40 -0000	1.89
***************
*** 286,289 ****
--- 286,317 ----
  
    //=========================================================================
+   enum Op { OpLess, OpEqual, OpGreater };
+   bool HandleVersionCompare(Op op, const char* lhs_str, const char* rhs_str)
+   {
+   // Parse out up to 4 components.
+   unsigned int lhs[4] = {0,0,0,0};
+   unsigned int rhs[4] = {0,0,0,0};
+   sscanf(lhs_str, "%u.%u.%u.%u", &lhs[0], &lhs[1], &lhs[2], &lhs[3]);
+   sscanf(rhs_str, "%u.%u.%u.%u", &rhs[0], &rhs[1], &rhs[2], &rhs[3]);
+ 
+   // Do component-wise comparison.
+   for(unsigned int i=0; i < 4; ++i)
+     {
+     if(lhs[i] < rhs[i])
+       {
+       // lhs < rhs, so true if operation is LESS
+       return op == OpLess;
+       }
+     else if(lhs[i] > rhs[i])
+       {
+       // lhs > rhs, so true if operation is GREATER
+       return op == OpGreater;
+       }
+     }
+   // lhs == rhs, so true if operation is EQUAL
+   return op == OpEqual;
+   }
+ 
+   //=========================================================================
    // level 0 processes parenthetical expressions
    bool HandleLevel0(std::list<std::string> &newArgs,
***************
*** 553,556 ****
--- 581,604 ----
          }
  
+       if (argP1 != newArgs.end() && argP2 != newArgs.end() &&
+         (*(argP1) == "VERSION_LESS" || *(argP1) == "VERSION_GREATER" ||
+          *(argP1) == "VERSION_EQUAL"))
+         {
+         def = cmIfCommand::GetVariableOrString(arg->c_str(), makefile);
+         def2 = cmIfCommand::GetVariableOrString((argP2)->c_str(), makefile);
+         Op op = OpEqual;
+         if(*argP1 == "VERSION_LESS")
+           {
+           op = OpLess;
+           }
+         else if(*argP1 == "VERSION_GREATER")
+           {
+           op = OpGreater;
+           }
+         bool result = HandleVersionCompare(op, def, def2);
+         HandleBinaryOp(result,
+           reducible, arg, newArgs, argP1, argP2);
+         }
+ 
        // is file A newer than file B
        if (argP1 != newArgs.end() && argP2 != newArgs.end() &&

Index: cmIfCommand.h
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmIfCommand.h,v
retrieving revision 1.49
retrieving revision 1.50
diff -C 2 -d -r1.49 -r1.50
*** cmIfCommand.h	20 Aug 2008 15:45:16 -0000	1.49
--- cmIfCommand.h	10 Sep 2008 15:58:40 -0000	1.50
***************
*** 178,181 ****
--- 178,186 ----
        "True if the given string or variable's value is lexicographically "
        "less (or greater, or equal) than the string on the right.\n"
+       "  if(version1 VERSION_LESS version2)\n"
+       "  if(version1 VERSION_EQUAL version2)\n"
+       "  if(version1 VERSION_GREATER version2)\n"
+       "Component-wise integer version number comparison (version format is "
+       "major[.minor[.patch[.tweak]]]).\n"
        "  if(DEFINED variable)\n"
        "True if the given variable is defined. It does not matter if the "



More information about the Cmake-commits mailing list