2001-01-11 19:55:47 +00:00
|
|
|
/*=========================================================================
|
|
|
|
|
2002-10-23 22:03:27 +00:00
|
|
|
Program: CMake - Cross-Platform Makefile Generator
|
2001-01-11 19:55:47 +00:00
|
|
|
Module: $RCSfile$
|
|
|
|
Language: C++
|
|
|
|
Date: $Date$
|
|
|
|
Version: $Revision$
|
|
|
|
|
2002-10-23 22:03:27 +00: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 19:55:47 +00:00
|
|
|
|
2002-01-21 20:30:43 +00: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 19:55:47 +00:00
|
|
|
|
|
|
|
=========================================================================*/
|
2001-09-27 20:50:59 +00:00
|
|
|
#include "cmSystemTools.h"
|
2001-04-26 14:49:12 +00:00
|
|
|
#include <ctype.h>
|
2002-11-19 19:40:47 +00:00
|
|
|
#include <errno.h>
|
2003-02-14 15:56:21 +00:00
|
|
|
#include <time.h>
|
2002-11-19 19:40:47 +00:00
|
|
|
|
2003-06-23 18:10:12 +00:00
|
|
|
#include <cmsys/RegularExpression.hxx>
|
2003-06-23 12:58:58 +00:00
|
|
|
#include <cmsys/Directory.hxx>
|
2003-08-04 02:32:15 +00:00
|
|
|
#include <cmsys/Process.h>
|
2000-09-12 09:30:35 +00:00
|
|
|
|
2001-09-27 20:50:59 +00:00
|
|
|
// support for realpath call
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/param.h>
|
2002-03-05 23:25:13 +00:00
|
|
|
#include <sys/wait.h>
|
2001-09-27 20:50:59 +00:00
|
|
|
#endif
|
|
|
|
|
2002-09-10 19:38:26 +00:00
|
|
|
#if defined(_WIN32) && (defined(_MSC_VER) || defined(__BORLANDC__))
|
2002-03-14 16:11:39 +00:00
|
|
|
#include <string.h>
|
2000-08-29 19:26:29 +00:00
|
|
|
#include <windows.h>
|
2000-09-12 09:30:35 +00:00
|
|
|
#include <direct.h>
|
2004-01-26 20:03:09 +00:00
|
|
|
#include <io.h>
|
2001-02-23 00:24:43 +00:00
|
|
|
#define _unlink unlink
|
2000-09-12 09:30:35 +00:00
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2004-01-26 19:55:57 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2002-11-06 18:06:49 +00:00
|
|
|
|
2002-11-08 20:46:08 +00:00
|
|
|
bool cmSystemTools::s_RunCommandHideConsole = false;
|
2001-11-26 23:28:27 +00:00
|
|
|
bool cmSystemTools::s_DisableRunCommandOutput = false;
|
2001-02-23 00:24:43 +00:00
|
|
|
bool cmSystemTools::s_ErrorOccured = false;
|
2003-01-21 17:50:48 +00:00
|
|
|
bool cmSystemTools::s_FatalErrorOccured = false;
|
2001-11-19 22:52:08 +00:00
|
|
|
bool cmSystemTools::s_DisableMessages = false;
|
2003-08-21 20:22:23 +00:00
|
|
|
bool cmSystemTools::s_ForceUnixPaths = false;
|
2001-02-23 00:24:43 +00:00
|
|
|
|
2002-09-27 21:28:15 +00: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();
|
|
|
|
}
|
|
|
|
|
2002-09-18 18:18:43 +00:00
|
|
|
void (*cmSystemTools::s_ErrorCallback)(const char*, const char*, bool&, void*);
|
2004-01-07 16:24:22 +00:00
|
|
|
void (*cmSystemTools::s_StdoutCallback)(const char*, int len, void*);
|
2002-09-18 18:18:43 +00:00
|
|
|
void* cmSystemTools::s_ErrorCallbackClientData = 0;
|
2004-01-07 16:24:22 +00:00
|
|
|
void* cmSystemTools::s_StdoutCallbackClientData = 0;
|
2001-10-29 15:19:34 +00:00
|
|
|
|
2003-06-23 12:58:58 +00:00
|
|
|
// replace replace with with as many times as it shows up in source.
|
|
|
|
// write the result into source.
|
2001-02-16 16:34:23 +00:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
2003-06-23 12:58:58 +00:00
|
|
|
void cmSystemTools::ExpandRegistryValues(std::string& source)
|
|
|
|
{
|
|
|
|
// 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 18:10:12 +00:00
|
|
|
cmsys::RegularExpression regEntry("\\[(HKEY[^]]*)\\]");
|
2003-06-23 12:58:58 +00:00
|
|
|
|
|
|
|
// check for black line or comment
|
|
|
|
while (regEntry.find(source))
|
2001-02-16 16:34:23 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
// the arguments are the second match
|
|
|
|
std::string key = regEntry.match(1);
|
|
|
|
std::string val;
|
|
|
|
if (ReadRegistryValue(key.c_str(), val))
|
2001-02-16 16:34:23 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
std::string reg = "[";
|
|
|
|
reg += key + "]";
|
|
|
|
cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
|
2001-02-16 16:34:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
std::string reg = "[";
|
|
|
|
reg += key + "]";
|
|
|
|
cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
|
2001-02-16 16:34:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-03-08 15:30:18 +00:00
|
|
|
#else
|
2003-06-23 12:58:58 +00:00
|
|
|
void cmSystemTools::ExpandRegistryValues(std::string&)
|
|
|
|
{
|
2001-03-08 15:30:18 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
#endif
|
2001-03-08 15:30:18 +00:00
|
|
|
|
2003-06-23 12:58:58 +00:00
|
|
|
std::string cmSystemTools::EscapeQuotes(const char* str)
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
std::string result = "";
|
|
|
|
for(const char* ch = str; *ch != '\0'; ++ch)
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
if(*ch == '"')
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
result += '\\';
|
2000-08-29 19:26:29 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
result += *ch;
|
2000-08-29 19:26:29 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
return result;
|
2000-08-29 19:26:29 +00:00
|
|
|
}
|
|
|
|
|
2003-06-23 12:58:58 +00:00
|
|
|
std::string cmSystemTools::EscapeSpaces(const char* str)
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
// if there are spaces
|
|
|
|
std::string temp = str;
|
|
|
|
if (temp.find(" ") != std::string::npos &&
|
|
|
|
temp.find("\"")==std::string::npos)
|
2002-11-08 20:46:08 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
result = "\"";
|
|
|
|
result += str;
|
|
|
|
result += "\"";
|
|
|
|
return result;
|
2002-11-08 20:46:08 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
return str;
|
|
|
|
#else
|
|
|
|
std::string result = "";
|
|
|
|
for(const char* ch = str; *ch != '\0'; ++ch)
|
2000-08-29 19:26:29 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
if(*ch == ' ')
|
|
|
|
{
|
|
|
|
result += '\\';
|
|
|
|
}
|
|
|
|
result += *ch;
|
2000-08-29 19:26:29 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
return result;
|
|
|
|
#endif
|
2000-08-29 19:26:29 +00:00
|
|
|
}
|
2000-09-12 09:30:35 +00:00
|
|
|
|
2003-06-23 12:58:58 +00:00
|
|
|
std::string cmSystemTools::RemoveEscapes(const char* s)
|
2001-05-11 14:53:17 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
std::string result = "";
|
|
|
|
for(const char* ch = s; *ch; ++ch)
|
2001-05-11 21:11:06 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
if(*ch == '\\' && *(ch+1) != ';')
|
|
|
|
{
|
|
|
|
++ch;
|
|
|
|
switch (*ch)
|
|
|
|
{
|
|
|
|
case '\\': result.insert(result.end(), '\\'); break;
|
|
|
|
case '"': result.insert(result.end(), '"'); break;
|
|
|
|
case ' ': result.insert(result.end(), ' '); break;
|
|
|
|
case 't': result.insert(result.end(), '\t'); break;
|
|
|
|
case 'n': result.insert(result.end(), '\n'); break;
|
|
|
|
case 'r': result.insert(result.end(), '\r'); break;
|
2003-07-09 21:17:34 +00:00
|
|
|
case '#': result.insert(result.end(), '#'); break;
|
2003-07-09 21:25:24 +00:00
|
|
|
case '(': result.insert(result.end(), '('); break;
|
|
|
|
case ')': result.insert(result.end(), ')'); break;
|
2003-06-23 12:58:58 +00:00
|
|
|
case '0': result.insert(result.end(), '\0'); break;
|
|
|
|
case '\0':
|
|
|
|
{
|
|
|
|
cmSystemTools::Error("Trailing backslash in argument:\n", s);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
std::string chStr(1, *ch);
|
|
|
|
cmSystemTools::Error("Invalid escape sequence \\", chStr.c_str(),
|
|
|
|
"\nin argument ", s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.insert(result.end(), *ch);
|
|
|
|
}
|
2001-05-11 21:11:06 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
return result;
|
|
|
|
}
|
2002-04-01 18:34:38 +00:00
|
|
|
|
2003-06-23 12:58:58 +00: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 14:53:17 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
message += m1;
|
2001-05-11 14:53:17 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
if(m2)
|
2001-05-11 14:53:17 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
message += m2;
|
2001-05-11 14:53:17 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
if(m3)
|
2001-05-11 14:53:17 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
message += m3;
|
2001-05-11 14:53:17 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
if(m4)
|
2001-05-11 14:53:17 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
message += m4;
|
2001-05-11 14:53:17 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00: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 16:24:22 +00:00
|
|
|
void cmSystemTools::SetStdoutCallback(StdoutCallback f, void* clientData)
|
|
|
|
{
|
|
|
|
s_StdoutCallback = f;
|
|
|
|
s_StdoutCallbackClientData = clientData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmSystemTools::Stdout(const char* s)
|
|
|
|
{
|
|
|
|
if(s_StdoutCallback)
|
|
|
|
{
|
|
|
|
(*s_StdoutCallback)(s, strlen(s), s_StdoutCallbackClientData);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout << s;
|
2004-01-21 20:55:47 +00:00
|
|
|
std::cout.flush();
|
2004-01-07 16:24:22 +00: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 20:55:47 +00:00
|
|
|
std::cout.flush();
|
2004-01-07 16:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-23 12:58:58 +00:00
|
|
|
void cmSystemTools::Message(const char* m1, const char *title)
|
|
|
|
{
|
|
|
|
if(s_DisableMessages)
|
2001-05-11 14:53:17 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
return;
|
2001-05-11 14:53:17 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
if(s_ErrorCallback)
|
2001-05-11 14:53:17 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
(*s_ErrorCallback)(m1, title, s_DisableMessages, s_ErrorCallbackClientData);
|
|
|
|
return;
|
2001-05-11 14:53:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
std::cerr << m1 << std::endl << std::flush;
|
2001-05-11 14:53:17 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
|
2002-06-18 21:20:27 +00:00
|
|
|
}
|
2002-04-01 18:34:38 +00:00
|
|
|
|
|
|
|
|
2003-06-23 12:58:58 +00:00
|
|
|
void cmSystemTools::ReportLastSystemError(const char* msg)
|
2002-04-01 18:34:38 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
std::string m = msg;
|
|
|
|
m += ": System Error: ";
|
2003-06-24 14:16:28 +00:00
|
|
|
m += Superclass::GetLastSystemError();
|
2003-06-23 12:58:58 +00:00
|
|
|
cmSystemTools::Error(m.c_str());
|
|
|
|
}
|
|
|
|
|
2002-04-01 18:34:38 +00:00
|
|
|
|
2003-06-23 12:58:58 +00:00
|
|
|
bool cmSystemTools::IsOn(const char* val)
|
|
|
|
{
|
|
|
|
if (!val)
|
2002-04-01 18:34:38 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
std::basic_string<char> v = val;
|
2002-04-01 18:34:38 +00:00
|
|
|
|
2003-06-23 12:58:58 +00:00
|
|
|
for(std::basic_string<char>::iterator c = v.begin();
|
|
|
|
c != v.end(); c++)
|
2002-04-01 18:34:38 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
*c = toupper(*c);
|
2002-04-01 18:34:38 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
|
|
|
|
}
|
2002-04-16 17:48:35 +00:00
|
|
|
|
2003-06-23 12:58:58 +00:00
|
|
|
bool cmSystemTools::IsNOTFOUND(const char* val)
|
|
|
|
{
|
2004-05-21 01:27:37 +00:00
|
|
|
cmsys::RegularExpression reg("-NOTFOUND$");
|
|
|
|
if(reg.find(val))
|
2002-04-01 18:34:38 +00:00
|
|
|
{
|
2002-04-16 17:48:35 +00:00
|
|
|
return true;
|
2002-04-01 18:34:38 +00:00
|
|
|
}
|
2004-05-21 01:27:37 +00:00
|
|
|
return std::string("NOTFOUND") == val;
|
2002-06-18 21:20:27 +00:00
|
|
|
}
|
2002-04-01 18:34:38 +00:00
|
|
|
|
|
|
|
|
2003-06-23 12:58:58 +00:00
|
|
|
bool cmSystemTools::IsOff(const char* val)
|
2002-04-01 18:34:38 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
if (!val || strlen(val) == 0)
|
2002-04-01 18:34:38 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
return true;
|
2002-04-01 18:34:38 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
std::basic_string<char> v = val;
|
2002-04-01 18:34:38 +00:00
|
|
|
|
2003-06-23 12:58:58 +00:00
|
|
|
for(std::basic_string<char>::iterator c = v.begin();
|
|
|
|
c != v.end(); c++)
|
2002-04-01 18:34:38 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
*c = toupper(*c);
|
2002-04-01 18:34:38 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
|
|
|
|
v == "N" || cmSystemTools::IsNOTFOUND(v.c_str()) || v == "IGNORE");
|
2001-05-04 15:30:46 +00:00
|
|
|
}
|
|
|
|
|
2003-08-07 23:00:53 +00:00
|
|
|
std::vector<cmStdString> cmSystemTools::ParseArguments(const char* command)
|
2003-08-04 02:32:15 +00:00
|
|
|
{
|
2003-08-07 23:00:53 +00:00
|
|
|
std::vector<cmStdString> args;
|
2003-08-04 02:32:15 +00:00
|
|
|
std::string arg;
|
2003-08-04 11:12:42 +00:00
|
|
|
|
2003-08-04 15:48:18 +00:00
|
|
|
bool win_path = false;
|
|
|
|
|
|
|
|
if ( command[0] != '/' && command[1] == ':' && command[2] == '\\' ||
|
|
|
|
command[0] == '\"' && command[1] != '/' && command[2] == ':' && command[3] == '\\' ||
|
|
|
|
command[0] == '\\' && command[1] == '\\')
|
|
|
|
{
|
|
|
|
win_path = true;
|
|
|
|
}
|
2003-08-04 02:32:15 +00: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 11:55:40 +00:00
|
|
|
arg.append(1, *c);
|
|
|
|
++c;
|
2003-08-04 02:32:15 +00:00
|
|
|
}
|
|
|
|
if(*c)
|
|
|
|
{
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
args.push_back(arg);
|
|
|
|
}
|
|
|
|
else if(*c)
|
|
|
|
{
|
|
|
|
// Parse an unquoted argument.
|
|
|
|
while(*c && *c != ' ' && *c != '\t')
|
|
|
|
{
|
2003-08-04 15:48:18 +00:00
|
|
|
if(*c == '\\' && !win_path)
|
2003-08-04 11:12:42 +00:00
|
|
|
{
|
|
|
|
++c;
|
|
|
|
if(*c)
|
|
|
|
{
|
|
|
|
arg.append(1, *c);
|
|
|
|
++c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
arg.append(1, *c);
|
|
|
|
++c;
|
|
|
|
}
|
2003-08-04 02:32:15 +00:00
|
|
|
}
|
|
|
|
args.push_back(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-07 23:00:53 +00:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmSystemTools::RunSingleCommand(
|
|
|
|
const char* command,
|
|
|
|
std::string* output,
|
|
|
|
int *retVal,
|
|
|
|
const char* dir,
|
|
|
|
bool verbose,
|
|
|
|
int timeout)
|
|
|
|
{
|
|
|
|
if(s_DisableRunCommandOutput)
|
2003-08-04 02:32:15 +00:00
|
|
|
{
|
2003-08-07 23:00:53 +00:00
|
|
|
verbose = false;
|
2003-08-04 02:32:15 +00:00
|
|
|
}
|
2003-08-07 23:00:53 +00:00
|
|
|
|
|
|
|
std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
|
|
|
|
|
|
|
|
if(args.size() < 1)
|
2003-08-04 02:32:15 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-08-07 23:00:53 +00:00
|
|
|
std::vector<const char*> argv;
|
|
|
|
for(std::vector<cmStdString>::const_iterator a = args.begin();
|
|
|
|
a != args.end(); ++a)
|
|
|
|
{
|
|
|
|
argv.push_back(a->c_str());
|
|
|
|
}
|
|
|
|
argv.push_back(0);
|
|
|
|
|
2003-08-04 02:32:15 +00:00
|
|
|
if ( output )
|
|
|
|
{
|
|
|
|
*output = "";
|
|
|
|
}
|
2003-08-04 15:48:18 +00:00
|
|
|
|
2003-08-04 02:32:15 +00:00
|
|
|
cmsysProcess* cp = cmsysProcess_New();
|
|
|
|
cmsysProcess_SetCommand(cp, &*argv.begin());
|
|
|
|
cmsysProcess_SetWorkingDirectory(cp, dir);
|
2003-08-12 21:18:16 +00:00
|
|
|
if(cmSystemTools::GetRunCommandHideConsole())
|
|
|
|
{
|
|
|
|
cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
|
|
|
|
}
|
2003-08-04 02:32:15 +00:00
|
|
|
cmsysProcess_SetTimeout(cp, timeout);
|
|
|
|
cmsysProcess_Execute(cp);
|
|
|
|
|
2003-11-26 16:41:57 +00:00
|
|
|
std::vector<char> tempOutput;
|
2003-08-04 02:32:15 +00:00
|
|
|
char* data;
|
|
|
|
int length;
|
2004-01-26 19:41:29 +00:00
|
|
|
if ( output || verbose )
|
|
|
|
{
|
2003-12-05 16:53:17 +00:00
|
|
|
while(cmsysProcess_WaitForData(cp, &data, &length, 0))
|
2003-08-04 02:32:15 +00:00
|
|
|
{
|
|
|
|
if ( output )
|
|
|
|
{
|
2003-11-26 16:41:57 +00:00
|
|
|
tempOutput.insert(tempOutput.end(), data, data+length);
|
2003-08-04 02:32:15 +00:00
|
|
|
}
|
|
|
|
if(verbose)
|
|
|
|
{
|
2004-01-07 16:24:22 +00:00
|
|
|
cmSystemTools::Stdout(data, length);
|
2003-08-04 02:32:15 +00:00
|
|
|
}
|
|
|
|
}
|
2004-01-26 19:41:29 +00:00
|
|
|
}
|
2003-08-04 02:32:15 +00:00
|
|
|
|
|
|
|
cmsysProcess_WaitForExit(cp, 0);
|
2003-11-26 16:41:57 +00:00
|
|
|
if ( output )
|
|
|
|
{
|
|
|
|
output->append(&*tempOutput.begin(), tempOutput.size());
|
|
|
|
}
|
2003-08-04 02:32:15 +00: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 13:50:37 +00:00
|
|
|
else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exception)
|
|
|
|
{
|
2004-02-24 15:04:02 +00:00
|
|
|
const char* exception_str = cmsysProcess_GetExceptionString(cp);
|
|
|
|
if ( verbose )
|
|
|
|
{
|
|
|
|
std::cerr << exception_str << std::endl;
|
|
|
|
}
|
2004-02-24 23:48:44 +00:00
|
|
|
if ( output )
|
|
|
|
{
|
|
|
|
output->append(exception_str, strlen(exception_str));
|
|
|
|
}
|
2004-01-15 13:50:37 +00:00
|
|
|
result = false;
|
|
|
|
}
|
2003-12-30 15:26:18 +00:00
|
|
|
else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Error)
|
2003-08-04 02:32:15 +00:00
|
|
|
{
|
2004-02-24 15:04:02 +00:00
|
|
|
const char* error_str = cmsysProcess_GetErrorString(cp);
|
|
|
|
if ( verbose )
|
|
|
|
{
|
|
|
|
std::cerr << error_str << std::endl;
|
|
|
|
}
|
2004-02-24 23:48:44 +00:00
|
|
|
if ( output )
|
|
|
|
{
|
|
|
|
output->append(error_str, strlen(error_str));
|
|
|
|
}
|
2003-08-04 02:32:15 +00:00
|
|
|
result = false;
|
|
|
|
}
|
2004-01-15 13:50:37 +00:00
|
|
|
else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Expired)
|
|
|
|
{
|
2004-02-24 15:04:02 +00:00
|
|
|
const char* error_str = "Process terminated due to timeout\n";
|
|
|
|
if ( verbose )
|
|
|
|
{
|
|
|
|
std::cerr << error_str << std::endl;
|
|
|
|
}
|
2004-02-24 23:48:44 +00:00
|
|
|
if ( output )
|
|
|
|
{
|
|
|
|
output->append(error_str, strlen(error_str));
|
|
|
|
}
|
2004-01-15 13:50:37 +00:00
|
|
|
result = false;
|
|
|
|
}
|
2003-08-04 02:32:15 +00:00
|
|
|
|
|
|
|
cmsysProcess_Delete(cp);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
bool cmSystemTools::RunCommand(const char* command,
|
|
|
|
std::string& output,
|
|
|
|
const char* dir,
|
|
|
|
bool verbose,
|
|
|
|
int timeout)
|
2001-05-03 20:55:18 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
int dummy;
|
|
|
|
return cmSystemTools::RunCommand(command, output, dummy,
|
|
|
|
dir, verbose, timeout);
|
2001-05-03 20:55:18 +00:00
|
|
|
}
|
|
|
|
|
2003-07-07 22:27:57 +00: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 18:53:32 +00:00
|
|
|
{
|
2003-07-07 22:27:57 +00:00
|
|
|
#if defined(__BORLANDC__)
|
|
|
|
return cmWin32ProcessExecution::BorlandRunCommand(command, dir, output,
|
|
|
|
retVal,
|
|
|
|
verbose, timeout,
|
|
|
|
cmSystemTools::GetRunCommandHideConsole());
|
|
|
|
#else // Visual studio
|
|
|
|
::SetLastError(ERROR_SUCCESS);
|
|
|
|
if ( ! command )
|
2003-06-19 18:27:01 +00:00
|
|
|
{
|
2003-07-07 22:27:57 +00:00
|
|
|
cmSystemTools::Error("No command specified");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmWin32ProcessExecution resProc;
|
|
|
|
if(cmSystemTools::GetRunCommandHideConsole())
|
|
|
|
{
|
|
|
|
resProc.SetHideWindows(true);
|
2003-06-19 18:27:01 +00:00
|
|
|
}
|
2001-05-03 20:55:18 +00:00
|
|
|
|
2003-07-07 22:27:57 +00:00
|
|
|
if ( cmSystemTools::GetWindows9xComspecSubstitute() )
|
2002-10-11 14:16:01 +00:00
|
|
|
{
|
2003-07-07 22:27:57 +00:00
|
|
|
resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
|
|
|
|
}
|
|
|
|
if ( !resProc.StartProcess(command, dir, verbose) )
|
|
|
|
{
|
|
|
|
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 14:16:01 +00:00
|
|
|
{
|
2003-07-07 22:27:57 +00: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 12:41:28 +00:00
|
|
|
}
|
2003-07-07 22:27:57 +00: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 12:41:28 +00:00
|
|
|
{
|
2003-07-07 22:27:57 +00:00
|
|
|
output += "\n";
|
2002-10-11 14:16:01 +00:00
|
|
|
}
|
2003-07-07 22:27:57 +00:00
|
|
|
multiLine = true;
|
2001-05-25 01:17:02 +00:00
|
|
|
}
|
2003-07-07 22:27:57 +00:00
|
|
|
fin.close();
|
|
|
|
cmSystemTools::RemoveFile(tempFile.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // We have popen
|
|
|
|
|
|
|
|
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 01:17:02 +00:00
|
|
|
{
|
2003-07-07 22:27:57 +00:00
|
|
|
commandInDir = "cd \"";
|
|
|
|
commandInDir += dir;
|
|
|
|
commandInDir += "\" && ";
|
|
|
|
commandInDir += command;
|
2001-05-25 01:17:02 +00:00
|
|
|
}
|
2003-07-07 22:27:57 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
commandInDir = command;
|
|
|
|
}
|
|
|
|
commandInDir += " 2>&1";
|
|
|
|
command = commandInDir.c_str();
|
|
|
|
const int BUFFER_SIZE = 4096;
|
|
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
if(verbose)
|
|
|
|
{
|
2004-01-07 16:24:22 +00:00
|
|
|
cmSystemTools::Stdout("running ");
|
|
|
|
cmSystemTools::Stdout(command);
|
|
|
|
cmSystemTools::Stdout("\n");
|
2003-07-07 22:27:57 +00:00
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
FILE* cpipe = popen(command, "r");
|
|
|
|
if(!cpipe)
|
2001-09-13 15:27:54 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
return false;
|
2001-09-13 15:27:54 +00:00
|
|
|
}
|
2003-07-07 22:27:57 +00:00
|
|
|
fgets(buffer, BUFFER_SIZE, cpipe);
|
|
|
|
while(!feof(cpipe))
|
2003-06-03 14:30:23 +00:00
|
|
|
{
|
2003-07-07 12:41:28 +00:00
|
|
|
if(verbose)
|
2003-06-23 12:58:58 +00:00
|
|
|
{
|
2004-01-07 16:24:22 +00:00
|
|
|
cmSystemTools::Stdout(buffer);
|
2003-06-23 12:58:58 +00:00
|
|
|
}
|
2003-07-07 22:27:57 +00:00
|
|
|
output += buffer;
|
|
|
|
fgets(buffer, BUFFER_SIZE, cpipe);
|
2003-06-03 14:30:23 +00:00
|
|
|
}
|
2003-07-07 22:27:57 +00:00
|
|
|
|
|
|
|
retVal = pclose(cpipe);
|
|
|
|
if (WIFEXITED(retVal))
|
2001-05-25 01:17:02 +00:00
|
|
|
{
|
2003-07-07 22:27:57 +00:00
|
|
|
retVal = WEXITSTATUS(retVal);
|
|
|
|
return true;
|
2001-05-25 01:17:02 +00:00
|
|
|
}
|
2003-07-07 22:27:57 +00:00
|
|
|
if (WIFSIGNALED(retVal))
|
2003-07-07 12:41:28 +00:00
|
|
|
{
|
2003-07-07 22:27:57 +00: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
|
|
|
|
#ifdef SIGBUS
|
|
|
|
case SIGBUS:
|
|
|
|
error << "SIGBUS";
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#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 12:41:28 +00:00
|
|
|
}
|
|
|
|
|
2003-07-07 22:27:57 +00: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 01:17:02 +00:00
|
|
|
}
|
|
|
|
|
2003-06-23 12:58:58 +00:00
|
|
|
bool cmSystemTools::DoesFileExistWithExtensions(
|
|
|
|
const char* name,
|
|
|
|
const std::vector<std::string>& headerExts)
|
2002-07-15 13:45:47 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
std::string hname;
|
|
|
|
|
|
|
|
for( std::vector<std::string>::const_iterator ext = headerExts.begin();
|
|
|
|
ext != headerExts.end(); ++ext )
|
2002-07-15 13:45:47 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
hname = name;
|
|
|
|
hname += ".";
|
|
|
|
hname += *ext;
|
|
|
|
if(cmSystemTools::FileExists(hname.c_str()))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2002-07-15 13:45:47 +00:00
|
|
|
}
|
2003-06-23 12:58:58 +00:00
|
|
|
return false;
|
2002-07-15 13:45:47 +00:00
|
|
|
}
|
|
|
|
|
2003-06-23 12:58:58 +00:00
|
|
|
bool cmSystemTools::cmCopyFile(const char* source, const char* destination)
|
2002-09-13 20:38:35 +00:00
|
|
|
{
|
2004-01-26 20:50:38 +00:00
|
|
|
mode_t perm = 0;
|
|
|
|
return cmSystemTools::GetPermissions(source, perm) &&
|
|
|
|
Superclass::CopyFileAlways(source, destination) &&
|
|
|
|
cmSystemTools::SetPermissions(destination, perm);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmSystemTools::CopyFileIfDifferent(const char* source,
|
|
|
|
const char* destination)
|
|
|
|
{
|
|
|
|
mode_t perm = 0;
|
|
|
|
return cmSystemTools::GetPermissions(source, perm) &&
|
|
|
|
Superclass::CopyFileIfDifferent(source, destination) &&
|
|
|
|
cmSystemTools::SetPermissions(destination, perm);
|
2002-09-13 20:38:35 +00:00
|
|
|
}
|
2002-07-15 13:45:47 +00:00
|
|
|
|
2001-06-21 16:01:18 +00:00
|
|
|
void cmSystemTools::Glob(const char *directory, const char *regexp,
|
|
|
|
std::vector<std::string>& files)
|
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
cmsys::Directory d;
|
2003-06-23 18:10:12 +00:00
|
|
|
cmsys::RegularExpression reg(regexp);
|
2001-06-21 16:01:18 +00:00
|
|
|
|
|
|
|
if (d.Load(directory))
|
|
|
|
{
|
2002-03-13 15:25:11 +00:00
|
|
|
size_t numf;
|
2002-09-30 01:57:48 +00:00
|
|
|
unsigned int i;
|
2001-06-21 16:01:18 +00: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 14:54:29 +00:00
|
|
|
|
|
|
|
void cmSystemTools::GlobDirs(const char *fullPath,
|
|
|
|
std::vector<std::string>& files)
|
|
|
|
{
|
|
|
|
std::string path = fullPath;
|
2001-11-13 17:38:53 +00:00
|
|
|
std::string::size_type pos = path.find("/*");
|
2001-09-20 14:54:29 +00: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 12:58:58 +00:00
|
|
|
cmsys::Directory d;
|
2001-09-20 14:54:29 +00:00
|
|
|
if (d.Load(startPath.c_str()))
|
|
|
|
{
|
2002-02-27 23:11:12 +00:00
|
|
|
for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
|
2001-09-20 14:54:29 +00: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 15:10:46 +00:00
|
|
|
|
|
|
|
|
2002-12-11 23:13:33 +00:00
|
|
|
void cmSystemTools::ExpandList(std::vector<std::string> const& arguments,
|
|
|
|
std::vector<std::string>& newargs)
|
2002-03-06 15:10:46 +00:00
|
|
|
{
|
|
|
|
std::vector<std::string>::const_iterator i;
|
|
|
|
for(i = arguments.begin();i != arguments.end(); ++i)
|
|
|
|
{
|
2002-12-11 23:13:33 +00:00
|
|
|
cmSystemTools::ExpandListArgument(*i, newargs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmSystemTools::ExpandListArgument(const std::string& arg,
|
|
|
|
std::vector<std::string>& newargs)
|
|
|
|
{
|
|
|
|
std::string newarg;
|
|
|
|
// If argument is empty, it is an empty list.
|
|
|
|
if(arg.length() == 0)
|
|
|
|
{
|
|
|
|
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-08 23:05:29 +00:00
|
|
|
return;
|
2002-12-11 23:13:33 +00:00
|
|
|
}
|
2003-12-08 23:05:29 +00: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-11 23:13:33 +00:00
|
|
|
{
|
2003-12-08 23:05:29 +00:00
|
|
|
switch(*c)
|
2002-03-06 15:10:46 +00:00
|
|
|
{
|
2003-12-08 23:05:29 +00:00
|
|
|
case '\\':
|
2002-12-11 23:13:33 +00:00
|
|
|
{
|
2003-12-08 23:05:29 +00:00
|
|
|
// We only want to allow escaping of semicolons. Other
|
|
|
|
// escapes should not be processed here.
|
|
|
|
++c;
|
|
|
|
if(*c == ';')
|
2002-04-18 16:02:00 +00:00
|
|
|
{
|
2003-12-08 23:05:29 +00:00
|
|
|
newarg += ';';
|
2002-04-18 16:02:00 +00:00
|
|
|
}
|
2002-12-11 23:13:33 +00:00
|
|
|
else
|
2002-03-26 21:44:49 +00:00
|
|
|
{
|
2003-12-08 23:05:29 +00:00
|
|
|
newarg += '\\';
|
|
|
|
if(*c)
|
2002-04-01 19:50:00 +00:00
|
|
|
{
|
2003-12-08 23:05:29 +00:00
|
|
|
newarg += *c;
|
2002-04-18 16:02:00 +00:00
|
|
|
}
|
2003-12-08 23:05:29 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case '[':
|
|
|
|
{
|
|
|
|
++squareNesting;
|
|
|
|
newarg += '[';
|
|
|
|
} break;
|
|
|
|
case ']':
|
|
|
|
{
|
|
|
|
--squareNesting;
|
|
|
|
newarg += ']';
|
|
|
|
} break;
|
|
|
|
case ';':
|
|
|
|
{
|
|
|
|
// Break the string here if we are not nested inside square
|
|
|
|
// brackets.
|
|
|
|
if(squareNesting == 0)
|
|
|
|
{
|
|
|
|
if(newarg.length())
|
2002-04-18 16:02:00 +00:00
|
|
|
{
|
2003-12-08 23:05:29 +00:00
|
|
|
// Add an argument if the string is not empty.
|
|
|
|
newargs.push_back(newarg);
|
|
|
|
newarg = "";
|
2002-04-01 19:50:00 +00:00
|
|
|
}
|
2002-03-26 21:44:49 +00:00
|
|
|
}
|
2003-12-08 23:05:29 +00:00
|
|
|
else
|
2002-12-11 23:13:33 +00:00
|
|
|
{
|
2003-12-08 23:05:29 +00:00
|
|
|
newarg += ';';
|
2002-12-11 23:13:33 +00:00
|
|
|
}
|
2003-12-08 23:05:29 +00:00
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// Just append this character.
|
|
|
|
newarg += *c;
|
|
|
|
} break;
|
2002-03-06 15:10:46 +00:00
|
|
|
}
|
|
|
|
}
|
2003-12-08 23:05:29 +00:00
|
|
|
if(newarg.length())
|
|
|
|
{
|
|
|
|
// Add the last argument if the string is not empty.
|
|
|
|
newargs.push_back(newarg);
|
|
|
|
}
|
2002-03-06 15:10:46 +00:00
|
|
|
}
|
2002-04-03 21:14:06 +00:00
|
|
|
|
2003-12-15 22:26:00 +00:00
|
|
|
bool cmSystemTools::SimpleGlob(const cmStdString& glob,
|
|
|
|
std::vector<cmStdString>& files,
|
2002-11-05 13:51:40 +00:00
|
|
|
int type /* = 0 */)
|
|
|
|
{
|
2002-12-09 03:35:19 +00:00
|
|
|
files.clear();
|
2002-11-05 13:51:40 +00: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 16:37:00 +00:00
|
|
|
bool res = false;
|
2003-06-23 12:58:58 +00:00
|
|
|
cmsys::Directory d;
|
2002-11-05 13:51:40 +00: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 20:46:08 +00:00
|
|
|
cmSystemTools::FileFormat cmSystemTools::GetFileFormat(const char* cext)
|
2002-09-25 13:30:49 +00:00
|
|
|
{
|
|
|
|
if ( ! cext || *cext == 0 )
|
|
|
|
{
|
|
|
|
return cmSystemTools::NO_FILE_FORMAT;
|
|
|
|
}
|
2003-04-03 13:44:49 +00:00
|
|
|
//std::string ext = cmSystemTools::LowerCase(cext);
|
|
|
|
std::string ext = cext;
|
2002-09-25 13:30:49 +00:00
|
|
|
if ( ext == "c" || ext == ".c" ) { return cmSystemTools::C_FILE_FORMAT; }
|
2003-03-13 17:24:16 +00: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 == "m" || ext == ".m" ||
|
|
|
|
ext == "mm" || ext == ".mm"
|
|
|
|
) { return cmSystemTools::CXX_FILE_FORMAT; }
|
2002-09-25 13:30:49 +00:00
|
|
|
if ( ext == "java" || ext == ".java" ) { return cmSystemTools::JAVA_FILE_FORMAT; }
|
2003-03-13 17:24:16 +00:00
|
|
|
if (
|
2003-04-03 13:44:49 +00:00
|
|
|
ext == "H" || ext == ".H" ||
|
2003-03-13 17:24:16 +00: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; }
|
2002-09-25 13:30:49 +00:00
|
|
|
if ( ext == "rc" || ext == ".rc" ) { return cmSystemTools::RESOURCE_FILE_FORMAT; }
|
|
|
|
if ( ext == "def" || ext == ".def" ) { return cmSystemTools::DEFINITION_FILE_FORMAT; }
|
|
|
|
if ( ext == "lib" || ext == ".lib" ||
|
|
|
|
ext == "a" || ext == ".a") { return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
|
|
|
|
if ( ext == "o" || ext == ".o" ||
|
|
|
|
ext == "obj" || ext == ".obj") { return cmSystemTools::OBJECT_FILE_FORMAT; }
|
|
|
|
#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-19 23:17:17 +00:00
|
|
|
|
2003-06-23 12:58:58 +00:00
|
|
|
bool cmSystemTools::Split(const char* s, std::vector<cmStdString>& l)
|
2003-03-27 17:24:30 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
std::vector<std::string> temp;
|
2004-04-20 22:28:23 +00:00
|
|
|
bool res = Superclass::Split(s, temp);
|
2003-06-23 12:58:58 +00:00
|
|
|
for(std::vector<std::string>::const_iterator i = temp.begin();
|
|
|
|
i != temp.end(); ++i)
|
2002-12-11 19:15:35 +00:00
|
|
|
{
|
2003-06-23 12:58:58 +00:00
|
|
|
l.push_back(*i);
|
2002-12-11 19:15:35 +00:00
|
|
|
}
|
2004-04-20 22:28:23 +00:00
|
|
|
return res;
|
2002-12-11 16:49:08 +00:00
|
|
|
}
|
2003-08-21 20:22:23 +00: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
|
|
|
|
}
|
|
|
|
|
2003-10-30 00:49:50 +00: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 19:52:12 +00:00
|
|
|
|
2003-11-26 21:11:43 +00:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
bool cmSystemTools::CreateSymlink(const char*, const char*)
|
2003-11-26 19:52:12 +00:00
|
|
|
{
|
|
|
|
// Should we create a copy here?
|
|
|
|
return false;
|
|
|
|
}
|
2003-11-26 21:11:43 +00:00
|
|
|
#else
|
|
|
|
bool cmSystemTools::CreateSymlink(const char* origName, const char* newName)
|
|
|
|
{
|
|
|
|
return (symlink(origName, newName) >= 0);
|
|
|
|
}
|
|
|
|
#endif
|
2003-12-22 17:24:26 +00:00
|
|
|
|
|
|
|
|
2003-12-26 20:00:27 +00:00
|
|
|
std::vector<cmStdString> cmSystemTools::SplitString(const char* p, char sep)
|
2003-12-22 17:24:26 +00:00
|
|
|
{
|
|
|
|
std::string path = p;
|
2003-12-26 20:00:27 +00:00
|
|
|
std::vector<cmStdString> paths;
|
2003-12-22 17:24:26 +00:00
|
|
|
std::string::size_type pos1 = 0;
|
|
|
|
std::string::size_type pos2 = path.find(sep, pos1+1);
|
|
|
|
while(pos2 != std::string::npos)
|
|
|
|
{
|
2003-12-26 20:00:27 +00:00
|
|
|
paths.push_back(path.substr(pos1, pos2-pos1));
|
|
|
|
pos1 = pos2+1;
|
2003-12-22 17:24:26 +00:00
|
|
|
pos2 = path.find(sep, pos1+1);
|
|
|
|
}
|
2003-12-26 20:00:27 +00:00
|
|
|
paths.push_back(path.substr(pos1, pos2-pos1));
|
2003-12-22 17:24:26 +00:00
|
|
|
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// compute the relative path from here to there
|
|
|
|
std::string cmSystemTools::RelativePath(const char* local, const char* remote)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
std::string lowerCaseLocal = cmSystemTools::LowerCase(std::string(local));
|
|
|
|
std::string lowerCaseRemote = cmSystemTools::LowerCase(std::string(remote));
|
|
|
|
remote = lowerCaseRemote.c_str();
|
|
|
|
local = lowerCaseLocal.c_str();
|
|
|
|
#endif
|
|
|
|
// check for driveletter: as the start of the path
|
|
|
|
// if not on the same drive then full path to remote must be used.
|
|
|
|
if(local[0] && local[0] != '/')
|
|
|
|
{
|
|
|
|
if(remote[0] && local[0] != remote[0])
|
|
|
|
{
|
|
|
|
return remote;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::string relativePath; // result string
|
|
|
|
// split up both paths into arrays of strings using / as a separator
|
2003-12-26 20:00:27 +00:00
|
|
|
std::vector<cmStdString> fileSplit = cmSystemTools::SplitString(local);
|
|
|
|
std::vector<cmStdString> relativeSplit = cmSystemTools::SplitString(remote);
|
2003-12-22 17:24:26 +00:00
|
|
|
// count up how many mathing directory names there are from the start
|
2003-12-22 21:21:27 +00:00
|
|
|
unsigned int sameCount = 0;
|
2003-12-22 17:24:26 +00:00
|
|
|
while(sameCount < fileSplit.size()-1 && sameCount < relativeSplit.size()-1 &&
|
|
|
|
fileSplit[sameCount] == relativeSplit[sameCount])
|
|
|
|
{
|
|
|
|
sameCount++;
|
|
|
|
}
|
2004-01-20 19:35:25 +00:00
|
|
|
if(sameCount == 0)
|
|
|
|
{
|
|
|
|
return std::string(remote);
|
|
|
|
}
|
2003-12-22 17:24:26 +00:00
|
|
|
// put in sameCount number of ../ into the path
|
2003-12-22 21:21:27 +00:00
|
|
|
unsigned int i;
|
2003-12-22 17:24:26 +00:00
|
|
|
for(i = sameCount; i < fileSplit.size(); ++i)
|
|
|
|
{
|
|
|
|
relativePath += "../";
|
|
|
|
}
|
|
|
|
// now put the rest of path that did not match back
|
|
|
|
for(i = sameCount; i < relativeSplit.size()-1; ++i)
|
|
|
|
{
|
|
|
|
relativePath += relativeSplit[i] + "/";
|
|
|
|
}
|
|
|
|
relativePath += relativeSplit[i];
|
|
|
|
return relativePath;
|
|
|
|
}
|
2004-01-26 18:32:46 +00:00
|
|
|
class cmDeletingCharVector : public std::vector<char*>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~cmDeletingCharVector()
|
|
|
|
{
|
|
|
|
for(std::vector<char*>::iterator i = this->begin();
|
|
|
|
i != this->end(); ++i)
|
|
|
|
{
|
2004-01-27 19:51:18 +00:00
|
|
|
delete []*i;
|
2004-01-26 18:32:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
bool cmSystemTools::PutEnv(const char* value)
|
|
|
|
{
|
|
|
|
static cmDeletingCharVector localEnvironment;
|
|
|
|
char* envVar = new char[strlen(value)+1];
|
|
|
|
strcpy(envVar, value);
|
2004-01-26 19:00:22 +00:00
|
|
|
int ret = putenv(envVar);
|
2004-01-26 18:32:46 +00:00
|
|
|
// save the pointer in the static vector so that it can
|
|
|
|
// be deleted on exit
|
|
|
|
localEnvironment.push_back(envVar);
|
2004-01-26 19:00:22 +00:00
|
|
|
return ret == 0;
|
2004-01-26 18:32:46 +00:00
|
|
|
}
|
2004-01-26 19:55:57 +00:00
|
|
|
|
|
|
|
bool cmSystemTools::GetPermissions(const char* file, mode_t& mode)
|
|
|
|
{
|
|
|
|
if ( !file )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if ( stat(file, &st) < 0 )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mode = st.st_mode;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmSystemTools::SetPermissions(const char* file, mode_t mode)
|
|
|
|
{
|
|
|
|
if ( !file )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( !cmSystemTools::FileExists(file) )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( chmod(file, mode) < 0 )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|