forked from carbonengine/trinity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifiedTime.cpp
More file actions
216 lines (182 loc) · 4.62 KB
/
Copy pathModifiedTime.cpp
File metadata and controls
216 lines (182 loc) · 4.62 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright © 2023 CCP ehf.
#include "stdafx.h"
#include "ModifiedTime.h"
#include "WorkQueue.h"
#include "Macro.h"
#include "CachingIncludeHandler.h"
#include "EffectData.h"
#include "md5.h"
#include "ShaderCompilerConfig.h"
namespace
{
CachingIncludeHandler s_includeHandler;
char* GetNextWord( char* string )
{
char* end = string;
while( *end && *end != ' ' && *end != '\n' )
{
++end;
}
switch( *end )
{
case 0:
return end;
case '\n':
*end = 0;
return end;
default:
*end = 0;
return end + 1;
}
}
struct HashCheckArguments
{
std::string sourcePath;
std::string outputPath;
std::vector<Macro> defines;
};
std::mutex s_modifiedOutputsCS;
std::set<std::string> s_modifiedOutputs;
const std::regex s_include( "#[[:space:]]*include[[:space:]]*[<\"]([^>\"]*)" );
bool IsOutputUpToDate( const char* path, const std::string& sourceHash )
{
FILE* file = nullptr;
fopen_s( &file, path, "rb" );
if( !file )
{
return false;
}
uint32_t fileVersion;
if( fread( &fileVersion, sizeof( fileVersion ), 1, file ) != 1 )
{
fclose( file );
return false;
}
if( fileVersion != DATA_VERSION )
{
fclose( file );
return false;
}
uint8_t compilerVersion[4];
if( fread( compilerVersion, sizeof( compilerVersion ), 1, file ) != 1 )
{
fclose( file );
return false;
}
if( compilerVersion[0] != ShaderCompilerVersion[0] || compilerVersion[1] != ShaderCompilerVersion[1] || compilerVersion[2] != ShaderCompilerVersion[2] )
{
fclose( file );
return false;
}
char buffer[MD5::HashBytes * 2];
if( fread( buffer, sizeof( buffer ), 1, file ) != 1 )
{
fclose( file );
return false;
}
fclose( file );
return sourceHash == std::string( buffer, buffer + sizeof( buffer ) );
}
bool CheckHash( const HashCheckArguments& query )
{
{
std::lock_guard scope( s_modifiedOutputsCS );
if( s_modifiedOutputs.find( query.outputPath ) != s_modifiedOutputs.end() )
{
return true;
}
}
auto in = ::GetSourceHash( query.sourcePath.c_str(), query.defines );
if( !IsOutputUpToDate( query.outputPath.c_str(), in ) )
{
std::lock_guard scope( s_modifiedOutputsCS );
s_modifiedOutputs.insert( query.outputPath );
}
return true;
}
void GetSourceHash( const char* sourcePath, const char* parentData, const char* rootPath, std::set<std::string>& visited, MD5& md5 )
{
if( visited.find( sourcePath ) != end( visited ) )
{
return;
}
visited.insert( sourcePath );
if( auto opened = s_includeHandler.Open( sourcePath, parentData, rootPath ) )
{
md5.add( opened->data, opened->size );
std::cmatch match;
auto begin = opened->data;
while( std::regex_search( begin, opened->data + opened->size, match, s_include ) )
{
GetSourceHash( match[1].str().c_str(), opened->data, rootPath, visited, md5 );
begin += match.position() + match.length();
}
}
}
}
extern unsigned g_optimizationLevel;
extern bool g_avoidFlowControl;
extern bool g_generatePDB;
extern bool g_skipOptimization;
std::string GetSourceHash( const char* sourcePath, const std::vector<Macro>& defines )
{
MD5 md5;
std::set<std::string> visited;
// Include defines from command line and relevant compiler settings into the hash
for( auto& each : defines )
{
md5.add( each.name.c_str(), each.name.length() );
md5.add( each.value.c_str(), each.value.length() );
}
// safety mechanism so that teamcity overwrites submitted shaders that have been built with undesirable settings
md5.add( &g_optimizationLevel, sizeof( g_optimizationLevel ) );
md5.add( &g_avoidFlowControl, sizeof( g_avoidFlowControl ) );
md5.add( &g_generatePDB, sizeof( g_generatePDB ) );
md5.add( &g_skipOptimization, sizeof( g_skipOptimization ) );
GetSourceHash( sourcePath, nullptr, sourcePath, visited, md5 );
return md5.getHash();
}
void PrintOutOfDateFiles( size_t workerCount )
{
WorkQueue<HashCheckArguments, decltype( &CheckHash )> workQueue( workerCount, &CheckHash );
char buffer[4096];
while( !feof( stdin ) )
{
if( !fgets( buffer, sizeof( buffer ), stdin ) )
{
break;
}
HashCheckArguments query;
char* line = buffer;
auto next = GetNextWord( line );
query.sourcePath = line;
line = next;
next = GetNextWord( line );
query.outputPath = line;
line = next;
while( *line )
{
Macro macro;
next = GetNextWord( line );
if( *next == 0 )
{
printf( "Invalid input string near %s\n", line );
fflush( stdout );
return;
}
macro.name = line;
line = next;
next = GetNextWord( line );
macro.value = line;
query.defines.push_back( macro );
line = next;
}
workQueue.Put( query );
}
workQueue.Join();
for( auto it = s_modifiedOutputs.begin(); it != s_modifiedOutputs.end(); ++it )
{
puts( it->c_str() );
puts( "\n" );
}
}