2001-01-11 22:55:47 +03:00
|
|
|
/*=========================================================================
|
|
|
|
|
2002-10-24 02:03:27 +04:00
|
|
|
Program: CMake - Cross-Platform Makefile Generator
|
2001-01-11 22:55:47 +03:00
|
|
|
Module: $RCSfile$
|
|
|
|
Language: C++
|
|
|
|
Date: $Date$
|
|
|
|
Version: $Revision$
|
|
|
|
|
2002-10-24 02:03:27 +04:00
|
|
|
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
|
|
|
|
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
|
2001-01-11 22:55:47 +03:00
|
|
|
|
2002-01-21 23:30:43 +03:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
PURPOSE. See the above copyright notices for more information.
|
2001-01-11 22:55:47 +03:00
|
|
|
|
|
|
|
=========================================================================*/
|
2001-09-28 00:50:59 +04:00
|
|
|
#include "cmSystemTools.h"
|
2001-04-26 18:49:12 +04:00
|
|
|
#include <ctype.h>
|
2002-11-19 22:40:47 +03:00
|
|
|
#include <errno.h>
|
2003-02-14 18:56:21 +03:00
|
|
|
#include <time.h>
|
2007-10-08 00:22:16 +04:00
|
|
|
#include <string.h>
|
2002-11-19 22:40:47 +03:00
|
|
|
|
2003-06-23 22:10:12 +04:00
|
|
|
#include <cmsys/RegularExpression.hxx>
|
2003-06-23 16:58:58 +04:00
|
|
|
#include <cmsys/Directory.hxx>
|
2007-02-01 19:45:37 +03:00
|
|
|
#include <cmsys/System.h>
|
2007-12-20 01:15:41 +03:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
|
|
|
# include <cmsys/Terminal.h>
|
|
|
|
#endif
|
2008-08-14 17:53:17 +04:00
|
|
|
#include <cmsys/stl/algorithm>
|
2000-09-12 13:30:35 +04:00
|
|
|
|
2007-10-08 00:22:16 +04:00
|
|
|
#if defined(_WIN32)
|
|
|
|
# include <windows.h>
|
2000-09-12 13:30:35 +04:00
|
|
|
#else
|
2007-10-08 00:22:16 +04:00
|
|
|
# include <sys/types.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# include <utime.h>
|
2007-10-08 18:03:37 +04:00
|
|
|
# include <sys/wait.h>
|
2000-09-12 13:30:35 +04:00
|
|
|
#endif
|
|
|
|
|
2004-01-26 22:55:57 +03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2007-10-08 18:05:42 +04:00
|
|
|
#if defined(_WIN32) && \
|
|
|
|
(defined(_MSC_VER) || defined(__WATCOMC__) || \
|
|
|
|
defined(__BORLANDC__) || defined(__MINGW32__))
|
2007-10-08 00:22:16 +04:00
|
|
|
# include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2005-12-30 22:51:15 +03:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
|
|
|
# include <libtar/libtar.h>
|
|
|
|
# include <memory> // auto_ptr
|
|
|
|
# include <fcntl.h>
|
2006-10-19 23:00:10 +04:00
|
|
|
# include <cm_zlib.h>
|
2007-07-16 23:10:40 +04:00
|
|
|
# include <cmsys/MD5.h>
|
2005-12-30 22:51:15 +03:00
|
|
|
#endif
|
|
|
|
|
2008-02-28 00:26:35 +03:00
|
|
|
#if defined(CMAKE_USE_ELF_PARSER)
|
|
|
|
# include "cmELF.h"
|
|
|
|
#endif
|
|
|
|
|
2008-04-14 19:43:45 +04:00
|
|
|
class cmSystemToolsFileTime
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
FILETIME timeCreation;
|
|
|
|
FILETIME timeLastAccess;
|
|
|
|
FILETIME timeLastWrite;
|
|
|
|
#else
|
|
|
|
struct utimbuf timeBuf;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2005-05-03 22:58:13 +04:00
|
|
|
#if defined(__sgi) && !defined(__GNUC__)
|
|
|
|
# pragma set woff 1375 /* base class destructor not virtual */
|
|
|
|
#endif
|
2002-11-06 21:06:49 +03:00
|
|
|
|
2006-03-18 00:14:04 +03:00
|
|
|
#if !defined(HAVE_ENVIRON_NOT_REQUIRE_PROTOTYPE)
|
2006-03-17 23:46:20 +03:00
|
|
|
// For GetEnvironmentVariables
|
|
|
|
# if defined(_WIN32)
|
|
|
|
extern __declspec( dllimport ) char** environ;
|
|
|
|
# else
|
|
|
|
extern char** environ;
|
|
|
|
# endif
|
|
|
|
#endif
|
2006-03-17 18:58:37 +03:00
|
|
|
|
2007-10-08 00:22:16 +04:00
|
|
|
#ifdef _WIN32
|
2007-10-05 17:46:28 +04:00
|
|
|
class cmSystemToolsWindowsHandle
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cmSystemToolsWindowsHandle(HANDLE h): handle_(h) {}
|
|
|
|
~cmSystemToolsWindowsHandle()
|
|
|
|
{
|
|
|
|
if(this->handle_ != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
CloseHandle(this->handle_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
operator bool() const { return this->handle_ != INVALID_HANDLE_VALUE; }
|
2007-10-05 23:51:01 +04:00
|
|
|
bool operator !() const { return this->handle_ == INVALID_HANDLE_VALUE; }
|
2007-10-05 17:46:28 +04:00
|
|
|
operator HANDLE() const { return this->handle_; }
|
|
|
|
private:
|
|
|
|
HANDLE handle_;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2002-11-08 23:46:08 +03:00
|
|
|
bool cmSystemTools::s_RunCommandHideConsole = false;
|
2001-11-27 02:28:27 +03:00
|
|
|
bool cmSystemTools::s_DisableRunCommandOutput = false;
|
2001-02-23 03:24:43 +03:00
|
|
|
bool cmSystemTools::s_ErrorOccured = false;
|
2003-01-21 20:50:48 +03:00
|
|
|
bool cmSystemTools::s_FatalErrorOccured = false;
|
2001-11-20 01:52:08 +03:00
|
|
|
bool cmSystemTools::s_DisableMessages = false;
|
2003-08-22 00:22:23 +04:00
|
|
|
bool cmSystemTools::s_ForceUnixPaths = false;
|
2001-02-23 03:24:43 +03:00
|
|
|
|
2002-09-28 01:28:15 +04:00
|
|
|
std::string cmSystemTools::s_Windows9xComspecSubstitute = "command.com";
|
|
|
|
void cmSystemTools::SetWindows9xComspecSubstitute(const char* str)
|
|
|
|
{
|
|
|
|
if ( str )
|
|
|
|
{
|
|
|
|
cmSystemTools::s_Windows9xComspecSubstitute = str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const char* cmSystemTools::GetWindows9xComspecSubstitute()
|
|
|
|
{
|
|
|
|
return cmSystemTools::s_Windows9xComspecSubstitute.c_str();
|
|
|
|
}
|
|
|
|
|
2006-05-12 21:53:21 +04:00
|
|
|
void (*cmSystemTools::s_ErrorCallback)(const char*, const char*,
|
|
|
|
bool&, void*);
|
2004-01-07 19:24:22 +03:00
|
|
|
void (*cmSystemTools::s_StdoutCallback)(const char*, int len, void*);
|
2002-09-18 22:18:43 +04:00
|
|
|
void* cmSystemTools::s_ErrorCallbackClientData = 0;
|
2004-01-07 19:24:22 +03:00
|
|
|
void* cmSystemTools::s_StdoutCallbackClientData = 0;
|
2001-10-29 18:19:34 +03:00
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
// replace replace with with as many times as it shows up in source.
|
|
|
|
// write the result into source.
|
2001-02-16 19:34:23 +03:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
2008-05-27 22:47:00 +04:00
|
|
|
void cmSystemTools::ExpandRegistryValues(std::string& source, KeyWOW64 view)
|
2003-06-23 16:58:58 +04:00
|
|
|
{
|
|
|
|
// Regular expression to match anything inside [...] that begins in HKEY.
|
|
|
|
// Note that there is a special rule for regular expressions to match a
|
|
|
|
// close square-bracket inside a list delimited by square brackets.
|
|
|
|
// The "[^]]" part of this expression will match any character except
|
|
|
|
// a close square-bracket. The ']' character must be the first in the
|
|
|
|
// list of characters inside the [^...] block of the expression.
|
2003-06-23 22:10:12 +04:00
|
|
|
cmsys::RegularExpression regEntry("\\[(HKEY[^]]*)\\]");
|
2003-06-23 16:58:58 +04:00
|
|
|
|
|
|
|
// check for black line or comment
|
|
|
|
while (regEntry.find(source))
|
2001-02-16 19:34:23 +03:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
// the arguments are the second match
|
|
|
|
std::string key = regEntry.match(1);
|
|
|
|
std::string val;
|
2008-05-27 22:47:00 +04:00
|
|
|
if (ReadRegistryValue(key.c_str(), val, view))
|
2001-02-16 19:34:23 +03:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
std::string reg = "[";
|
|
|
|
reg += key + "]";
|
|
|
|
cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
|
2001-02-16 19:34:23 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
std::string reg = "[";
|
|
|
|
reg += key + "]";
|
|
|
|
cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
|
2001-02-16 19:34:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-03-08 18:30:18 +03:00
|
|
|
#else
|
2008-05-27 22:47:00 +04:00
|
|
|
void cmSystemTools::ExpandRegistryValues(std::string& source, KeyWOW64)
|
2003-06-23 16:58:58 +04:00
|
|
|
{
|
2008-04-09 00:06:44 +04:00
|
|
|
cmsys::RegularExpression regEntry("\\[(HKEY[^]]*)\\]");
|
|
|
|
while (regEntry.find(source))
|
|
|
|
{
|
|
|
|
// the arguments are the second match
|
|
|
|
std::string key = regEntry.match(1);
|
|
|
|
std::string val;
|
|
|
|
std::string reg = "[";
|
|
|
|
reg += key + "]";
|
|
|
|
cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
|
|
|
|
}
|
|
|
|
|
2001-03-08 18:30:18 +03:00
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
#endif
|
2001-03-08 18:30:18 +03:00
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
std::string cmSystemTools::EscapeQuotes(const char* str)
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
std::string result = "";
|
|
|
|
for(const char* ch = str; *ch != '\0'; ++ch)
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
if(*ch == '"')
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
result += '\\';
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
result += *ch;
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
return result;
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
std::string cmSystemTools::EscapeSpaces(const char* str)
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
2004-06-23 01:23:28 +04:00
|
|
|
bool useDoubleQ = true;
|
|
|
|
#else
|
|
|
|
bool useDoubleQ = false;
|
|
|
|
#endif
|
|
|
|
if(cmSystemTools::s_ForceUnixPaths)
|
|
|
|
{
|
|
|
|
useDoubleQ = false;
|
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
|
2004-06-23 01:23:28 +04:00
|
|
|
if(useDoubleQ)
|
2002-11-08 23:46:08 +03:00
|
|
|
{
|
2004-06-23 01:23:28 +04:00
|
|
|
std::string result;
|
|
|
|
|
|
|
|
// if there are spaces
|
|
|
|
std::string temp = str;
|
|
|
|
if (temp.find(" ") != std::string::npos &&
|
|
|
|
temp.find("\"")==std::string::npos)
|
|
|
|
{
|
|
|
|
result = "\"";
|
|
|
|
result += str;
|
|
|
|
result += "\"";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return str;
|
2002-11-08 23:46:08 +03:00
|
|
|
}
|
2004-06-23 01:23:28 +04:00
|
|
|
else
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2004-06-23 01:23:28 +04:00
|
|
|
std::string result = "";
|
|
|
|
for(const char* ch = str; *ch != '\0'; ++ch)
|
2003-06-23 16:58:58 +04:00
|
|
|
{
|
2004-06-23 01:23:28 +04:00
|
|
|
if(*ch == ' ')
|
|
|
|
{
|
|
|
|
result += '\\';
|
|
|
|
}
|
|
|
|
result += *ch;
|
2003-06-23 16:58:58 +04:00
|
|
|
}
|
2004-06-23 01:23:28 +04:00
|
|
|
return result;
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
|
|
|
}
|
2000-09-12 13:30:35 +04:00
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
void cmSystemTools::Error(const char* m1, const char* m2,
|
|
|
|
const char* m3, const char* m4)
|
|
|
|
{
|
|
|
|
std::string message = "CMake Error: ";
|
|
|
|
if(m1)
|
2001-05-11 18:53:17 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
message += m1;
|
2001-05-11 18:53:17 +04:00
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
if(m2)
|
2001-05-11 18:53:17 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
message += m2;
|
2001-05-11 18:53:17 +04:00
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
if(m3)
|
2001-05-11 18:53:17 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
message += m3;
|
2001-05-11 18:53:17 +04:00
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
if(m4)
|
2001-05-11 18:53:17 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
message += m4;
|
2001-05-11 18:53:17 +04:00
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
cmSystemTools::s_ErrorOccured = true;
|
|
|
|
cmSystemTools::Message(message.c_str(),"Error");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void cmSystemTools::SetErrorCallback(ErrorCallback f, void* clientData)
|
|
|
|
{
|
|
|
|
s_ErrorCallback = f;
|
|
|
|
s_ErrorCallbackClientData = clientData;
|
|
|
|
}
|
|
|
|
|
2004-01-07 19:24:22 +03:00
|
|
|
void cmSystemTools::SetStdoutCallback(StdoutCallback f, void* clientData)
|
|
|
|
{
|
|
|
|
s_StdoutCallback = f;
|
|
|
|
s_StdoutCallbackClientData = clientData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmSystemTools::Stdout(const char* s)
|
|
|
|
{
|
|
|
|
if(s_StdoutCallback)
|
|
|
|
{
|
2007-09-24 21:21:25 +04:00
|
|
|
(*s_StdoutCallback)(s, static_cast<int>(strlen(s)),
|
|
|
|
s_StdoutCallbackClientData);
|
2004-01-07 19:24:22 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout << s;
|
2004-01-21 23:55:47 +03:00
|
|
|
std::cout.flush();
|
2004-01-07 19:24:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmSystemTools::Stdout(const char* s, int length)
|
|
|
|
{
|
|
|
|
if(s_StdoutCallback)
|
|
|
|
{
|
|
|
|
(*s_StdoutCallback)(s, length, s_StdoutCallbackClientData);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout.write(s, length);
|
2004-01-21 23:55:47 +03:00
|
|
|
std::cout.flush();
|
2004-01-07 19:24:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
void cmSystemTools::Message(const char* m1, const char *title)
|
|
|
|
{
|
|
|
|
if(s_DisableMessages)
|
2001-05-11 18:53:17 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
return;
|
2001-05-11 18:53:17 +04:00
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
if(s_ErrorCallback)
|
2001-05-11 18:53:17 +04:00
|
|
|
{
|
2006-03-10 21:54:57 +03:00
|
|
|
(*s_ErrorCallback)(m1, title, s_DisableMessages,
|
|
|
|
s_ErrorCallbackClientData);
|
2003-06-23 16:58:58 +04:00
|
|
|
return;
|
2001-05-11 18:53:17 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
std::cerr << m1 << std::endl << std::flush;
|
2001-05-11 18:53:17 +04:00
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
|
2002-06-19 01:20:27 +04:00
|
|
|
}
|
2002-04-01 22:34:38 +04:00
|
|
|
|
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
void cmSystemTools::ReportLastSystemError(const char* msg)
|
2002-04-01 22:34:38 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
std::string m = msg;
|
|
|
|
m += ": System Error: ";
|
2003-06-24 18:16:28 +04:00
|
|
|
m += Superclass::GetLastSystemError();
|
2003-06-23 16:58:58 +04:00
|
|
|
cmSystemTools::Error(m.c_str());
|
|
|
|
}
|
|
|
|
|
2002-04-01 22:34:38 +04:00
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
bool cmSystemTools::IsOn(const char* val)
|
|
|
|
{
|
|
|
|
if (!val)
|
2002-04-01 22:34:38 +04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
std::basic_string<char> v = val;
|
2002-04-01 22:34:38 +04:00
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
for(std::basic_string<char>::iterator c = v.begin();
|
|
|
|
c != v.end(); c++)
|
2002-04-01 22:34:38 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
*c = toupper(*c);
|
2002-04-01 22:34:38 +04:00
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
|
|
|
|
}
|
2002-04-16 21:48:35 +04:00
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
bool cmSystemTools::IsNOTFOUND(const char* val)
|
|
|
|
{
|
2007-09-17 19:17:38 +04:00
|
|
|
size_t len = strlen(val);
|
2004-05-21 19:52:07 +04:00
|
|
|
const char* notfound = "-NOTFOUND";
|
2007-09-17 19:17:38 +04:00
|
|
|
const size_t lenNotFound = 9;
|
2004-05-21 19:52:07 +04:00
|
|
|
if(len < lenNotFound-1)
|
2002-04-01 22:34:38 +04:00
|
|
|
{
|
2004-05-21 19:52:07 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(len == lenNotFound-1)
|
|
|
|
{
|
|
|
|
return ( strcmp(val, "NOTFOUND") == 0);
|
2002-04-01 22:34:38 +04:00
|
|
|
}
|
2004-05-21 19:52:07 +04:00
|
|
|
return ((strncmp((val + (len - lenNotFound)), notfound, lenNotFound) == 0));
|
2002-06-19 01:20:27 +04:00
|
|
|
}
|
2002-04-01 22:34:38 +04:00
|
|
|
|
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
bool cmSystemTools::IsOff(const char* val)
|
2002-04-01 22:34:38 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
if (!val || strlen(val) == 0)
|
2002-04-01 22:34:38 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
return true;
|
2002-04-01 22:34:38 +04:00
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
std::basic_string<char> v = val;
|
2002-04-01 22:34:38 +04:00
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
for(std::basic_string<char>::iterator c = v.begin();
|
|
|
|
c != v.end(); c++)
|
2002-04-01 22:34:38 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
*c = toupper(*c);
|
2002-04-01 22:34:38 +04:00
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
|
|
|
|
v == "N" || cmSystemTools::IsNOTFOUND(v.c_str()) || v == "IGNORE");
|
2001-05-04 19:30:46 +04:00
|
|
|
}
|
|
|
|
|
2007-02-01 19:45:37 +03:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmSystemTools::ParseWindowsCommandLine(const char* command,
|
|
|
|
std::vector<std::string>& args)
|
|
|
|
{
|
|
|
|
// See the MSDN document "Parsing C Command-Line Arguments" at
|
|
|
|
// http://msdn2.microsoft.com/en-us/library/a1y7w461.aspx for rules
|
|
|
|
// of parsing the windows command line.
|
|
|
|
|
|
|
|
bool in_argument = false;
|
|
|
|
bool in_quotes = false;
|
|
|
|
int backslashes = 0;
|
|
|
|
std::string arg;
|
|
|
|
for(const char* c = command;*c; ++c)
|
|
|
|
{
|
|
|
|
if(*c == '\\')
|
|
|
|
{
|
|
|
|
++backslashes;
|
|
|
|
in_argument = true;
|
|
|
|
}
|
|
|
|
else if(*c == '"')
|
|
|
|
{
|
|
|
|
int backslash_pairs = backslashes >> 1;
|
|
|
|
int backslash_escaped = backslashes & 1;
|
|
|
|
arg.append(backslash_pairs, '\\');
|
|
|
|
backslashes = 0;
|
|
|
|
if(backslash_escaped)
|
|
|
|
{
|
|
|
|
/* An odd number of backslashes precede this quote.
|
|
|
|
It is escaped. */
|
|
|
|
arg.append(1, '"');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* An even number of backslashes precede this quote.
|
|
|
|
It is not escaped. */
|
|
|
|
in_quotes = !in_quotes;
|
|
|
|
}
|
|
|
|
in_argument = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
arg.append(backslashes, '\\');
|
|
|
|
backslashes = 0;
|
|
|
|
if(isspace(*c))
|
|
|
|
{
|
|
|
|
if(in_quotes)
|
|
|
|
{
|
|
|
|
arg.append(1, *c);
|
|
|
|
}
|
|
|
|
else if(in_argument)
|
|
|
|
{
|
|
|
|
args.push_back(arg);
|
|
|
|
arg = "";
|
|
|
|
in_argument = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
in_argument = true;
|
|
|
|
arg.append(1, *c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arg.append(backslashes, '\\');
|
|
|
|
if(in_argument)
|
|
|
|
{
|
|
|
|
args.push_back(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cmSystemTools::EscapeWindowsShellArgument(const char* arg,
|
|
|
|
int shell_flags)
|
|
|
|
{
|
|
|
|
char local_buffer[1024];
|
|
|
|
char* buffer = local_buffer;
|
|
|
|
int size = cmsysSystem_Shell_GetArgumentSizeForWindows(arg, shell_flags);
|
|
|
|
if(size > 1024)
|
|
|
|
{
|
|
|
|
buffer = new char[size];
|
|
|
|
}
|
|
|
|
cmsysSystem_Shell_GetArgumentForWindows(arg, buffer, shell_flags);
|
|
|
|
std::string result(buffer);
|
|
|
|
if(buffer != local_buffer)
|
|
|
|
{
|
|
|
|
delete [] buffer;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-08-08 03:00:53 +04:00
|
|
|
std::vector<cmStdString> cmSystemTools::ParseArguments(const char* command)
|
2003-08-04 06:32:15 +04:00
|
|
|
{
|
2003-08-08 03:00:53 +04:00
|
|
|
std::vector<cmStdString> args;
|
2003-08-04 06:32:15 +04:00
|
|
|
std::string arg;
|
2003-08-04 15:12:42 +04:00
|
|
|
|
2003-08-04 19:48:18 +04:00
|
|
|
bool win_path = false;
|
|
|
|
|
|
|
|
if ( command[0] != '/' && command[1] == ':' && command[2] == '\\' ||
|
2006-03-10 21:54:57 +03:00
|
|
|
command[0] == '\"' && command[1] != '/' && command[2] == ':'
|
|
|
|
&& command[3] == '\\' ||
|
|
|
|
command[0] == '\'' && command[1] != '/' && command[2] == ':'
|
|
|
|
&& command[3] == '\\' ||
|
2003-08-04 19:48:18 +04:00
|
|
|
command[0] == '\\' && command[1] == '\\')
|
|
|
|
{
|
|
|
|
win_path = true;
|
|
|
|
}
|
2003-08-04 06:32:15 +04:00
|
|
|
// Split the command into an argv array.
|
|
|
|
for(const char* c = command; *c;)
|
|
|
|
{
|
|
|
|
// Skip over whitespace.
|
|
|
|
while(*c == ' ' || *c == '\t')
|
|
|
|
{
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
arg = "";
|
|
|
|
if(*c == '"')
|
|
|
|
{
|
|
|
|
// Parse a quoted argument.
|
|
|
|
++c;
|
|
|
|
while(*c && *c != '"')
|
|
|
|
{
|
2003-08-04 15:55:40 +04:00
|
|
|
arg.append(1, *c);
|
|
|
|
++c;
|
2003-08-04 06:32:15 +04:00
|
|
|
}
|
|
|
|
if(*c)
|
|
|
|
{
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
args.push_back(arg);
|
|
|
|
}
|
2005-02-17 18:51:18 +03:00
|
|
|
else if(*c == '\'')
|
|
|
|
{
|
|
|
|
// Parse a quoted argument.
|
|
|
|
++c;
|
|
|
|
while(*c && *c != '\'')
|
|
|
|
{
|
|
|
|
arg.append(1, *c);
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
if(*c)
|
|
|
|
{
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
args.push_back(arg);
|
|
|
|
}
|
2003-08-04 06:32:15 +04:00
|
|
|
else if(*c)
|
|
|
|
{
|
|
|
|
// Parse an unquoted argument.
|
|
|
|
while(*c && *c != ' ' && *c != '\t')
|
|
|
|
{
|
2003-08-04 19:48:18 +04:00
|
|
|
if(*c == '\\' && !win_path)
|
2003-08-04 15:12:42 +04:00
|
|
|
{
|
|
|
|
++c;
|
|
|
|
if(*c)
|
|
|
|
{
|
|
|
|
arg.append(1, *c);
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
arg.append(1, *c);
|
|
|
|
++c;
|
|
|
|
}
|
2003-08-04 06:32:15 +04:00
|
|
|
}
|
|
|
|
args.push_back(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-08 03:00:53 +04:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2004-10-25 21:16:05 +04:00
|
|
|
|
2008-01-01 23:13:41 +03:00
|
|
|
bool cmSystemTools::RunSingleCommand(std::vector<cmStdString>const& command,
|
|
|
|
std::string* output ,
|
|
|
|
int* retVal , const char* dir ,
|
|
|
|
bool verbose ,
|
|
|
|
double timeout )
|
|
|
|
{
|
2004-10-25 21:16:05 +04:00
|
|
|
std::vector<const char*> argv;
|
2008-01-01 23:13:41 +03:00
|
|
|
for(std::vector<cmStdString>::const_iterator a = command.begin();
|
|
|
|
a != command.end(); ++a)
|
2003-08-08 03:00:53 +04:00
|
|
|
{
|
|
|
|
argv.push_back(a->c_str());
|
|
|
|
}
|
|
|
|
argv.push_back(0);
|
2003-08-04 06:32:15 +04:00
|
|
|
if ( output )
|
|
|
|
{
|
|
|
|
*output = "";
|
|
|
|
}
|
2004-10-25 21:16:05 +04:00
|
|
|
|
2003-08-04 06:32:15 +04:00
|
|
|
cmsysProcess* cp = cmsysProcess_New();
|
|
|
|
cmsysProcess_SetCommand(cp, &*argv.begin());
|
|
|
|
cmsysProcess_SetWorkingDirectory(cp, dir);
|
2003-08-13 01:18:16 +04:00
|
|
|
if(cmSystemTools::GetRunCommandHideConsole())
|
|
|
|
{
|
|
|
|
cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
|
|
|
|
}
|
2003-08-04 06:32:15 +04:00
|
|
|
cmsysProcess_SetTimeout(cp, timeout);
|
|
|
|
cmsysProcess_Execute(cp);
|
|
|
|
|
2003-11-26 19:41:57 +03:00
|
|
|
std::vector<char> tempOutput;
|
2003-08-04 06:32:15 +04:00
|
|
|
char* data;
|
|
|
|
int length;
|
2004-01-26 22:41:29 +03:00
|
|
|
if ( output || verbose )
|
|
|
|
{
|
2003-12-05 19:53:17 +03:00
|
|
|
while(cmsysProcess_WaitForData(cp, &data, &length, 0))
|
2003-08-04 06:32:15 +04:00
|
|
|
{
|
2005-08-18 01:39:59 +04:00
|
|
|
if(output || verbose)
|
|
|
|
{
|
|
|
|
// Translate NULL characters in the output into valid text.
|
|
|
|
// Visual Studio 7 puts these characters in the output of its
|
|
|
|
// build process.
|
|
|
|
for(int i=0; i < length; ++i)
|
|
|
|
{
|
|
|
|
if(data[i] == '\0')
|
|
|
|
{
|
|
|
|
data[i] = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-08-04 06:32:15 +04:00
|
|
|
if ( output )
|
|
|
|
{
|
2003-11-26 19:41:57 +03:00
|
|
|
tempOutput.insert(tempOutput.end(), data, data+length);
|
2003-08-04 06:32:15 +04:00
|
|
|
}
|
|
|
|
if(verbose)
|
|
|
|
{
|
2004-01-07 19:24:22 +03:00
|
|
|
cmSystemTools::Stdout(data, length);
|
2003-08-04 06:32:15 +04:00
|
|
|
}
|
|
|
|
}
|
2004-01-26 22:41:29 +03:00
|
|
|
}
|
2003-08-04 06:32:15 +04:00
|
|
|
|
|
|
|
cmsysProcess_WaitForExit(cp, 0);
|
2005-11-17 23:44:43 +03:00
|
|
|
if ( output && tempOutput.begin() != tempOutput.end())
|
2003-11-26 19:41:57 +03:00
|
|
|
{
|
|
|
|
output->append(&*tempOutput.begin(), tempOutput.size());
|
|
|
|
}
|
2003-08-04 06:32:15 +04:00
|
|
|
|
|
|
|
bool result = true;
|
|
|
|
if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
|
|
|
|
{
|
|
|
|
if ( retVal )
|
|
|
|
{
|
|
|
|
*retVal = cmsysProcess_GetExitValue(cp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( cmsysProcess_GetExitValue(cp) != 0 )
|
|
|
|
{
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-01-15 16:50:37 +03:00
|
|
|
else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exception)
|
|
|
|
{
|
2004-02-24 18:04:02 +03:00
|
|
|
const char* exception_str = cmsysProcess_GetExceptionString(cp);
|
|
|
|
if ( verbose )
|
|
|
|
{
|
|
|
|
std::cerr << exception_str << std::endl;
|
|
|
|
}
|
2004-02-25 02:48:44 +03:00
|
|
|
if ( output )
|
|
|
|
{
|
|
|
|
output->append(exception_str, strlen(exception_str));
|
|
|
|
}
|
2004-01-15 16:50:37 +03:00
|
|
|
result = false;
|
|
|
|
}
|
2003-12-30 18:26:18 +03:00
|
|
|
else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Error)
|
2003-08-04 06:32:15 +04:00
|
|
|
{
|
2004-02-24 18:04:02 +03:00
|
|
|
const char* error_str = cmsysProcess_GetErrorString(cp);
|
|
|
|
if ( verbose )
|
|
|
|
{
|
|
|
|
std::cerr << error_str << std::endl;
|
|
|
|
}
|
2004-02-25 02:48:44 +03:00
|
|
|
if ( output )
|
|
|
|
{
|
|
|
|
output->append(error_str, strlen(error_str));
|
|
|
|
}
|
2003-08-04 06:32:15 +04:00
|
|
|
result = false;
|
|
|
|
}
|
2004-01-15 16:50:37 +03:00
|
|
|
else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Expired)
|
|
|
|
{
|
2004-02-24 18:04:02 +03:00
|
|
|
const char* error_str = "Process terminated due to timeout\n";
|
|
|
|
if ( verbose )
|
|
|
|
{
|
|
|
|
std::cerr << error_str << std::endl;
|
|
|
|
}
|
2004-02-25 02:48:44 +03:00
|
|
|
if ( output )
|
|
|
|
{
|
|
|
|
output->append(error_str, strlen(error_str));
|
|
|
|
}
|
2004-01-15 16:50:37 +03:00
|
|
|
result = false;
|
|
|
|
}
|
2003-08-04 06:32:15 +04:00
|
|
|
|
|
|
|
cmsysProcess_Delete(cp);
|
|
|
|
return result;
|
|
|
|
}
|
2008-01-01 23:13:41 +03:00
|
|
|
|
|
|
|
bool cmSystemTools::RunSingleCommand(
|
|
|
|
const char* command,
|
|
|
|
std::string* output,
|
|
|
|
int *retVal,
|
|
|
|
const char* dir,
|
|
|
|
bool verbose,
|
|
|
|
double timeout)
|
|
|
|
{
|
|
|
|
if(s_DisableRunCommandOutput)
|
|
|
|
{
|
|
|
|
verbose = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
|
|
|
|
|
|
|
|
if(args.size() < 1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return cmSystemTools::RunSingleCommand(args, output,retVal,
|
|
|
|
dir, verbose, timeout);
|
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
bool cmSystemTools::RunCommand(const char* command,
|
|
|
|
std::string& output,
|
|
|
|
const char* dir,
|
|
|
|
bool verbose,
|
|
|
|
int timeout)
|
2001-05-04 00:55:18 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
int dummy;
|
|
|
|
return cmSystemTools::RunCommand(command, output, dummy,
|
|
|
|
dir, verbose, timeout);
|
2001-05-04 00:55:18 +04:00
|
|
|
}
|
|
|
|
|
2003-07-08 02:27:57 +04:00
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
|
|
|
#include "cmWin32ProcessExecution.h"
|
|
|
|
// use this for shell commands like echo and dir
|
|
|
|
bool RunCommandViaWin32(const char* command,
|
|
|
|
const char* dir,
|
|
|
|
std::string& output,
|
|
|
|
int& retVal,
|
|
|
|
bool verbose,
|
|
|
|
int timeout)
|
2001-05-09 22:53:32 +04:00
|
|
|
{
|
2003-07-08 02:27:57 +04:00
|
|
|
#if defined(__BORLANDC__)
|
2006-03-10 21:54:57 +03:00
|
|
|
return
|
|
|
|
cmWin32ProcessExecution::
|
|
|
|
BorlandRunCommand(command, dir, output,
|
|
|
|
retVal,
|
|
|
|
verbose, timeout,
|
|
|
|
cmSystemTools::GetRunCommandHideConsole());
|
2003-07-08 02:27:57 +04:00
|
|
|
#else // Visual studio
|
|
|
|
::SetLastError(ERROR_SUCCESS);
|
|
|
|
if ( ! command )
|
2003-06-19 22:27:01 +04:00
|
|
|
{
|
2003-07-08 02:27:57 +04:00
|
|
|
cmSystemTools::Error("No command specified");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cmWin32ProcessExecution resProc;
|
|
|
|
if(cmSystemTools::GetRunCommandHideConsole())
|
|
|
|
{
|
|
|
|
resProc.SetHideWindows(true);
|
2003-06-19 22:27:01 +04:00
|
|
|
}
|
2001-05-04 00:55:18 +04:00
|
|
|
|
2003-07-08 02:27:57 +04:00
|
|
|
if ( cmSystemTools::GetWindows9xComspecSubstitute() )
|
2002-10-11 18:16:01 +04:00
|
|
|
{
|
2003-07-08 02:27:57 +04:00
|
|
|
resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
|
|
|
|
}
|
|
|
|
if ( !resProc.StartProcess(command, dir, verbose) )
|
|
|
|
{
|
2004-06-24 00:34:38 +04:00
|
|
|
output = resProc.GetOutput();
|
|
|
|
if(verbose)
|
|
|
|
{
|
|
|
|
cmSystemTools::Stdout(output.c_str());
|
|
|
|
}
|
2003-07-08 02:27:57 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
resProc.Wait(timeout);
|
|
|
|
output = resProc.GetOutput();
|
|
|
|
retVal = resProc.GetExitValue();
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// use this for shell commands like echo and dir
|
|
|
|
bool RunCommandViaSystem(const char* command,
|
|
|
|
const char* dir,
|
|
|
|
std::string& output,
|
|
|
|
int& retVal,
|
|
|
|
bool verbose)
|
|
|
|
{
|
|
|
|
std::cout << "@@ " << command << std::endl;
|
|
|
|
|
|
|
|
std::string commandInDir;
|
|
|
|
if(dir)
|
|
|
|
{
|
|
|
|
commandInDir = "cd ";
|
|
|
|
commandInDir += cmSystemTools::ConvertToOutputPath(dir);
|
|
|
|
commandInDir += " && ";
|
|
|
|
commandInDir += command;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
commandInDir = command;
|
|
|
|
}
|
|
|
|
command = commandInDir.c_str();
|
|
|
|
std::string commandToFile = command;
|
|
|
|
commandToFile += " > ";
|
|
|
|
std::string tempFile;
|
|
|
|
tempFile += _tempnam(0, "cmake");
|
|
|
|
|
|
|
|
commandToFile += tempFile;
|
|
|
|
retVal = system(commandToFile.c_str());
|
|
|
|
std::ifstream fin(tempFile.c_str());
|
|
|
|
if(!fin)
|
|
|
|
{
|
|
|
|
if(verbose)
|
2002-10-11 18:16:01 +04:00
|
|
|
{
|
2003-07-08 02:27:57 +04:00
|
|
|
std::string errormsg = "RunCommand produced no output: command: \"";
|
|
|
|
errormsg += command;
|
|
|
|
errormsg += "\"";
|
|
|
|
errormsg += "\nOutput file: ";
|
|
|
|
errormsg += tempFile;
|
|
|
|
cmSystemTools::Error(errormsg.c_str());
|
2003-07-07 16:41:28 +04:00
|
|
|
}
|
2003-07-08 02:27:57 +04:00
|
|
|
fin.close();
|
|
|
|
cmSystemTools::RemoveFile(tempFile.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool multiLine = false;
|
|
|
|
std::string line;
|
|
|
|
while(cmSystemTools::GetLineFromStream(fin, line))
|
|
|
|
{
|
|
|
|
output += line;
|
|
|
|
if(multiLine)
|
2003-07-07 16:41:28 +04:00
|
|
|
{
|
2003-07-08 02:27:57 +04:00
|
|
|
output += "\n";
|
2002-10-11 18:16:01 +04:00
|
|
|
}
|
2003-07-08 02:27:57 +04:00
|
|
|
multiLine = true;
|
2001-05-25 05:17:02 +04:00
|
|
|
}
|
2003-07-08 02:27:57 +04:00
|
|
|
fin.close();
|
|
|
|
cmSystemTools::RemoveFile(tempFile.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // We have popen
|
|
|
|
|
2006-12-14 18:03:25 +03:00
|
|
|
// BeOS seems to return from a successful pclose() before the process has
|
|
|
|
// legitimately exited, or at least before SIGCHLD is thrown...the signal may
|
|
|
|
// come quite some time after pclose returns! This causes havoc with later
|
|
|
|
// parts of CMake that expect to catch the signal from other child processes,
|
|
|
|
// so we explicitly wait to catch it here. This should be safe to do with
|
|
|
|
// popen() so long as we don't actually collect the zombie process ourselves.
|
|
|
|
#ifdef __BEOS__
|
|
|
|
#include <signal.h>
|
|
|
|
#undef SIGBUS // this is the same as SIGSEGV on BeOS and causes issues below.
|
|
|
|
static volatile bool beos_seen_signal = false;
|
|
|
|
static void beos_popen_workaround(int sig)
|
|
|
|
{
|
|
|
|
beos_seen_signal = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-07-08 02:27:57 +04:00
|
|
|
bool RunCommandViaPopen(const char* command,
|
|
|
|
const char* dir,
|
|
|
|
std::string& output,
|
|
|
|
int& retVal,
|
|
|
|
bool verbose,
|
|
|
|
int /*timeout*/)
|
|
|
|
{
|
|
|
|
// if only popen worked on windows.....
|
|
|
|
std::string commandInDir;
|
|
|
|
if(dir)
|
2001-05-25 05:17:02 +04:00
|
|
|
{
|
2003-07-08 02:27:57 +04:00
|
|
|
commandInDir = "cd \"";
|
|
|
|
commandInDir += dir;
|
|
|
|
commandInDir += "\" && ";
|
|
|
|
commandInDir += command;
|
2001-05-25 05:17:02 +04:00
|
|
|
}
|
2003-07-08 02:27:57 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
commandInDir = command;
|
|
|
|
}
|
2009-04-24 19:44:42 +04:00
|
|
|
#ifndef __VMS
|
2003-07-08 02:27:57 +04:00
|
|
|
commandInDir += " 2>&1";
|
2009-04-24 19:44:42 +04:00
|
|
|
#endif
|
2003-07-08 02:27:57 +04:00
|
|
|
command = commandInDir.c_str();
|
|
|
|
const int BUFFER_SIZE = 4096;
|
|
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
if(verbose)
|
|
|
|
{
|
2004-01-07 19:24:22 +03:00
|
|
|
cmSystemTools::Stdout("running ");
|
|
|
|
cmSystemTools::Stdout(command);
|
|
|
|
cmSystemTools::Stdout("\n");
|
2003-07-08 02:27:57 +04:00
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
2006-12-14 18:03:25 +03:00
|
|
|
|
|
|
|
#ifdef __BEOS__
|
|
|
|
beos_seen_signal = false;
|
|
|
|
signal(SIGCHLD, beos_popen_workaround);
|
|
|
|
#endif
|
|
|
|
|
2003-07-08 02:27:57 +04:00
|
|
|
FILE* cpipe = popen(command, "r");
|
|
|
|
if(!cpipe)
|
2001-09-13 19:27:54 +04:00
|
|
|
{
|
2006-12-14 18:03:25 +03:00
|
|
|
#ifdef __BEOS__
|
|
|
|
signal(SIGCHLD, SIG_DFL);
|
|
|
|
#endif
|
2003-06-23 16:58:58 +04:00
|
|
|
return false;
|
2001-09-13 19:27:54 +04:00
|
|
|
}
|
2003-07-08 02:27:57 +04:00
|
|
|
fgets(buffer, BUFFER_SIZE, cpipe);
|
|
|
|
while(!feof(cpipe))
|
2003-06-03 18:30:23 +04:00
|
|
|
{
|
2003-07-07 16:41:28 +04:00
|
|
|
if(verbose)
|
2003-06-23 16:58:58 +04:00
|
|
|
{
|
2004-01-07 19:24:22 +03:00
|
|
|
cmSystemTools::Stdout(buffer);
|
2003-06-23 16:58:58 +04:00
|
|
|
}
|
2003-07-08 02:27:57 +04:00
|
|
|
output += buffer;
|
2008-09-16 01:53:28 +04:00
|
|
|
buffer[0] = 0;
|
2003-07-08 02:27:57 +04:00
|
|
|
fgets(buffer, BUFFER_SIZE, cpipe);
|
2003-06-03 18:30:23 +04:00
|
|
|
}
|
2003-07-08 02:27:57 +04:00
|
|
|
|
|
|
|
retVal = pclose(cpipe);
|
2006-12-14 18:03:25 +03:00
|
|
|
|
|
|
|
#ifdef __BEOS__
|
|
|
|
for (int i = 0; (!beos_seen_signal) && (i < 3); i++)
|
|
|
|
{
|
|
|
|
::sleep(1); // signals should interrupt this...
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!beos_seen_signal)
|
|
|
|
{
|
|
|
|
signal(SIGCHLD, SIG_DFL); // oh well, didn't happen. Go on anyhow.
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-07-08 02:27:57 +04:00
|
|
|
if (WIFEXITED(retVal))
|
2001-05-25 05:17:02 +04:00
|
|
|
{
|
2003-07-08 02:27:57 +04:00
|
|
|
retVal = WEXITSTATUS(retVal);
|
|
|
|
return true;
|
2001-05-25 05:17:02 +04:00
|
|
|
}
|
2003-07-08 02:27:57 +04:00
|
|
|
if (WIFSIGNALED(retVal))
|
2003-07-07 16:41:28 +04:00
|
|
|
{
|
2003-07-08 02:27:57 +04:00
|
|
|
retVal = WTERMSIG(retVal);
|
|
|
|
cmOStringStream error;
|
|
|
|
error << "\nProcess terminated due to ";
|
|
|
|
switch (retVal)
|
|
|
|
{
|
|
|
|
#ifdef SIGKILL
|
|
|
|
case SIGKILL:
|
|
|
|
error << "SIGKILL";
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGFPE
|
|
|
|
case SIGFPE:
|
|
|
|
error << "SIGFPE";
|
|
|
|
break;
|
|
|
|
#endif
|
2008-09-16 01:53:28 +04:00
|
|
|
#ifndef __HAIKU__
|
2003-07-08 02:27:57 +04:00
|
|
|
#ifdef SIGBUS
|
|
|
|
case SIGBUS:
|
|
|
|
error << "SIGBUS";
|
|
|
|
break;
|
|
|
|
#endif
|
2008-09-16 01:53:28 +04:00
|
|
|
#endif
|
2003-07-08 02:27:57 +04:00
|
|
|
#ifdef SIGSEGV
|
|
|
|
case SIGSEGV:
|
|
|
|
error << "SIGSEGV";
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
error << "signal " << retVal;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
output += error.str();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // endif WIN32 not CYGWIN
|
|
|
|
|
|
|
|
|
|
|
|
// run a command unix uses popen (easy)
|
|
|
|
// windows uses system and ShortPath
|
|
|
|
bool cmSystemTools::RunCommand(const char* command,
|
|
|
|
std::string& output,
|
|
|
|
int &retVal,
|
|
|
|
const char* dir,
|
|
|
|
bool verbose,
|
|
|
|
int timeout)
|
|
|
|
{
|
|
|
|
if(s_DisableRunCommandOutput)
|
|
|
|
{
|
|
|
|
verbose = false;
|
2003-07-07 16:41:28 +04:00
|
|
|
}
|
|
|
|
|
2003-07-08 02:27:57 +04:00
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
|
|
|
// if the command does not start with a quote, then
|
|
|
|
// try to find the program, and if the program can not be
|
|
|
|
// found use system to run the command as it must be a built in
|
|
|
|
// shell command like echo or dir
|
|
|
|
int count = 0;
|
|
|
|
if(command[0] == '\"')
|
|
|
|
{
|
|
|
|
// count the number of quotes
|
|
|
|
for(const char* s = command; *s != 0; ++s)
|
|
|
|
{
|
|
|
|
if(*s == '\"')
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
if(count > 2)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if there are more than two double quotes use
|
|
|
|
// GetShortPathName, the cmd.exe program in windows which
|
|
|
|
// is used by system fails to execute if there are more than
|
|
|
|
// one set of quotes in the arguments
|
|
|
|
if(count > 2)
|
|
|
|
{
|
|
|
|
cmsys::RegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
|
|
|
|
if(quoted.find(command))
|
|
|
|
{
|
|
|
|
std::string shortCmd;
|
|
|
|
std::string cmd = quoted.match(1);
|
|
|
|
std::string args = quoted.match(2);
|
|
|
|
if(! cmSystemTools::FileExists(cmd.c_str()) )
|
|
|
|
{
|
|
|
|
shortCmd = cmd;
|
|
|
|
}
|
|
|
|
else if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd))
|
|
|
|
{
|
|
|
|
cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
shortCmd += " ";
|
|
|
|
shortCmd += args;
|
|
|
|
|
|
|
|
//return RunCommandViaSystem(shortCmd.c_str(), dir,
|
|
|
|
// output, retVal, verbose);
|
|
|
|
//return WindowsRunCommand(shortCmd.c_str(), dir,
|
|
|
|
//output, retVal, verbose);
|
|
|
|
return RunCommandViaWin32(shortCmd.c_str(), dir,
|
|
|
|
output, retVal, verbose, timeout);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmSystemTools::Error("Could not parse command line with quotes ",
|
|
|
|
command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if there is only one set of quotes or no quotes then just run the command
|
|
|
|
//return RunCommandViaSystem(command, dir, output, retVal, verbose);
|
|
|
|
//return WindowsRunCommand(command, dir, output, retVal, verbose);
|
|
|
|
return ::RunCommandViaWin32(command, dir, output, retVal, verbose, timeout);
|
|
|
|
#else
|
|
|
|
return ::RunCommandViaPopen(command, dir, output, retVal, verbose, timeout);
|
|
|
|
#endif
|
2001-05-25 05:17:02 +04:00
|
|
|
}
|
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
bool cmSystemTools::DoesFileExistWithExtensions(
|
|
|
|
const char* name,
|
|
|
|
const std::vector<std::string>& headerExts)
|
2002-07-15 17:45:47 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
std::string hname;
|
|
|
|
|
|
|
|
for( std::vector<std::string>::const_iterator ext = headerExts.begin();
|
|
|
|
ext != headerExts.end(); ++ext )
|
2002-07-15 17:45:47 +04:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
hname = name;
|
|
|
|
hname += ".";
|
|
|
|
hname += *ext;
|
|
|
|
if(cmSystemTools::FileExists(hname.c_str()))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2002-07-15 17:45:47 +04:00
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
return false;
|
2002-07-15 17:45:47 +04:00
|
|
|
}
|
|
|
|
|
2009-04-21 19:36:59 +04:00
|
|
|
std::string cmSystemTools::FileExistsInParentDirectories(const char* fname,
|
|
|
|
const char* directory, const char* toplevel)
|
|
|
|
{
|
|
|
|
std::string file = fname;
|
2009-04-22 17:11:06 +04:00
|
|
|
cmSystemTools::ConvertToUnixSlashes(file);
|
2009-04-21 19:36:59 +04:00
|
|
|
std::string dir = directory;
|
2009-04-22 17:11:06 +04:00
|
|
|
cmSystemTools::ConvertToUnixSlashes(dir);
|
2009-04-21 19:37:09 +04:00
|
|
|
std::string prevDir;
|
|
|
|
while(dir != prevDir)
|
2009-04-21 19:36:59 +04:00
|
|
|
{
|
|
|
|
std::string path = dir + "/" + file;
|
2009-04-22 17:11:06 +04:00
|
|
|
if ( cmSystemTools::FileExists(path.c_str()) )
|
2009-04-21 19:36:59 +04:00
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
if ( dir.size() < strlen(toplevel) )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2009-04-21 19:37:09 +04:00
|
|
|
prevDir = dir;
|
2009-04-22 17:11:06 +04:00
|
|
|
dir = cmSystemTools::GetParentDirectory(dir.c_str());
|
2009-04-21 19:36:59 +04:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
bool cmSystemTools::cmCopyFile(const char* source, const char* destination)
|
2002-09-14 00:38:35 +04:00
|
|
|
{
|
2004-09-29 20:20:52 +04:00
|
|
|
return Superclass::CopyFileAlways(source, destination);
|
2004-01-26 23:50:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cmSystemTools::CopyFileIfDifferent(const char* source,
|
|
|
|
const char* destination)
|
|
|
|
{
|
2004-09-29 20:20:52 +04:00
|
|
|
return Superclass::CopyFileIfDifferent(source, destination);
|
2002-09-14 00:38:35 +04:00
|
|
|
}
|
2002-07-15 17:45:47 +04:00
|
|
|
|
2009-04-15 17:57:57 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmSystemTools::RenameFile(const char* oldname, const char* newname)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
/* On Windows the move functions will not replace existing files.
|
|
|
|
Check if the destination exists. */
|
|
|
|
struct stat newFile;
|
|
|
|
if(stat(newname, &newFile) == 0)
|
|
|
|
{
|
|
|
|
/* The destination exists. We have to replace it carefully. The
|
|
|
|
MoveFileEx function does what we need but is not available on
|
|
|
|
Win9x. */
|
|
|
|
OSVERSIONINFO osv;
|
|
|
|
DWORD attrs;
|
|
|
|
|
|
|
|
/* Make sure the destination is not read only. */
|
|
|
|
attrs = GetFileAttributes(newname);
|
|
|
|
if(attrs & FILE_ATTRIBUTE_READONLY)
|
|
|
|
{
|
|
|
|
SetFileAttributes(newname, attrs & ~FILE_ATTRIBUTE_READONLY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the windows version number. */
|
|
|
|
osv.dwOSVersionInfoSize = sizeof(osv);
|
|
|
|
GetVersionEx(&osv);
|
|
|
|
if(osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
|
|
|
|
{
|
|
|
|
/* This is Win9x. There is no MoveFileEx implementation. We
|
|
|
|
cannot quite rename the file atomically. Just delete the
|
|
|
|
destination and then move the file. */
|
|
|
|
DeleteFile(newname);
|
2009-04-15 19:00:03 +04:00
|
|
|
return MoveFile(oldname, newname) != 0;
|
2009-04-15 17:57:57 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This is not Win9x. Use the MoveFileEx implementation. */
|
2009-04-15 19:00:03 +04:00
|
|
|
return MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING) != 0;
|
2009-04-15 17:57:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The destination does not exist. Just move the file. */
|
2009-04-15 19:00:03 +04:00
|
|
|
return MoveFile(oldname, newname) != 0;
|
2009-04-15 17:57:57 +04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* On UNIX we have an OS-provided call to do this atomically. */
|
|
|
|
return rename(oldname, newname) == 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-07-16 18:54:32 +04:00
|
|
|
bool cmSystemTools::ComputeFileMD5(const char* source, char* md5out)
|
|
|
|
{
|
2007-07-16 23:10:40 +04:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2007-07-16 18:54:32 +04:00
|
|
|
if(!cmSystemTools::FileExists(source))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open files
|
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
cmsys_ios::ifstream fin(source, cmsys_ios::ios::binary | cmsys_ios::ios::in);
|
|
|
|
#else
|
|
|
|
cmsys_ios::ifstream fin(source);
|
|
|
|
#endif
|
|
|
|
if(!fin)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmsysMD5* md5 = cmsysMD5_New();
|
|
|
|
cmsysMD5_Initialize(md5);
|
|
|
|
|
|
|
|
// Should be efficient enough on most system:
|
|
|
|
const int bufferSize = 4096;
|
|
|
|
char buffer[bufferSize];
|
|
|
|
// This copy loop is very sensitive on certain platforms with
|
|
|
|
// slightly broken stream libraries (like HPUX). Normally, it is
|
|
|
|
// incorrect to not check the error condition on the fin.read()
|
|
|
|
// before using the data, but the fin.gcount() will be zero if an
|
|
|
|
// error occurred. Therefore, the loop should be safe everywhere.
|
|
|
|
while(fin)
|
|
|
|
{
|
|
|
|
fin.read(buffer, bufferSize);
|
|
|
|
if(fin.gcount())
|
|
|
|
{
|
|
|
|
cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(buffer),
|
|
|
|
fin.gcount());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmsysMD5_FinalizeHex(md5, md5out);
|
|
|
|
cmsysMD5_Delete(md5);
|
|
|
|
|
|
|
|
fin.close();
|
|
|
|
return true;
|
2007-08-14 18:27:34 +04:00
|
|
|
#else
|
|
|
|
(void)source;
|
|
|
|
(void)md5out;
|
2007-07-16 23:10:40 +04:00
|
|
|
cmSystemTools::Message("md5sum not supported in bootstrapping mode","Error");
|
|
|
|
return false;
|
|
|
|
#endif
|
2007-07-16 18:54:32 +04:00
|
|
|
}
|
|
|
|
|
2008-01-24 22:41:18 +03:00
|
|
|
std::string cmSystemTools::ComputeStringMD5(const char* input)
|
|
|
|
{
|
2008-01-25 00:11:06 +03:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2008-01-24 22:41:18 +03:00
|
|
|
char md5out[32];
|
|
|
|
cmsysMD5* md5 = cmsysMD5_New();
|
|
|
|
cmsysMD5_Initialize(md5);
|
|
|
|
cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(input), -1);
|
|
|
|
cmsysMD5_FinalizeHex(md5, md5out);
|
|
|
|
cmsysMD5_Delete(md5);
|
|
|
|
return std::string(md5out, 32);
|
2008-01-25 00:11:06 +03:00
|
|
|
#else
|
|
|
|
(void)input;
|
|
|
|
cmSystemTools::Message("md5sum not supported in bootstrapping mode","Error");
|
2008-01-25 16:11:04 +03:00
|
|
|
return "";
|
2008-01-25 00:11:06 +03:00
|
|
|
#endif
|
2008-01-24 22:41:18 +03:00
|
|
|
}
|
|
|
|
|
2001-06-21 20:01:18 +04:00
|
|
|
void cmSystemTools::Glob(const char *directory, const char *regexp,
|
|
|
|
std::vector<std::string>& files)
|
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
cmsys::Directory d;
|
2003-06-23 22:10:12 +04:00
|
|
|
cmsys::RegularExpression reg(regexp);
|
2001-06-21 20:01:18 +04:00
|
|
|
|
|
|
|
if (d.Load(directory))
|
|
|
|
{
|
2002-03-13 18:25:11 +03:00
|
|
|
size_t numf;
|
2002-09-30 05:57:48 +04:00
|
|
|
unsigned int i;
|
2001-06-21 20:01:18 +04:00
|
|
|
numf = d.GetNumberOfFiles();
|
|
|
|
for (i = 0; i < numf; i++)
|
|
|
|
{
|
|
|
|
std::string fname = d.GetFile(i);
|
|
|
|
if (reg.find(fname))
|
|
|
|
{
|
|
|
|
files.push_back(fname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-20 18:54:29 +04:00
|
|
|
|
|
|
|
void cmSystemTools::GlobDirs(const char *fullPath,
|
|
|
|
std::vector<std::string>& files)
|
|
|
|
{
|
|
|
|
std::string path = fullPath;
|
2001-11-13 20:38:53 +03:00
|
|
|
std::string::size_type pos = path.find("/*");
|
2001-09-20 18:54:29 +04:00
|
|
|
if(pos == std::string::npos)
|
|
|
|
{
|
|
|
|
files.push_back(fullPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string startPath = path.substr(0, pos);
|
|
|
|
std::string finishPath = path.substr(pos+2);
|
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
cmsys::Directory d;
|
2001-09-20 18:54:29 +04:00
|
|
|
if (d.Load(startPath.c_str()))
|
|
|
|
{
|
2002-02-28 02:11:12 +03:00
|
|
|
for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
|
2001-09-20 18:54:29 +04:00
|
|
|
{
|
|
|
|
if((std::string(d.GetFile(i)) != ".")
|
|
|
|
&& (std::string(d.GetFile(i)) != ".."))
|
|
|
|
{
|
|
|
|
std::string fname = startPath;
|
|
|
|
fname +="/";
|
|
|
|
fname += d.GetFile(i);
|
|
|
|
if(cmSystemTools::FileIsDirectory(fname.c_str()))
|
|
|
|
{
|
|
|
|
fname += finishPath;
|
|
|
|
cmSystemTools::GlobDirs(fname.c_str(), files);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-03-06 18:10:46 +03:00
|
|
|
|
|
|
|
|
2002-12-12 02:13:33 +03:00
|
|
|
void cmSystemTools::ExpandList(std::vector<std::string> const& arguments,
|
|
|
|
std::vector<std::string>& newargs)
|
2002-03-06 18:10:46 +03:00
|
|
|
{
|
|
|
|
std::vector<std::string>::const_iterator i;
|
|
|
|
for(i = arguments.begin();i != arguments.end(); ++i)
|
|
|
|
{
|
2002-12-12 02:13:33 +03:00
|
|
|
cmSystemTools::ExpandListArgument(*i, newargs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmSystemTools::ExpandListArgument(const std::string& arg,
|
2006-02-10 03:03:27 +03:00
|
|
|
std::vector<std::string>& newargs,
|
|
|
|
bool emptyArgs)
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
|
|
|
// If argument is empty, it is an empty list.
|
2006-02-10 03:03:27 +03:00
|
|
|
if(arg.length() == 0 && !emptyArgs)
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// if there are no ; in the name then just copy the current string
|
|
|
|
if(arg.find(';') == std::string::npos)
|
|
|
|
{
|
|
|
|
newargs.push_back(arg);
|
2003-12-09 02:05:29 +03:00
|
|
|
return;
|
2002-12-12 02:13:33 +03:00
|
|
|
}
|
2005-04-14 00:25:55 +04:00
|
|
|
std::vector<char> newArgVec;
|
2003-12-09 02:05:29 +03:00
|
|
|
// Break the string at non-escaped semicolons not nested in [].
|
|
|
|
int squareNesting = 0;
|
|
|
|
for(const char* c = arg.c_str(); *c; ++c)
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
2003-12-09 02:05:29 +03:00
|
|
|
switch(*c)
|
2002-03-06 18:10:46 +03:00
|
|
|
{
|
2003-12-09 02:05:29 +03:00
|
|
|
case '\\':
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
2003-12-09 02:05:29 +03:00
|
|
|
// We only want to allow escaping of semicolons. Other
|
|
|
|
// escapes should not be processed here.
|
|
|
|
++c;
|
|
|
|
if(*c == ';')
|
2002-04-18 20:02:00 +04:00
|
|
|
{
|
2005-04-14 00:25:55 +04:00
|
|
|
newArgVec.push_back(*c);
|
2002-04-18 20:02:00 +04:00
|
|
|
}
|
2002-12-12 02:13:33 +03:00
|
|
|
else
|
2002-03-27 00:44:49 +03:00
|
|
|
{
|
2005-04-14 00:25:55 +04:00
|
|
|
newArgVec.push_back('\\');
|
2003-12-09 02:05:29 +03:00
|
|
|
if(*c)
|
2002-04-01 23:50:00 +04:00
|
|
|
{
|
2005-04-14 00:25:55 +04:00
|
|
|
newArgVec.push_back(*c);
|
2002-04-18 20:02:00 +04:00
|
|
|
}
|
2007-05-17 19:27:46 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Terminate the loop properly.
|
|
|
|
--c;
|
|
|
|
}
|
2003-12-09 02:05:29 +03:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case '[':
|
|
|
|
{
|
|
|
|
++squareNesting;
|
2005-04-14 00:25:55 +04:00
|
|
|
newArgVec.push_back(*c);
|
2003-12-09 02:05:29 +03:00
|
|
|
} break;
|
|
|
|
case ']':
|
|
|
|
{
|
2008-04-09 01:37:13 +04:00
|
|
|
--squareNesting;
|
2005-04-14 00:25:55 +04:00
|
|
|
newArgVec.push_back(*c);
|
2003-12-09 02:05:29 +03:00
|
|
|
} break;
|
|
|
|
case ';':
|
|
|
|
{
|
|
|
|
// Break the string here if we are not nested inside square
|
|
|
|
// brackets.
|
|
|
|
if(squareNesting == 0)
|
|
|
|
{
|
2006-02-10 03:03:27 +03:00
|
|
|
if ( newArgVec.size() || emptyArgs )
|
2002-04-18 20:02:00 +04:00
|
|
|
{
|
2005-04-14 00:25:55 +04:00
|
|
|
// Add the last argument if the string is not empty.
|
|
|
|
newArgVec.push_back(0);
|
|
|
|
newargs.push_back(&*newArgVec.begin());
|
|
|
|
newArgVec.clear();
|
2002-04-01 23:50:00 +04:00
|
|
|
}
|
2002-03-27 00:44:49 +03:00
|
|
|
}
|
2003-12-09 02:05:29 +03:00
|
|
|
else
|
2002-12-12 02:13:33 +03:00
|
|
|
{
|
2005-04-14 00:25:55 +04:00
|
|
|
newArgVec.push_back(*c);
|
2002-12-12 02:13:33 +03:00
|
|
|
}
|
2003-12-09 02:05:29 +03:00
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// Just append this character.
|
2005-04-14 00:25:55 +04:00
|
|
|
newArgVec.push_back(*c);
|
2003-12-09 02:05:29 +03:00
|
|
|
} break;
|
2002-03-06 18:10:46 +03:00
|
|
|
}
|
|
|
|
}
|
2006-02-10 03:03:27 +03:00
|
|
|
if ( newArgVec.size() || emptyArgs )
|
2003-12-09 02:05:29 +03:00
|
|
|
{
|
|
|
|
// Add the last argument if the string is not empty.
|
2005-04-14 00:25:55 +04:00
|
|
|
newArgVec.push_back(0);
|
|
|
|
newargs.push_back(&*newArgVec.begin());
|
2003-12-09 02:05:29 +03:00
|
|
|
}
|
2002-03-06 18:10:46 +03:00
|
|
|
}
|
2002-04-04 01:14:06 +04:00
|
|
|
|
2003-12-16 01:26:00 +03:00
|
|
|
bool cmSystemTools::SimpleGlob(const cmStdString& glob,
|
|
|
|
std::vector<cmStdString>& files,
|
2002-11-05 16:51:40 +03:00
|
|
|
int type /* = 0 */)
|
|
|
|
{
|
2002-12-09 06:35:19 +03:00
|
|
|
files.clear();
|
2002-11-05 16:51:40 +03:00
|
|
|
if ( glob[glob.size()-1] != '*' )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string path = cmSystemTools::GetFilenamePath(glob);
|
|
|
|
std::string ppath = cmSystemTools::GetFilenameName(glob);
|
|
|
|
ppath = ppath.substr(0, ppath.size()-1);
|
|
|
|
if ( path.size() == 0 )
|
|
|
|
{
|
|
|
|
path = "/";
|
|
|
|
}
|
|
|
|
|
2002-11-06 19:37:00 +03:00
|
|
|
bool res = false;
|
2003-06-23 16:58:58 +04:00
|
|
|
cmsys::Directory d;
|
2002-11-05 16:51:40 +03:00
|
|
|
if (d.Load(path.c_str()))
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
|
|
|
|
{
|
|
|
|
if((std::string(d.GetFile(i)) != ".")
|
|
|
|
&& (std::string(d.GetFile(i)) != ".."))
|
|
|
|
{
|
|
|
|
std::string fname = path;
|
|
|
|
if ( path[path.size()-1] != '/' )
|
|
|
|
{
|
|
|
|
fname +="/";
|
|
|
|
}
|
|
|
|
fname += d.GetFile(i);
|
|
|
|
std::string sfname = d.GetFile(i);
|
|
|
|
if ( type > 0 && cmSystemTools::FileIsDirectory(fname.c_str()) )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( type < 0 && !cmSystemTools::FileIsDirectory(fname.c_str()) )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( sfname.size() >= ppath.size() &&
|
|
|
|
sfname.substr(0, ppath.size()) ==
|
|
|
|
ppath )
|
|
|
|
{
|
|
|
|
files.push_back(fname);
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-11-08 23:46:08 +03:00
|
|
|
cmSystemTools::FileFormat cmSystemTools::GetFileFormat(const char* cext)
|
2002-09-25 17:30:49 +04:00
|
|
|
{
|
|
|
|
if ( ! cext || *cext == 0 )
|
|
|
|
{
|
|
|
|
return cmSystemTools::NO_FILE_FORMAT;
|
|
|
|
}
|
2003-04-03 17:44:49 +04:00
|
|
|
//std::string ext = cmSystemTools::LowerCase(cext);
|
|
|
|
std::string ext = cext;
|
2008-08-19 23:55:10 +04:00
|
|
|
if ( ext == "c" || ext == ".c" ||
|
|
|
|
ext == "m" || ext == ".m"
|
|
|
|
) { return cmSystemTools::C_FILE_FORMAT; }
|
2003-03-13 20:24:16 +03:00
|
|
|
if (
|
|
|
|
ext == "C" || ext == ".C" ||
|
|
|
|
ext == "M" || ext == ".M" ||
|
|
|
|
ext == "c++" || ext == ".c++" ||
|
|
|
|
ext == "cc" || ext == ".cc" ||
|
|
|
|
ext == "cpp" || ext == ".cpp" ||
|
|
|
|
ext == "cxx" || ext == ".cxx" ||
|
|
|
|
ext == "mm" || ext == ".mm"
|
|
|
|
) { return cmSystemTools::CXX_FILE_FORMAT; }
|
2004-08-06 22:51:41 +04:00
|
|
|
if (
|
|
|
|
ext == "f" || ext == ".f" ||
|
|
|
|
ext == "F" || ext == ".F" ||
|
2004-08-12 00:37:56 +04:00
|
|
|
ext == "f77" || ext == ".f77" ||
|
|
|
|
ext == "f90" || ext == ".f90" ||
|
|
|
|
ext == "for" || ext == ".for" ||
|
|
|
|
ext == "f95" || ext == ".f95"
|
2004-08-06 22:51:41 +04:00
|
|
|
) { return cmSystemTools::FORTRAN_FILE_FORMAT; }
|
2006-03-10 21:54:57 +03:00
|
|
|
if ( ext == "java" || ext == ".java" )
|
|
|
|
{ return cmSystemTools::JAVA_FILE_FORMAT; }
|
2003-03-13 20:24:16 +03:00
|
|
|
if (
|
2003-04-03 17:44:49 +04:00
|
|
|
ext == "H" || ext == ".H" ||
|
2003-03-13 20:24:16 +03:00
|
|
|
ext == "h" || ext == ".h" ||
|
|
|
|
ext == "h++" || ext == ".h++" ||
|
|
|
|
ext == "hm" || ext == ".hm" ||
|
|
|
|
ext == "hpp" || ext == ".hpp" ||
|
|
|
|
ext == "hxx" || ext == ".hxx" ||
|
|
|
|
ext == "in" || ext == ".in" ||
|
|
|
|
ext == "txx" || ext == ".txx"
|
|
|
|
) { return cmSystemTools::HEADER_FILE_FORMAT; }
|
2006-03-10 21:54:57 +03:00
|
|
|
if ( ext == "rc" || ext == ".rc" )
|
|
|
|
{ return cmSystemTools::RESOURCE_FILE_FORMAT; }
|
|
|
|
if ( ext == "def" || ext == ".def" )
|
|
|
|
{ return cmSystemTools::DEFINITION_FILE_FORMAT; }
|
2002-09-25 17:30:49 +04:00
|
|
|
if ( ext == "lib" || ext == ".lib" ||
|
2006-03-10 21:54:57 +03:00
|
|
|
ext == "a" || ext == ".a")
|
|
|
|
{ return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
|
2002-09-25 17:30:49 +04:00
|
|
|
if ( ext == "o" || ext == ".o" ||
|
2006-03-10 21:54:57 +03:00
|
|
|
ext == "obj" || ext == ".obj")
|
|
|
|
{ return cmSystemTools::OBJECT_FILE_FORMAT; }
|
2002-09-25 17:30:49 +04:00
|
|
|
#ifdef __APPLE__
|
|
|
|
if ( ext == "dylib" || ext == ".dylib" )
|
|
|
|
{ return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
|
|
|
|
if ( ext == "so" || ext == ".so" ||
|
|
|
|
ext == "bundle" || ext == ".bundle" )
|
|
|
|
{ return cmSystemTools::MODULE_FILE_FORMAT; }
|
|
|
|
#else // __APPLE__
|
|
|
|
if ( ext == "so" || ext == ".so" ||
|
|
|
|
ext == "sl" || ext == ".sl" ||
|
|
|
|
ext == "dll" || ext == ".dll" )
|
|
|
|
{ return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
|
|
|
|
#endif // __APPLE__
|
|
|
|
return cmSystemTools::UNKNOWN_FILE_FORMAT;
|
|
|
|
}
|
2002-11-20 02:17:17 +03:00
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
bool cmSystemTools::Split(const char* s, std::vector<cmStdString>& l)
|
2003-03-27 20:24:30 +03:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
std::vector<std::string> temp;
|
2004-04-21 02:28:23 +04:00
|
|
|
bool res = Superclass::Split(s, temp);
|
2003-06-23 16:58:58 +04:00
|
|
|
for(std::vector<std::string>::const_iterator i = temp.begin();
|
|
|
|
i != temp.end(); ++i)
|
2002-12-11 22:15:35 +03:00
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
l.push_back(*i);
|
2002-12-11 22:15:35 +03:00
|
|
|
}
|
2004-04-21 02:28:23 +04:00
|
|
|
return res;
|
2002-12-11 19:49:08 +03:00
|
|
|
}
|
2003-08-22 00:22:23 +04:00
|
|
|
|
|
|
|
std::string cmSystemTools::ConvertToOutputPath(const char* path)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
if(s_ForceUnixPaths)
|
|
|
|
{
|
|
|
|
return cmSystemTools::ConvertToUnixOutputPath(path);
|
|
|
|
}
|
|
|
|
return cmSystemTools::ConvertToWindowsOutputPath(path);
|
|
|
|
#else
|
|
|
|
return cmSystemTools::ConvertToUnixOutputPath(path);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-03-08 19:49:26 +03:00
|
|
|
void cmSystemTools::ConvertToOutputSlashes(std::string& path)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
if(!s_ForceUnixPaths)
|
|
|
|
{
|
|
|
|
// Convert to windows slashes.
|
|
|
|
std::string::size_type pos = 0;
|
|
|
|
while((pos = path.find('/', pos)) != std::string::npos)
|
|
|
|
{
|
|
|
|
path[pos++] = '\\';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static_cast<void>(path);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-06-24 00:34:38 +04:00
|
|
|
std::string cmSystemTools::ConvertToRunCommandPath(const char* path)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
return cmSystemTools::ConvertToWindowsOutputPath(path);
|
|
|
|
#else
|
|
|
|
return cmSystemTools::ConvertToUnixOutputPath(path);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-10-30 03:49:50 +03:00
|
|
|
bool cmSystemTools::StringEndsWith(const char* str1, const char* str2)
|
|
|
|
{
|
|
|
|
if ( !str1 || !str2 || strlen(str1) < strlen(str2) )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return !strncmp(str1 + (strlen(str1)-strlen(str2)), str2, strlen(str2));
|
|
|
|
}
|
2003-11-26 22:52:12 +03:00
|
|
|
|
2003-12-22 20:24:26 +03:00
|
|
|
// compute the relative path from here to there
|
|
|
|
std::string cmSystemTools::RelativePath(const char* local, const char* remote)
|
|
|
|
{
|
2004-10-13 19:37:55 +04:00
|
|
|
if(!cmSystemTools::FileIsFullPath(local))
|
|
|
|
{
|
2006-03-10 21:54:57 +03:00
|
|
|
cmSystemTools::Error("RelativePath must be passed a full path to local: ",
|
|
|
|
local);
|
2004-10-13 19:37:55 +04:00
|
|
|
}
|
|
|
|
if(!cmSystemTools::FileIsFullPath(remote))
|
|
|
|
{
|
2006-05-12 21:53:21 +04:00
|
|
|
cmSystemTools::Error
|
|
|
|
("RelativePath must be passed a full path to remote: ", remote);
|
2004-10-13 19:37:55 +04:00
|
|
|
}
|
2006-02-07 18:23:17 +03:00
|
|
|
return cmsys::SystemTools::RelativePath(local, remote);
|
2003-12-22 20:24:26 +03:00
|
|
|
}
|
2006-02-07 18:23:17 +03:00
|
|
|
|
2006-03-17 23:46:20 +03:00
|
|
|
#ifdef CMAKE_BUILD_WITH_CMAKE
|
2008-11-26 22:38:43 +03:00
|
|
|
//----------------------------------------------------------------------
|
2006-03-17 19:44:07 +03:00
|
|
|
bool cmSystemTools::UnsetEnv(const char* value)
|
|
|
|
{
|
2006-03-17 23:46:20 +03:00
|
|
|
#if !defined(HAVE_UNSETENV)
|
|
|
|
std::string var = value;
|
|
|
|
var += "=";
|
|
|
|
return cmSystemTools::PutEnv(var.c_str());
|
|
|
|
#else
|
|
|
|
unsetenv(value);
|
|
|
|
return true;
|
|
|
|
#endif
|
2006-03-17 19:44:07 +03:00
|
|
|
}
|
|
|
|
|
2008-11-26 22:38:43 +03:00
|
|
|
//----------------------------------------------------------------------
|
2006-03-17 19:14:14 +03:00
|
|
|
std::vector<std::string> cmSystemTools::GetEnvironmentVariables()
|
2006-03-17 18:58:37 +03:00
|
|
|
{
|
2006-03-17 19:14:14 +03:00
|
|
|
std::vector<std::string> env;
|
2006-03-17 18:58:37 +03:00
|
|
|
int cc;
|
|
|
|
for ( cc = 0; environ[cc]; ++ cc )
|
|
|
|
{
|
|
|
|
env.push_back(environ[cc]);
|
|
|
|
}
|
|
|
|
return env;
|
|
|
|
}
|
2008-11-26 22:38:43 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
std::vector<std::string> cmSystemTools::AppendEnv(
|
|
|
|
std::vector<std::string>* env)
|
|
|
|
{
|
|
|
|
std::vector<std::string> origEnv = GetEnvironmentVariables();
|
|
|
|
|
|
|
|
if (env && env->size()>0)
|
|
|
|
{
|
|
|
|
std::vector<std::string>::const_iterator eit;
|
|
|
|
|
|
|
|
for (eit = env->begin(); eit!= env->end(); ++eit)
|
|
|
|
{
|
|
|
|
PutEnv(eit->c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return origEnv;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void cmSystemTools::RestoreEnv(const std::vector<std::string>& env)
|
|
|
|
{
|
|
|
|
std::vector<std::string>::const_iterator eit;
|
|
|
|
|
|
|
|
// First clear everything in the current environment:
|
|
|
|
//
|
|
|
|
std::vector<std::string> currentEnv = GetEnvironmentVariables();
|
|
|
|
for (eit = currentEnv.begin(); eit!= currentEnv.end(); ++eit)
|
|
|
|
{
|
|
|
|
std::string var(*eit);
|
|
|
|
|
2008-11-26 23:41:16 +03:00
|
|
|
std::string::size_type pos = var.find("=");
|
2008-11-26 22:38:43 +03:00
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
var = var.substr(0, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnsetEnv(var.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then put back each entry from the original environment:
|
|
|
|
//
|
|
|
|
for (eit = env.begin(); eit!= env.end(); ++eit)
|
|
|
|
{
|
|
|
|
PutEnv(eit->c_str());
|
|
|
|
}
|
|
|
|
}
|
2006-03-17 23:46:20 +03:00
|
|
|
#endif
|
2006-03-17 18:58:37 +03:00
|
|
|
|
2006-02-15 18:22:55 +03:00
|
|
|
void cmSystemTools::EnableVSConsoleOutput()
|
|
|
|
{
|
|
|
|
// Visual Studio 8 2005 (devenv.exe or VCExpress.exe) will not
|
|
|
|
// display output to the console unless this environment variable is
|
|
|
|
// set. We need it to capture the output of these build tools.
|
|
|
|
// Note for future work that one could pass "/out \\.\pipe\NAME" to
|
|
|
|
// either of these executables where NAME is created with
|
|
|
|
// CreateNamedPipe. This would bypass the internal buffering of the
|
|
|
|
// output and allow it to be captured on the fly.
|
|
|
|
#ifdef _WIN32
|
|
|
|
cmSystemTools::PutEnv("vsconsoleoutput=1");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-12-26 21:14:19 +03:00
|
|
|
bool cmSystemTools::IsPathToFramework(const char* path)
|
|
|
|
{
|
|
|
|
if(cmSystemTools::FileIsFullPath(path))
|
|
|
|
{
|
|
|
|
std::string libname = path;
|
|
|
|
if(libname.find(".framework") == libname.size()+1-sizeof(".framework"))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2005-12-29 00:30:55 +03:00
|
|
|
|
2005-12-29 01:02:16 +03:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2005-12-30 22:22:46 +03:00
|
|
|
struct cmSystemToolsGZStruct
|
|
|
|
{
|
|
|
|
gzFile GZFile;
|
|
|
|
};
|
|
|
|
|
2005-12-31 00:28:30 +03:00
|
|
|
extern "C" {
|
2006-03-10 21:54:57 +03:00
|
|
|
int cmSystemToolsGZStructOpen(void* call_data, const char *pathname,
|
|
|
|
int oflags, mode_t mode);
|
2006-01-03 16:39:24 +03:00
|
|
|
int cmSystemToolsGZStructClose(void* call_data);
|
|
|
|
ssize_t cmSystemToolsGZStructRead(void* call_data, void* buf, size_t count);
|
2006-03-10 21:54:57 +03:00
|
|
|
ssize_t cmSystemToolsGZStructWrite(void* call_data, const void* buf,
|
|
|
|
size_t count);
|
2005-12-31 00:28:30 +03:00
|
|
|
}
|
|
|
|
|
2006-03-10 21:54:57 +03:00
|
|
|
int cmSystemToolsGZStructOpen(void* call_data, const char *pathname,
|
|
|
|
int oflags, mode_t mode)
|
2005-12-29 20:19:13 +03:00
|
|
|
{
|
2005-12-30 23:32:20 +03:00
|
|
|
const char *gzoflags;
|
2005-12-29 20:19:13 +03:00
|
|
|
int fd;
|
|
|
|
|
2005-12-30 22:22:46 +03:00
|
|
|
cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
|
|
|
|
|
2005-12-29 20:19:13 +03:00
|
|
|
switch (oflags & O_ACCMODE)
|
|
|
|
{
|
|
|
|
case O_WRONLY:
|
|
|
|
gzoflags = "wb";
|
|
|
|
break;
|
|
|
|
case O_RDONLY:
|
|
|
|
gzoflags = "rb";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case O_RDWR:
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(pathname, oflags, mode);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-12-05 17:14:32 +03:00
|
|
|
// no fchmod on BeOS 5...do pathname instead.
|
2008-09-16 01:53:28 +04:00
|
|
|
#if defined(__BEOS__) && !defined(__ZETA__) && !defined(__HAIKU__)
|
2006-12-05 01:26:41 +03:00
|
|
|
if ((oflags & O_CREAT) && chmod(pathname, mode))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#elif !defined(_WIN32) || defined(__CYGWIN__)
|
2005-12-29 20:19:13 +03:00
|
|
|
if ((oflags & O_CREAT) && fchmod(fd, mode))
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-10-19 23:00:10 +04:00
|
|
|
gzf->GZFile = gzdopen(fd, gzoflags);
|
2005-12-30 22:22:46 +03:00
|
|
|
if (!gzf->GZFile)
|
2005-12-29 20:19:13 +03:00
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-12-30 22:22:46 +03:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2005-12-31 00:28:30 +03:00
|
|
|
int cmSystemToolsGZStructClose(void* call_data)
|
2005-12-30 22:22:46 +03:00
|
|
|
{
|
|
|
|
cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
|
2006-10-19 23:00:10 +04:00
|
|
|
return gzclose(gzf->GZFile);
|
2005-12-30 22:22:46 +03:00
|
|
|
}
|
|
|
|
|
2005-12-31 00:28:30 +03:00
|
|
|
ssize_t cmSystemToolsGZStructRead(void* call_data, void* buf, size_t count)
|
2005-12-30 22:22:46 +03:00
|
|
|
{
|
|
|
|
cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
|
2007-09-24 21:21:25 +04:00
|
|
|
return gzread(gzf->GZFile, buf, static_cast<int>(count));
|
2005-12-30 22:22:46 +03:00
|
|
|
}
|
|
|
|
|
2006-03-10 21:54:57 +03:00
|
|
|
ssize_t cmSystemToolsGZStructWrite(void* call_data, const void* buf,
|
|
|
|
size_t count)
|
2005-12-30 22:22:46 +03:00
|
|
|
{
|
|
|
|
cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
|
2007-09-27 22:44:11 +04:00
|
|
|
return gzwrite(gzf->GZFile, (void*)buf, static_cast<int>(count));
|
2005-12-29 20:19:13 +03:00
|
|
|
}
|
|
|
|
|
2005-12-29 01:02:16 +03:00
|
|
|
#endif
|
2005-12-29 00:30:55 +03:00
|
|
|
|
2006-03-10 21:54:57 +03:00
|
|
|
bool cmSystemTools::CreateTar(const char* outFileName,
|
|
|
|
const std::vector<cmStdString>& files,
|
|
|
|
bool gzip, bool verbose)
|
2005-12-29 00:30:55 +03:00
|
|
|
{
|
2005-12-29 01:02:16 +03:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2005-12-29 00:30:55 +03:00
|
|
|
TAR *t;
|
|
|
|
char buf[TAR_MAXPATHLEN];
|
|
|
|
char pathname[TAR_MAXPATHLEN];
|
2005-12-30 22:22:46 +03:00
|
|
|
cmSystemToolsGZStruct gzs;
|
|
|
|
|
|
|
|
tartype_t gztype = {
|
2005-12-31 00:28:30 +03:00
|
|
|
(openfunc_t)cmSystemToolsGZStructOpen,
|
|
|
|
(closefunc_t)cmSystemToolsGZStructClose,
|
|
|
|
(readfunc_t)cmSystemToolsGZStructRead,
|
|
|
|
(writefunc_t)cmSystemToolsGZStructWrite,
|
2005-12-30 22:22:46 +03:00
|
|
|
&gzs
|
2005-12-29 20:19:13 +03:00
|
|
|
};
|
|
|
|
|
2005-12-29 00:30:55 +03:00
|
|
|
// Ok, this libtar is not const safe. for now use auto_ptr hack
|
|
|
|
char* realName = new char[ strlen(outFileName) + 1 ];
|
|
|
|
std::auto_ptr<char> realNamePtr(realName);
|
|
|
|
strcpy(realName, outFileName);
|
2007-07-31 06:51:21 +04:00
|
|
|
int options = 0;
|
|
|
|
if(verbose)
|
|
|
|
{
|
|
|
|
options |= TAR_VERBOSE;
|
|
|
|
}
|
|
|
|
#ifdef __CYGWIN__
|
|
|
|
options |= TAR_GNU;
|
|
|
|
#endif
|
2005-12-29 00:30:55 +03:00
|
|
|
if (tar_open(&t, realName,
|
2005-12-29 20:19:13 +03:00
|
|
|
(gzip? &gztype : NULL),
|
2005-12-29 00:30:55 +03:00
|
|
|
O_WRONLY | O_CREAT, 0644,
|
2007-07-31 06:51:21 +04:00
|
|
|
options) == -1)
|
2005-12-29 00:30:55 +03:00
|
|
|
{
|
2005-12-29 18:43:12 +03:00
|
|
|
cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
|
2005-12-29 00:44:32 +03:00
|
|
|
return false;
|
2005-12-29 00:30:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<cmStdString>::const_iterator it;
|
|
|
|
for (it = files.begin(); it != files.end(); ++ it )
|
|
|
|
{
|
|
|
|
strncpy(pathname, it->c_str(), sizeof(pathname));
|
|
|
|
pathname[sizeof(pathname)-1] = 0;
|
|
|
|
strncpy(buf, pathname, sizeof(buf));
|
|
|
|
buf[sizeof(buf)-1] = 0;
|
|
|
|
if (tar_append_tree(t, buf, pathname) != 0)
|
|
|
|
{
|
2005-12-29 18:43:12 +03:00
|
|
|
cmOStringStream ostr;
|
2006-03-10 21:54:57 +03:00
|
|
|
ostr << "Problem with tar_append_tree(\"" << buf << "\", \""
|
|
|
|
<< pathname << "\"): "
|
2005-12-29 18:43:12 +03:00
|
|
|
<< strerror(errno);
|
|
|
|
cmSystemTools::Error(ostr.str().c_str());
|
2005-12-29 00:30:55 +03:00
|
|
|
tar_close(t);
|
2005-12-29 00:44:32 +03:00
|
|
|
return false;
|
2005-12-29 00:30:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tar_append_eof(t) != 0)
|
|
|
|
{
|
2005-12-29 18:43:12 +03:00
|
|
|
cmSystemTools::Error("Problem with tar_append_eof(): ", strerror(errno));
|
2005-12-29 00:30:55 +03:00
|
|
|
tar_close(t);
|
2005-12-29 00:44:32 +03:00
|
|
|
return false;
|
2005-12-29 00:30:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tar_close(t) != 0)
|
|
|
|
{
|
2005-12-29 18:43:12 +03:00
|
|
|
cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
#else
|
2006-03-16 19:01:05 +03:00
|
|
|
(void)outFileName;
|
|
|
|
(void)files;
|
|
|
|
(void)gzip;
|
|
|
|
(void)verbose;
|
2005-12-29 18:43:12 +03:00
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-03-10 21:54:57 +03:00
|
|
|
bool cmSystemTools::ExtractTar(const char* outFileName,
|
|
|
|
const std::vector<cmStdString>& files,
|
|
|
|
bool gzip, bool verbose)
|
2005-12-29 18:43:12 +03:00
|
|
|
{
|
2005-12-29 20:19:13 +03:00
|
|
|
(void)files;
|
2005-12-29 18:43:12 +03:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
|
|
|
TAR *t;
|
2005-12-30 22:22:46 +03:00
|
|
|
cmSystemToolsGZStruct gzs;
|
|
|
|
|
|
|
|
tartype_t gztype = {
|
2005-12-31 00:28:30 +03:00
|
|
|
cmSystemToolsGZStructOpen,
|
|
|
|
cmSystemToolsGZStructClose,
|
|
|
|
cmSystemToolsGZStructRead,
|
|
|
|
cmSystemToolsGZStructWrite,
|
2005-12-30 22:22:46 +03:00
|
|
|
&gzs
|
2005-12-29 20:19:13 +03:00
|
|
|
};
|
|
|
|
|
2005-12-29 18:43:12 +03:00
|
|
|
// Ok, this libtar is not const safe. for now use auto_ptr hack
|
|
|
|
char* realName = new char[ strlen(outFileName) + 1 ];
|
|
|
|
std::auto_ptr<char> realNamePtr(realName);
|
|
|
|
strcpy(realName, outFileName);
|
|
|
|
if (tar_open(&t, realName,
|
2005-12-29 20:19:13 +03:00
|
|
|
(gzip? &gztype : NULL),
|
2005-12-29 18:43:12 +03:00
|
|
|
O_RDONLY
|
|
|
|
#ifdef _WIN32
|
|
|
|
| O_BINARY
|
|
|
|
#endif
|
|
|
|
, 0,
|
2005-12-29 20:19:13 +03:00
|
|
|
(verbose?TAR_VERBOSE:0)
|
2005-12-29 18:43:12 +03:00
|
|
|
| 0) == -1)
|
|
|
|
{
|
|
|
|
cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tar_extract_all(t, 0) != 0)
|
|
|
|
{
|
|
|
|
cmSystemTools::Error("Problem with tar_extract_all(): ", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tar_close(t) != 0)
|
|
|
|
{
|
|
|
|
cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
|
2005-12-29 00:44:32 +03:00
|
|
|
return false;
|
2005-12-29 00:30:55 +03:00
|
|
|
}
|
2005-12-29 00:44:32 +03:00
|
|
|
return true;
|
2005-12-29 01:02:16 +03:00
|
|
|
#else
|
2006-03-16 19:01:05 +03:00
|
|
|
(void)outFileName;
|
|
|
|
(void)gzip;
|
|
|
|
(void)verbose;
|
2005-12-29 01:02:16 +03:00
|
|
|
return false;
|
|
|
|
#endif
|
2005-12-29 00:30:55 +03:00
|
|
|
}
|
2005-12-29 20:19:13 +03:00
|
|
|
|
2006-03-10 21:54:57 +03:00
|
|
|
bool cmSystemTools::ListTar(const char* outFileName,
|
|
|
|
std::vector<cmStdString>& files, bool gzip,
|
|
|
|
bool verbose)
|
2005-12-29 20:19:13 +03:00
|
|
|
{
|
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
|
|
|
TAR *t;
|
2005-12-30 22:22:46 +03:00
|
|
|
cmSystemToolsGZStruct gzs;
|
|
|
|
|
|
|
|
tartype_t gztype = {
|
2005-12-31 00:28:30 +03:00
|
|
|
cmSystemToolsGZStructOpen,
|
|
|
|
cmSystemToolsGZStructClose,
|
|
|
|
cmSystemToolsGZStructRead,
|
|
|
|
cmSystemToolsGZStructWrite,
|
2005-12-30 22:22:46 +03:00
|
|
|
&gzs
|
2005-12-29 20:19:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Ok, this libtar is not const safe. for now use auto_ptr hack
|
|
|
|
char* realName = new char[ strlen(outFileName) + 1 ];
|
|
|
|
std::auto_ptr<char> realNamePtr(realName);
|
|
|
|
strcpy(realName, outFileName);
|
|
|
|
if (tar_open(&t, realName,
|
|
|
|
(gzip? &gztype : NULL),
|
|
|
|
O_RDONLY
|
|
|
|
#ifdef _WIN32
|
|
|
|
| O_BINARY
|
|
|
|
#endif
|
|
|
|
, 0,
|
|
|
|
(verbose?TAR_VERBOSE:0)
|
|
|
|
| 0) == -1)
|
|
|
|
{
|
|
|
|
cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-12-30 22:31:13 +03:00
|
|
|
while ((th_read(t)) == 0)
|
2005-12-29 20:19:13 +03:00
|
|
|
{
|
|
|
|
const char* filename = th_get_pathname(t);
|
|
|
|
files.push_back(filename);
|
|
|
|
|
|
|
|
if ( verbose )
|
|
|
|
{
|
|
|
|
th_print_long_ls(t);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout << filename << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
th_print(t);
|
|
|
|
#endif
|
|
|
|
if (TH_ISREG(t) && tar_skip_regfile(t) != 0)
|
|
|
|
{
|
2006-03-10 21:54:57 +03:00
|
|
|
cmSystemTools::Error("Problem with tar_skip_regfile(): ",
|
|
|
|
strerror(errno));
|
2005-12-29 20:19:13 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tar_close(t) != 0)
|
|
|
|
{
|
|
|
|
cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
#else
|
2006-03-16 19:01:05 +03:00
|
|
|
(void)outFileName;
|
|
|
|
(void)files;
|
|
|
|
(void)gzip;
|
|
|
|
(void)verbose;
|
2005-12-29 20:19:13 +03:00
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
2006-04-04 21:04:28 +04:00
|
|
|
|
|
|
|
int cmSystemTools::WaitForLine(cmsysProcess* process, std::string& line,
|
|
|
|
double timeout,
|
|
|
|
std::vector<char>& out,
|
|
|
|
std::vector<char>& err)
|
|
|
|
{
|
|
|
|
line = "";
|
|
|
|
std::vector<char>::iterator outiter = out.begin();
|
|
|
|
std::vector<char>::iterator erriter = err.begin();
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
// Check for a newline in stdout.
|
|
|
|
for(;outiter != out.end(); ++outiter)
|
|
|
|
{
|
|
|
|
if((*outiter == '\r') && ((outiter+1) == out.end()))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(*outiter == '\n' || *outiter == '\0')
|
|
|
|
{
|
|
|
|
int length = outiter-out.begin();
|
|
|
|
if(length > 1 && *(outiter-1) == '\r')
|
|
|
|
{
|
|
|
|
--length;
|
|
|
|
}
|
|
|
|
if(length > 0)
|
|
|
|
{
|
|
|
|
line.append(&out[0], length);
|
|
|
|
}
|
|
|
|
out.erase(out.begin(), outiter+1);
|
|
|
|
return cmsysProcess_Pipe_STDOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a newline in stderr.
|
|
|
|
for(;erriter != err.end(); ++erriter)
|
|
|
|
{
|
|
|
|
if((*erriter == '\r') && ((erriter+1) == err.end()))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(*erriter == '\n' || *erriter == '\0')
|
|
|
|
{
|
|
|
|
int length = erriter-err.begin();
|
|
|
|
if(length > 1 && *(erriter-1) == '\r')
|
|
|
|
{
|
|
|
|
--length;
|
|
|
|
}
|
|
|
|
if(length > 0)
|
|
|
|
{
|
|
|
|
line.append(&err[0], length);
|
|
|
|
}
|
|
|
|
err.erase(err.begin(), erriter+1);
|
|
|
|
return cmsysProcess_Pipe_STDERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No newlines found. Wait for more data from the process.
|
|
|
|
int length;
|
|
|
|
char* data;
|
|
|
|
int pipe = cmsysProcess_WaitForData(process, &data, &length, &timeout);
|
|
|
|
if(pipe == cmsysProcess_Pipe_Timeout)
|
|
|
|
{
|
|
|
|
// Timeout has been exceeded.
|
|
|
|
return pipe;
|
|
|
|
}
|
|
|
|
else if(pipe == cmsysProcess_Pipe_STDOUT)
|
|
|
|
{
|
|
|
|
// Append to the stdout buffer.
|
|
|
|
std::vector<char>::size_type size = out.size();
|
|
|
|
out.insert(out.end(), data, data+length);
|
|
|
|
outiter = out.begin()+size;
|
|
|
|
}
|
|
|
|
else if(pipe == cmsysProcess_Pipe_STDERR)
|
|
|
|
{
|
|
|
|
// Append to the stderr buffer.
|
|
|
|
std::vector<char>::size_type size = err.size();
|
|
|
|
err.insert(err.end(), data, data+length);
|
|
|
|
erriter = err.begin()+size;
|
|
|
|
}
|
|
|
|
else if(pipe == cmsysProcess_Pipe_None)
|
|
|
|
{
|
|
|
|
// Both stdout and stderr pipes have broken. Return leftover data.
|
|
|
|
if(!out.empty())
|
|
|
|
{
|
|
|
|
line.append(&out[0], outiter-out.begin());
|
|
|
|
out.erase(out.begin(), out.end());
|
|
|
|
return cmsysProcess_Pipe_STDOUT;
|
|
|
|
}
|
|
|
|
else if(!err.empty())
|
|
|
|
{
|
|
|
|
line.append(&err[0], erriter-err.begin());
|
|
|
|
err.erase(err.begin(), err.end());
|
|
|
|
return cmsysProcess_Pipe_STDERR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return cmsysProcess_Pipe_None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-11 19:21:36 +04:00
|
|
|
void cmSystemTools::DoNotInheritStdPipes()
|
|
|
|
{
|
2007-09-25 18:57:49 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
// Check to see if we are attached to a console
|
|
|
|
// if so, then do not stop the inherited pipes
|
|
|
|
// or stdout and stderr will not show up in dos
|
|
|
|
// shell windows
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO hOutInfo;
|
|
|
|
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
if(GetConsoleScreenBufferInfo(hOut, &hOutInfo))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2007-09-11 19:21:36 +04:00
|
|
|
{
|
|
|
|
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
DuplicateHandle(GetCurrentProcess(), out,
|
|
|
|
GetCurrentProcess(), &out, 0, FALSE,
|
|
|
|
DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
|
|
|
|
SetStdHandle(STD_OUTPUT_HANDLE, out);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
HANDLE out = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
DuplicateHandle(GetCurrentProcess(), out,
|
|
|
|
GetCurrentProcess(), &out, 0, FALSE,
|
|
|
|
DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
|
|
|
|
SetStdHandle(STD_ERROR_HANDLE, out);
|
|
|
|
}
|
2007-09-11 20:23:59 +04:00
|
|
|
#endif
|
2007-09-11 19:21:36 +04:00
|
|
|
}
|
2007-10-05 17:46:28 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmSystemTools::CopyFileTime(const char* fromFile, const char* toFile)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
cmSystemToolsWindowsHandle hFrom =
|
|
|
|
CreateFile(fromFile, GENERIC_READ, FILE_SHARE_READ, 0,
|
|
|
|
OPEN_EXISTING, 0, 0);
|
|
|
|
cmSystemToolsWindowsHandle hTo =
|
|
|
|
CreateFile(toFile, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
|
|
|
|
if(!hFrom || !hTo)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
FILETIME timeCreation;
|
|
|
|
FILETIME timeLastAccess;
|
|
|
|
FILETIME timeLastWrite;
|
|
|
|
if(!GetFileTime(hFrom, &timeCreation, &timeLastAccess, &timeLastWrite))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2007-10-05 18:03:16 +04:00
|
|
|
if(!SetFileTime(hTo, &timeCreation, &timeLastAccess, &timeLastWrite))
|
2007-10-05 17:46:28 +04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
struct stat fromStat;
|
|
|
|
if(stat(fromFile, &fromStat) < 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct utimbuf buf;
|
|
|
|
buf.actime = fromStat.st_atime;
|
|
|
|
buf.modtime = fromStat.st_mtime;
|
|
|
|
if(utime(toFile, &buf) < 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
2007-12-14 01:56:50 +03:00
|
|
|
|
2008-04-14 19:43:45 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
cmSystemToolsFileTime* cmSystemTools::FileTimeNew()
|
|
|
|
{
|
|
|
|
return new cmSystemToolsFileTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmSystemTools::FileTimeDelete(cmSystemToolsFileTime* t)
|
|
|
|
{
|
|
|
|
delete t;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmSystemTools::FileTimeGet(const char* fname, cmSystemToolsFileTime* t)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
cmSystemToolsWindowsHandle h =
|
|
|
|
CreateFile(fname, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
|
|
|
|
if(!h)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!GetFileTime(h, &t->timeCreation, &t->timeLastAccess, &t->timeLastWrite))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
struct stat st;
|
|
|
|
if(stat(fname, &st) < 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
t->timeBuf.actime = st.st_atime;
|
|
|
|
t->timeBuf.modtime = st.st_mtime;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmSystemTools::FileTimeSet(const char* fname, cmSystemToolsFileTime* t)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
cmSystemToolsWindowsHandle h =
|
2008-04-14 20:44:01 +04:00
|
|
|
CreateFile(fname, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
|
2008-04-14 19:43:45 +04:00
|
|
|
if(!h)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!SetFileTime(h, &t->timeCreation, &t->timeLastAccess, &t->timeLastWrite))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if(utime(fname, &t->timeBuf) < 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-12-14 01:56:50 +03:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
static std::string cmSystemToolsExecutableDirectory;
|
|
|
|
void cmSystemTools::FindExecutableDirectory(const char* argv0)
|
|
|
|
{
|
2009-01-20 18:06:39 +03:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
(void)argv0; // ignore this on windows
|
|
|
|
char modulepath[_MAX_PATH];
|
|
|
|
::GetModuleFileName(NULL, modulepath, sizeof(modulepath));
|
|
|
|
cmSystemToolsExecutableDirectory =
|
|
|
|
cmSystemTools::GetFilenamePath(modulepath);
|
|
|
|
return;
|
|
|
|
#else
|
2007-12-14 01:56:50 +03:00
|
|
|
std::string errorMsg;
|
|
|
|
std::string exe;
|
|
|
|
if(cmSystemTools::FindProgramPath(argv0, exe, errorMsg))
|
|
|
|
{
|
2008-03-28 21:29:29 +03:00
|
|
|
// remove symlinks
|
|
|
|
exe = cmSystemTools::GetRealPath(exe.c_str());
|
2007-12-14 01:56:50 +03:00
|
|
|
cmSystemToolsExecutableDirectory =
|
|
|
|
cmSystemTools::GetFilenamePath(exe.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// ???
|
|
|
|
}
|
2009-01-20 18:06:39 +03:00
|
|
|
#endif
|
2007-12-14 01:56:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
const char* cmSystemTools::GetExecutableDirectory()
|
|
|
|
{
|
|
|
|
return cmSystemToolsExecutableDirectory.c_str();
|
|
|
|
}
|
2007-12-20 01:15:41 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
|
|
|
void cmSystemTools::MakefileColorEcho(int color, const char* message,
|
|
|
|
bool newline, bool enabled)
|
|
|
|
{
|
|
|
|
// On some platforms (an MSYS prompt) cmsysTerminal may not be able
|
|
|
|
// to determine whether the stream is displayed on a tty. In this
|
|
|
|
// case it assumes no unless we tell it otherwise. Since we want
|
|
|
|
// color messages to be displayed for users we will assume yes.
|
|
|
|
// However, we can test for some situations when the answer is most
|
|
|
|
// likely no.
|
|
|
|
int assumeTTY = cmsysTerminal_Color_AssumeTTY;
|
|
|
|
if(cmSystemTools::GetEnv("DART_TEST_FROM_DART") ||
|
|
|
|
cmSystemTools::GetEnv("DASHBOARD_TEST_FROM_CTEST") ||
|
|
|
|
cmSystemTools::GetEnv("CTEST_INTERACTIVE_DEBUG_MODE"))
|
|
|
|
{
|
|
|
|
// Avoid printing color escapes during dashboard builds.
|
|
|
|
assumeTTY = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(enabled)
|
|
|
|
{
|
|
|
|
cmsysTerminal_cfprintf(color | assumeTTY, stdout, "%s%s",
|
|
|
|
message, newline? "\n" : "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Color is disabled. Print without color.
|
|
|
|
fprintf(stdout, "%s%s", message, newline? "\n" : "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2008-02-21 21:58:41 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmSystemTools::GuessLibrarySOName(std::string const& fullPath,
|
|
|
|
std::string& soname)
|
|
|
|
{
|
2008-02-28 00:26:35 +03:00
|
|
|
// For ELF shared libraries use a real parser to get the correct
|
|
|
|
// soname.
|
|
|
|
#if defined(CMAKE_USE_ELF_PARSER)
|
|
|
|
cmELF elf(fullPath.c_str());
|
|
|
|
if(elf)
|
|
|
|
{
|
|
|
|
return elf.GetSOName(soname);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-02-21 21:58:41 +03:00
|
|
|
// If the file is not a symlink we have no guess for its soname.
|
|
|
|
if(!cmSystemTools::FileIsSymlink(fullPath.c_str()))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!cmSystemTools::ReadSymlink(fullPath.c_str(), soname))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the symlink has a path component we have no guess for the soname.
|
|
|
|
if(!cmSystemTools::GetFilenamePath(soname).empty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the symlink points at an extended version of the same name
|
|
|
|
// assume it is the soname.
|
|
|
|
std::string name = cmSystemTools::GetFilenameName(fullPath);
|
|
|
|
if(soname.length() > name.length() &&
|
|
|
|
soname.substr(0, name.length()) == name)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-03-01 20:51:07 +03:00
|
|
|
|
2008-04-08 21:42:30 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
#if defined(CMAKE_USE_ELF_PARSER)
|
|
|
|
std::string::size_type cmSystemToolsFindRPath(std::string const& have,
|
|
|
|
std::string const& want)
|
|
|
|
{
|
|
|
|
// Search for the desired rpath.
|
|
|
|
std::string::size_type pos = have.find(want);
|
|
|
|
|
|
|
|
// If the path is not present we are done.
|
|
|
|
if(pos == std::string::npos)
|
|
|
|
{
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a regex to match a properly separated path instance.
|
|
|
|
std::string regex_str = "(^|:)(";
|
|
|
|
for(std::string::const_iterator i = want.begin(); i != want.end(); ++i)
|
|
|
|
{
|
|
|
|
int ch = *i;
|
|
|
|
if(!(('a' <= ch && ch <= 'z') ||
|
|
|
|
('A' <= ch && ch <= 'Z') ||
|
|
|
|
('0' <= ch && ch <= '9')))
|
|
|
|
{
|
|
|
|
// Escape the non-alphanumeric character.
|
|
|
|
regex_str += "\\";
|
|
|
|
}
|
|
|
|
// Store the character.
|
|
|
|
regex_str.append(1, static_cast<char>(ch));
|
|
|
|
}
|
|
|
|
regex_str += ")(:|$)";
|
|
|
|
|
|
|
|
// Look for the separated path.
|
|
|
|
cmsys::RegularExpression regex(regex_str.c_str());
|
|
|
|
if(regex.find(have))
|
|
|
|
{
|
|
|
|
// Return the position of the path portion.
|
|
|
|
return regex.start(2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The desired rpath was not found.
|
|
|
|
return std::string::npos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-08-14 17:53:21 +04:00
|
|
|
#if defined(CMAKE_USE_ELF_PARSER)
|
|
|
|
struct cmSystemToolsRPathInfo
|
|
|
|
{
|
|
|
|
unsigned long Position;
|
|
|
|
unsigned long Size;
|
|
|
|
std::string Name;
|
|
|
|
std::string Value;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2008-03-01 20:51:07 +03:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmSystemTools::ChangeRPath(std::string const& file,
|
2008-03-02 22:35:23 +03:00
|
|
|
std::string const& oldRPath,
|
2008-03-01 20:51:07 +03:00
|
|
|
std::string const& newRPath,
|
2008-05-27 18:22:03 +04:00
|
|
|
std::string* emsg,
|
|
|
|
bool* changed)
|
2008-03-01 20:51:07 +03:00
|
|
|
{
|
|
|
|
#if defined(CMAKE_USE_ELF_PARSER)
|
2008-05-27 18:22:03 +04:00
|
|
|
if(changed)
|
|
|
|
{
|
|
|
|
*changed = false;
|
|
|
|
}
|
2008-08-14 17:53:21 +04:00
|
|
|
int rp_count = 0;
|
|
|
|
cmSystemToolsRPathInfo rp[2];
|
2008-03-01 20:51:07 +03:00
|
|
|
{
|
2008-04-08 21:42:30 +04:00
|
|
|
// Parse the ELF binary.
|
2008-03-01 20:51:07 +03:00
|
|
|
cmELF elf(file.c_str());
|
2008-04-08 21:42:30 +04:00
|
|
|
|
2008-08-14 17:53:21 +04:00
|
|
|
// Get the RPATH and RUNPATH entries from it.
|
|
|
|
int se_count = 0;
|
|
|
|
cmELF::StringEntry const* se[2] = {0, 0};
|
|
|
|
const char* se_name[2] = {0, 0};
|
|
|
|
if(cmELF::StringEntry const* se_rpath = elf.GetRPath())
|
|
|
|
{
|
|
|
|
se[se_count] = se_rpath;
|
|
|
|
se_name[se_count] = "RPATH";
|
|
|
|
++se_count;
|
|
|
|
}
|
|
|
|
if(cmELF::StringEntry const* se_runpath = elf.GetRunPath())
|
2008-03-03 00:19:26 +03:00
|
|
|
{
|
2008-08-14 17:53:21 +04:00
|
|
|
se[se_count] = se_runpath;
|
|
|
|
se_name[se_count] = "RUNPATH";
|
|
|
|
++se_count;
|
|
|
|
}
|
|
|
|
if(se_count == 0)
|
|
|
|
{
|
|
|
|
if(newRPath.empty())
|
|
|
|
{
|
|
|
|
// The new rpath is empty and there is no rpath anyway so it is
|
|
|
|
// okay.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(emsg)
|
|
|
|
{
|
|
|
|
*emsg = "No valid ELF RPATH or RUNPATH entry exists in the file; ";
|
|
|
|
*emsg += elf.GetErrorMessage();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-03-03 00:19:26 +03:00
|
|
|
}
|
2008-04-08 21:42:30 +04:00
|
|
|
|
2008-08-14 17:53:21 +04:00
|
|
|
for(int i=0; i < se_count; ++i)
|
2008-03-01 20:51:07 +03:00
|
|
|
{
|
2008-08-14 17:53:21 +04:00
|
|
|
// If both RPATH and RUNPATH refer to the same string literal it
|
|
|
|
// needs to be changed only once.
|
|
|
|
if(rp_count && rp[0].Position == se[i]->Position)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-04-08 21:42:30 +04:00
|
|
|
// Make sure the current rpath contains the old rpath.
|
2008-08-14 17:53:21 +04:00
|
|
|
std::string::size_type pos =
|
|
|
|
cmSystemToolsFindRPath(se[i]->Value, oldRPath);
|
2008-04-08 21:42:30 +04:00
|
|
|
if(pos == std::string::npos)
|
2008-03-02 22:35:23 +03:00
|
|
|
{
|
2008-04-08 21:42:30 +04:00
|
|
|
// If it contains the new rpath instead then it is okay.
|
2008-08-14 17:53:21 +04:00
|
|
|
if(cmSystemToolsFindRPath(se[i]->Value, newRPath) != std::string::npos)
|
2008-03-02 22:35:23 +03:00
|
|
|
{
|
2008-08-14 17:53:21 +04:00
|
|
|
continue;
|
2008-03-02 22:35:23 +03:00
|
|
|
}
|
|
|
|
if(emsg)
|
|
|
|
{
|
2008-04-07 18:55:52 +04:00
|
|
|
cmOStringStream e;
|
2008-08-14 17:53:21 +04:00
|
|
|
e << "The current " << se_name[i] << " is:\n"
|
|
|
|
<< " " << se[i]->Value << "\n"
|
2008-04-08 21:42:30 +04:00
|
|
|
<< "which does not contain:\n"
|
2008-04-07 18:55:52 +04:00
|
|
|
<< " " << oldRPath << "\n"
|
|
|
|
<< "as was expected.";
|
|
|
|
*emsg = e.str();
|
2008-03-02 22:35:23 +03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-08-14 17:53:21 +04:00
|
|
|
// Store information about the entry in the file.
|
|
|
|
rp[rp_count].Position = se[i]->Position;
|
|
|
|
rp[rp_count].Size = se[i]->Size;
|
|
|
|
rp[rp_count].Name = se_name[i];
|
|
|
|
|
|
|
|
// Construct the new value which preserves the part of the path
|
|
|
|
// not being changed.
|
|
|
|
rp[rp_count].Value = se[i]->Value.substr(0, pos);
|
|
|
|
rp[rp_count].Value += newRPath;
|
|
|
|
rp[rp_count].Value += se[i]->Value.substr(pos+oldRPath.length(),
|
|
|
|
oldRPath.npos);
|
|
|
|
|
|
|
|
// Make sure there is enough room to store the new rpath and at
|
|
|
|
// least one null terminator.
|
|
|
|
if(rp[rp_count].Size < rp[rp_count].Value.length()+1)
|
2008-03-01 20:51:07 +03:00
|
|
|
{
|
2008-08-14 17:53:21 +04:00
|
|
|
if(emsg)
|
|
|
|
{
|
|
|
|
*emsg = "The replacement path is too long for the ";
|
|
|
|
*emsg += se_name[i];
|
|
|
|
*emsg += " entry.";
|
|
|
|
}
|
|
|
|
return false;
|
2008-03-01 20:51:07 +03:00
|
|
|
}
|
2008-08-14 17:53:21 +04:00
|
|
|
|
|
|
|
// This entry is ready for update.
|
|
|
|
++rp_count;
|
2008-03-01 20:51:07 +03:00
|
|
|
}
|
|
|
|
}
|
2008-03-02 22:35:23 +03:00
|
|
|
|
2008-08-14 17:53:21 +04:00
|
|
|
// If no runtime path needs to be changed, we are done.
|
|
|
|
if(rp_count == 0)
|
2008-03-01 20:51:07 +03:00
|
|
|
{
|
2008-08-14 17:53:21 +04:00
|
|
|
return true;
|
2008-03-01 20:51:07 +03:00
|
|
|
}
|
|
|
|
|
2008-08-15 17:47:21 +04:00
|
|
|
{
|
2008-08-14 17:53:21 +04:00
|
|
|
// Open the file for update.
|
2008-03-01 20:51:07 +03:00
|
|
|
std::ofstream f(file.c_str(),
|
|
|
|
std::ios::in | std::ios::out | std::ios::binary);
|
|
|
|
if(!f)
|
|
|
|
{
|
|
|
|
if(emsg)
|
|
|
|
{
|
|
|
|
*emsg = "Error opening file for update.";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-08-14 17:53:21 +04:00
|
|
|
|
|
|
|
// Store the new RPATH and RUNPATH strings.
|
|
|
|
for(int i=0; i < rp_count; ++i)
|
2008-03-01 20:51:07 +03:00
|
|
|
{
|
2008-08-14 17:53:21 +04:00
|
|
|
// Seek to the RPATH position.
|
|
|
|
if(!f.seekp(rp[i].Position))
|
2008-03-01 20:51:07 +03:00
|
|
|
{
|
2008-08-14 17:53:21 +04:00
|
|
|
if(emsg)
|
|
|
|
{
|
|
|
|
*emsg = "Error seeking to ";
|
|
|
|
*emsg += rp[i].Name;
|
|
|
|
*emsg += " position.";
|
|
|
|
}
|
|
|
|
return false;
|
2008-03-01 20:51:07 +03:00
|
|
|
}
|
|
|
|
|
2008-08-14 17:53:21 +04:00
|
|
|
// Write the new rpath. Follow it with enough null terminators to
|
|
|
|
// fill the string table entry.
|
|
|
|
f << rp[i].Value;
|
|
|
|
for(unsigned long j=rp[i].Value.length(); j < rp[i].Size; ++j)
|
|
|
|
{
|
|
|
|
f << '\0';
|
|
|
|
}
|
2008-03-01 20:51:07 +03:00
|
|
|
|
2008-08-14 17:53:21 +04:00
|
|
|
// Make sure it wrote correctly.
|
|
|
|
if(!f)
|
2008-05-27 18:22:03 +04:00
|
|
|
{
|
2008-08-14 17:53:21 +04:00
|
|
|
if(emsg)
|
|
|
|
{
|
|
|
|
*emsg = "Error writing the new ";
|
|
|
|
*emsg += rp[i].Name;
|
|
|
|
*emsg += " string to the file.";
|
|
|
|
}
|
|
|
|
return false;
|
2008-05-27 18:22:03 +04:00
|
|
|
}
|
2008-03-01 20:51:07 +03:00
|
|
|
}
|
2008-08-15 17:47:21 +04:00
|
|
|
}
|
2008-08-14 17:53:21 +04:00
|
|
|
|
|
|
|
// Everything was updated successfully.
|
|
|
|
if(changed)
|
2008-03-01 20:51:07 +03:00
|
|
|
{
|
2008-08-14 17:53:21 +04:00
|
|
|
*changed = true;
|
2008-03-01 20:51:07 +03:00
|
|
|
}
|
2008-08-14 17:53:21 +04:00
|
|
|
return true;
|
2008-03-01 20:51:07 +03:00
|
|
|
#else
|
|
|
|
(void)file;
|
2008-03-02 22:35:23 +03:00
|
|
|
(void)oldRPath;
|
2008-03-01 20:51:07 +03:00
|
|
|
(void)newRPath;
|
2008-03-01 23:16:49 +03:00
|
|
|
(void)emsg;
|
2008-05-27 18:22:03 +04:00
|
|
|
(void)changed;
|
2008-03-01 20:51:07 +03:00
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
2008-04-14 23:02:34 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2008-08-14 17:53:26 +04:00
|
|
|
bool cmSystemTools::RemoveRPath(std::string const& file, std::string* emsg,
|
|
|
|
bool* removed)
|
2008-04-14 23:02:34 +04:00
|
|
|
{
|
|
|
|
#if defined(CMAKE_USE_ELF_PARSER)
|
2008-08-14 17:53:26 +04:00
|
|
|
if(removed)
|
|
|
|
{
|
|
|
|
*removed = false;
|
|
|
|
}
|
2008-08-14 17:53:17 +04:00
|
|
|
int zeroCount = 0;
|
|
|
|
unsigned long zeroPosition[2] = {0,0};
|
|
|
|
unsigned long zeroSize[2] = {0,0};
|
|
|
|
unsigned long bytesBegin = 0;
|
2008-04-14 23:02:34 +04:00
|
|
|
std::vector<char> bytes;
|
|
|
|
{
|
|
|
|
// Parse the ELF binary.
|
|
|
|
cmELF elf(file.c_str());
|
|
|
|
|
2008-08-14 17:53:17 +04:00
|
|
|
// Get the RPATH and RUNPATH entries from it and sort them by index
|
|
|
|
// in the dynamic section header.
|
|
|
|
int se_count = 0;
|
|
|
|
cmELF::StringEntry const* se[2] = {0, 0};
|
|
|
|
if(cmELF::StringEntry const* se_rpath = elf.GetRPath())
|
2008-04-14 23:02:34 +04:00
|
|
|
{
|
2008-08-14 17:53:17 +04:00
|
|
|
se[se_count++] = se_rpath;
|
2008-04-14 23:02:34 +04:00
|
|
|
}
|
2008-08-14 17:53:17 +04:00
|
|
|
if(cmELF::StringEntry const* se_runpath = elf.GetRunPath())
|
2008-04-14 23:02:34 +04:00
|
|
|
{
|
2008-08-14 17:53:17 +04:00
|
|
|
se[se_count++] = se_runpath;
|
|
|
|
}
|
|
|
|
if(se_count == 0)
|
|
|
|
{
|
|
|
|
// There is no RPATH or RUNPATH anyway.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(se_count == 2 && se[1]->IndexInSection < se[0]->IndexInSection)
|
|
|
|
{
|
|
|
|
cmsys_stl::swap(se[0], se[1]);
|
|
|
|
}
|
2008-04-14 23:02:34 +04:00
|
|
|
|
2008-08-14 17:53:17 +04:00
|
|
|
// Get the size of the dynamic section header.
|
|
|
|
unsigned int count = elf.GetDynamicEntryCount();
|
|
|
|
if(count == 0)
|
|
|
|
{
|
|
|
|
// This should happen only for invalid ELF files where a DT_NULL
|
|
|
|
// appears before the end of the table.
|
|
|
|
if(emsg)
|
2008-04-14 23:02:34 +04:00
|
|
|
{
|
2008-08-14 17:53:17 +04:00
|
|
|
*emsg = "DYNAMIC section contains a DT_NULL before the end.";
|
2008-04-14 23:02:34 +04:00
|
|
|
}
|
2008-08-14 17:53:17 +04:00
|
|
|
return false;
|
|
|
|
}
|
2008-04-14 23:02:34 +04:00
|
|
|
|
2008-08-14 17:53:17 +04:00
|
|
|
// Save information about the string entries to be zeroed.
|
|
|
|
zeroCount = se_count;
|
|
|
|
for(int i=0; i < se_count; ++i)
|
|
|
|
{
|
|
|
|
zeroPosition[i] = se[i]->Position;
|
|
|
|
zeroSize[i] = se[i]->Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the range of file positions corresponding to each entry and
|
|
|
|
// the rest of the table after them.
|
|
|
|
unsigned long entryBegin[3] = {0,0,0};
|
|
|
|
unsigned long entryEnd[2] = {0,0};
|
|
|
|
for(int i=0; i < se_count; ++i)
|
|
|
|
{
|
|
|
|
entryBegin[i] = elf.GetDynamicEntryPosition(se[i]->IndexInSection);
|
|
|
|
entryEnd[i] = elf.GetDynamicEntryPosition(se[i]->IndexInSection+1);
|
|
|
|
}
|
|
|
|
entryBegin[se_count] = elf.GetDynamicEntryPosition(count);
|
2008-04-14 23:02:34 +04:00
|
|
|
|
2008-08-14 17:53:17 +04:00
|
|
|
// The data are to be written over the old table entries starting at
|
|
|
|
// the first one being removed.
|
|
|
|
bytesBegin = entryBegin[0];
|
|
|
|
unsigned long bytesEnd = entryBegin[se_count];
|
|
|
|
|
|
|
|
// Allocate a buffer to hold the part of the file to be written.
|
|
|
|
// Initialize it with zeros.
|
|
|
|
bytes.resize(bytesEnd - bytesBegin, 0);
|
|
|
|
|
|
|
|
// Read the part of the DYNAMIC section header that will move.
|
|
|
|
// The remainder of the buffer will be left with zeros which
|
|
|
|
// represent a DT_NULL entry.
|
|
|
|
char* data = &bytes[0];
|
|
|
|
for(int i=0; i < se_count; ++i)
|
|
|
|
{
|
|
|
|
// Read data between the entries being removed.
|
|
|
|
unsigned long sz = entryBegin[i+1] - entryEnd[i];
|
|
|
|
if(sz > 0 && !elf.ReadBytes(entryEnd[i], sz, data))
|
2008-04-14 23:02:34 +04:00
|
|
|
{
|
|
|
|
if(emsg)
|
|
|
|
{
|
|
|
|
*emsg = "Failed to read DYNAMIC section header.";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-08-14 17:53:17 +04:00
|
|
|
data += sz;
|
2008-04-14 23:02:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file for update.
|
|
|
|
std::ofstream f(file.c_str(),
|
|
|
|
std::ios::in | std::ios::out | std::ios::binary);
|
|
|
|
if(!f)
|
|
|
|
{
|
|
|
|
if(emsg)
|
|
|
|
{
|
|
|
|
*emsg = "Error opening file for update.";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the new DYNAMIC table header.
|
2008-08-14 17:53:17 +04:00
|
|
|
if(!f.seekp(bytesBegin))
|
2008-04-14 23:02:34 +04:00
|
|
|
{
|
|
|
|
if(emsg)
|
|
|
|
{
|
|
|
|
*emsg = "Error seeking to DYNAMIC table header for RPATH.";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!f.write(&bytes[0], bytes.size()))
|
|
|
|
{
|
|
|
|
if(emsg)
|
|
|
|
{
|
|
|
|
*emsg = "Error replacing DYNAMIC table header.";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-08-14 17:53:17 +04:00
|
|
|
// Fill the RPATH and RUNPATH strings with zero bytes.
|
|
|
|
for(int i=0; i < zeroCount; ++i)
|
2008-04-14 23:02:34 +04:00
|
|
|
{
|
2008-08-14 17:53:17 +04:00
|
|
|
if(!f.seekp(zeroPosition[i]))
|
2008-04-14 23:02:34 +04:00
|
|
|
{
|
2008-08-14 17:53:17 +04:00
|
|
|
if(emsg)
|
|
|
|
{
|
|
|
|
*emsg = "Error seeking to RPATH position.";
|
|
|
|
}
|
|
|
|
return false;
|
2008-04-14 23:02:34 +04:00
|
|
|
}
|
2008-08-14 17:53:17 +04:00
|
|
|
for(unsigned long j=0; j < zeroSize[i]; ++j)
|
2008-04-14 23:02:34 +04:00
|
|
|
{
|
2008-08-14 17:53:17 +04:00
|
|
|
f << '\0';
|
|
|
|
}
|
|
|
|
if(!f)
|
|
|
|
{
|
|
|
|
if(emsg)
|
|
|
|
{
|
|
|
|
*emsg = "Error writing the empty rpath string to the file.";
|
|
|
|
}
|
|
|
|
return false;
|
2008-04-14 23:02:34 +04:00
|
|
|
}
|
|
|
|
}
|
2008-08-14 17:53:17 +04:00
|
|
|
|
|
|
|
// Everything was updated successfully.
|
2008-08-14 17:53:26 +04:00
|
|
|
if(removed)
|
|
|
|
{
|
|
|
|
*removed = true;
|
|
|
|
}
|
2008-08-14 17:53:17 +04:00
|
|
|
return true;
|
2008-04-14 23:02:34 +04:00
|
|
|
#else
|
|
|
|
(void)file;
|
|
|
|
(void)emsg;
|
2008-08-14 17:53:26 +04:00
|
|
|
(void)removed;
|
2008-04-14 23:02:34 +04:00
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmSystemTools::CheckRPath(std::string const& file,
|
|
|
|
std::string const& newRPath)
|
|
|
|
{
|
|
|
|
#if defined(CMAKE_USE_ELF_PARSER)
|
|
|
|
// Parse the ELF binary.
|
|
|
|
cmELF elf(file.c_str());
|
|
|
|
|
|
|
|
// Get the RPATH or RUNPATH entry from it.
|
|
|
|
cmELF::StringEntry const* se = elf.GetRPath();
|
|
|
|
if(!se)
|
|
|
|
{
|
|
|
|
se = elf.GetRunPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the current rpath contains the new rpath.
|
|
|
|
if(newRPath.empty())
|
|
|
|
{
|
|
|
|
if(!se)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(se &&
|
|
|
|
cmSystemToolsFindRPath(se->Value, newRPath) != std::string::npos)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
(void)file;
|
|
|
|
(void)newRPath;
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|