Debian Codegraph @

XCode/GSXCBuildDatabase.m

raw
1 /*
2 Copyright (C) 2022 Free Software Foundation, Inc.
3
4 Written by: Gregory John Casamento <[email protected]>
5 Date: Nov 2022
6
7 This file is part of the GNUstep XCode Library
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free
21 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110 USA.
23 */
24
25 #import <Foundation/NSDictionary.h>
26 #import <Foundation/NSArray.h>
27 #import <Foundation/NSString.h>
28 #import <Foundation/NSFileManager.h>
29
30 #import <XCode/GSXCBuildContext.h>
31 #import <XCode/GSXCBuildDatabase.h>
32 #import <XCode/PBXSourcesBuildPhase.h>
33 #import <XCode/PBXBuildFile.h>
34
35 #import "GSXCColors.h"
36 #import "xcsystem.h"
37
38 @implementation GSXCRecord : NSObject
39
40 + (instancetype) recordWithContentsOfFile: (NSString *)path
41 {
42 return [[self alloc] initWithContentsOfFile: path];
43 }
44
45 - (instancetype) initWithContentsOfFile: (NSString *)path
46 {
47 NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
48 return [self initWithDictionary: dict];
49 }
50
51 - (instancetype) initWithDictionary: (NSDictionary *)dict
52 {
53 self = [super init];
54 if (self != nil)
55 {
56 ASSIGN(_dictionary, [dict mutableCopy]);
57 }
58 return self;
59 }
60
61 - (instancetype) init
62 {
63 self = [super init];
64 if (self != nil)
65 {
66 ASSIGN(_dictionary, [NSMutableDictionary dictionary]);
67 }
68 return self;
69 }
70
71 - (void) dealloc
72 {
73 RELEASE(_dictionary);
74
75 [super dealloc];
76 }
77
78 - (id) copyWithZone: (NSZone *)z
79 {
80 Class c = [self class];
81 id copy = [[c alloc] initWithDictionary: _dictionary];
82
83 return copy;
84 }
85
86 - (NSDictionary *) dictionary
87 {
88 return [_dictionary copy];
89 }
90
91 @end
92
93 @implementation GSXCFileRecord : GSXCRecord
94
95 - (instancetype) initWithDictonary: (NSDictionary *)dict
96 {
97 self = [super initWithDictionary: dict];
98 if (self != nil)
99 {
100 NSString *fn = [dict objectForKey: @"fileName"];
101 NSDate *dm = [dict objectForKey: @"dateModified"];
102 NSDate *db = [dict objectForKey: @"dateBuilt"];
103
104 [self setFileName: fn];
105 [self setDateModified: dm];
106 [self setDateBuilt: db];
107 }
108 return self;
109 }
110
111 - (instancetype) initWithFile: (PBXBuildFile *)f path: (NSString *)path
112 {
113 self = [super init];
114 if (self != nil)
115 {
116 NSError *error = nil;
117 NSFileManager *mgr = [NSFileManager defaultManager];
118 PBXFileReference *fr = [f fileRef];
119 NSString *fullPath = [fr buildPath];
120 NSString *fileName = [fullPath lastPathComponent];
121 NSDictionary *attrs = [mgr attributesOfItemAtPath: fullPath error: &error];
122
123 ASSIGN(_fileReference, fr);
124 [self setFileName: fileName];
125
126 if (error == nil)
127 {
128 NSDate *srcModified = [attrs objectForKey: NSFileModificationDate];
129 NSString *outputPath = [path stringByAppendingPathComponent:
130 [fileName stringByAppendingString: @".o"]];
131 NSDictionary *objAttrs = [mgr attributesOfItemAtPath: outputPath error: &error];
132
133 if (srcModified == nil)
134 {
135 srcModified = [attrs objectForKey: NSFileCreationDate];
136 [self setDateModified: srcModified];
137 }
138 else
139 {
140 [self setDateModified: srcModified];
141 }
142
143 if (error == nil)
144 {
145 NSDate *objModified = [objAttrs objectForKey: NSFileModificationDate];
146 if (objModified == nil)
147 {
148 objModified = [objAttrs objectForKey: NSFileCreationDate];
149 }
150
151 [self setDateBuilt: objModified];
152 }
153 else
154 {
155 [self setDateBuilt: [NSDate distantPast]];
156 }
157 }
158 else
159 {
160 NSDebugLog(@"file = %@, error = %@", fullPath, error);
161 }
162
163 NSDebugLog(@"%@ - %@, %@", _fileName, _dateModified, _dateBuilt);
164 }
165 return self;
166 }
167
168 + (instancetype) recordWithBuildFile: (PBXBuildFile *)f path: (NSString *)path
169 {
170 return AUTORELEASE( [[self alloc] initWithFile: f path: path] );
171 }
172
173 - (id) copyWithZone: (NSZone *)z
174 {
175 Class c = [self class];
176 id copy = [[c alloc] initWithDictionary: _dictionary];
177
178 return copy;
179 }
180
181 - (void) dealloc
182 {
183 RELEASE(_fileName);
184 RELEASE(_dateModified);
185 RELEASE(_dateBuilt);
186
187 [super dealloc];
188 }
189
190 - (void) setFileName: (NSString *)fn
191 {
192 [_dictionary setObject: fn forKey: @"fileName"];
193 ASSIGN(_fileName, fn);
194 }
195
196 - (NSString *) fileName
197 {
198 return _fileName;
199 }
200
201 - (void) setDateModified: (NSDate *)d
202 {
203 [_dictionary setObject: d forKey: @"dateModified"];
204 ASSIGN(_dateModified, d);
205 }
206
207 - (NSDate *) dateModified
208 {
209 return _dateModified;
210 }
211
212 - (void) setDateBuilt: (NSDate *)d
213 {
214 [_dictionary setObject: d forKey: @"dateBuilt"];
215 ASSIGN(_dateBuilt, d);
216 }
217
218 - (NSDate *) dateBuilt
219 {
220 return _dateBuilt;
221 }
222
223 - (PBXFileReference *) fileReference
224 {
225 return _fileReference;
226 }
227
228 @end
229
230 @implementation GSXCBuildDatabase : NSObject
231
232 + (instancetype) buildDatabaseWithTarget: (PBXTarget *)target
233 {
234 return AUTORELEASE( [[self alloc] initWithTarget: target] );
235 }
236
237 - (BOOL) _construct
238 {
239 GSXCBuildContext *ctx = [GSXCBuildContext sharedBuildContext];
240 NSFileManager *mgr = [NSFileManager defaultManager];
241 NSEnumerator *en = [[_target buildPhases] objectEnumerator];
242 PBXBuildPhase *p = nil;
243 NSString *buildDir = [ctx objectForKey: @"TARGET_BUILD_DIR"];
244
245 if (buildDir == nil)
246 {
247 buildDir = @"./build";
248 }
249
250 // if build dir doesn't exist, then it's a fresh build... just return.
251 if ([mgr fileExistsAtPath: buildDir] == NO)
252 {
253 return NO;
254 }
255
256 buildDir = [buildDir stringByAppendingPathComponent: [_target name]];
257
258 xcprintf("=== Evaluating existing build... %s\n", [[_target name] cStringUsingEncoding: NSUTF8StringEncoding]);
259 while ( (p = [en nextObject]) != nil )
260 {
261 NSDebugLog(@"Phase = %@", p);
262
263 if ([p isKindOfClass: [PBXSourcesBuildPhase class]])
264 {
265 NSArray *files = [p files];
266 NSEnumerator *fen = [files objectEnumerator];
267 PBXBuildFile *bf = nil;
268
269 while ( (bf = [fen nextObject]) != nil )
270 {
271 GSXCFileRecord *fr = [GSXCFileRecord recordWithBuildFile: bf path: buildDir];
272
273 if ([[fr dateModified] compare: [fr dateBuilt]] == NSOrderedDescending)
274 {
275 xcprintf("\t* Checking %s%s%s%s - %smodified%s\n", BOLD, CYAN, [[fr fileName] cString], RESET, GREEN, RESET);
276 [self addRecord: fr];
277 }
278 else
279 {
280 xcprintf("\t* Checking %s%s%s%s - %salready built%s\n", BOLD, CYAN, [[fr fileName] cString], RESET, YELLOW, RESET);
281 }
282 }
283 }
284 }
285
286 return YES;
287 }
288
289 - (instancetype) init
290 {
291 self = [super init];
292 if (self != nil)
293 {
294 _records = [[NSMutableArray alloc] initWithCapacity: 10];
295 [self setTarget: nil];
296 }
297 return self;
298 }
299
300 - (instancetype) initWithTarget: (PBXTarget *)target
301 {
302 self = [super init];
303 if (self != nil)
304 {
305 _records = [[NSMutableArray alloc] initWithCapacity: 10];
306 [self setTarget: target];
307 if( [self _construct] == NO )
308 {
309 return nil;
310 }
311 }
312 return self;
313 }
314
315 - (void) dealloc
316 {
317 RELEASE(_records);
318 _target = nil;
319
320 [super dealloc];
321 }
322
323 - (void) setTarget: (PBXTarget *)target
324 {
325 _target = target;
326 }
327
328 - (PBXTarget *) target
329 {
330 return _target;
331 }
332
333 - (void) addRecord: (GSXCRecord *)record
334 {
335 [_records addObject: record];
336 }
337
338 - (NSArray *) files
339 {
340 NSMutableArray *array = [NSMutableArray arrayWithCapacity: [_records count]];
341 NSEnumerator *en = [_records objectEnumerator];
342 id f = nil;
343
344 while ((f = [en nextObject]) != nil)
345 {
346 id fr = [f fileReference];
347 [array addObject: fr];
348 }
349
350 return array;
351 }
352
353 - (BOOL) isEmpty
354 {
355 return ([_records count] == 0);
356 }
357
358 @end