2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2001-01-11 22:55:47 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2001-01-11 22:55:47 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2011-07-28 18:23:13 +04:00
|
|
|
#include "cmSystemTools.h"
|
2016-04-29 16:40:20 +03:00
|
|
|
|
2015-02-10 23:50:16 +03:00
|
|
|
#include "cmAlgorithms.h"
|
2016-09-01 21:59:28 +03:00
|
|
|
|
2007-12-20 01:15:41 +03:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2016-05-16 17:34:04 +03:00
|
|
|
#include "cmArchiveWrite.h"
|
|
|
|
#include "cmLocale.h"
|
|
|
|
#include <cm_libarchive.h>
|
|
|
|
#ifndef __LA_INT64_T
|
|
|
|
#define __LA_INT64_T la_int64_t
|
|
|
|
#endif
|
2007-12-20 01:15:41 +03:00
|
|
|
#endif
|
2016-09-01 21:59:28 +03:00
|
|
|
|
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
|
|
|
#include "cmCryptoHash.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CMAKE_USE_ELF_PARSER)
|
|
|
|
#include "cmELF.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CMAKE_USE_MACH_PARSER)
|
|
|
|
#include "cmMachO.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <cmsys/Directory.hxx>
|
|
|
|
#include <cmsys/Encoding.hxx>
|
2014-01-04 09:47:13 +04:00
|
|
|
#include <cmsys/FStream.hxx>
|
2016-09-01 21:59:28 +03:00
|
|
|
#include <cmsys/RegularExpression.hxx>
|
|
|
|
#include <cmsys/System.h>
|
|
|
|
#include <cmsys/SystemTools.hxx>
|
2015-02-06 00:29:59 +03:00
|
|
|
#include <cmsys/Terminal.h>
|
2016-09-01 21:59:28 +03:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <set>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <time.h>
|
2000-09-12 13:30:35 +04:00
|
|
|
|
2007-10-08 00:22:16 +04:00
|
|
|
#if defined(_WIN32)
|
2016-05-16 17:34:04 +03:00
|
|
|
#include <windows.h>
|
|
|
|
// include wincrypt.h after windows.h
|
|
|
|
#include <wincrypt.h>
|
2000-09-12 13:30:35 +04:00
|
|
|
#else
|
2016-05-16 17:34:04 +03:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <utime.h>
|
2000-09-12 13:30:35 +04:00
|
|
|
#endif
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
#if defined(_WIN32) && \
|
|
|
|
(defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__))
|
|
|
|
#include <io.h>
|
2007-10-08 00:22:16 +04:00
|
|
|
#endif
|
|
|
|
|
2016-09-01 21:59:28 +03:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <mach-o/dyld.h>
|
2008-02-28 00:26:35 +03:00
|
|
|
#endif
|
|
|
|
|
2016-09-01 21:59:28 +03:00
|
|
|
#ifdef __QNX__
|
|
|
|
#include <malloc.h> /* for malloc/free on QNX */
|
2014-12-19 22:31:22 +03:00
|
|
|
#endif
|
|
|
|
|
2015-09-10 17:04:20 +03:00
|
|
|
static bool cm_isspace(char c)
|
|
|
|
{
|
|
|
|
return ((c & 0x80) == 0) && isspace(c);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
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
|
2016-05-16 17:34:04 +03:00
|
|
|
#if defined(_WIN32)
|
|
|
|
extern __declspec(dllimport) char** environ;
|
|
|
|
#else
|
2006-03-17 23:46:20 +03:00
|
|
|
extern char** environ;
|
2016-05-16 17:34:04 +03:00
|
|
|
#endif
|
2006-03-17 23:46:20 +03:00
|
|
|
#endif
|
2006-03-17 18:58:37 +03:00
|
|
|
|
2014-06-12 16:46:40 +04:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2016-05-16 17:34:04 +03:00
|
|
|
static std::string cm_archive_entry_pathname(struct archive_entry* entry)
|
2014-06-12 16:46:40 +04:00
|
|
|
{
|
|
|
|
#if cmsys_STL_HAS_WSTRING
|
2016-05-16 17:34:04 +03:00
|
|
|
return cmsys::Encoding::ToNarrow(archive_entry_pathname_w(entry));
|
2014-06-12 16:46:40 +04:00
|
|
|
#else
|
|
|
|
return archive_entry_pathname(entry);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
static int cm_archive_read_open_file(struct archive* a, const char* file,
|
2014-06-12 16:46:40 +04:00
|
|
|
int block_size)
|
|
|
|
{
|
|
|
|
#if cmsys_STL_HAS_WSTRING
|
|
|
|
std::wstring wfile = cmsys::Encoding::ToWide(file);
|
|
|
|
return archive_read_open_filename_w(a, wfile.c_str(), block_size);
|
|
|
|
#else
|
|
|
|
return archive_read_open_filename(a, file, block_size);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-08 00:22:16 +04:00
|
|
|
#ifdef _WIN32
|
2007-10-05 17:46:28 +04:00
|
|
|
class cmSystemToolsWindowsHandle
|
|
|
|
{
|
|
|
|
public:
|
2016-05-16 17:34:04 +03:00
|
|
|
cmSystemToolsWindowsHandle(HANDLE h)
|
|
|
|
: handle_(h)
|
|
|
|
{
|
|
|
|
}
|
2007-10-05 17:46:28 +04:00
|
|
|
~cmSystemToolsWindowsHandle()
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
if (this->handle_ != INVALID_HANDLE_VALUE) {
|
2007-10-05 17:46:28 +04:00
|
|
|
CloseHandle(this->handle_);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-10-05 17:46:28 +04:00
|
|
|
operator bool() const { return this->handle_ != INVALID_HANDLE_VALUE; }
|
2016-05-16 17:34:04 +03: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_;
|
|
|
|
};
|
2012-03-01 20:57:34 +04:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
#include <crt_externs.h>
|
2016-09-01 21:59:28 +03:00
|
|
|
|
2012-03-01 20:57:34 +04:00
|
|
|
#define environ (*_NSGetEnviron())
|
2007-10-05 17:46:28 +04:00
|
|
|
#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
|
|
|
|
2014-05-15 17:38:30 +04:00
|
|
|
cmSystemTools::MessageCallback cmSystemTools::s_MessageCallback;
|
2014-05-15 17:44:25 +04:00
|
|
|
cmSystemTools::OutputCallback cmSystemTools::s_StdoutCallback;
|
|
|
|
cmSystemTools::OutputCallback cmSystemTools::s_StderrCallback;
|
2014-05-15 17:40:03 +04:00
|
|
|
cmSystemTools::InterruptCallback cmSystemTools::s_InterruptCallback;
|
2014-05-15 17:38:30 +04:00
|
|
|
void* cmSystemTools::s_MessageCallbackClientData;
|
2014-05-15 17:44:25 +04:00
|
|
|
void* cmSystemTools::s_StdoutCallbackClientData;
|
|
|
|
void* cmSystemTools::s_StderrCallbackClientData;
|
2014-05-15 17:40:03 +04:00
|
|
|
void* cmSystemTools::s_InterruptCallbackClientData;
|
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[^]]*)\\]");
|
2011-07-28 18:23:13 +04:00
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
// check for black line or comment
|
2016-05-16 17:34:04 +03:00
|
|
|
while (regEntry.find(source)) {
|
2003-06-23 16:58:58 +04:00
|
|
|
// the arguments are the second match
|
|
|
|
std::string key = regEntry.match(1);
|
|
|
|
std::string val;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (ReadRegistryValue(key.c_str(), val, view)) {
|
2003-06-23 16:58:58 +04:00
|
|
|
std::string reg = "[";
|
|
|
|
reg += key + "]";
|
|
|
|
cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
|
2016-05-16 17:34:04 +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
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2001-02-16 19:34:23 +03:00
|
|
|
}
|
2001-03-08 18:30:18 +03:00
|
|
|
#else
|
2016-08-17 02:49:57 +03:00
|
|
|
void cmSystemTools::ExpandRegistryValues(std::string& source,
|
|
|
|
KeyWOW64 /*unused*/)
|
2003-06-23 16:58:58 +04:00
|
|
|
{
|
2008-04-09 00:06:44 +04:00
|
|
|
cmsys::RegularExpression regEntry("\\[(HKEY[^]]*)\\]");
|
2016-05-16 17:34:04 +03:00
|
|
|
while (regEntry.find(source)) {
|
2008-04-09 00:06:44 +04:00
|
|
|
// 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");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
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
|
|
|
|
2014-02-08 09:29:59 +04:00
|
|
|
std::string cmSystemTools::EscapeQuotes(const std::string& str)
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2014-02-08 09:29:59 +04:00
|
|
|
std::string result;
|
|
|
|
result.reserve(str.size());
|
2016-05-16 17:34:04 +03:00
|
|
|
for (const char* ch = str.c_str(); *ch != '\0'; ++ch) {
|
|
|
|
if (*ch == '"') {
|
2003-06-23 16:58:58 +04:00
|
|
|
result += '\\';
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
result += *ch;
|
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
return result;
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
|
|
|
|
2013-09-16 19:29:06 +04:00
|
|
|
std::string cmSystemTools::HelpFileName(std::string name)
|
|
|
|
{
|
|
|
|
cmSystemTools::ReplaceString(name, "<", "");
|
|
|
|
cmSystemTools::ReplaceString(name, ">", "");
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2011-11-11 09:00:35 +04:00
|
|
|
std::string cmSystemTools::TrimWhitespace(const std::string& s)
|
|
|
|
{
|
|
|
|
std::string::const_iterator start = s.begin();
|
2016-06-10 19:36:24 +03:00
|
|
|
while (start != s.end() && cm_isspace(*start)) {
|
2011-11-11 09:00:35 +04:00
|
|
|
++start;
|
2016-06-10 19:36:24 +03:00
|
|
|
}
|
|
|
|
if (start == s.end()) {
|
2011-11-11 09:00:35 +04:00
|
|
|
return "";
|
2016-06-10 19:36:24 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string::const_iterator stop = s.end() - 1;
|
2016-06-10 19:36:24 +03:00
|
|
|
while (cm_isspace(*stop)) {
|
2011-11-11 09:00:35 +04:00
|
|
|
--stop;
|
2016-06-10 19:36:24 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return std::string(start, stop + 1);
|
2011-11-11 09:00:35 +04:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmSystemTools::Error(const char* m1, const char* m2, const char* m3,
|
|
|
|
const char* m4)
|
2003-06-23 16:58:58 +04:00
|
|
|
{
|
|
|
|
std::string message = "CMake Error: ";
|
2016-05-16 17:34:04 +03:00
|
|
|
if (m1) {
|
2003-06-23 16:58:58 +04:00
|
|
|
message += m1;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (m2) {
|
2003-06-23 16:58:58 +04:00
|
|
|
message += m2;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (m3) {
|
2003-06-23 16:58:58 +04:00
|
|
|
message += m3;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (m4) {
|
2003-06-23 16:58:58 +04:00
|
|
|
message += m4;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
cmSystemTools::s_ErrorOccured = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
cmSystemTools::Message(message.c_str(), "Error");
|
2003-06-23 16:58:58 +04:00
|
|
|
}
|
|
|
|
|
2012-01-02 22:07:43 +04:00
|
|
|
void cmSystemTools::SetInterruptCallback(InterruptCallback f, void* clientData)
|
|
|
|
{
|
|
|
|
s_InterruptCallback = f;
|
|
|
|
s_InterruptCallbackClientData = clientData;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmSystemTools::GetInterruptFlag()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (s_InterruptCallback) {
|
2012-01-02 22:07:43 +04:00
|
|
|
return (*s_InterruptCallback)(s_InterruptCallbackClientData);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-01-02 22:07:43 +04:00
|
|
|
return false;
|
|
|
|
}
|
2003-06-23 16:58:58 +04:00
|
|
|
|
2014-05-15 17:38:30 +04:00
|
|
|
void cmSystemTools::SetMessageCallback(MessageCallback f, void* clientData)
|
2003-06-23 16:58:58 +04:00
|
|
|
{
|
2014-05-15 17:38:30 +04:00
|
|
|
s_MessageCallback = f;
|
|
|
|
s_MessageCallbackClientData = clientData;
|
2003-06-23 16:58:58 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 17:44:25 +04:00
|
|
|
void cmSystemTools::SetStdoutCallback(OutputCallback f, void* clientData)
|
2004-01-07 19:24:22 +03:00
|
|
|
{
|
|
|
|
s_StdoutCallback = f;
|
|
|
|
s_StdoutCallbackClientData = clientData;
|
|
|
|
}
|
|
|
|
|
2014-05-15 17:44:25 +04:00
|
|
|
void cmSystemTools::SetStderrCallback(OutputCallback f, void* clientData)
|
|
|
|
{
|
|
|
|
s_StderrCallback = f;
|
|
|
|
s_StderrCallbackClientData = clientData;
|
|
|
|
}
|
|
|
|
|
2004-01-07 19:24:22 +03:00
|
|
|
void cmSystemTools::Stdout(const char* s)
|
|
|
|
{
|
2014-05-15 17:44:25 +04:00
|
|
|
cmSystemTools::Stdout(s, strlen(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmSystemTools::Stderr(const char* s)
|
|
|
|
{
|
|
|
|
cmSystemTools::Stderr(s, strlen(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmSystemTools::Stderr(const char* s, size_t length)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (s_StderrCallback) {
|
2014-05-15 17:44:25 +04:00
|
|
|
(*s_StderrCallback)(s, length, s_StderrCallbackClientData);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2011-07-26 11:36:40 +04:00
|
|
|
std::cerr.write(s, length);
|
|
|
|
std::cerr.flush();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-07-26 11:36:40 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 17:44:25 +04:00
|
|
|
void cmSystemTools::Stdout(const char* s, size_t length)
|
2004-01-07 19:24:22 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (s_StdoutCallback) {
|
2004-01-07 19:24:22 +03:00
|
|
|
(*s_StdoutCallback)(s, length, s_StdoutCallbackClientData);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2004-01-07 19:24:22 +03:00
|
|
|
std::cout.write(s, length);
|
2004-01-21 23:55:47 +03:00
|
|
|
std::cout.flush();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2004-01-07 19:24:22 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmSystemTools::Message(const char* m1, const char* title)
|
2003-06-23 16:58:58 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (s_DisableMessages) {
|
2003-06-23 16:58:58 +04:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (s_MessageCallback) {
|
2014-05-15 17:38:30 +04:00
|
|
|
(*s_MessageCallback)(m1, title, s_DisableMessages,
|
|
|
|
s_MessageCallbackClientData);
|
2003-06-23 16:58:58 +04:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-08-18 21:36:29 +03:00
|
|
|
std::cerr << m1 << std::endl << std::flush;
|
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());
|
|
|
|
}
|
|
|
|
|
2010-08-23 18:14:40 +04:00
|
|
|
bool cmSystemTools::IsInternallyOn(const char* val)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!val) {
|
2010-08-23 18:14:40 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-08-23 18:14:40 +04:00
|
|
|
std::basic_string<char> v = val;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (v.size() > 4) {
|
2015-01-28 01:50:42 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-08-23 18:14:40 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::basic_string<char>::iterator c = v.begin(); c != v.end(); c++) {
|
2010-08-23 18:14:40 +04:00
|
|
|
*c = static_cast<char>(toupper(*c));
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-01-28 01:52:50 +03:00
|
|
|
return v == "I_ON";
|
2010-08-23 18:14:40 +04:00
|
|
|
}
|
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
bool cmSystemTools::IsOn(const char* val)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!val) {
|
2002-04-01 22:34:38 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-02-08 14:42:14 +04:00
|
|
|
size_t len = strlen(val);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (len > 4) {
|
2014-02-08 14:42:14 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-02-08 14:42:14 +04:00
|
|
|
std::basic_string<char> v(val, len);
|
2011-07-28 18:23:13 +04:00
|
|
|
|
2014-02-08 14:42:14 +04:00
|
|
|
static std::set<std::string> onValues;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (onValues.empty()) {
|
2014-02-08 14:42:14 +04:00
|
|
|
onValues.insert("ON");
|
|
|
|
onValues.insert("1");
|
|
|
|
onValues.insert("YES");
|
|
|
|
onValues.insert("TRUE");
|
|
|
|
onValues.insert("Y");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
for (std::basic_string<char>::iterator c = v.begin(); c != v.end(); c++) {
|
2009-10-02 00:47:08 +04:00
|
|
|
*c = static_cast<char>(toupper(*c));
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-02-08 14:42:14 +04:00
|
|
|
return (onValues.count(v) > 0);
|
2003-06-23 16:58:58 +04:00
|
|
|
}
|
2002-04-16 21:48:35 +04:00
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
bool cmSystemTools::IsNOTFOUND(const char* val)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (strcmp(val, "NOTFOUND") == 0) {
|
2013-12-30 19:09:46 +04:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-12-30 19:09:46 +04:00
|
|
|
return cmHasLiteralSuffix(val, "-NOTFOUND");
|
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
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!val || !*val) {
|
2003-06-23 16:58:58 +04:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-02-08 14:42:14 +04:00
|
|
|
size_t len = strlen(val);
|
|
|
|
// Try and avoid toupper() for large strings.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (len > 6) {
|
2014-02-08 14:42:14 +04:00
|
|
|
return cmSystemTools::IsNOTFOUND(val);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-07-28 18:23:13 +04:00
|
|
|
|
2014-02-08 14:42:14 +04:00
|
|
|
static std::set<std::string> offValues;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (offValues.empty()) {
|
2014-02-08 14:42:14 +04:00
|
|
|
offValues.insert("OFF");
|
|
|
|
offValues.insert("0");
|
|
|
|
offValues.insert("NO");
|
|
|
|
offValues.insert("FALSE");
|
|
|
|
offValues.insert("N");
|
|
|
|
offValues.insert("IGNORE");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-02-08 14:42:14 +04:00
|
|
|
// Try and avoid toupper().
|
|
|
|
std::basic_string<char> v(val, len);
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::basic_string<char>::iterator c = v.begin(); c != v.end(); c++) {
|
2009-10-02 00:47:08 +04:00
|
|
|
*c = static_cast<char>(toupper(*c));
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-02-08 14:42:14 +04:00
|
|
|
return (offValues.count(v) > 0);
|
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;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (const char* c = command; *c; ++c) {
|
|
|
|
if (*c == '\\') {
|
2007-02-01 19:45:37 +03:00
|
|
|
++backslashes;
|
|
|
|
in_argument = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (*c == '"') {
|
|
|
|
int backslash_pairs = backslashes >> 1;
|
2007-02-01 19:45:37 +03:00
|
|
|
int backslash_escaped = backslashes & 1;
|
|
|
|
arg.append(backslash_pairs, '\\');
|
|
|
|
backslashes = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (backslash_escaped) {
|
2007-02-01 19:45:37 +03:00
|
|
|
/* An odd number of backslashes precede this quote.
|
|
|
|
It is escaped. */
|
|
|
|
arg.append(1, '"');
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2007-02-01 19:45:37 +03:00
|
|
|
/* An even number of backslashes precede this quote.
|
|
|
|
It is not escaped. */
|
|
|
|
in_quotes = !in_quotes;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
in_argument = true;
|
|
|
|
} else {
|
2007-02-01 19:45:37 +03:00
|
|
|
arg.append(backslashes, '\\');
|
|
|
|
backslashes = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cm_isspace(*c)) {
|
|
|
|
if (in_quotes) {
|
2007-02-01 19:45:37 +03:00
|
|
|
arg.append(1, *c);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (in_argument) {
|
2007-02-01 19:45:37 +03:00
|
|
|
args.push_back(arg);
|
|
|
|
arg = "";
|
|
|
|
in_argument = false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2007-02-01 19:45:37 +03:00
|
|
|
in_argument = true;
|
|
|
|
arg.append(1, *c);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-02-01 19:45:37 +03:00
|
|
|
arg.append(backslashes, '\\');
|
2016-05-16 17:34:04 +03:00
|
|
|
if (in_argument) {
|
2007-02-01 19:45:37 +03:00
|
|
|
args.push_back(arg);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-02-01 19:45:37 +03:00
|
|
|
}
|
|
|
|
|
2009-07-14 00:22:48 +04:00
|
|
|
class cmSystemToolsArgV
|
|
|
|
{
|
|
|
|
char** ArgV;
|
2016-05-16 17:34:04 +03:00
|
|
|
|
2009-07-14 00:22:48 +04:00
|
|
|
public:
|
2016-05-16 17:34:04 +03:00
|
|
|
cmSystemToolsArgV(char** argv)
|
|
|
|
: ArgV(argv)
|
|
|
|
{
|
|
|
|
}
|
2009-07-14 00:22:48 +04:00
|
|
|
~cmSystemToolsArgV()
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
for (char** arg = this->ArgV; arg && *arg; ++arg) {
|
2009-07-14 00:22:48 +04:00
|
|
|
free(*arg);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
free(this->ArgV);
|
|
|
|
}
|
2009-07-14 00:22:48 +04:00
|
|
|
void Store(std::vector<std::string>& args) const
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
for (char** arg = this->ArgV; arg && *arg; ++arg) {
|
2009-07-14 00:22:48 +04:00
|
|
|
args.push_back(*arg);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-07-14 00:22:48 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
void cmSystemTools::ParseUnixCommandLine(const char* command,
|
|
|
|
std::vector<std::string>& args)
|
|
|
|
{
|
|
|
|
// Invoke the underlying parser.
|
|
|
|
cmSystemToolsArgV argv = cmsysSystem_Parse_CommandForUnix(command, 0);
|
|
|
|
argv.Store(args);
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:21:34 +04:00
|
|
|
std::vector<std::string> cmSystemTools::ParseArguments(const char* command)
|
2003-08-04 06:32:15 +04:00
|
|
|
{
|
2014-02-10 09:21:34 +04:00
|
|
|
std::vector<std::string> 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;
|
|
|
|
|
2009-09-11 16:18:15 +04:00
|
|
|
if ((command[0] != '/' && command[1] == ':' && command[2] == '\\') ||
|
2016-05-16 17:34:04 +03:00
|
|
|
(command[0] == '\"' && command[1] != '/' && command[2] == ':' &&
|
|
|
|
command[3] == '\\') ||
|
|
|
|
(command[0] == '\'' && command[1] != '/' && command[2] == ':' &&
|
|
|
|
command[3] == '\\') ||
|
|
|
|
(command[0] == '\\' && command[1] == '\\')) {
|
2003-08-04 19:48:18 +04:00
|
|
|
win_path = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2003-08-04 06:32:15 +04:00
|
|
|
// Split the command into an argv array.
|
2016-05-16 17:34:04 +03:00
|
|
|
for (const char* c = command; *c;) {
|
2003-08-04 06:32:15 +04:00
|
|
|
// Skip over whitespace.
|
2016-05-16 17:34:04 +03:00
|
|
|
while (*c == ' ' || *c == '\t') {
|
2003-08-04 06:32:15 +04:00
|
|
|
++c;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2003-08-04 06:32:15 +04:00
|
|
|
arg = "";
|
2016-05-16 17:34:04 +03:00
|
|
|
if (*c == '"') {
|
2003-08-04 06:32:15 +04:00
|
|
|
// Parse a quoted argument.
|
|
|
|
++c;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (*c && *c != '"') {
|
2003-08-04 15:55:40 +04:00
|
|
|
arg.append(1, *c);
|
|
|
|
++c;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (*c) {
|
2003-08-04 06:32:15 +04:00
|
|
|
++c;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
args.push_back(arg);
|
|
|
|
} else if (*c == '\'') {
|
2005-02-17 18:51:18 +03:00
|
|
|
// Parse a quoted argument.
|
|
|
|
++c;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (*c && *c != '\'') {
|
2005-02-17 18:51:18 +03:00
|
|
|
arg.append(1, *c);
|
|
|
|
++c;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (*c) {
|
2005-02-17 18:51:18 +03:00
|
|
|
++c;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
args.push_back(arg);
|
|
|
|
} else if (*c) {
|
2003-08-04 06:32:15 +04:00
|
|
|
// Parse an unquoted argument.
|
2016-05-16 17:34:04 +03:00
|
|
|
while (*c && *c != ' ' && *c != '\t') {
|
|
|
|
if (*c == '\\' && !win_path) {
|
2003-08-04 15:12:42 +04:00
|
|
|
++c;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (*c) {
|
2003-08-04 15:12:42 +04:00
|
|
|
arg.append(1, *c);
|
|
|
|
++c;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2003-08-04 15:12:42 +04:00
|
|
|
arg.append(1, *c);
|
|
|
|
++c;
|
2003-08-04 06:32:15 +04:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
args.push_back(arg);
|
2003-08-04 06:32:15 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-07-28 18:23:13 +04:00
|
|
|
|
2003-08-08 03:00:53 +04:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmSystemTools::RunSingleCommand(std::vector<std::string> const& command,
|
2015-04-20 22:36:57 +03:00
|
|
|
std::string* captureStdOut,
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string* captureStdErr, int* retVal,
|
|
|
|
const char* dir, OutputOption outputflag,
|
|
|
|
double timeout)
|
2008-01-01 23:13:41 +03:00
|
|
|
{
|
2004-10-25 21:16:05 +04:00
|
|
|
std::vector<const char*> argv;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::const_iterator a = command.begin();
|
|
|
|
a != command.end(); ++a) {
|
2003-08-08 03:00:53 +04:00
|
|
|
argv.push_back(a->c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-06-27 23:44:16 +03:00
|
|
|
argv.push_back(CM_NULLPTR);
|
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);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmSystemTools::GetRunCommandHideConsole()) {
|
2003-08-13 01:18:16 +04:00
|
|
|
cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-07-26 11:26:18 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (outputflag == OUTPUT_PASSTHROUGH) {
|
2013-07-29 10:52:54 +04:00
|
|
|
cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDOUT, 1);
|
|
|
|
cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDERR, 1);
|
2016-06-27 23:44:16 +03:00
|
|
|
captureStdOut = CM_NULLPTR;
|
|
|
|
captureStdErr = CM_NULLPTR;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (outputflag == OUTPUT_MERGE ||
|
|
|
|
(captureStdErr && captureStdErr == captureStdOut)) {
|
2016-01-15 00:11:23 +03:00
|
|
|
cmsysProcess_SetOption(cp, cmsysProcess_Option_MergeOutput, 1);
|
2016-06-27 23:44:16 +03:00
|
|
|
captureStdErr = CM_NULLPTR;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-01-15 00:11:23 +03:00
|
|
|
assert(!captureStdErr || captureStdErr != captureStdOut);
|
2013-07-29 10:52:54 +04:00
|
|
|
|
2003-08-04 06:32:15 +04:00
|
|
|
cmsysProcess_SetTimeout(cp, timeout);
|
|
|
|
cmsysProcess_Execute(cp);
|
2011-07-28 18:23:13 +04:00
|
|
|
|
2015-04-20 22:36:57 +03:00
|
|
|
std::vector<char> tempStdOut;
|
|
|
|
std::vector<char> tempStdErr;
|
2003-08-04 06:32:15 +04:00
|
|
|
char* data;
|
|
|
|
int length;
|
2011-07-26 11:26:18 +04:00
|
|
|
int pipe;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (outputflag != OUTPUT_PASSTHROUGH &&
|
|
|
|
(captureStdOut || captureStdErr || outputflag != OUTPUT_NONE)) {
|
2016-06-27 23:44:16 +03:00
|
|
|
while ((pipe = cmsysProcess_WaitForData(cp, &data, &length, CM_NULLPTR)) >
|
|
|
|
0) {
|
2016-01-15 00:14:25 +03:00
|
|
|
// Translate NULL characters in the output into valid text.
|
|
|
|
// Visual Studio 7 puts these characters in the output of its
|
|
|
|
// build process.
|
2016-05-16 17:34:04 +03:00
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
if (data[i] == '\0') {
|
2016-01-15 00:14:25 +03:00
|
|
|
data[i] = ' ';
|
2005-08-18 01:39:59 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-01-15 00:14:25 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (pipe == cmsysProcess_Pipe_STDOUT) {
|
|
|
|
if (outputflag != OUTPUT_NONE) {
|
2016-01-15 00:11:23 +03:00
|
|
|
cmSystemTools::Stdout(data, length);
|
2011-07-28 18:28:52 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
if (captureStdOut) {
|
|
|
|
tempStdOut.insert(tempStdOut.end(), data, data + length);
|
|
|
|
}
|
|
|
|
} else if (pipe == cmsysProcess_Pipe_STDERR) {
|
|
|
|
if (outputflag != OUTPUT_NONE) {
|
2016-01-15 00:11:23 +03:00
|
|
|
cmSystemTools::Stderr(data, length);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (captureStdErr) {
|
|
|
|
tempStdErr.insert(tempStdErr.end(), data, data + length);
|
2011-07-28 18:28:52 +04:00
|
|
|
}
|
2005-08-18 01:39:59 +04:00
|
|
|
}
|
2004-01-26 22:41:29 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-07-28 18:23:13 +04:00
|
|
|
|
2016-06-27 23:44:16 +03:00
|
|
|
cmsysProcess_WaitForExit(cp, CM_NULLPTR);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (captureStdOut) {
|
2016-01-15 00:11:00 +03:00
|
|
|
captureStdOut->assign(tempStdOut.begin(), tempStdOut.end());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (captureStdErr) {
|
2016-01-15 00:11:00 +03:00
|
|
|
captureStdErr->assign(tempStdErr.begin(), tempStdErr.end());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-07-28 18:23:13 +04:00
|
|
|
|
2003-08-04 06:32:15 +04:00
|
|
|
bool result = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmsysProcess_GetState(cp) == cmsysProcess_State_Exited) {
|
|
|
|
if (retVal) {
|
2003-08-04 06:32:15 +04:00
|
|
|
*retVal = cmsysProcess_GetExitValue(cp);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
|
|
|
if (cmsysProcess_GetExitValue(cp) != 0) {
|
2003-08-04 06:32:15 +04:00
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +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);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (outputflag != OUTPUT_NONE) {
|
2004-02-24 18:04:02 +03:00
|
|
|
std::cerr << exception_str << std::endl;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (captureStdErr) {
|
2015-04-20 22:36:57 +03:00
|
|
|
captureStdErr->append(exception_str, strlen(exception_str));
|
2004-01-15 16:50:37 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
result = false;
|
|
|
|
} else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Error) {
|
2004-02-24 18:04:02 +03:00
|
|
|
const char* error_str = cmsysProcess_GetErrorString(cp);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (outputflag != OUTPUT_NONE) {
|
2004-02-24 18:04:02 +03:00
|
|
|
std::cerr << error_str << std::endl;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (captureStdErr) {
|
2015-04-20 22:36:57 +03:00
|
|
|
captureStdErr->append(error_str, strlen(error_str));
|
2003-08-04 06:32:15 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
result = false;
|
|
|
|
} 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";
|
2016-05-16 17:34:04 +03:00
|
|
|
if (outputflag != OUTPUT_NONE) {
|
2004-02-24 18:04:02 +03:00
|
|
|
std::cerr << error_str << std::endl;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (captureStdErr) {
|
2015-04-20 22:36:57 +03:00
|
|
|
captureStdErr->append(error_str, strlen(error_str));
|
2004-01-15 16:50:37 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
result = false;
|
|
|
|
}
|
2011-07-28 18:23:13 +04:00
|
|
|
|
2003-08-04 06:32:15 +04:00
|
|
|
cmsysProcess_Delete(cp);
|
|
|
|
return result;
|
|
|
|
}
|
2008-01-01 23:13:41 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmSystemTools::RunSingleCommand(const char* command,
|
|
|
|
std::string* captureStdOut,
|
|
|
|
std::string* captureStdErr, int* retVal,
|
|
|
|
const char* dir, OutputOption outputflag,
|
|
|
|
double timeout)
|
2008-01-01 23:13:41 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (s_DisableRunCommandOutput) {
|
2011-07-26 11:26:18 +04:00
|
|
|
outputflag = OUTPUT_NONE;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-01-01 23:13:41 +03:00
|
|
|
|
2014-02-10 09:21:34 +04:00
|
|
|
std::vector<std::string> args = cmSystemTools::ParseArguments(command);
|
2008-01-01 23:13:41 +03:00
|
|
|
|
2016-05-27 00:05:30 +03:00
|
|
|
if (args.empty()) {
|
2008-01-01 23:13:41 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-20 22:36:57 +03:00
|
|
|
return cmSystemTools::RunSingleCommand(args, captureStdOut, captureStdErr,
|
|
|
|
retVal, dir, outputflag, timeout);
|
2008-01-01 23:13:41 +03:00
|
|
|
}
|
2001-05-25 05:17:02 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string cmSystemTools::PrintSingleCommand(
|
|
|
|
std::vector<std::string> const& command)
|
2013-11-15 22:31:00 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (command.empty()) {
|
2015-01-07 10:58:51 +03:00
|
|
|
return std::string();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-01-07 10:58:51 +03:00
|
|
|
|
2015-02-19 01:50:36 +03:00
|
|
|
return cmWrap('"', command, '"', " ");
|
2013-11-15 22:31:00 +04:00
|
|
|
}
|
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
bool cmSystemTools::DoesFileExistWithExtensions(
|
2016-05-16 17:34:04 +03:00
|
|
|
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;
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::const_iterator ext = headerExts.begin();
|
|
|
|
ext != headerExts.end(); ++ext) {
|
2003-06-23 16:58:58 +04:00
|
|
|
hname = name;
|
|
|
|
hname += ".";
|
|
|
|
hname += *ext;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmSystemTools::FileExists(hname.c_str())) {
|
2003-06-23 16:58:58 +04:00
|
|
|
return true;
|
2002-07-15 17:45:47 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03: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,
|
2016-05-16 17:34:04 +03:00
|
|
|
const char* directory,
|
|
|
|
const char* toplevel)
|
2009-04-21 19:36:59 +04:00
|
|
|
{
|
|
|
|
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;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (dir != prevDir) {
|
2009-04-21 19:36:59 +04:00
|
|
|
std::string path = dir + "/" + file;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmSystemTools::FileExists(path.c_str())) {
|
2009-04-21 19:36:59 +04:00
|
|
|
return path;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (dir.size() < strlen(toplevel)) {
|
2009-04-21 19:36:59 +04:00
|
|
|
break;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-04-21 19:37:09 +04:00
|
|
|
prevDir = dir;
|
2014-10-15 16:54:05 +04:00
|
|
|
dir = cmSystemTools::GetParentDirectory(dir);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2011-07-28 18:23:13 +04:00
|
|
|
bool cmSystemTools::CopyFileIfDifferent(const char* source,
|
2016-05-16 17:34:04 +03:00
|
|
|
const char* destination)
|
2004-01-26 23:50:38 +03:00
|
|
|
{
|
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
|
|
|
|
2014-02-12 18:45:56 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
cmSystemTools::WindowsFileRetry cmSystemTools::GetWindowsFileRetry()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
static WindowsFileRetry retry = { 0, 0 };
|
|
|
|
if (!retry.Count) {
|
|
|
|
unsigned int data[2] = { 0, 0 };
|
|
|
|
HKEY const keys[2] = { HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE };
|
|
|
|
wchar_t const* const values[2] = { L"FilesystemRetryCount",
|
|
|
|
L"FilesystemRetryDelay" };
|
|
|
|
for (int k = 0; k < 2; ++k) {
|
2014-02-12 18:45:56 +04:00
|
|
|
HKEY hKey;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (RegOpenKeyExW(keys[k], L"Software\\Kitware\\CMake\\Config", 0,
|
|
|
|
KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
|
|
|
|
for (int v = 0; v < 2; ++v) {
|
2014-02-12 18:45:56 +04:00
|
|
|
DWORD dwData, dwType, dwSize = 4;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!data[v] &&
|
|
|
|
RegQueryValueExW(hKey, values[v], 0, &dwType, (BYTE*)&dwData,
|
|
|
|
&dwSize) == ERROR_SUCCESS &&
|
|
|
|
dwType == REG_DWORD && dwSize == 4) {
|
2014-02-12 18:45:56 +04:00
|
|
|
data[v] = static_cast<unsigned int>(dwData);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
RegCloseKey(hKey);
|
2014-02-12 18:45:56 +04:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
retry.Count = data[0] ? data[0] : 5;
|
|
|
|
retry.Delay = data[1] ? data[1] : 500;
|
|
|
|
}
|
2014-02-12 18:45:56 +04:00
|
|
|
return retry;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-04-15 17:57:57 +04:00
|
|
|
bool cmSystemTools::RenameFile(const char* oldname, const char* newname)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2016-05-16 17:34:04 +03:00
|
|
|
#ifndef INVALID_FILE_ATTRIBUTES
|
|
|
|
#define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
|
|
|
|
#endif
|
2013-01-31 20:42:17 +04:00
|
|
|
/* Windows MoveFileEx may not replace read-only or in-use files. If it
|
|
|
|
fails then remove the read-only attribute from any existing destination.
|
|
|
|
Try multiple times since we may be racing against another process
|
|
|
|
creating/opening the destination file just before our MoveFileEx. */
|
2014-02-12 18:45:56 +04:00
|
|
|
WindowsFileRetry retry = cmSystemTools::GetWindowsFileRetry();
|
2016-05-16 17:34:04 +03:00
|
|
|
while (
|
|
|
|
!MoveFileExW(SystemTools::ConvertToWindowsExtendedPath(oldname).c_str(),
|
|
|
|
SystemTools::ConvertToWindowsExtendedPath(newname).c_str(),
|
|
|
|
MOVEFILE_REPLACE_EXISTING) &&
|
|
|
|
--retry.Count) {
|
2014-05-13 18:10:38 +04:00
|
|
|
DWORD last_error = GetLastError();
|
|
|
|
// Try again only if failure was due to access/sharing permissions.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (last_error != ERROR_ACCESS_DENIED &&
|
|
|
|
last_error != ERROR_SHARING_VIOLATION) {
|
2013-01-31 20:42:17 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
DWORD attrs = GetFileAttributesW(
|
|
|
|
SystemTools::ConvertToWindowsExtendedPath(newname).c_str());
|
|
|
|
if ((attrs != INVALID_FILE_ATTRIBUTES) &&
|
|
|
|
(attrs & FILE_ATTRIBUTE_READONLY)) {
|
2013-01-31 20:42:17 +04:00
|
|
|
// Remove the read-only attribute from the destination file.
|
2015-08-15 08:14:06 +03:00
|
|
|
SetFileAttributesW(
|
|
|
|
SystemTools::ConvertToWindowsExtendedPath(newname).c_str(),
|
2016-05-16 17:34:04 +03:00
|
|
|
attrs & ~FILE_ATTRIBUTE_READONLY);
|
|
|
|
} else {
|
2013-01-31 20:42:17 +04:00
|
|
|
// The file may be temporarily in use so wait a bit.
|
2014-02-12 18:45:56 +04:00
|
|
|
cmSystemTools::Delay(retry.Delay);
|
2009-04-15 17:57:57 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-02-12 18:45:56 +04:00
|
|
|
return retry.Count > 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
|
|
|
|
}
|
|
|
|
|
2014-02-08 00:06:13 +04:00
|
|
|
bool cmSystemTools::ComputeFileMD5(const std::string& source, char* md5out)
|
2007-07-16 18:54:32 +04:00
|
|
|
{
|
2007-07-16 23:10:40 +04:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2011-11-16 04:37:38 +04:00
|
|
|
cmCryptoHashMD5 md5;
|
|
|
|
std::string str = md5.HashFile(source);
|
|
|
|
strncpy(md5out, str.c_str(), 32);
|
|
|
|
return !str.empty();
|
2007-08-14 18:27:34 +04:00
|
|
|
#else
|
|
|
|
(void)source;
|
|
|
|
(void)md5out;
|
2016-05-16 17:34:04 +03:00
|
|
|
cmSystemTools::Message("md5sum not supported in bootstrapping mode",
|
|
|
|
"Error");
|
2007-07-16 23:10:40 +04:00
|
|
|
return false;
|
|
|
|
#endif
|
2007-07-16 18:54:32 +04:00
|
|
|
}
|
|
|
|
|
2014-02-08 00:06:13 +04:00
|
|
|
std::string cmSystemTools::ComputeStringMD5(const std::string& input)
|
2008-01-24 22:41:18 +03:00
|
|
|
{
|
2008-01-25 00:11:06 +03:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2011-11-16 04:37:38 +04:00
|
|
|
cmCryptoHashMD5 md5;
|
|
|
|
return md5.HashString(input);
|
2008-01-25 00:11:06 +03:00
|
|
|
#else
|
|
|
|
(void)input;
|
2016-05-16 17:34:04 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-08-22 00:15:38 +03:00
|
|
|
std::string cmSystemTools::ComputeCertificateThumbprint(
|
|
|
|
const std::string& source)
|
|
|
|
{
|
|
|
|
std::string thumbprint;
|
|
|
|
|
2015-09-02 16:23:32 +03:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE) && defined(_WIN32)
|
2015-08-22 00:15:38 +03:00
|
|
|
BYTE* certData = NULL;
|
|
|
|
CRYPT_INTEGER_BLOB cryptBlob;
|
|
|
|
HCERTSTORE certStore = NULL;
|
|
|
|
PCCERT_CONTEXT certContext = NULL;
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
HANDLE certFile = CreateFileW(
|
|
|
|
cmsys::Encoding::ToWide(source.c_str()).c_str(), GENERIC_READ,
|
|
|
|
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
|
|
|
if (certFile != INVALID_HANDLE_VALUE && certFile != NULL) {
|
2015-08-22 00:15:38 +03:00
|
|
|
DWORD fileSize = GetFileSize(certFile, NULL);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (fileSize != INVALID_FILE_SIZE) {
|
2015-08-22 00:15:38 +03:00
|
|
|
certData = new BYTE[fileSize];
|
2016-05-16 17:34:04 +03:00
|
|
|
if (certData != NULL) {
|
2015-08-22 00:15:38 +03:00
|
|
|
DWORD dwRead = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (ReadFile(certFile, certData, fileSize, &dwRead, NULL)) {
|
2015-08-22 00:15:38 +03:00
|
|
|
cryptBlob.cbData = fileSize;
|
|
|
|
cryptBlob.pbData = certData;
|
|
|
|
|
|
|
|
// Verify that this is a valid cert
|
2016-05-16 17:34:04 +03:00
|
|
|
if (PFXIsPFXBlob(&cryptBlob)) {
|
2015-08-22 00:15:38 +03:00
|
|
|
// Open the certificate as a store
|
2016-05-16 17:34:04 +03:00
|
|
|
certStore = PFXImportCertStore(&cryptBlob, NULL, CRYPT_EXPORTABLE);
|
|
|
|
if (certStore != NULL) {
|
2015-08-22 00:15:38 +03:00
|
|
|
// There should only be 1 cert.
|
2016-05-16 17:34:04 +03:00
|
|
|
certContext =
|
|
|
|
CertEnumCertificatesInStore(certStore, certContext);
|
|
|
|
if (certContext != NULL) {
|
2015-08-22 00:15:38 +03:00
|
|
|
// The hash is 20 bytes
|
|
|
|
BYTE hashData[20];
|
|
|
|
DWORD hashLength = 20;
|
|
|
|
|
|
|
|
// Buffer to print the hash. Each byte takes 2 chars +
|
|
|
|
// terminating character
|
|
|
|
char hashPrint[41];
|
2016-05-16 17:34:04 +03:00
|
|
|
char* pHashPrint = hashPrint;
|
2015-08-22 00:15:38 +03:00
|
|
|
// Get the hash property from the certificate
|
2016-05-16 17:34:04 +03:00
|
|
|
if (CertGetCertificateContextProperty(
|
|
|
|
certContext, CERT_HASH_PROP_ID, hashData, &hashLength)) {
|
|
|
|
for (DWORD i = 0; i < hashLength; i++) {
|
2015-08-22 00:15:38 +03:00
|
|
|
// Convert each byte to hexadecimal
|
|
|
|
sprintf(pHashPrint, "%02X", hashData[i]);
|
|
|
|
pHashPrint += 2;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-08-22 00:15:38 +03:00
|
|
|
*pHashPrint = '\0';
|
|
|
|
thumbprint = hashPrint;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
CertFreeCertificateContext(certContext);
|
2015-08-22 00:15:38 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
CertCloseStore(certStore, 0);
|
2015-08-22 00:15:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
delete[] certData;
|
2015-08-22 00:15:38 +03:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
CloseHandle(certFile);
|
|
|
|
}
|
2015-08-22 00:15:38 +03:00
|
|
|
#else
|
|
|
|
(void)source;
|
|
|
|
cmSystemTools::Message("ComputeCertificateThumbprint is not implemented",
|
2016-05-16 17:34:04 +03:00
|
|
|
"Error");
|
2015-08-22 00:15:38 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return thumbprint;
|
|
|
|
}
|
|
|
|
|
2014-02-08 00:46:27 +04:00
|
|
|
void cmSystemTools::Glob(const std::string& directory,
|
|
|
|
const std::string& regexp,
|
2001-06-21 20:01:18 +04:00
|
|
|
std::vector<std::string>& files)
|
|
|
|
{
|
2003-06-23 16:58:58 +04:00
|
|
|
cmsys::Directory d;
|
2014-02-08 00:46:27 +04:00
|
|
|
cmsys::RegularExpression reg(regexp.c_str());
|
2011-07-28 18:23:13 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (d.Load(directory)) {
|
2002-03-13 18:25:11 +03:00
|
|
|
size_t numf;
|
2016-05-16 17:34:04 +03:00
|
|
|
unsigned int i;
|
2001-06-21 20:01:18 +04:00
|
|
|
numf = d.GetNumberOfFiles();
|
2016-05-16 17:34:04 +03:00
|
|
|
for (i = 0; i < numf; i++) {
|
2001-06-21 20:01:18 +04:00
|
|
|
std::string fname = d.GetFile(i);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (reg.find(fname)) {
|
2001-06-21 20:01:18 +04:00
|
|
|
files.push_back(fname);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2001-06-21 20:01:18 +04:00
|
|
|
}
|
|
|
|
|
2014-02-08 00:46:27 +04:00
|
|
|
void cmSystemTools::GlobDirs(const std::string& path,
|
2001-09-20 18:54:29 +04:00
|
|
|
std::vector<std::string>& files)
|
|
|
|
{
|
2001-11-13 20:38:53 +03:00
|
|
|
std::string::size_type pos = path.find("/*");
|
2016-05-16 17:34:04 +03:00
|
|
|
if (pos == std::string::npos) {
|
2014-02-08 00:46:27 +04:00
|
|
|
files.push_back(path);
|
2001-09-20 18:54:29 +04:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2001-09-20 18:54:29 +04:00
|
|
|
std::string startPath = path.substr(0, pos);
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string finishPath = path.substr(pos + 2);
|
2001-09-20 18:54:29 +04:00
|
|
|
|
2003-06-23 16:58:58 +04:00
|
|
|
cmsys::Directory d;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (d.Load(startPath)) {
|
|
|
|
for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i) {
|
|
|
|
if ((std::string(d.GetFile(i)) != ".") &&
|
|
|
|
(std::string(d.GetFile(i)) != "..")) {
|
2001-09-20 18:54:29 +04:00
|
|
|
std::string fname = startPath;
|
2016-05-16 17:34:04 +03:00
|
|
|
fname += "/";
|
2001-09-20 18:54:29 +04:00
|
|
|
fname += d.GetFile(i);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmSystemTools::FileIsDirectory(fname)) {
|
2001-09-20 18:54:29 +04:00
|
|
|
fname += finishPath;
|
2014-03-11 03:04:11 +04:00
|
|
|
cmSystemTools::GlobDirs(fname, files);
|
2001-09-20 18:54:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2001-09-20 18:54:29 +04:00
|
|
|
}
|
2002-03-06 18:10:46 +03:00
|
|
|
|
2011-07-28 18:23:13 +04:00
|
|
|
void cmSystemTools::ExpandList(std::vector<std::string> const& arguments,
|
2002-12-12 02:13:33 +03:00
|
|
|
std::vector<std::string>& newargs)
|
2002-03-06 18:10:46 +03:00
|
|
|
{
|
|
|
|
std::vector<std::string>::const_iterator i;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (i = arguments.begin(); i != arguments.end(); ++i) {
|
2002-12-12 02:13:33 +03:00
|
|
|
cmSystemTools::ExpandListArgument(*i, newargs);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-12-12 02:13:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!emptyArgs && arg.empty()) {
|
2002-12-12 02:13:33 +03:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-12-12 02:13:33 +03:00
|
|
|
// if there are no ; in the name then just copy the current string
|
2016-05-16 17:34:04 +03:00
|
|
|
if (arg.find(';') == std::string::npos) {
|
2002-12-12 02:13:33 +03:00
|
|
|
newargs.push_back(arg);
|
2003-12-09 02:05:29 +03:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-02-08 20:35:52 +04:00
|
|
|
std::string newArg;
|
2016-05-16 17:34:04 +03:00
|
|
|
const char* last = arg.c_str();
|
2003-12-09 02:05:29 +03:00
|
|
|
// Break the string at non-escaped semicolons not nested in [].
|
|
|
|
int squareNesting = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (const char* c = last; *c; ++c) {
|
|
|
|
switch (*c) {
|
|
|
|
case '\\': {
|
2003-12-09 02:05:29 +03:00
|
|
|
// We only want to allow escaping of semicolons. Other
|
|
|
|
// escapes should not be processed here.
|
2014-02-08 20:35:52 +04:00
|
|
|
const char* next = c + 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (*next == ';') {
|
2014-02-08 20:35:52 +04:00
|
|
|
newArg.append(last, c - last);
|
|
|
|
// Skip over the escape character
|
|
|
|
last = c = next;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case '[': {
|
2003-12-09 02:05:29 +03:00
|
|
|
++squareNesting;
|
2016-05-16 17:34:04 +03:00
|
|
|
} break;
|
|
|
|
case ']': {
|
2008-04-09 01:37:13 +04:00
|
|
|
--squareNesting;
|
2016-05-16 17:34:04 +03:00
|
|
|
} break;
|
|
|
|
case ';': {
|
2003-12-09 02:05:29 +03:00
|
|
|
// Break the string here if we are not nested inside square
|
|
|
|
// brackets.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (squareNesting == 0) {
|
2014-02-08 20:35:52 +04:00
|
|
|
newArg.append(last, c - last);
|
|
|
|
// Skip over the semicolon
|
|
|
|
last = c + 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!newArg.empty() || emptyArgs) {
|
2005-04-14 00:25:55 +04:00
|
|
|
// Add the last argument if the string is not empty.
|
2014-02-08 20:35:52 +04:00
|
|
|
newargs.push_back(newArg);
|
|
|
|
newArg = "";
|
2002-03-27 00:44:49 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default: {
|
2003-12-09 02:05:29 +03:00
|
|
|
// Just append this character.
|
2016-05-16 17:34:04 +03:00
|
|
|
} break;
|
2002-03-06 18:10:46 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-02-08 20:35:52 +04:00
|
|
|
newArg.append(last);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!newArg.empty() || emptyArgs) {
|
2003-12-09 02:05:29 +03:00
|
|
|
// Add the last argument if the string is not empty.
|
2014-02-08 20:35:52 +04:00
|
|
|
newargs.push_back(newArg);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-03-06 18:10:46 +03:00
|
|
|
}
|
2002-04-04 01:14:06 +04:00
|
|
|
|
2014-02-10 09:21:34 +04:00
|
|
|
bool cmSystemTools::SimpleGlob(const std::string& glob,
|
|
|
|
std::vector<std::string>& files,
|
2002-11-05 16:51:40 +03:00
|
|
|
int type /* = 0 */)
|
|
|
|
{
|
2002-12-09 06:35:19 +03:00
|
|
|
files.clear();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (glob[glob.size() - 1] != '*') {
|
2002-11-05 16:51:40 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-11-05 16:51:40 +03:00
|
|
|
std::string path = cmSystemTools::GetFilenamePath(glob);
|
|
|
|
std::string ppath = cmSystemTools::GetFilenameName(glob);
|
2016-05-16 17:34:04 +03:00
|
|
|
ppath = ppath.substr(0, ppath.size() - 1);
|
|
|
|
if (path.empty()) {
|
2002-11-05 16:51:40 +03:00
|
|
|
path = "/";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-11-05 16:51:40 +03:00
|
|
|
|
2002-11-06 19:37:00 +03:00
|
|
|
bool res = false;
|
2003-06-23 16:58:58 +04:00
|
|
|
cmsys::Directory d;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (d.Load(path)) {
|
|
|
|
for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i) {
|
|
|
|
if ((std::string(d.GetFile(i)) != ".") &&
|
|
|
|
(std::string(d.GetFile(i)) != "..")) {
|
2002-11-05 16:51:40 +03:00
|
|
|
std::string fname = path;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (path[path.size() - 1] != '/') {
|
|
|
|
fname += "/";
|
|
|
|
}
|
2002-11-05 16:51:40 +03:00
|
|
|
fname += d.GetFile(i);
|
|
|
|
std::string sfname = d.GetFile(i);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (type > 0 && cmSystemTools::FileIsDirectory(fname)) {
|
2002-11-05 16:51:40 +03:00
|
|
|
continue;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (type < 0 && !cmSystemTools::FileIsDirectory(fname)) {
|
2002-11-05 16:51:40 +03:00
|
|
|
continue;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (sfname.size() >= ppath.size() &&
|
|
|
|
sfname.substr(0, ppath.size()) == ppath) {
|
2002-11-05 16:51:40 +03:00
|
|
|
files.push_back(fname);
|
|
|
|
res = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-11-05 16:51:40 +03:00
|
|
|
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
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!cext || *cext == 0) {
|
2002-09-25 17:30:49 +04:00
|
|
|
return cmSystemTools::NO_FILE_FORMAT;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
// std::string ext = cmSystemTools::LowerCase(cext);
|
2003-04-03 17:44:49 +04:00
|
|
|
std::string ext = cext;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (ext == "c" || ext == ".c" || ext == "m" || ext == ".m") {
|
|
|
|
return cmSystemTools::C_FILE_FORMAT;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (ext == "f" || ext == ".f" || ext == "F" || ext == ".F" || ext == "f77" ||
|
|
|
|
ext == ".f77" || ext == "f90" || ext == ".f90" || ext == "for" ||
|
|
|
|
ext == ".for" || ext == "f95" || ext == ".f95") {
|
|
|
|
return cmSystemTools::FORTRAN_FILE_FORMAT;
|
|
|
|
}
|
|
|
|
if (ext == "java" || ext == ".java") {
|
|
|
|
return cmSystemTools::JAVA_FILE_FORMAT;
|
|
|
|
}
|
|
|
|
if (ext == "H" || ext == ".H" || 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;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2002-09-25 17:30:49 +04:00
|
|
|
#ifdef __APPLE__
|
2016-05-16 17:34:04 +03:00
|
|
|
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;
|
|
|
|
}
|
2002-09-25 17:30:49 +04:00
|
|
|
#endif // __APPLE__
|
|
|
|
return cmSystemTools::UNKNOWN_FILE_FORMAT;
|
|
|
|
}
|
2002-11-20 02:17:17 +03:00
|
|
|
|
2014-02-10 09:21:34 +04:00
|
|
|
bool cmSystemTools::Split(const char* s, std::vector<std::string>& 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);
|
2014-11-22 13:00:45 +03:00
|
|
|
l.insert(l.end(), temp.begin(), temp.end());
|
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__)
|
2016-05-16 17:34:04 +03:00
|
|
|
if (s_ForceUnixPaths) {
|
2003-08-22 00:22:23 +04:00
|
|
|
return cmSystemTools::ConvertToUnixOutputPath(path);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2003-08-22 00:22:23 +04:00
|
|
|
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__)
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!s_ForceUnixPaths) {
|
2007-03-08 19:49:26 +03:00
|
|
|
// Convert to windows slashes.
|
|
|
|
std::string::size_type pos = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
while ((pos = path.find('/', pos)) != std::string::npos) {
|
2007-03-08 19:49:26 +03:00
|
|
|
path[pos++] = '\\';
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-03-08 19:49:26 +03:00
|
|
|
#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-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)
|
|
|
|
{
|
2016-05-16 17:34:04 +03: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);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!cmSystemTools::FileIsFullPath(remote)) {
|
|
|
|
cmSystemTools::Error("RelativePath must be passed a full path to remote: ",
|
|
|
|
remote);
|
|
|
|
}
|
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
|
|
|
|
2013-06-19 16:35:23 +04:00
|
|
|
std::string cmSystemTools::CollapseCombinedPath(std::string const& dir,
|
|
|
|
std::string const& file)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (dir.empty() || dir == ".") {
|
2013-06-19 16:35:23 +04:00
|
|
|
return file;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-06-19 16:35:23 +04:00
|
|
|
|
|
|
|
std::vector<std::string> dirComponents;
|
|
|
|
std::vector<std::string> fileComponents;
|
2014-10-15 16:54:05 +04:00
|
|
|
cmSystemTools::SplitPath(dir, dirComponents);
|
|
|
|
cmSystemTools::SplitPath(file, fileComponents);
|
2013-06-19 16:35:23 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (fileComponents.empty()) {
|
2013-06-19 16:35:23 +04:00
|
|
|
return dir;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (fileComponents[0] != "") {
|
2013-06-19 16:35:23 +04:00
|
|
|
// File is not a relative path.
|
|
|
|
return file;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-06-19 16:35:23 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
std::vector<std::string>::iterator i = fileComponents.begin() + 1;
|
|
|
|
while (i != fileComponents.end() && *i == ".." && dirComponents.size() > 1) {
|
|
|
|
++i; // Remove ".." file component.
|
2013-06-19 16:35:23 +04:00
|
|
|
dirComponents.pop_back(); // Remove last dir component.
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-06-19 16:35:23 +04:00
|
|
|
|
|
|
|
dirComponents.insert(dirComponents.end(), i, fileComponents.end());
|
|
|
|
return cmSystemTools::JoinPath(dirComponents);
|
|
|
|
}
|
|
|
|
|
2006-03-17 23:46:20 +03:00
|
|
|
#ifdef CMAKE_BUILD_WITH_CMAKE
|
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
|
|
|
}
|
|
|
|
|
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;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (cc = 0; environ[cc]; ++cc) {
|
2006-03-17 18:58:37 +03:00
|
|
|
env.push_back(environ[cc]);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2006-03-17 18:58:37 +03:00
|
|
|
return env;
|
|
|
|
}
|
2008-11-26 22:38:43 +03:00
|
|
|
|
2012-04-25 00:24:17 +04:00
|
|
|
void cmSystemTools::AppendEnv(std::vector<std::string> const& env)
|
2008-11-26 22:38:43 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::const_iterator eit = env.begin();
|
|
|
|
eit != env.end(); ++eit) {
|
2014-11-23 13:05:50 +03:00
|
|
|
cmSystemTools::PutEnv(*eit);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-11-26 22:38:43 +03:00
|
|
|
}
|
|
|
|
|
2012-04-25 00:24:17 +04:00
|
|
|
cmSystemTools::SaveRestoreEnvironment::SaveRestoreEnvironment()
|
2008-11-26 22:38:43 +03:00
|
|
|
{
|
2012-04-25 00:24:17 +04:00
|
|
|
this->Env = cmSystemTools::GetEnvironmentVariables();
|
|
|
|
}
|
2008-11-26 22:38:43 +03:00
|
|
|
|
2012-04-25 00:24:17 +04:00
|
|
|
cmSystemTools::SaveRestoreEnvironment::~SaveRestoreEnvironment()
|
|
|
|
{
|
2008-11-26 22:38:43 +03:00
|
|
|
// First clear everything in the current environment:
|
|
|
|
std::vector<std::string> currentEnv = GetEnvironmentVariables();
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::const_iterator eit = currentEnv.begin();
|
|
|
|
eit != currentEnv.end(); ++eit) {
|
2008-11-26 22:38:43 +03:00
|
|
|
std::string var(*eit);
|
|
|
|
|
2016-05-24 22:24:10 +03:00
|
|
|
std::string::size_type pos = var.find('=');
|
2016-05-16 17:34:04 +03:00
|
|
|
if (pos != std::string::npos) {
|
2008-11-26 22:38:43 +03:00
|
|
|
var = var.substr(0, pos);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-11-26 22:38:43 +03:00
|
|
|
|
2012-04-25 00:24:17 +04:00
|
|
|
cmSystemTools::UnsetEnv(var.c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-11-26 22:38:43 +03:00
|
|
|
|
|
|
|
// Then put back each entry from the original environment:
|
2012-04-25 00:24:17 +04:00
|
|
|
cmSystemTools::AppendEnv(this->Env);
|
2009-12-23 22:34:46 +03:00
|
|
|
}
|
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()
|
|
|
|
{
|
2013-07-10 19:50:49 +04:00
|
|
|
#ifdef _WIN32
|
2006-02-15 18:22:55 +03:00
|
|
|
// 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.
|
|
|
|
cmSystemTools::PutEnv("vsconsoleoutput=1");
|
2013-07-10 19:50:49 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
#ifdef CMAKE_BUILD_WITH_CMAKE
|
2013-07-10 19:50:49 +04:00
|
|
|
// VS sets an environment variable to tell MS tools like "cl" to report
|
|
|
|
// output through a backdoor pipe instead of stdout/stderr. Unset the
|
|
|
|
// environment variable to close this backdoor for any path of process
|
|
|
|
// invocations that passes through CMake so we can capture the output.
|
|
|
|
cmSystemTools::UnsetEnv("VS_UNICODE_OUTPUT");
|
2016-05-16 17:34:04 +03:00
|
|
|
#endif
|
2006-02-15 18:22:55 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-12-26 21:14:19 +03:00
|
|
|
bool cmSystemTools::IsPathToFramework(const char* path)
|
|
|
|
{
|
2015-04-27 18:45:49 +03:00
|
|
|
return (cmSystemTools::FileIsFullPath(path) &&
|
|
|
|
cmHasLiteralSuffix(path, ".framework"));
|
2005-12-26 21:14:19 +03:00
|
|
|
}
|
2005-12-29 00:30:55 +03:00
|
|
|
|
2011-07-28 18:23:13 +04:00
|
|
|
bool cmSystemTools::CreateTar(const char* outFileName,
|
2014-02-10 09:21:34 +04:00
|
|
|
const std::vector<std::string>& files,
|
2016-05-16 17:34:04 +03:00
|
|
|
cmTarCompression compressType, bool verbose,
|
|
|
|
std::string const& mtime,
|
2015-04-07 13:36:52 +03:00
|
|
|
std::string const& format)
|
2005-12-30 22:22:46 +03:00
|
|
|
{
|
2009-11-08 03:35:35 +03:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2009-10-30 20:10:56 +03:00
|
|
|
std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
|
2015-08-20 23:02:20 +03:00
|
|
|
cmsys::ofstream fout(outFileName, std::ios::out | std::ios::binary);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!fout) {
|
2010-08-06 19:33:32 +04:00
|
|
|
std::string e = "Cannot open output file \"";
|
|
|
|
e += outFileName;
|
|
|
|
e += "\": ";
|
|
|
|
e += cmSystemTools::GetLastSystemError();
|
|
|
|
cmSystemTools::Error(e.c_str());
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-01-09 18:33:00 +03:00
|
|
|
cmArchiveWrite::Compress compress = cmArchiveWrite::CompressNone;
|
2016-05-16 17:34:04 +03:00
|
|
|
switch (compressType) {
|
2015-01-09 18:33:00 +03:00
|
|
|
case TarCompressGZip:
|
|
|
|
compress = cmArchiveWrite::CompressGZip;
|
|
|
|
break;
|
|
|
|
case TarCompressBZip2:
|
|
|
|
compress = cmArchiveWrite::CompressBZip2;
|
|
|
|
break;
|
|
|
|
case TarCompressXZ:
|
|
|
|
compress = cmArchiveWrite::CompressXZ;
|
|
|
|
break;
|
|
|
|
case TarCompressNone:
|
|
|
|
compress = cmArchiveWrite::CompressNone;
|
|
|
|
break;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-07 13:36:52 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmArchiveWrite a(fout, compress, format.empty() ? "paxr" : format);
|
2015-04-07 13:36:52 +03:00
|
|
|
|
2015-01-14 21:22:29 +03:00
|
|
|
a.SetMTime(mtime);
|
2010-08-06 19:33:32 +04:00
|
|
|
a.SetVerbose(verbose);
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::const_iterator i = files.begin();
|
|
|
|
i != files.end(); ++i) {
|
2010-08-06 19:33:32 +04:00
|
|
|
std::string path = *i;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmSystemTools::FileIsFullPath(path.c_str())) {
|
2010-08-06 19:33:32 +04:00
|
|
|
// Get the relative path to the file.
|
|
|
|
path = cmSystemTools::RelativePath(cwd.c_str(), path.c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!a.Add(path)) {
|
2010-08-06 19:33:32 +04:00
|
|
|
break;
|
2009-10-30 20:10:56 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!a) {
|
2010-08-06 19:33:32 +04:00
|
|
|
cmSystemTools::Error(a.GetError().c_str());
|
2009-11-08 03:35:35 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-10-30 20:10:56 +03:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
(void)outFileName;
|
|
|
|
(void)files;
|
|
|
|
(void)verbose;
|
|
|
|
return false;
|
|
|
|
#endif
|
2005-12-31 00:28:30 +03:00
|
|
|
}
|
|
|
|
|
2009-10-30 20:10:56 +03:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2016-05-16 17:34:04 +03:00
|
|
|
namespace {
|
|
|
|
#define BSDTAR_FILESIZE_PRINTF "%lu"
|
|
|
|
#define BSDTAR_FILESIZE_TYPE unsigned long
|
|
|
|
void list_item_verbose(FILE* out, struct archive_entry* entry)
|
|
|
|
{
|
|
|
|
char tmp[100];
|
|
|
|
size_t w;
|
|
|
|
const char* p;
|
|
|
|
const char* fmt;
|
|
|
|
time_t tim;
|
|
|
|
static time_t now;
|
2009-10-30 20:10:56 +03:00
|
|
|
size_t u_width = 6;
|
|
|
|
size_t gs_width = 13;
|
2011-07-28 18:23:13 +04:00
|
|
|
|
2009-10-30 20:10:56 +03:00
|
|
|
/*
|
|
|
|
* We avoid collecting the entire list in memory at once by
|
|
|
|
* listing things as we see them. However, that also means we can't
|
|
|
|
* just pre-compute the field widths. Instead, we start with guesses
|
|
|
|
* and just widen them as necessary. These numbers are completely
|
|
|
|
* arbitrary.
|
|
|
|
*/
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!now) {
|
2009-10-30 20:10:56 +03:00
|
|
|
time(&now);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
fprintf(out, "%s %d ", archive_entry_strmode(entry),
|
2009-10-30 20:10:56 +03:00
|
|
|
archive_entry_nlink(entry));
|
2011-07-28 18:23:13 +04:00
|
|
|
|
2009-10-30 20:10:56 +03:00
|
|
|
/* Use uname if it's present, else uid. */
|
|
|
|
p = archive_entry_uname(entry);
|
2016-06-27 23:44:16 +03:00
|
|
|
if ((p == CM_NULLPTR) || (*p == '\0')) {
|
2016-05-16 17:34:04 +03:00
|
|
|
sprintf(tmp, "%lu ", (unsigned long)archive_entry_uid(entry));
|
2009-10-30 20:10:56 +03:00
|
|
|
p = tmp;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-10-30 20:10:56 +03:00
|
|
|
w = strlen(p);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (w > u_width) {
|
2009-10-30 20:10:56 +03:00
|
|
|
u_width = w;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-10-30 20:10:56 +03:00
|
|
|
fprintf(out, "%-*s ", (int)u_width, p);
|
|
|
|
/* Use gname if it's present, else gid. */
|
|
|
|
p = archive_entry_gname(entry);
|
2016-06-27 23:44:16 +03:00
|
|
|
if (p != CM_NULLPTR && p[0] != '\0') {
|
2009-10-30 20:10:56 +03:00
|
|
|
fprintf(out, "%s", p);
|
|
|
|
w = strlen(p);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
|
|
|
sprintf(tmp, "%lu", (unsigned long)archive_entry_gid(entry));
|
2009-10-30 20:10:56 +03:00
|
|
|
w = strlen(tmp);
|
|
|
|
fprintf(out, "%s", tmp);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-07-28 18:23:13 +04:00
|
|
|
|
2009-10-30 20:10:56 +03:00
|
|
|
/*
|
|
|
|
* Print device number or file size, right-aligned so as to make
|
|
|
|
* total width of group and devnum/filesize fields be gs_width.
|
|
|
|
* If gs_width is too small, grow it.
|
|
|
|
*/
|
2016-05-16 17:34:04 +03:00
|
|
|
if (archive_entry_filetype(entry) == AE_IFCHR ||
|
|
|
|
archive_entry_filetype(entry) == AE_IFBLK) {
|
|
|
|
sprintf(tmp, "%lu,%lu", (unsigned long)archive_entry_rdevmajor(entry),
|
2009-10-30 20:10:56 +03:00
|
|
|
(unsigned long)archive_entry_rdevminor(entry));
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2009-10-30 20:10:56 +03:00
|
|
|
/*
|
|
|
|
* Note the use of platform-dependent macros to format
|
|
|
|
* the filesize here. We need the format string and the
|
|
|
|
* corresponding type for the cast.
|
|
|
|
*/
|
|
|
|
sprintf(tmp, BSDTAR_FILESIZE_PRINTF,
|
|
|
|
(BSDTAR_FILESIZE_TYPE)archive_entry_size(entry));
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (w + strlen(tmp) >= gs_width) {
|
|
|
|
gs_width = w + strlen(tmp) + 1;
|
|
|
|
}
|
2009-10-30 20:10:56 +03:00
|
|
|
fprintf(out, "%*s", (int)(gs_width - w), tmp);
|
2009-11-05 23:00:15 +03:00
|
|
|
|
2009-10-30 20:10:56 +03:00
|
|
|
/* Format the time using 'ls -l' conventions. */
|
|
|
|
tim = archive_entry_mtime(entry);
|
2016-09-05 23:09:49 +03:00
|
|
|
#define HALF_YEAR ((time_t)365 * 86400 / 2)
|
2009-10-30 20:10:56 +03:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
2016-05-16 17:34:04 +03:00
|
|
|
/* Windows' strftime function does not support %e format. */
|
|
|
|
#define DAY_FMT "%d"
|
2009-10-30 20:10:56 +03:00
|
|
|
#else
|
2016-05-16 17:34:04 +03:00
|
|
|
#define DAY_FMT "%e" /* Day number without leading zeros */
|
2005-12-29 20:19:13 +03:00
|
|
|
#endif
|
2016-05-16 17:34:04 +03:00
|
|
|
if (tim < now - HALF_YEAR || tim > now + HALF_YEAR) {
|
2009-10-30 20:10:56 +03:00
|
|
|
fmt = DAY_FMT " %b %Y";
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2009-10-30 20:10:56 +03:00
|
|
|
fmt = DAY_FMT " %b %H:%M";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-10-30 20:10:56 +03:00
|
|
|
strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
|
|
|
|
fprintf(out, " %s ", tmp);
|
2014-06-12 16:46:40 +04:00
|
|
|
fprintf(out, "%s", cm_archive_entry_pathname(entry).c_str());
|
2009-11-05 23:00:15 +03:00
|
|
|
|
2009-10-30 20:10:56 +03:00
|
|
|
/* Extra information for links. */
|
|
|
|
if (archive_entry_hardlink(entry)) /* Hard link */
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
fprintf(out, " link to %s", archive_entry_hardlink(entry));
|
|
|
|
} else if (archive_entry_symlink(entry)) /* Symbolic link */
|
|
|
|
{
|
2009-10-30 20:10:56 +03:00
|
|
|
fprintf(out, " -> %s", archive_entry_symlink(entry));
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-10-30 20:10:56 +03:00
|
|
|
}
|
2011-07-28 18:23:13 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
long copy_data(struct archive* ar, struct archive* aw)
|
2009-10-30 20:10:56 +03:00
|
|
|
{
|
2010-06-29 17:52:12 +04:00
|
|
|
long r;
|
2016-05-16 17:34:04 +03:00
|
|
|
const void* buff;
|
2009-10-30 20:10:56 +03:00
|
|
|
size_t size;
|
2011-12-20 22:16:45 +04:00
|
|
|
#if defined(ARCHIVE_VERSION_NUMBER) && ARCHIVE_VERSION_NUMBER >= 3000000
|
|
|
|
__LA_INT64_T offset;
|
|
|
|
#else
|
2009-10-30 20:10:56 +03:00
|
|
|
off_t offset;
|
2011-12-20 22:16:45 +04:00
|
|
|
#endif
|
2011-07-28 18:23:13 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
for (;;) {
|
2009-10-30 20:10:56 +03:00
|
|
|
r = archive_read_data_block(ar, &buff, &size, &offset);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r == ARCHIVE_EOF) {
|
2009-10-30 20:10:56 +03:00
|
|
|
return (ARCHIVE_OK);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (r != ARCHIVE_OK) {
|
2009-10-30 20:10:56 +03:00
|
|
|
return (r);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-10-30 20:10:56 +03:00
|
|
|
r = archive_write_data_block(aw, buff, size, offset);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r != ARCHIVE_OK) {
|
2009-10-30 20:10:56 +03:00
|
|
|
cmSystemTools::Message("archive_write_data_block()",
|
|
|
|
archive_error_string(aw));
|
|
|
|
return (r);
|
2005-12-29 00:30:55 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-04-08 16:48:08 +04:00
|
|
|
#if !defined(__clang__) && !defined(__HP_aCC)
|
2014-04-04 19:47:04 +04:00
|
|
|
return r; /* this should not happen but it quiets some compilers */
|
2014-04-02 17:35:36 +04:00
|
|
|
#endif
|
2009-10-30 20:10:56 +03:00
|
|
|
}
|
2005-12-29 00:30:55 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool extract_tar(const char* outFileName, bool verbose, bool extract)
|
2009-10-30 20:10:56 +03:00
|
|
|
{
|
2015-02-06 21:23:07 +03:00
|
|
|
cmLocaleRAII localeRAII;
|
|
|
|
static_cast<void>(localeRAII);
|
2009-10-30 20:10:56 +03:00
|
|
|
struct archive* a = archive_read_new();
|
2016-05-16 17:34:04 +03:00
|
|
|
struct archive* ext = archive_write_disk_new();
|
2015-08-22 19:14:12 +03:00
|
|
|
archive_read_support_filter_all(a);
|
2009-10-30 20:10:56 +03:00
|
|
|
archive_read_support_format_all(a);
|
2016-05-16 17:34:04 +03:00
|
|
|
struct archive_entry* entry;
|
2014-06-12 16:46:40 +04:00
|
|
|
int r = cm_archive_read_open_file(a, outFileName, 10240);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r) {
|
2009-10-30 20:10:56 +03:00
|
|
|
cmSystemTools::Error("Problem with archive_read_open_file(): ",
|
|
|
|
archive_error_string(a));
|
2014-07-23 19:13:17 +04:00
|
|
|
archive_write_free(ext);
|
|
|
|
archive_read_close(a);
|
2005-12-29 00:44:32 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
for (;;) {
|
2009-10-30 20:10:56 +03:00
|
|
|
r = archive_read_next_header(a, &entry);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r == ARCHIVE_EOF) {
|
2009-10-30 20:10:56 +03:00
|
|
|
break;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (r != ARCHIVE_OK) {
|
2009-10-30 20:10:56 +03:00
|
|
|
cmSystemTools::Error("Problem with archive_read_next_header(): ",
|
|
|
|
archive_error_string(a));
|
2012-11-26 15:22:52 +04:00
|
|
|
break;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (verbose) {
|
|
|
|
if (extract) {
|
2013-11-02 00:52:18 +04:00
|
|
|
cmSystemTools::Stdout("x ");
|
2014-06-12 16:46:40 +04:00
|
|
|
cmSystemTools::Stdout(cm_archive_entry_pathname(entry).c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2013-11-02 00:52:18 +04:00
|
|
|
list_item_verbose(stdout, entry);
|
2009-10-30 20:10:56 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
cmSystemTools::Stdout("\n");
|
|
|
|
} else if (!extract) {
|
2014-06-12 16:46:40 +04:00
|
|
|
cmSystemTools::Stdout(cm_archive_entry_pathname(entry).c_str());
|
2013-11-02 00:52:18 +04:00
|
|
|
cmSystemTools::Stdout("\n");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (extract) {
|
2010-09-09 20:57:14 +04:00
|
|
|
r = archive_write_disk_set_options(ext, ARCHIVE_EXTRACT_TIME);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r != ARCHIVE_OK) {
|
|
|
|
cmSystemTools::Error("Problem with archive_write_disk_set_options(): ",
|
|
|
|
archive_error_string(ext));
|
2012-11-26 15:22:52 +04:00
|
|
|
break;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-09-09 20:57:14 +04:00
|
|
|
|
2009-10-30 20:10:56 +03:00
|
|
|
r = archive_write_header(ext, entry);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r == ARCHIVE_OK) {
|
2009-10-30 20:10:56 +03:00
|
|
|
copy_data(a, ext);
|
|
|
|
r = archive_write_finish_entry(ext);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r != ARCHIVE_OK) {
|
2009-10-30 20:10:56 +03:00
|
|
|
cmSystemTools::Error("Problem with archive_write_finish_entry(): ",
|
|
|
|
archive_error_string(ext));
|
2012-11-26 15:22:52 +04:00
|
|
|
break;
|
2009-10-30 20:10:56 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-11-02 00:52:22 +04:00
|
|
|
#ifdef _WIN32
|
2016-05-16 17:34:04 +03:00
|
|
|
else if (const char* linktext = archive_entry_symlink(entry)) {
|
2013-11-02 00:52:22 +04:00
|
|
|
std::cerr << "cmake -E tar: warning: skipping symbolic link \""
|
2016-05-16 17:34:04 +03:00
|
|
|
<< cm_archive_entry_pathname(entry) << "\" -> \"" << linktext
|
|
|
|
<< "\"." << std::endl;
|
|
|
|
}
|
2013-11-02 00:52:22 +04:00
|
|
|
#endif
|
2016-05-16 17:34:04 +03:00
|
|
|
else {
|
2013-11-02 00:52:18 +04:00
|
|
|
cmSystemTools::Error("Problem with archive_write_header(): ",
|
|
|
|
archive_error_string(ext));
|
|
|
|
cmSystemTools::Error("Current file: ",
|
2014-06-12 16:46:40 +04:00
|
|
|
cm_archive_entry_pathname(entry).c_str());
|
2013-11-02 00:52:18 +04:00
|
|
|
break;
|
2009-10-30 20:10:56 +03:00
|
|
|
}
|
2005-12-29 18:43:12 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-07-23 19:13:17 +04:00
|
|
|
archive_write_free(ext);
|
2009-10-30 20:10:56 +03:00
|
|
|
archive_read_close(a);
|
2015-08-22 19:14:12 +03:00
|
|
|
archive_read_free(a);
|
2012-11-26 15:22:52 +04:00
|
|
|
return r == ARCHIVE_EOF || r == ARCHIVE_OK;
|
2009-10-30 20:10:56 +03:00
|
|
|
}
|
2005-12-29 18:43:12 +03:00
|
|
|
}
|
2011-07-28 18:23:13 +04:00
|
|
|
#endif
|
2005-12-29 18:43:12 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmSystemTools::ExtractTar(const char* outFileName, bool verbose)
|
2005-12-29 18:43:12 +03:00
|
|
|
{
|
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2009-10-30 20:10:56 +03:00
|
|
|
return extract_tar(outFileName, verbose, true);
|
2005-12-29 01:02:16 +03:00
|
|
|
#else
|
2006-03-16 19:01:05 +03:00
|
|
|
(void)outFileName;
|
|
|
|
(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
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmSystemTools::ListTar(const char* outFileName, bool verbose)
|
2005-12-29 20:19:13 +03:00
|
|
|
{
|
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2009-10-30 20:10:56 +03:00
|
|
|
return extract_tar(outFileName, verbose, false);
|
2005-12-29 20:19:13 +03:00
|
|
|
#else
|
2006-03-16 19:01:05 +03:00
|
|
|
(void)outFileName;
|
|
|
|
(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,
|
2016-05-16 17:34:04 +03:00
|
|
|
double timeout, std::vector<char>& out,
|
2006-04-04 21:04:28 +04:00
|
|
|
std::vector<char>& err)
|
|
|
|
{
|
|
|
|
line = "";
|
|
|
|
std::vector<char>::iterator outiter = out.begin();
|
|
|
|
std::vector<char>::iterator erriter = err.begin();
|
2016-05-16 17:34:04 +03:00
|
|
|
while (1) {
|
2006-04-04 21:04:28 +04:00
|
|
|
// Check for a newline in stdout.
|
2016-05-16 17:34:04 +03:00
|
|
|
for (; outiter != out.end(); ++outiter) {
|
|
|
|
if ((*outiter == '\r') && ((outiter + 1) == out.end())) {
|
2006-04-04 21:04:28 +04:00
|
|
|
break;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (*outiter == '\n' || *outiter == '\0') {
|
|
|
|
std::vector<char>::size_type length = outiter - out.begin();
|
|
|
|
if (length > 1 && *(outiter - 1) == '\r') {
|
2006-04-04 21:04:28 +04:00
|
|
|
--length;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (length > 0) {
|
2006-04-04 21:04:28 +04:00
|
|
|
line.append(&out[0], length);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
out.erase(out.begin(), outiter + 1);
|
|
|
|
return cmsysProcess_Pipe_STDOUT;
|
2006-04-04 21:04:28 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2006-04-04 21:04:28 +04:00
|
|
|
|
|
|
|
// Check for a newline in stderr.
|
2016-05-16 17:34:04 +03:00
|
|
|
for (; erriter != err.end(); ++erriter) {
|
|
|
|
if ((*erriter == '\r') && ((erriter + 1) == err.end())) {
|
2006-04-04 21:04:28 +04:00
|
|
|
break;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (*erriter == '\n' || *erriter == '\0') {
|
|
|
|
std::vector<char>::size_type length = erriter - err.begin();
|
|
|
|
if (length > 1 && *(erriter - 1) == '\r') {
|
2006-04-04 21:04:28 +04:00
|
|
|
--length;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (length > 0) {
|
2006-04-04 21:04:28 +04:00
|
|
|
line.append(&err[0], length);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
err.erase(err.begin(), erriter + 1);
|
|
|
|
return cmsysProcess_Pipe_STDERR;
|
2006-04-04 21:04:28 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2006-04-04 21:04:28 +04:00
|
|
|
|
|
|
|
// No newlines found. Wait for more data from the process.
|
|
|
|
int length;
|
|
|
|
char* data;
|
|
|
|
int pipe = cmsysProcess_WaitForData(process, &data, &length, &timeout);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (pipe == cmsysProcess_Pipe_Timeout) {
|
2006-04-04 21:04:28 +04:00
|
|
|
// Timeout has been exceeded.
|
|
|
|
return pipe;
|
2016-08-18 21:36:29 +03:00
|
|
|
}
|
|
|
|
if (pipe == cmsysProcess_Pipe_STDOUT) {
|
2006-04-04 21:04:28 +04:00
|
|
|
// Append to the stdout buffer.
|
|
|
|
std::vector<char>::size_type size = out.size();
|
2016-05-16 17:34:04 +03:00
|
|
|
out.insert(out.end(), data, data + length);
|
|
|
|
outiter = out.begin() + size;
|
|
|
|
} else if (pipe == cmsysProcess_Pipe_STDERR) {
|
2006-04-04 21:04:28 +04:00
|
|
|
// Append to the stderr buffer.
|
|
|
|
std::vector<char>::size_type size = err.size();
|
2016-05-16 17:34:04 +03:00
|
|
|
err.insert(err.end(), data, data + length);
|
|
|
|
erriter = err.begin() + size;
|
|
|
|
} else if (pipe == cmsysProcess_Pipe_None) {
|
2006-04-04 21:04:28 +04:00
|
|
|
// Both stdout and stderr pipes have broken. Return leftover data.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!out.empty()) {
|
|
|
|
line.append(&out[0], outiter - out.begin());
|
2006-04-04 21:04:28 +04:00
|
|
|
out.erase(out.begin(), out.end());
|
|
|
|
return cmsysProcess_Pipe_STDOUT;
|
2016-08-18 21:36:29 +03:00
|
|
|
}
|
|
|
|
if (!err.empty()) {
|
2016-05-16 17:34:04 +03:00
|
|
|
line.append(&err[0], erriter - err.begin());
|
2006-04-04 21:04:28 +04:00
|
|
|
err.erase(err.begin(), err.end());
|
|
|
|
return cmsysProcess_Pipe_STDERR;
|
|
|
|
}
|
2016-08-18 21:36:29 +03:00
|
|
|
return cmsysProcess_Pipe_None;
|
2006-04-04 21:04:28 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2006-04-04 21:04:28 +04:00
|
|
|
}
|
|
|
|
|
2007-09-11 19:21:36 +04:00
|
|
|
void cmSystemTools::DoNotInheritStdPipes()
|
2011-07-28 18:23:13 +04:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2007-09-25 18:57:49 +04:00
|
|
|
// 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);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (GetConsoleScreenBufferInfo(hOut, &hOutInfo)) {
|
2007-09-25 18:57:49 +04:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-09-11 19:21:36 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03: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);
|
2007-09-11 19:21:36 +04:00
|
|
|
}
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
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 19:21:36 +04:00
|
|
|
}
|
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__)
|
2016-05-16 17:34:04 +03:00
|
|
|
cmSystemToolsWindowsHandle hFrom = CreateFileW(
|
|
|
|
SystemTools::ConvertToWindowsExtendedPath(fromFile).c_str(), GENERIC_READ,
|
|
|
|
FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
|
|
|
|
cmSystemToolsWindowsHandle hTo = CreateFileW(
|
|
|
|
SystemTools::ConvertToWindowsExtendedPath(toFile).c_str(),
|
|
|
|
FILE_WRITE_ATTRIBUTES, 0, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
|
|
|
|
if (!hFrom || !hTo) {
|
2007-10-05 17:46:28 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-10-05 17:46:28 +04:00
|
|
|
FILETIME timeCreation;
|
|
|
|
FILETIME timeLastAccess;
|
|
|
|
FILETIME timeLastWrite;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!GetFileTime(hFrom, &timeCreation, &timeLastAccess, &timeLastWrite)) {
|
2007-10-05 17:46:28 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!SetFileTime(hTo, &timeCreation, &timeLastAccess, &timeLastWrite)) {
|
2007-10-05 17:46:28 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-10-05 17:46:28 +04:00
|
|
|
#else
|
|
|
|
struct stat fromStat;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (stat(fromFile, &fromStat) < 0) {
|
2007-10-05 17:46:28 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-10-05 17:46:28 +04:00
|
|
|
|
|
|
|
struct utimbuf buf;
|
|
|
|
buf.actime = fromStat.st_atime;
|
|
|
|
buf.modtime = fromStat.st_mtime;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (utime(toFile, &buf) < 0) {
|
2007-10-05 17:46:28 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-10-05 17:46:28 +04:00
|
|
|
#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__)
|
2016-05-16 17:34:04 +03:00
|
|
|
cmSystemToolsWindowsHandle h = CreateFileW(
|
|
|
|
SystemTools::ConvertToWindowsExtendedPath(fname).c_str(), GENERIC_READ,
|
|
|
|
FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
|
|
|
|
if (!h) {
|
2008-04-14 19:43:45 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!GetFileTime(h, &t->timeCreation, &t->timeLastAccess,
|
|
|
|
&t->timeLastWrite)) {
|
2008-04-14 19:43:45 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-04-14 19:43:45 +04:00
|
|
|
#else
|
|
|
|
struct stat st;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (stat(fname, &st) < 0) {
|
2008-04-14 19:43:45 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-04-14 19:43:45 +04:00
|
|
|
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__)
|
2016-05-16 17:34:04 +03:00
|
|
|
cmSystemToolsWindowsHandle h = CreateFileW(
|
|
|
|
SystemTools::ConvertToWindowsExtendedPath(fname).c_str(),
|
|
|
|
FILE_WRITE_ATTRIBUTES, 0, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
|
|
|
|
if (!h) {
|
2008-04-14 19:43:45 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!SetFileTime(h, &t->timeCreation, &t->timeLastAccess,
|
|
|
|
&t->timeLastWrite)) {
|
2008-04-14 19:43:45 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-04-14 19:43:45 +04:00
|
|
|
#else
|
2016-05-16 17:34:04 +03:00
|
|
|
if (utime(fname, &t->timeBuf) < 0) {
|
2008-04-14 19:43:45 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-04-14 19:43:45 +04:00
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-17 18:07:26 +04:00
|
|
|
#ifdef _WIN32
|
2016-05-16 17:34:04 +03:00
|
|
|
#ifndef CRYPT_SILENT
|
|
|
|
#define CRYPT_SILENT 0x40 /* Not defined by VS 6 version of header. */
|
|
|
|
#endif
|
2011-05-17 18:07:26 +04:00
|
|
|
static int WinCryptRandom(void* data, size_t size)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
HCRYPTPROV hProvider = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL,
|
|
|
|
CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
|
|
|
|
result = CryptGenRandom(hProvider, (DWORD)size, (BYTE*)data) ? 1 : 0;
|
2011-05-17 18:07:26 +04:00
|
|
|
CryptReleaseContext(hProvider, 0);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-05-17 18:07:26 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
unsigned int cmSystemTools::RandomSeed()
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
unsigned int seed = 0;
|
|
|
|
|
|
|
|
// Try using a real random source.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (WinCryptRandom(&seed, sizeof(seed))) {
|
2011-05-17 18:07:26 +04:00
|
|
|
return seed;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-05-17 18:07:26 +04:00
|
|
|
|
|
|
|
// Fall back to the time and pid.
|
|
|
|
FILETIME ft;
|
|
|
|
GetSystemTimeAsFileTime(&ft);
|
|
|
|
unsigned int t1 = static_cast<unsigned int>(ft.dwHighDateTime);
|
|
|
|
unsigned int t2 = static_cast<unsigned int>(ft.dwLowDateTime);
|
|
|
|
unsigned int pid = static_cast<unsigned int>(GetCurrentProcessId());
|
|
|
|
return t1 ^ t2 ^ pid;
|
|
|
|
#else
|
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned int integer;
|
|
|
|
char bytes[sizeof(unsigned int)];
|
|
|
|
} seed;
|
|
|
|
|
|
|
|
// Try using a real random source.
|
2016-02-17 17:02:04 +03:00
|
|
|
cmsys::ifstream fin;
|
2016-06-27 23:44:16 +03:00
|
|
|
fin.rdbuf()->pubsetbuf(CM_NULLPTR, 0); // Unbuffered read.
|
2016-02-17 17:02:04 +03:00
|
|
|
fin.open("/dev/urandom");
|
2016-05-16 17:34:04 +03:00
|
|
|
if (fin.good() && fin.read(seed.bytes, sizeof(seed)) &&
|
|
|
|
fin.gcount() == sizeof(seed)) {
|
2011-05-17 18:07:26 +04:00
|
|
|
return seed.integer;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-05-17 18:07:26 +04:00
|
|
|
|
|
|
|
// Fall back to the time and pid.
|
|
|
|
struct timeval t;
|
2016-06-27 23:44:16 +03:00
|
|
|
gettimeofday(&t, CM_NULLPTR);
|
2011-05-17 18:07:26 +04:00
|
|
|
unsigned int pid = static_cast<unsigned int>(getpid());
|
2011-05-19 15:56:04 +04:00
|
|
|
unsigned int tv_sec = static_cast<unsigned int>(t.tv_sec);
|
|
|
|
unsigned int tv_usec = static_cast<unsigned int>(t.tv_usec);
|
2011-05-17 18:07:26 +04:00
|
|
|
// Since tv_usec never fills more than 11 bits we shift it to fill
|
|
|
|
// in the slow-changing high-order bits of tv_sec.
|
|
|
|
return tv_sec ^ (tv_usec << 21) ^ pid;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-11-08 00:30:59 +04:00
|
|
|
static std::string cmSystemToolsCMakeCommand;
|
|
|
|
static std::string cmSystemToolsCTestCommand;
|
|
|
|
static std::string cmSystemToolsCPackCommand;
|
|
|
|
static std::string cmSystemToolsCMakeCursesCommand;
|
|
|
|
static std::string cmSystemToolsCMakeGUICommand;
|
2015-09-18 15:47:27 +03:00
|
|
|
static std::string cmSystemToolsCMClDepsCommand;
|
2013-11-08 00:30:59 +04:00
|
|
|
static std::string cmSystemToolsCMakeRoot;
|
|
|
|
void cmSystemTools::FindCMakeResources(const char* argv0)
|
|
|
|
{
|
|
|
|
std::string exe_dir;
|
2009-01-20 18:06:39 +03:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
(void)argv0; // ignore this on windows
|
2013-12-05 09:17:24 +04:00
|
|
|
wchar_t modulepath[_MAX_PATH];
|
|
|
|
::GetModuleFileNameW(NULL, modulepath, sizeof(modulepath));
|
|
|
|
exe_dir =
|
|
|
|
cmSystemTools::GetFilenamePath(cmsys::Encoding::ToNarrow(modulepath));
|
2013-11-08 00:30:59 +04:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
(void)argv0; // ignore this on OS X
|
2016-05-16 17:34:04 +03:00
|
|
|
#define CM_EXE_PATH_LOCAL_SIZE 16384
|
2013-11-08 00:30:59 +04:00
|
|
|
char exe_path_local[CM_EXE_PATH_LOCAL_SIZE];
|
2016-05-16 17:34:04 +03:00
|
|
|
#if defined(MAC_OS_X_VERSION_10_3) && !defined(MAC_OS_X_VERSION_10_4)
|
2013-11-08 00:30:59 +04:00
|
|
|
unsigned long exe_path_size = CM_EXE_PATH_LOCAL_SIZE;
|
2016-05-16 17:34:04 +03:00
|
|
|
#else
|
2013-11-08 00:30:59 +04:00
|
|
|
uint32_t exe_path_size = CM_EXE_PATH_LOCAL_SIZE;
|
2016-05-16 17:34:04 +03:00
|
|
|
#endif
|
|
|
|
#undef CM_EXE_PATH_LOCAL_SIZE
|
2013-11-08 00:30:59 +04:00
|
|
|
char* exe_path = exe_path_local;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (_NSGetExecutablePath(exe_path, &exe_path_size) < 0) {
|
2013-11-08 00:30:59 +04:00
|
|
|
exe_path = (char*)malloc(exe_path_size);
|
|
|
|
_NSGetExecutablePath(exe_path, &exe_path_size);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-11-08 00:30:59 +04:00
|
|
|
exe_dir =
|
2016-05-16 17:34:04 +03:00
|
|
|
cmSystemTools::GetFilenamePath(cmSystemTools::GetRealPath(exe_path));
|
|
|
|
if (exe_path != exe_path_local) {
|
2013-11-08 00:30:59 +04:00
|
|
|
free(exe_path);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (cmSystemTools::GetFilenameName(exe_dir) == "MacOS") {
|
2013-11-08 00:30:59 +04:00
|
|
|
// The executable is inside an application bundle.
|
2016-05-27 17:03:43 +03:00
|
|
|
// Look for ..<CMAKE_BIN_DIR> (install tree) and then fall back to
|
2013-11-08 00:30:59 +04:00
|
|
|
// ../../../bin (build tree).
|
|
|
|
exe_dir = cmSystemTools::GetFilenamePath(exe_dir);
|
2016-05-27 17:03:43 +03:00
|
|
|
if (cmSystemTools::FileExists(exe_dir + CMAKE_BIN_DIR "/cmake")) {
|
|
|
|
exe_dir += CMAKE_BIN_DIR;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2013-11-08 00:30:59 +04:00
|
|
|
exe_dir = cmSystemTools::GetFilenamePath(exe_dir);
|
|
|
|
exe_dir = cmSystemTools::GetFilenamePath(exe_dir);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-01-20 18:06:39 +03:00
|
|
|
#else
|
2007-12-14 01:56:50 +03:00
|
|
|
std::string errorMsg;
|
|
|
|
std::string exe;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmSystemTools::FindProgramPath(argv0, exe, errorMsg)) {
|
2008-03-28 21:29:29 +03:00
|
|
|
// remove symlinks
|
2014-10-15 16:54:05 +04:00
|
|
|
exe = cmSystemTools::GetRealPath(exe);
|
2016-05-16 17:34:04 +03:00
|
|
|
exe_dir = cmSystemTools::GetFilenamePath(exe);
|
|
|
|
} else {
|
2007-12-14 01:56:50 +03:00
|
|
|
// ???
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-01-20 18:06:39 +03:00
|
|
|
#endif
|
2013-11-08 00:30:59 +04:00
|
|
|
cmSystemToolsCMakeCommand = exe_dir;
|
|
|
|
cmSystemToolsCMakeCommand += "/cmake";
|
|
|
|
cmSystemToolsCMakeCommand += cmSystemTools::GetExecutableExtension();
|
2014-06-25 21:34:15 +04:00
|
|
|
#ifndef CMAKE_BUILD_WITH_CMAKE
|
|
|
|
// The bootstrap cmake does not provide the other tools,
|
|
|
|
// so use the directory where they are about to be built.
|
|
|
|
exe_dir = CMAKE_BOOTSTRAP_BINARY_DIR "/bin";
|
|
|
|
#endif
|
2013-11-08 00:30:59 +04:00
|
|
|
cmSystemToolsCTestCommand = exe_dir;
|
|
|
|
cmSystemToolsCTestCommand += "/ctest";
|
|
|
|
cmSystemToolsCTestCommand += cmSystemTools::GetExecutableExtension();
|
|
|
|
cmSystemToolsCPackCommand = exe_dir;
|
|
|
|
cmSystemToolsCPackCommand += "/cpack";
|
|
|
|
cmSystemToolsCPackCommand += cmSystemTools::GetExecutableExtension();
|
|
|
|
cmSystemToolsCMakeGUICommand = exe_dir;
|
|
|
|
cmSystemToolsCMakeGUICommand += "/cmake-gui";
|
|
|
|
cmSystemToolsCMakeGUICommand += cmSystemTools::GetExecutableExtension();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!cmSystemTools::FileExists(cmSystemToolsCMakeGUICommand.c_str())) {
|
2013-11-08 00:30:59 +04:00
|
|
|
cmSystemToolsCMakeGUICommand = "";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-11-08 00:30:59 +04:00
|
|
|
cmSystemToolsCMakeCursesCommand = exe_dir;
|
|
|
|
cmSystemToolsCMakeCursesCommand += "/ccmake";
|
|
|
|
cmSystemToolsCMakeCursesCommand += cmSystemTools::GetExecutableExtension();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!cmSystemTools::FileExists(cmSystemToolsCMakeCursesCommand.c_str())) {
|
2013-11-08 00:30:59 +04:00
|
|
|
cmSystemToolsCMakeCursesCommand = "";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-09-18 15:47:27 +03:00
|
|
|
cmSystemToolsCMClDepsCommand = exe_dir;
|
|
|
|
cmSystemToolsCMClDepsCommand += "/cmcldeps";
|
|
|
|
cmSystemToolsCMClDepsCommand += cmSystemTools::GetExecutableExtension();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!cmSystemTools::FileExists(cmSystemToolsCMClDepsCommand.c_str())) {
|
2015-09-18 15:47:27 +03:00
|
|
|
cmSystemToolsCMClDepsCommand = "";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-11-08 00:30:59 +04:00
|
|
|
|
|
|
|
#ifdef CMAKE_BUILD_WITH_CMAKE
|
2016-05-27 17:03:43 +03:00
|
|
|
// Install tree has
|
|
|
|
// - "<prefix><CMAKE_BIN_DIR>/cmake"
|
|
|
|
// - "<prefix><CMAKE_DATA_DIR>"
|
|
|
|
if (cmHasSuffix(exe_dir, CMAKE_BIN_DIR)) {
|
|
|
|
std::string const prefix =
|
|
|
|
exe_dir.substr(0, exe_dir.size() - strlen(CMAKE_BIN_DIR));
|
|
|
|
cmSystemToolsCMakeRoot = prefix + CMAKE_DATA_DIR;
|
|
|
|
}
|
|
|
|
if (cmSystemToolsCMakeRoot.empty() ||
|
|
|
|
!cmSystemTools::FileExists(
|
2016-05-16 17:34:04 +03:00
|
|
|
(cmSystemToolsCMakeRoot + "/Modules/CMake.cmake").c_str())) {
|
2013-11-08 00:30:59 +04:00
|
|
|
// Build tree has "<build>/bin[/<config>]/cmake" and
|
|
|
|
// "<build>/CMakeFiles/CMakeSourceDir.txt".
|
2016-05-27 17:03:43 +03:00
|
|
|
std::string dir = cmSystemTools::GetFilenamePath(exe_dir);
|
2013-11-08 00:30:59 +04:00
|
|
|
std::string src_dir_txt = dir + "/CMakeFiles/CMakeSourceDir.txt";
|
2014-01-04 09:47:13 +04:00
|
|
|
cmsys::ifstream fin(src_dir_txt.c_str());
|
2013-11-08 00:30:59 +04:00
|
|
|
std::string src_dir;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (fin && cmSystemTools::GetLineFromStream(fin, src_dir) &&
|
|
|
|
cmSystemTools::FileIsDirectory(src_dir)) {
|
2013-11-08 00:30:59 +04:00
|
|
|
cmSystemToolsCMakeRoot = src_dir;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2013-11-08 00:30:59 +04:00
|
|
|
dir = cmSystemTools::GetFilenamePath(dir);
|
|
|
|
src_dir_txt = dir + "/CMakeFiles/CMakeSourceDir.txt";
|
2014-01-04 09:47:13 +04:00
|
|
|
cmsys::ifstream fin2(src_dir_txt.c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
if (fin2 && cmSystemTools::GetLineFromStream(fin2, src_dir) &&
|
|
|
|
cmSystemTools::FileIsDirectory(src_dir)) {
|
2013-11-08 00:30:59 +04:00
|
|
|
cmSystemToolsCMakeRoot = src_dir;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-11-08 00:30:59 +04:00
|
|
|
#else
|
|
|
|
// Bootstrap build knows its source.
|
2014-06-25 21:32:48 +04:00
|
|
|
cmSystemToolsCMakeRoot = CMAKE_BOOTSTRAP_SOURCE_DIR;
|
2013-11-08 00:30:59 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const& cmSystemTools::GetCMakeCommand()
|
|
|
|
{
|
|
|
|
return cmSystemToolsCMakeCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const& cmSystemTools::GetCTestCommand()
|
|
|
|
{
|
|
|
|
return cmSystemToolsCTestCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const& cmSystemTools::GetCPackCommand()
|
|
|
|
{
|
|
|
|
return cmSystemToolsCPackCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const& cmSystemTools::GetCMakeCursesCommand()
|
|
|
|
{
|
|
|
|
return cmSystemToolsCMakeCursesCommand;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const& cmSystemTools::GetCMakeGUICommand()
|
|
|
|
{
|
|
|
|
return cmSystemToolsCMakeGUICommand;
|
2007-12-14 01:56:50 +03:00
|
|
|
}
|
|
|
|
|
2015-09-18 15:47:27 +03:00
|
|
|
std::string const& cmSystemTools::GetCMClDepsCommand()
|
|
|
|
{
|
|
|
|
return cmSystemToolsCMClDepsCommand;
|
|
|
|
}
|
|
|
|
|
2013-11-08 00:30:59 +04:00
|
|
|
std::string const& cmSystemTools::GetCMakeRoot()
|
2007-12-14 01:56:50 +03:00
|
|
|
{
|
2013-11-08 00:30:59 +04:00
|
|
|
return cmSystemToolsCMakeRoot;
|
2007-12-14 01:56:50 +03:00
|
|
|
}
|
2007-12-20 01:15:41 +03:00
|
|
|
|
|
|
|
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;
|
2016-07-08 00:54:05 +03:00
|
|
|
if (cmSystemTools::HasEnv("DART_TEST_FROM_DART") ||
|
|
|
|
cmSystemTools::HasEnv("DASHBOARD_TEST_FROM_CTEST") ||
|
|
|
|
cmSystemTools::HasEnv("CTEST_INTERACTIVE_DEBUG_MODE")) {
|
2007-12-20 01:15:41 +03:00
|
|
|
// Avoid printing color escapes during dashboard builds.
|
|
|
|
assumeTTY = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-12-20 01:15:41 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (enabled && color != cmsysTerminal_Color_Normal) {
|
2015-02-07 00:39:00 +03:00
|
|
|
// Print with color. Delay the newline until later so that
|
|
|
|
// all color restore sequences appear before it.
|
|
|
|
cmsysTerminal_cfprintf(color | assumeTTY, stdout, "%s", message);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2007-12-20 01:15:41 +03:00
|
|
|
// Color is disabled. Print without color.
|
2015-02-07 00:39:00 +03:00
|
|
|
fprintf(stdout, "%s", message);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-02-07 00:39:00 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (newline) {
|
2015-02-07 00:39:00 +03:00
|
|
|
fprintf(stdout, "\n");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-12-20 01:15:41 +03:00
|
|
|
}
|
2008-02-21 21:58:41 +03:00
|
|
|
|
|
|
|
bool cmSystemTools::GuessLibrarySOName(std::string const& fullPath,
|
|
|
|
std::string& soname)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
// For ELF shared libraries use a real parser to get the correct
|
|
|
|
// soname.
|
2008-02-28 00:26:35 +03:00
|
|
|
#if defined(CMAKE_USE_ELF_PARSER)
|
|
|
|
cmELF elf(fullPath.c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
if (elf) {
|
2008-02-28 00:26:35 +03:00
|
|
|
return elf.GetSOName(soname);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-28 00:26:35 +03:00
|
|
|
#endif
|
|
|
|
|
2008-02-21 21:58:41 +03:00
|
|
|
// If the file is not a symlink we have no guess for its soname.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!cmSystemTools::FileIsSymlink(fullPath)) {
|
2008-02-21 21:58:41 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!cmSystemTools::ReadSymlink(fullPath, soname)) {
|
2008-02-21 21:58:41 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-21 21:58:41 +03:00
|
|
|
|
|
|
|
// If the symlink has a path component we have no guess for the soname.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!cmSystemTools::GetFilenamePath(soname).empty()) {
|
2008-02-21 21:58:41 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-02-21 21:58:41 +03:00
|
|
|
|
|
|
|
// If the symlink points at an extended version of the same name
|
|
|
|
// assume it is the soname.
|
|
|
|
std::string name = cmSystemTools::GetFilenameName(fullPath);
|
2016-06-02 00:29:53 +03:00
|
|
|
return soname.length() > name.length() &&
|
|
|
|
soname.compare(0, name.length(), name) == 0;
|
2008-02-21 21:58:41 +03:00
|
|
|
}
|
2008-03-01 20:51:07 +03:00
|
|
|
|
2013-04-27 08:04:44 +04:00
|
|
|
bool cmSystemTools::GuessLibraryInstallName(std::string const& fullPath,
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string& soname)
|
2013-04-27 08:04:44 +04:00
|
|
|
{
|
2014-12-19 22:31:22 +03:00
|
|
|
#if defined(CMAKE_USE_MACH_PARSER)
|
|
|
|
cmMachO macho(fullPath.c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
if (macho) {
|
2014-12-19 22:31:22 +03:00
|
|
|
return macho.GetInstallName(soname);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-12-19 22:31:22 +03:00
|
|
|
#else
|
|
|
|
(void)fullPath;
|
|
|
|
(void)soname;
|
|
|
|
#endif
|
2013-04-27 08:04:44 +04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2016-05-19 18:51:51 +03:00
|
|
|
std::string::size_type pos = 0;
|
|
|
|
while (pos < have.size()) {
|
|
|
|
// Look for an occurrence of the string.
|
|
|
|
std::string::size_type const beg = have.find(want, pos);
|
|
|
|
if (beg == std::string::npos) {
|
|
|
|
return std::string::npos;
|
|
|
|
}
|
2008-04-08 21:42:30 +04:00
|
|
|
|
2016-05-19 18:51:51 +03:00
|
|
|
// Make sure it is separated from preceding entries.
|
|
|
|
if (beg > 0 && have[beg - 1] != ':') {
|
|
|
|
pos = beg + 1;
|
|
|
|
continue;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-04-08 21:42:30 +04:00
|
|
|
|
2016-05-19 18:51:51 +03:00
|
|
|
// Make sure it is separated from following entries.
|
|
|
|
std::string::size_type const end = beg + want.size();
|
|
|
|
if (end < have.size() && have[end] != ':') {
|
|
|
|
pos = beg + 1;
|
|
|
|
continue;
|
2016-05-25 16:27:54 +03:00
|
|
|
}
|
2016-05-19 18:51:51 +03:00
|
|
|
|
2008-04-08 21:42:30 +04:00
|
|
|
// Return the position of the path portion.
|
2016-05-19 18:51:51 +03:00
|
|
|
return beg;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-05-19 18:51:51 +03:00
|
|
|
|
|
|
|
// The desired rpath was not found.
|
|
|
|
return std::string::npos;
|
2008-04-08 21:42:30 +04:00
|
|
|
}
|
|
|
|
#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,
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string const& newRPath, std::string* emsg,
|
2008-05-27 18:22:03 +04:00
|
|
|
bool* changed)
|
2008-03-01 20:51:07 +03:00
|
|
|
{
|
|
|
|
#if defined(CMAKE_USE_ELF_PARSER)
|
2016-05-16 17:34:04 +03:00
|
|
|
if (changed) {
|
2008-05-27 18:22:03 +04:00
|
|
|
*changed = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-08-14 17:53:21 +04:00
|
|
|
int rp_count = 0;
|
2015-12-25 16:08:51 +03:00
|
|
|
bool remove_rpath = true;
|
2008-08-14 17:53:21 +04:00
|
|
|
cmSystemToolsRPathInfo rp[2];
|
2008-03-01 20:51:07 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
// Parse the ELF binary.
|
|
|
|
cmELF elf(file.c_str());
|
|
|
|
|
|
|
|
// Get the RPATH and RUNPATH entries from it.
|
|
|
|
int se_count = 0;
|
2016-06-27 23:44:16 +03:00
|
|
|
cmELF::StringEntry const* se[2] = { CM_NULLPTR, CM_NULLPTR };
|
|
|
|
const char* se_name[2] = { CM_NULLPTR, CM_NULLPTR };
|
2016-05-16 17:34:04 +03:00
|
|
|
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()) {
|
|
|
|
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;
|
2008-08-14 17:53:21 +04:00
|
|
|
}
|
2016-08-18 21:36:29 +03:00
|
|
|
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
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
for (int i = 0; i < se_count; ++i) {
|
|
|
|
// 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-08-14 17:53:21 +04:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
// Make sure the current rpath contains the old rpath.
|
|
|
|
std::string::size_type pos =
|
|
|
|
cmSystemToolsFindRPath(se[i]->Value, oldRPath);
|
|
|
|
if (pos == std::string::npos) {
|
|
|
|
// If it contains the new rpath instead then it is okay.
|
|
|
|
if (cmSystemToolsFindRPath(se[i]->Value, newRPath) !=
|
|
|
|
std::string::npos) {
|
|
|
|
remove_rpath = false;
|
|
|
|
continue;
|
2008-03-02 22:35:23 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
if (emsg) {
|
|
|
|
std::ostringstream e;
|
|
|
|
/* clang-format off */
|
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.";
|
2016-05-16 17:34:04 +03:00
|
|
|
/* clang-format on */
|
|
|
|
*emsg = e.str();
|
2008-03-02 22:35:23 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return false;
|
2008-03-02 22:35:23 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03: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];
|
|
|
|
|
|
|
|
std::string::size_type prefix_len = pos;
|
|
|
|
|
|
|
|
// If oldRPath was at the end of the file's RPath, and newRPath is empty,
|
|
|
|
// we should remove the unnecessary ':' at the end.
|
|
|
|
if (newRPath.empty() && pos > 0 && se[i]->Value[pos - 1] == ':' &&
|
|
|
|
pos + oldRPath.length() == se[i]->Value.length()) {
|
|
|
|
prefix_len--;
|
2015-12-25 16:08:51 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
// Construct the new value which preserves the part of the path
|
|
|
|
// not being changed.
|
|
|
|
rp[rp_count].Value = se[i]->Value.substr(0, prefix_len);
|
|
|
|
rp[rp_count].Value += newRPath;
|
|
|
|
rp[rp_count].Value +=
|
|
|
|
se[i]->Value.substr(pos + oldRPath.length(), oldRPath.npos);
|
2008-08-14 17:53:21 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!rp[rp_count].Value.empty()) {
|
|
|
|
remove_rpath = false;
|
2015-12-25 16:08:51 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
// 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) {
|
|
|
|
if (emsg) {
|
|
|
|
*emsg = "The replacement path is too long for the ";
|
|
|
|
*emsg += se_name[i];
|
|
|
|
*emsg += " entry.";
|
2008-08-14 17:53:21 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return false;
|
2008-03-01 20:51:07 +03:00
|
|
|
}
|
2008-08-14 17:53:21 +04:00
|
|
|
|
2016-05-16 17:34:04 +03: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.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (rp_count == 0) {
|
2008-08-14 17:53:21 +04:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-03-01 20:51:07 +03:00
|
|
|
|
2015-12-25 16:08:51 +03:00
|
|
|
// If the resulting rpath is empty, just remove the entire entry instead.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (remove_rpath) {
|
2015-12-25 16:08:51 +03:00
|
|
|
return cmSystemTools::RemoveRPath(file, emsg, changed);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-12-25 16:08:51 +03:00
|
|
|
|
2008-08-15 17:47:21 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
// Open the file for update.
|
|
|
|
cmsys::ofstream f(file.c_str(),
|
|
|
|
std::ios::in | std::ios::out | std::ios::binary);
|
|
|
|
if (!f) {
|
|
|
|
if (emsg) {
|
|
|
|
*emsg = "Error opening file for update.";
|
2008-03-01 20:51:07 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return false;
|
2008-03-01 20:51:07 +03:00
|
|
|
}
|
2008-08-14 17:53:21 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
// Store the new RPATH and RUNPATH strings.
|
|
|
|
for (int i = 0; i < rp_count; ++i) {
|
|
|
|
// Seek to the RPATH position.
|
|
|
|
if (!f.seekp(rp[i].Position)) {
|
|
|
|
if (emsg) {
|
|
|
|
*emsg = "Error seeking to ";
|
|
|
|
*emsg += rp[i].Name;
|
|
|
|
*emsg += " position.";
|
2008-08-14 17:53:21 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return false;
|
2008-03-01 20:51:07 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03: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-08-14 17:53:21 +04:00
|
|
|
}
|
2008-03-01 20:51:07 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
// Make sure it wrote correctly.
|
|
|
|
if (!f) {
|
|
|
|
if (emsg) {
|
|
|
|
*emsg = "Error writing the new ";
|
|
|
|
*emsg += rp[i].Name;
|
|
|
|
*emsg += " string to the file.";
|
2008-08-14 17:53:21 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
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.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (changed) {
|
2008-08-14 17:53:21 +04:00
|
|
|
*changed = true;
|
2016-05-16 17:34:04 +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
|
|
|
|
2009-12-21 20:27:04 +03:00
|
|
|
bool cmSystemTools::VersionCompare(cmSystemTools::CompareOp op,
|
|
|
|
const char* lhss, const char* rhss)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
const char* endl = lhss;
|
|
|
|
const char* endr = rhss;
|
2014-10-25 21:27:40 +04:00
|
|
|
unsigned long lhs, rhs;
|
2009-12-21 20:27:04 +03:00
|
|
|
|
2014-10-25 21:27:40 +04:00
|
|
|
while (((*endl >= '0') && (*endl <= '9')) ||
|
2016-05-16 17:34:04 +03:00
|
|
|
((*endr >= '0') && (*endr <= '9'))) {
|
2014-10-25 21:27:40 +04:00
|
|
|
// Do component-wise comparison.
|
|
|
|
lhs = strtoul(endl, const_cast<char**>(&endl), 10);
|
|
|
|
rhs = strtoul(endr, const_cast<char**>(&endr), 10);
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (lhs < rhs) {
|
2009-12-21 20:27:04 +03:00
|
|
|
// lhs < rhs, so true if operation is LESS
|
2016-08-05 21:11:46 +03:00
|
|
|
return (op & cmSystemTools::OP_LESS) != 0;
|
2016-08-18 21:36:29 +03:00
|
|
|
}
|
|
|
|
if (lhs > rhs) {
|
2009-12-21 20:27:04 +03:00
|
|
|
// lhs > rhs, so true if operation is GREATER
|
2016-08-05 21:11:46 +03:00
|
|
|
return (op & cmSystemTools::OP_GREATER) != 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-10-25 21:27:40 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (*endr == '.') {
|
2014-10-25 21:27:40 +04:00
|
|
|
endr++;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-10-25 21:27:40 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (*endl == '.') {
|
2014-10-25 21:27:40 +04:00
|
|
|
endl++;
|
2009-12-21 20:27:04 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2009-12-21 20:27:04 +03:00
|
|
|
// lhs == rhs, so true if operation is EQUAL
|
2016-08-05 21:11:46 +03:00
|
|
|
return (op & cmSystemTools::OP_EQUAL) != 0;
|
2009-12-21 20:27:04 +03:00
|
|
|
}
|
|
|
|
|
2016-01-11 21:42:07 +03:00
|
|
|
bool cmSystemTools::VersionCompareEqual(std::string const& lhs,
|
|
|
|
std::string const& rhs)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
return cmSystemTools::VersionCompare(cmSystemTools::OP_EQUAL, lhs.c_str(),
|
|
|
|
rhs.c_str());
|
2016-01-11 21:42:07 +03:00
|
|
|
}
|
|
|
|
|
2015-09-30 23:22:43 +03:00
|
|
|
bool cmSystemTools::VersionCompareGreater(std::string const& lhs,
|
|
|
|
std::string const& rhs)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER, lhs.c_str(),
|
|
|
|
rhs.c_str());
|
2015-09-30 23:22:43 +03:00
|
|
|
}
|
|
|
|
|
2016-08-05 21:11:46 +03:00
|
|
|
bool cmSystemTools::VersionCompareGreaterEq(std::string const& lhs,
|
|
|
|
std::string const& rhs)
|
|
|
|
{
|
|
|
|
return cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
|
|
|
|
lhs.c_str(), rhs.c_str());
|
|
|
|
}
|
|
|
|
|
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)
|
2016-05-16 17:34:04 +03:00
|
|
|
if (removed) {
|
2008-08-14 17:53:26 +04:00
|
|
|
*removed = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-08-14 17:53:17 +04:00
|
|
|
int zeroCount = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
unsigned long zeroPosition[2] = { 0, 0 };
|
|
|
|
unsigned long zeroSize[2] = { 0, 0 };
|
2008-08-14 17:53:17 +04:00
|
|
|
unsigned long bytesBegin = 0;
|
2008-04-14 23:02:34 +04:00
|
|
|
std::vector<char> bytes;
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
// Parse the ELF binary.
|
|
|
|
cmELF elf(file.c_str());
|
2008-04-14 23:02:34 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
// Get the RPATH and RUNPATH entries from it and sort them by index
|
|
|
|
// in the dynamic section header.
|
|
|
|
int se_count = 0;
|
2016-06-27 23:44:16 +03:00
|
|
|
cmELF::StringEntry const* se[2] = { CM_NULLPTR, CM_NULLPTR };
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmELF::StringEntry const* se_rpath = elf.GetRPath()) {
|
|
|
|
se[se_count++] = se_rpath;
|
|
|
|
}
|
|
|
|
if (cmELF::StringEntry const* se_runpath = elf.GetRunPath()) {
|
|
|
|
se[se_count++] = se_runpath;
|
|
|
|
}
|
|
|
|
if (se_count == 0) {
|
|
|
|
// There is no RPATH or RUNPATH anyway.
|
|
|
|
return true;
|
2008-08-14 17:53:17 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
if (se_count == 2 && se[1]->IndexInSection < se[0]->IndexInSection) {
|
|
|
|
std::swap(se[0], se[1]);
|
2008-08-14 17:53:17 +04:00
|
|
|
}
|
2008-04-14 23:02:34 +04:00
|
|
|
|
2016-05-16 17:34:04 +03: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) {
|
|
|
|
*emsg = "DYNAMIC section contains a DT_NULL before the end.";
|
2008-04-14 23:02:34 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return false;
|
2008-08-14 17:53:17 +04:00
|
|
|
}
|
2008-04-14 23:02:34 +04:00
|
|
|
|
2016-05-16 17:34:04 +03: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);
|
|
|
|
|
|
|
|
// 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)) {
|
|
|
|
if (emsg) {
|
|
|
|
*emsg = "Failed to read DYNAMIC section header.";
|
2008-04-14 23:02:34 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return false;
|
2008-04-14 23:02:34 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
data += sz;
|
2008-04-14 23:02:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file for update.
|
2014-01-04 09:47:13 +04:00
|
|
|
cmsys::ofstream f(file.c_str(),
|
2016-05-16 17:34:04 +03:00
|
|
|
std::ios::in | std::ios::out | std::ios::binary);
|
|
|
|
if (!f) {
|
|
|
|
if (emsg) {
|
2008-04-14 23:02:34 +04:00
|
|
|
*emsg = "Error opening file for update.";
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return false;
|
|
|
|
}
|
2008-04-14 23:02:34 +04:00
|
|
|
|
|
|
|
// Write the new DYNAMIC table header.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!f.seekp(bytesBegin)) {
|
|
|
|
if (emsg) {
|
2008-04-14 23:02:34 +04:00
|
|
|
*emsg = "Error seeking to DYNAMIC table header for RPATH.";
|
|
|
|
}
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!f.write(&bytes[0], bytes.size())) {
|
|
|
|
if (emsg) {
|
|
|
|
*emsg = "Error replacing DYNAMIC table header.";
|
2008-04-14 23:02:34 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return false;
|
|
|
|
}
|
2008-04-14 23:02:34 +04:00
|
|
|
|
2008-08-14 17:53:17 +04:00
|
|
|
// Fill the RPATH and RUNPATH strings with zero bytes.
|
2016-05-16 17:34:04 +03:00
|
|
|
for (int i = 0; i < zeroCount; ++i) {
|
|
|
|
if (!f.seekp(zeroPosition[i])) {
|
|
|
|
if (emsg) {
|
2008-08-14 17:53:17 +04:00
|
|
|
*emsg = "Error seeking to RPATH position.";
|
2008-04-14 23:02:34 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (unsigned long j = 0; j < zeroSize[i]; ++j) {
|
2008-08-14 17:53:17 +04:00
|
|
|
f << '\0';
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!f) {
|
|
|
|
if (emsg) {
|
2008-08-14 17:53:17 +04:00
|
|
|
*emsg = "Error writing the empty rpath string to the file.";
|
2008-04-14 23:02:34 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return false;
|
2008-04-14 23:02:34 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-08-14 17:53:17 +04:00
|
|
|
|
|
|
|
// Everything was updated successfully.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (removed) {
|
2008-08-14 17:53:26 +04:00
|
|
|
*removed = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
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();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!se) {
|
2008-04-14 23:02:34 +04:00
|
|
|
se = elf.GetRunPath();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-04-14 23:02:34 +04:00
|
|
|
|
|
|
|
// Make sure the current rpath contains the new rpath.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (newRPath.empty()) {
|
|
|
|
if (!se) {
|
2008-04-14 23:02:34 +04:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
|
|
|
if (se &&
|
|
|
|
cmSystemToolsFindRPath(se->Value, newRPath) != std::string::npos) {
|
2008-04-14 23:02:34 +04:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-04-14 23:02:34 +04:00
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
(void)file;
|
|
|
|
(void)newRPath;
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
2010-06-04 22:32:08 +04:00
|
|
|
|
|
|
|
bool cmSystemTools::RepeatedRemoveDirectory(const char* dir)
|
|
|
|
{
|
|
|
|
// Windows sometimes locks files temporarily so try a few times.
|
2016-05-16 17:34:04 +03:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
if (cmSystemTools::RemoveADirectory(dir)) {
|
2010-06-04 22:32:08 +04:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
cmSystemTools::Delay(100);
|
|
|
|
}
|
2010-06-04 22:32:08 +04:00
|
|
|
return false;
|
|
|
|
}
|
2011-02-15 16:34:14 +03:00
|
|
|
|
|
|
|
std::vector<std::string> cmSystemTools::tokenize(const std::string& str,
|
2016-05-16 17:34:04 +03:00
|
|
|
const std::string& sep)
|
2011-02-15 16:34:14 +03:00
|
|
|
{
|
|
|
|
std::vector<std::string> tokens;
|
|
|
|
std::string::size_type tokend = 0;
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
do {
|
|
|
|
std::string::size_type tokstart = str.find_first_not_of(sep, tokend);
|
|
|
|
if (tokstart == std::string::npos) {
|
|
|
|
break; // no more tokens
|
|
|
|
}
|
|
|
|
tokend = str.find_first_of(sep, tokstart);
|
|
|
|
if (tokend == std::string::npos) {
|
2011-02-15 16:34:14 +03:00
|
|
|
tokens.push_back(str.substr(tokstart));
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
|
|
|
tokens.push_back(str.substr(tokstart, tokend - tokstart));
|
|
|
|
}
|
|
|
|
} while (tokend != std::string::npos);
|
2011-02-15 16:34:14 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (tokens.empty()) {
|
2011-02-15 16:34:14 +03:00
|
|
|
tokens.push_back("");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-02-15 16:34:14 +03:00
|
|
|
return tokens;
|
|
|
|
}
|
2014-11-26 01:45:26 +03:00
|
|
|
|
2014-12-05 17:18:11 +03:00
|
|
|
bool cmSystemTools::StringToLong(const char* str, long* value)
|
2014-11-26 01:45:26 +03:00
|
|
|
{
|
2014-12-05 17:18:11 +03:00
|
|
|
errno = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
char* endp;
|
2014-12-05 17:18:11 +03:00
|
|
|
*value = strtol(str, &endp, 10);
|
|
|
|
return (*endp == '\0') && (endp != str) && (errno == 0);
|
2014-11-26 01:45:26 +03:00
|
|
|
}
|
2015-06-24 16:41:58 +03:00
|
|
|
|
|
|
|
bool cmSystemTools::StringToULong(const char* str, unsigned long* value)
|
|
|
|
{
|
|
|
|
errno = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
char* endp;
|
2015-06-24 16:41:58 +03:00
|
|
|
*value = strtoul(str, &endp, 10);
|
|
|
|
return (*endp == '\0') && (endp != str) && (errno == 0);
|
|
|
|
}
|