From acdd032109f412a63122337cd9b7ebdde13849bc Mon Sep 17 00:00:00 2001 From: Ken Martin Date: Thu, 20 Jan 2005 14:38:16 -0500 Subject: [PATCH] ENH: now the set command can set environment variables --- Source/cmSetCommand.cxx | 37 ++++++++++++++++++++++++++++++++++++- Source/cmSetCommand.h | 5 ++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/Source/cmSetCommand.cxx b/Source/cmSetCommand.cxx index 60c9358c1..62a5949ce 100644 --- a/Source/cmSetCommand.cxx +++ b/Source/cmSetCommand.cxx @@ -24,6 +24,42 @@ bool cmSetCommand::InitialPass(std::vector const& args) this->SetError("called with incorrect number of arguments"); 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. if (args.size() == 1) { @@ -35,7 +71,6 @@ bool cmSetCommand::InitialPass(std::vector const& args) // SET (VAR value ) // SET (VAR 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 bool cache = false; // optional bool force = false; // optional diff --git a/Source/cmSetCommand.h b/Source/cmSetCommand.h index b168be006..0556053f5 100644 --- a/Source/cmSetCommand.h +++ b/Source/cmSetCommand.h @@ -87,7 +87,10 @@ public: "then this always writes into the current makefile. The FORCE option " "will overwrite the CACHE value removing any changes by the USER.\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);