From 222e9a2876fccaf5b7da399430b4ec0e4db5d4d4 Mon Sep 17 00:00:00 2001 From: Bill Hoffman Date: Mon, 26 Jan 2004 13:32:46 -0500 Subject: [PATCH] BUG: fix put/get env problems --- Source/cmCTest.cxx | 20 ++------------- Source/cmGlobalGenerator.cxx | 25 +++--------------- Source/cmSystemTools.cxx | 24 +++++++++++++++++ Source/cmSystemTools.h | 4 +++ Source/cmake.cxx | 50 +++++++++++++++++------------------- Source/cmake.h | 4 +-- 6 files changed, 58 insertions(+), 69 deletions(-) diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx index 37ed8f8fe..c64211d4d 100644 --- a/Source/cmCTest.cxx +++ b/Source/cmCTest.cxx @@ -3019,28 +3019,12 @@ int cmCTest::RunConfigurationScript() // set any environment variables if (ctestEnv) { - static char ctestEnvStatic[100][5000]; std::vector envArgs; cmSystemTools::ExpandListArgument(ctestEnv,envArgs); - int numArgs = envArgs.size(); - // we have a hard limit of 100 env args due to stupid format of putenv - if (numArgs > 100) - { - numArgs = 100; - } // for each variable/argument do a putenv - int i; - for (i = 0; i < numArgs; ++i) + for (unsigned i = 0; i < envArgs.size(); ++i) { - // also limit args to be at most 4K long - std::string::size_type size = envArgs[i].size(); - if(size > 4999) - { - size = 4999; - } - strncpy(ctestEnvStatic[i], envArgs[i].c_str(), size); - ctestEnvStatic[i][size] = 0; - putenv(ctestEnvStatic[i]); + cmSystemTools::PutEnv(envArgs[i].c_str()); } } diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx index 794033eec..e22b82fcb 100644 --- a/Source/cmGlobalGenerator.cxx +++ b/Source/cmGlobalGenerator.cxx @@ -43,7 +43,7 @@ cmGlobalGenerator::~cmGlobalGenerator() m_LocalGenerators.clear(); } - + void cmGlobalGenerator::EnableLanguage(const char* lang, cmMakefile *mf) { @@ -116,7 +116,6 @@ void cmGlobalGenerator::EnableLanguage(const char* lang, systemFile += "/Modules/CMakeDetermineSystem.cmake"; mf->ReadListFile(0, systemFile.c_str()); } - // check for a C compiler and configure it if(!isLocal && !this->GetLanguageEnabled("C") && @@ -137,20 +136,11 @@ void cmGlobalGenerator::EnableLanguage(const char* lang, this->SetLanguageEnabled("C"); // put CC in the environment in case user scripts want // to run configure - // see man putenv for explaination of this stupid code... if(mf->GetDefinition("CMAKE_C_COMPILER")) { - static char envCC[5000]; std::string env = "CC=${CMAKE_C_COMPILER}"; mf->ExpandVariablesInString(env); - unsigned int size = static_cast(env.size()); - if(size > 4999) - { - size = 4999; - } - strncpy(envCC, env.c_str(), size); - envCC[size] = 0; - putenv(envCC); + cmSystemTools::PutEnv(env.c_str()); } } @@ -166,20 +156,11 @@ void cmGlobalGenerator::EnableLanguage(const char* lang, this->SetLanguageEnabled("CXX"); // put CXX in the environment in case user scripts want // to run configure - // see man putenv for explaination of this stupid code... - static char envCXX[5000]; if(mf->GetDefinition("CMAKE_CXX_COMPILER")) { std::string env = "CXX=${CMAKE_CXX_COMPILER}"; mf->ExpandVariablesInString(env); - unsigned int size = static_cast(env.size()); - if(size > 4999) - { - size = 4999; - } - strncpy(envCXX, env.c_str(), size); - envCXX[size] = 0; - putenv(envCXX); + cmSystemTools::PutEnv(env.c_str()); } } // check for a Java compiler and configure it diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 9f0ea3df5..fe6cfa6f3 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -1177,3 +1177,27 @@ std::string cmSystemTools::RelativePath(const char* local, const char* remote) relativePath += relativeSplit[i]; return relativePath; } +class cmDeletingCharVector : public std::vector +{ +public: + ~cmDeletingCharVector() + { + for(std::vector::iterator i = this->begin(); + i != this->end(); ++i) + { + delete *i; + } + } +}; + + +bool cmSystemTools::PutEnv(const char* value) +{ + static cmDeletingCharVector localEnvironment; + char* envVar = new char[strlen(value)+1]; + strcpy(envVar, value); + putenv(envVar); + // save the pointer in the static vector so that it can + // be deleted on exit + localEnvironment.push_back(envVar); +} diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index 4c5e4252e..a8bbc8be7 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -256,6 +256,10 @@ public: static std::string RelativePath(const char* local, const char* remote); ///! split a path by separator into an array of strings, default is / static std::vector SplitString(const char* s, char separator = '/'); + /** put a string into the environment + of the form var=value */ + static bool PutEnv(const char* value); + private: static bool s_ForceUnixPaths; static bool s_RunCommandHideConsole; diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 50170c68e..c1c26989d 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -84,8 +84,7 @@ cmake::cmake() // encode the MAKEFLAGS variable in a strange way. if(getenv("MAKEFLAGS")) { - static char makeflags[] = "MAKEFLAGS="; - putenv(makeflags); + cmSystemTools::PutEnv("MAKEFLAGS="); } m_Local = false; @@ -876,36 +875,18 @@ void cmake::SetGlobalGenerator(cmGlobalGenerator *gg) delete m_GlobalGenerator; // restore the original environment variables CXX and CC // Restor CC - static char envCC[5000]; std::string env = "CC="; - if(m_CCEnvironment) + if(m_CCEnvironment.size()) { env += m_CCEnvironment; } - std::string::size_type size = env.size(); - if(size > 4999) - { - size = 4999; - } - strncpy(envCC, env.c_str(), size); - envCC[size] = 0; - putenv(envCC); - - // Restore CXX - static char envCXX[5000]; + cmSystemTools::PutEnv(env.c_str()); env = "CXX="; - if(m_CXXEnvironment) + if(m_CXXEnvironment.size()) { env += m_CXXEnvironment; } - size = env.size(); - if(size > 4999) - { - size = 4999; - } - strncpy(envCXX, env.c_str(), size); - envCXX[size] = 0; - putenv(envCXX); + cmSystemTools::PutEnv(env.c_str()); } // set the new @@ -915,9 +896,24 @@ void cmake::SetGlobalGenerator(cmGlobalGenerator *gg) // on windows. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths()); // Save the environment variables CXX and CC - m_CXXEnvironment = getenv("CXX"); - m_CCEnvironment = getenv("CC"); - + const char* cxx = getenv("CXX"); + const char* cc = getenv("CC"); + if(cxx) + { + m_CXXEnvironment = cxx; + } + else + { + m_CXXEnvironment = ""; + } + if(cc) + { + m_CCEnvironment = cc; + } + else + { + m_CCEnvironment = ""; + } // set the cmake instance just to be sure gg->SetCMakeInstance(this); } diff --git a/Source/cmake.h b/Source/cmake.h index d81807043..833b45424 100644 --- a/Source/cmake.h +++ b/Source/cmake.h @@ -298,8 +298,8 @@ private: bool m_InTryCompile; bool m_ScriptMode; std::string m_CMakeCommand; - const char* m_CXXEnvironment; - const char* m_CCEnvironment; + std::string m_CXXEnvironment; + std::string m_CCEnvironment; bool m_DebugTryCompile; };