Pārlūkot izejas kodu

ENH: now the set command can set environment variables

Ken Martin 21 gadi atpakaļ
vecāks
revīzija
acdd032109
2 mainītis faili ar 40 papildinājumiem un 2 dzēšanām
  1. 36 1
      Source/cmSetCommand.cxx
  2. 4 1
      Source/cmSetCommand.h

+ 36 - 1
Source/cmSetCommand.cxx

@@ -24,6 +24,42 @@ bool cmSetCommand::InitialPass(std::vector<std::string> const& args)
     this->SetError("called with incorrect number of arguments");
     this->SetError("called with incorrect number of arguments");
     return false;
     return false;
     }
     }
+  
+  // watch for ENV signatures
+  const char* variable = args[0].c_str(); // VAR is always first
+  bool haveEnvVariable = false;
+  if (!strncmp(variable,"ENV{",4) && strlen(variable) > 5)
+    {
+    // what is the variable name
+    char *varName = new char [strlen(variable)];
+    strncpy(varName,variable+4,strlen(variable)-5);
+    varName[strlen(variable)-5] = '\0';
+    std::string putEnvArg = varName;
+    putEnvArg += "=";
+    
+    // what is the current value if any
+    const char *currValue = getenv(varName);
+
+    // will it be set to something, then set it
+    if (args.size() > 1 && args[1].size())
+      {
+      // but only if it is different from current value
+      if (!currValue || strcmp(currValue,args[1].c_str()))
+        {
+        putEnvArg += args[1];
+        cmSystemTools::PutEnv(putEnvArg.c_str());
+        }
+      return true;
+      }
+    
+    // if it will be cleared, then clear it if it isn;t already clear
+    if (currValue)
+      {
+      cmSystemTools::PutEnv(putEnvArg.c_str());
+      }
+    return true;
+    }
+  
   // SET (VAR) // Removes the definition of VAR.
   // SET (VAR) // Removes the definition of VAR.
   if (args.size() == 1)
   if (args.size() == 1)
     {
     {
@@ -35,7 +71,6 @@ bool cmSetCommand::InitialPass(std::vector<std::string> const& args)
   //  SET (VAR value )
   //  SET (VAR value )
   //  SET (VAR CACHE TYPE "doc String" [FORCE])
   //  SET (VAR CACHE TYPE "doc String" [FORCE])
   //  SET (VAR value CACHE TYPE "doc string" [FORCE])
   //  SET (VAR value CACHE TYPE "doc string" [FORCE])
-  const char* variable = args[0].c_str(); // VAR is always first
   std::string value;  // optional
   std::string value;  // optional
   bool cache = false; // optional
   bool cache = false; // optional
   bool force = false; // optional
   bool force = false; // optional

+ 4 - 1
Source/cmSetCommand.h

@@ -87,7 +87,10 @@ public:
       "then this always writes into the current makefile. The FORCE option "
       "then this always writes into the current makefile. The FORCE option "
       "will overwrite the CACHE value removing any changes by the USER.\n"
       "will overwrite the CACHE value removing any changes by the USER.\n"
       "  SET(VAR VALUE1 ... VALUEN).\n"
       "  SET(VAR VALUE1 ... VALUEN).\n"
-      "In this case VAR is set to a ; separated list of values.";
+      "In this case VAR is set to a ; separated list of values.\n"
+      "VAR can be an environment variable such as:\n"
+      "  SET( ENV{PATH} /home/martink )\n"
+      "in which case the environment variable will be set.";
     }
     }
   
   
   cmTypeMacro(cmSetCommand, cmCommand);
   cmTypeMacro(cmSetCommand, cmCommand);