2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2005-10-12 21:36:07 +04: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.
|
2005-10-12 21:36:07 +04: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.
|
|
|
|
============================================================================*/
|
2005-10-12 21:36:07 +04:00
|
|
|
#include "cmFileTimeComparison.h"
|
|
|
|
|
2005-10-27 21:57:48 +04:00
|
|
|
// Use a hash table to avoid duplicate file time checks from disk.
|
2005-10-12 21:36:07 +04:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2016-06-23 01:16:15 +03:00
|
|
|
#ifdef CMake_HAVE_CXX_UNORDERED_MAP
|
2015-05-16 07:57:53 +03:00
|
|
|
#include <unordered_map>
|
|
|
|
#else
|
2016-05-16 17:34:04 +03:00
|
|
|
#include <cmsys/hash_map.hxx>
|
2005-10-12 21:36:07 +04:00
|
|
|
#endif
|
2015-05-16 07:57:53 +03:00
|
|
|
#endif
|
2005-10-12 21:36:07 +04:00
|
|
|
|
2013-12-05 09:17:24 +04:00
|
|
|
#include <cmsys/Encoding.hxx>
|
|
|
|
|
2005-10-27 21:57:48 +04:00
|
|
|
// Use a platform-specific API to get file times efficiently.
|
2005-10-12 21:36:07 +04:00
|
|
|
#if !defined(_WIN32) || defined(__CYGWIN__)
|
2016-05-16 17:34:04 +03:00
|
|
|
#define cmFileTimeComparison_Type struct stat
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/stat.h>
|
2005-10-12 21:36:07 +04:00
|
|
|
#else
|
2016-05-16 17:34:04 +03:00
|
|
|
#define cmFileTimeComparison_Type FILETIME
|
|
|
|
#include <windows.h>
|
2005-10-12 21:36:07 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
class cmFileTimeComparisonInternal
|
|
|
|
{
|
|
|
|
public:
|
2005-10-27 21:57:48 +04:00
|
|
|
// Internal comparison method.
|
2005-10-12 21:36:07 +04:00
|
|
|
inline bool FileTimeCompare(const char* f1, const char* f2, int* result);
|
|
|
|
|
2008-01-03 08:01:38 +03:00
|
|
|
bool FileTimesDiffer(const char* f1, const char* f2);
|
|
|
|
|
2005-10-12 21:36:07 +04:00
|
|
|
private:
|
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2005-10-27 21:57:48 +04:00
|
|
|
// Use a hash table to efficiently map from file name to modification time.
|
2005-10-12 21:36:07 +04:00
|
|
|
class HashString
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2005-10-12 21:36:07 +04:00
|
|
|
public:
|
2016-05-16 17:34:04 +03:00
|
|
|
size_t operator()(const std::string& s) const { return h(s.c_str()); }
|
2016-06-23 01:16:15 +03:00
|
|
|
#ifdef CMake_HAVE_CXX_UNORDERED_MAP
|
2015-05-16 07:57:53 +03:00
|
|
|
std::hash<const char*> h;
|
|
|
|
#else
|
2005-10-12 21:36:07 +04:00
|
|
|
cmsys::hash<const char*> h;
|
2015-05-16 07:57:53 +03:00
|
|
|
#endif
|
2016-05-16 17:34:04 +03:00
|
|
|
};
|
2016-06-23 01:16:15 +03:00
|
|
|
#ifdef CMake_HAVE_CXX_UNORDERED_MAP
|
2015-05-16 07:57:53 +03:00
|
|
|
typedef std::unordered_map<std::string,
|
|
|
|
#else
|
2014-02-10 09:21:34 +04:00
|
|
|
typedef cmsys::hash_map<std::string,
|
2015-05-16 07:57:53 +03:00
|
|
|
#endif
|
2016-05-16 17:34:04 +03:00
|
|
|
cmFileTimeComparison_Type, HashString>
|
|
|
|
FileStatsMap;
|
2005-10-12 21:36:07 +04:00
|
|
|
FileStatsMap Files;
|
|
|
|
#endif
|
|
|
|
|
2005-10-27 21:57:48 +04:00
|
|
|
// Internal methods to lookup and compare modification times.
|
2005-10-12 21:36:07 +04:00
|
|
|
inline bool Stat(const char* fname, cmFileTimeComparison_Type* st);
|
2005-10-27 21:57:48 +04:00
|
|
|
inline int Compare(cmFileTimeComparison_Type* st1,
|
|
|
|
cmFileTimeComparison_Type* st2);
|
2008-01-03 08:01:38 +03:00
|
|
|
inline bool TimesDiffer(cmFileTimeComparison_Type* st1,
|
|
|
|
cmFileTimeComparison_Type* st2);
|
2005-10-12 21:36:07 +04:00
|
|
|
};
|
|
|
|
|
2005-10-27 21:57:48 +04:00
|
|
|
bool cmFileTimeComparisonInternal::Stat(const char* fname,
|
|
|
|
cmFileTimeComparison_Type* st)
|
2005-10-12 21:36:07 +04:00
|
|
|
{
|
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2005-10-27 21:57:48 +04:00
|
|
|
// Use the stored time if available.
|
|
|
|
cmFileTimeComparisonInternal::FileStatsMap::iterator fit =
|
|
|
|
this->Files.find(fname);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (fit != this->Files.end()) {
|
2005-10-12 21:36:07 +04:00
|
|
|
*st = fit->second;
|
2005-10-13 18:07:12 +04:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-10-12 21:36:07 +04:00
|
|
|
#endif
|
2005-10-27 21:57:48 +04:00
|
|
|
|
2005-10-12 21:36:07 +04:00
|
|
|
#if !defined(_WIN32) || defined(__CYGWIN__)
|
2005-10-27 21:57:48 +04:00
|
|
|
// POSIX version. Use the stat function.
|
2005-10-12 21:36:07 +04:00
|
|
|
int res = ::stat(fname, st);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (res != 0) {
|
2005-10-12 21:36:07 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-10-12 21:36:07 +04:00
|
|
|
#else
|
2006-05-10 23:46:45 +04:00
|
|
|
// Windows version. Get the modification time from extended file
|
|
|
|
// attributes.
|
2005-10-27 21:57:48 +04:00
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fdata;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!GetFileAttributesExW(cmsys::Encoding::ToWide(fname).c_str(),
|
|
|
|
GetFileExInfoStandard, &fdata)) {
|
2005-10-12 21:36:07 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-10-27 21:57:48 +04:00
|
|
|
|
|
|
|
// Copy the file time to the output location.
|
|
|
|
*st = fdata.ftLastWriteTime;
|
2005-10-12 21:36:07 +04:00
|
|
|
#endif
|
2005-10-27 21:57:48 +04:00
|
|
|
|
2005-10-12 21:36:07 +04:00
|
|
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
2005-10-27 21:57:48 +04:00
|
|
|
// Store the time for future use.
|
2005-10-12 21:36:07 +04:00
|
|
|
this->Files[fname] = *st;
|
|
|
|
#endif
|
2005-10-27 21:57:48 +04:00
|
|
|
|
2005-10-12 21:36:07 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmFileTimeComparison::cmFileTimeComparison()
|
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Internals = new cmFileTimeComparisonInternal;
|
2005-10-12 21:36:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmFileTimeComparison::~cmFileTimeComparison()
|
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
delete this->Internals;
|
2005-10-12 21:36:07 +04:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmFileTimeComparison::FileTimeCompare(const char* f1, const char* f2,
|
|
|
|
int* result)
|
2005-10-12 21:36:07 +04:00
|
|
|
{
|
2006-03-15 19:02:08 +03:00
|
|
|
return this->Internals->FileTimeCompare(f1, f2, result);
|
2005-10-12 21:36:07 +04:00
|
|
|
}
|
|
|
|
|
2008-01-03 08:01:38 +03:00
|
|
|
bool cmFileTimeComparison::FileTimesDiffer(const char* f1, const char* f2)
|
|
|
|
{
|
|
|
|
return this->Internals->FileTimesDiffer(f1, f2);
|
|
|
|
}
|
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
int cmFileTimeComparisonInternal::Compare(cmFileTimeComparison_Type* s1,
|
2006-05-10 23:46:45 +04:00
|
|
|
cmFileTimeComparison_Type* s2)
|
2005-10-12 21:36:07 +04:00
|
|
|
{
|
|
|
|
#if !defined(_WIN32) || defined(__CYGWIN__)
|
2016-05-16 17:34:04 +03:00
|
|
|
#if CMake_STAT_HAS_ST_MTIM
|
2005-10-12 21:36:07 +04:00
|
|
|
// Compare using nanosecond resolution.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (s1->st_mtim.tv_sec < s2->st_mtim.tv_sec) {
|
2005-10-12 21:36:07 +04:00
|
|
|
return -1;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (s1->st_mtim.tv_sec > s2->st_mtim.tv_sec) {
|
2005-10-12 21:36:07 +04:00
|
|
|
return 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (s1->st_mtim.tv_nsec < s2->st_mtim.tv_nsec) {
|
2005-10-12 21:36:07 +04:00
|
|
|
return -1;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (s1->st_mtim.tv_nsec > s2->st_mtim.tv_nsec) {
|
2005-10-12 21:36:07 +04:00
|
|
|
return 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
#elif CMake_STAT_HAS_ST_MTIMESPEC
|
2015-10-08 20:37:37 +03:00
|
|
|
// Compare using nanosecond resolution.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (s1->st_mtimespec.tv_sec < s2->st_mtimespec.tv_sec) {
|
2015-10-08 20:37:37 +03:00
|
|
|
return -1;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (s1->st_mtimespec.tv_sec > s2->st_mtimespec.tv_sec) {
|
2015-10-08 20:37:37 +03:00
|
|
|
return 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (s1->st_mtimespec.tv_nsec < s2->st_mtimespec.tv_nsec) {
|
2015-10-08 20:37:37 +03:00
|
|
|
return -1;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (s1->st_mtimespec.tv_nsec > s2->st_mtimespec.tv_nsec) {
|
2015-10-08 20:37:37 +03:00
|
|
|
return 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
#else
|
2005-10-12 21:36:07 +04:00
|
|
|
// Compare using 1 second resolution.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (s1->st_mtime < s2->st_mtime) {
|
2005-10-12 21:36:07 +04:00
|
|
|
return -1;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (s1->st_mtime > s2->st_mtime) {
|
2005-10-12 21:36:07 +04:00
|
|
|
return 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
#endif
|
2005-10-27 21:57:48 +04:00
|
|
|
// Files have the same time.
|
2005-10-13 18:07:12 +04:00
|
|
|
return 0;
|
2005-10-12 21:36:07 +04:00
|
|
|
#else
|
2005-10-27 21:57:48 +04:00
|
|
|
// Compare using system-provided function.
|
2005-10-12 21:36:07 +04:00
|
|
|
return (int)CompareFileTime(s1, s2);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-01-03 08:01:38 +03:00
|
|
|
bool cmFileTimeComparisonInternal::TimesDiffer(cmFileTimeComparison_Type* s1,
|
|
|
|
cmFileTimeComparison_Type* s2)
|
|
|
|
{
|
|
|
|
#if !defined(_WIN32) || defined(__CYGWIN__)
|
2016-05-16 17:34:04 +03:00
|
|
|
#if CMake_STAT_HAS_ST_MTIM
|
2008-01-03 08:01:38 +03:00
|
|
|
// Times are integers in units of 1ns.
|
|
|
|
long long bil = 1000000000;
|
|
|
|
long long t1 = s1->st_mtim.tv_sec * bil + s1->st_mtim.tv_nsec;
|
|
|
|
long long t2 = s2->st_mtim.tv_sec * bil + s2->st_mtim.tv_nsec;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (t1 < t2) {
|
2008-01-03 08:01:38 +03:00
|
|
|
return (t2 - t1) >= bil;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (t2 < t1) {
|
2008-01-03 08:01:38 +03:00
|
|
|
return (t1 - t2) >= bil;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2008-01-03 08:01:38 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
#elif CMake_STAT_HAS_ST_MTIMESPEC
|
2015-10-08 20:37:37 +03:00
|
|
|
// Times are integers in units of 1ns.
|
|
|
|
long long bil = 1000000000;
|
|
|
|
long long t1 = s1->st_mtimespec.tv_sec * bil + s1->st_mtimespec.tv_nsec;
|
|
|
|
long long t2 = s2->st_mtimespec.tv_sec * bil + s2->st_mtimespec.tv_nsec;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (t1 < t2) {
|
2015-10-08 20:37:37 +03:00
|
|
|
return (t2 - t1) >= bil;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (t2 < t1) {
|
2015-10-08 20:37:37 +03:00
|
|
|
return (t1 - t2) >= bil;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-10-08 20:37:37 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
#else
|
2008-01-03 08:01:38 +03:00
|
|
|
// Times are integers in units of 1s.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (s1->st_mtime < s2->st_mtime) {
|
2008-01-03 08:01:38 +03:00
|
|
|
return (s2->st_mtime - s1->st_mtime) >= 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (s1->st_mtime > s2->st_mtime) {
|
2008-01-03 08:01:38 +03:00
|
|
|
return (s1->st_mtime - s2->st_mtime) >= 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2008-01-03 08:01:38 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
#endif
|
2008-01-03 08:01:38 +03:00
|
|
|
#else
|
|
|
|
// Times are integers in units of 100ns.
|
|
|
|
LARGE_INTEGER t1;
|
|
|
|
LARGE_INTEGER t2;
|
|
|
|
t1.LowPart = s1->dwLowDateTime;
|
|
|
|
t1.HighPart = s1->dwHighDateTime;
|
|
|
|
t2.LowPart = s2->dwLowDateTime;
|
|
|
|
t2.HighPart = s2->dwHighDateTime;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (t1.QuadPart < t2.QuadPart) {
|
2008-01-03 08:01:38 +03:00
|
|
|
return (t2.QuadPart - t1.QuadPart) >= static_cast<LONGLONG>(10000000);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (t2.QuadPart < t1.QuadPart) {
|
2008-01-03 08:01:38 +03:00
|
|
|
return (t1.QuadPart - t2.QuadPart) >= static_cast<LONGLONG>(10000000);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2008-01-03 08:01:38 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-01-03 08:01:38 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-10-27 21:57:48 +04:00
|
|
|
bool cmFileTimeComparisonInternal::FileTimeCompare(const char* f1,
|
2016-05-16 17:34:04 +03:00
|
|
|
const char* f2, int* result)
|
2005-10-12 21:36:07 +04:00
|
|
|
{
|
2005-10-27 21:57:48 +04:00
|
|
|
// Get the modification time for each file.
|
2005-10-12 21:36:07 +04:00
|
|
|
cmFileTimeComparison_Type s1;
|
2005-10-27 21:57:48 +04:00
|
|
|
cmFileTimeComparison_Type s2;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Stat(f1, &s1) && this->Stat(f2, &s2)) {
|
2005-10-27 21:57:48 +04:00
|
|
|
// Compare the two modification times.
|
|
|
|
*result = this->Compare(&s1, &s2);
|
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2005-10-27 21:57:48 +04:00
|
|
|
// No comparison available. Default to the same time.
|
|
|
|
*result = 0;
|
2005-10-12 21:36:07 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-10-12 21:36:07 +04:00
|
|
|
}
|
2008-01-03 08:01:38 +03:00
|
|
|
|
|
|
|
bool cmFileTimeComparisonInternal::FileTimesDiffer(const char* f1,
|
|
|
|
const char* f2)
|
|
|
|
{
|
|
|
|
// Get the modification time for each file.
|
|
|
|
cmFileTimeComparison_Type s1;
|
|
|
|
cmFileTimeComparison_Type s2;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Stat(f1, &s1) && this->Stat(f2, &s2)) {
|
2008-01-03 08:01:38 +03:00
|
|
|
// Compare the two modification times.
|
|
|
|
return this->TimesDiffer(&s1, &s2);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2008-01-03 08:01:38 +03:00
|
|
|
// No comparison available. Default to different times.
|
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2008-01-03 08:01:38 +03:00
|
|
|
}
|