-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdependency.cpp
More file actions
68 lines (58 loc) · 1.63 KB
/
dependency.cpp
File metadata and controls
68 lines (58 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "dependency.hpp"
#include <iostream>
#ifdef _WIN32
#include <windows.h>
#include <sys/types.h>
#include <sys/stat.h>
#else
#include <sys/stat.h>
#include <unistd.h>
#endif
DependencyChecker::DependencyChecker() {
}
bool DependencyChecker::fileExists(const std::string& filename) {
#ifdef _WIN32
struct _stat buffer;
return (_stat(filename.c_str(), &buffer) == 0);
#else
struct stat buffer;
return (stat(filename.c_str(), &buffer) == 0);
#endif
}
time_t DependencyChecker::getFileModTime(const std::string& filename) {
// 检查缓存
auto it = fileTimeCache.find(filename);
if (it != fileTimeCache.end()) {
return it->second;
}
#ifdef _WIN32
struct _stat fileInfo;
if (_stat(filename.c_str(), &fileInfo) != 0) {
return 0;
}
#else
struct stat fileInfo;
if (stat(filename.c_str(), &fileInfo) != 0) {
return 0;
}
#endif
time_t modTime = fileInfo.st_mtime;
fileTimeCache[filename] = modTime;
return modTime;
}
bool DependencyChecker::needsRecompile(const std::string& sourceFile, const std::string& objectFile) {
// 如果目标文件不存在,需要编译
if (!fileExists(objectFile)) {
return true;
}
// 如果源文件不存在,报错
if (!fileExists(sourceFile)) {
std::cerr << "Error: Source file does not exist: " << sourceFile << std::endl;
return false;
}
// 比较修改时间
time_t sourceTime = getFileModTime(sourceFile);
time_t objectTime = getFileModTime(objectFile);
// 如果源文件比目标文件新,需要重新编译
return sourceTime > objectTime;
}