forked from keysight-eggplant/libs-xcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBXFileReference.m
378 lines (330 loc) · 10.7 KB
/
PBXFileReference.m
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#import <stdlib.h>
#import "PBXCommon.h"
#import "PBXFileReference.h"
#import "PBXGroup.h"
#import "GSXCBuildContext.h"
#import "NSArray+Additions.h"
#import "NSString+PBXAdditions.h"
@implementation PBXFileReference
// Methods....
- (NSString *) sourceTree // getter
{
return sourceTree;
}
- (void) setSourceTree: (NSString *)object; // setter
{
ASSIGN(sourceTree,object);
}
- (NSString *) lastKnownFileType // getter
{
return lastKnownFileType;
}
- (void) setLastKnownFileType: (NSString *)object; // setter
{
ASSIGN(lastKnownFileType,object);
}
- (NSString *) path // getter
{
return path;
}
- (void) setPath: (NSString *)object; // setter
{
ASSIGN(path,object);
}
- (NSString *) fileEncoding // getter
{
return fileEncoding;
}
- (void) setFileEncoding: (NSString *)object; // setter
{
ASSIGN(fileEncoding,object);
}
- (NSString *) explicitFileType
{
return explicitFileType;
}
- (void) setExplicitFileType: (NSString *)object
{
ASSIGN(explicitFileType,object);
}
- (NSString *) name;
{
return name;
}
- (void) setName: (NSString *)object
{
ASSIGN(name,object);
}
- (NSString *) plistStructureDefinitionIdentifier
{
return plistStructureDefinitionIdentifier;
}
- (void) setPlistStructureDefinitionIdentifier: (NSString *)object
{
ASSIGN(plistStructureDefinitionIdentifier,object);
}
- (NSString *) xcLanguageSpecificationIdentifier
{
return xcLanguageSpecificationIdentifier;
}
- (void) setXcLanguageSpecificationIdentifier: (NSString *)object
{
ASSIGN(xcLanguageSpecificationIdentifier, object);
}
- (NSString *) lineEnding
{
return lineEnding;
}
- (void) setLineEnding: (NSString *)object
{
ASSIGN(lineEnding,object);
}
- (NSString *) resolvePathFor: (id)object
withGroup: (PBXGroup *)group
found: (BOOL *)found
{
NSString *result = @"";
NSArray *children = [group children];
NSEnumerator *en = [children objectEnumerator];
id file = nil;
while((file = [en nextObject]) != nil && *found == NO)
{
if(file == self) // have we found ourselves??
{
NSString *filePath = ([file path] == nil)?@"":[file path];
result = filePath;
*found = YES;
break;
}
else if([file isKindOfClass: [PBXGroup class]])
{
NSString *filePath = ([file path] == nil)?@"":[file path];
result = [filePath stringByAppendingPathComponent:
[self resolvePathFor: object
withGroup: file
found: found]];
}
}
return result;
}
- (NSArray *) allSubdirsAtPath: (NSString *)apath
{
NSDebugLog(@"apath = %@", apath);
NSMutableArray *results = [NSMutableArray arrayWithCapacity:10];
NSFileManager *manager = [[NSFileManager alloc] init];
NSDirectoryEnumerator *en = [manager enumeratorAtPath:apath];
NSString *fileName = nil;
NSDebugLog(@"cwd = %@", [manager currentDirectoryPath]);
while((fileName = [en nextObject]) != nil)
{
BOOL isDir = NO;
NSString *dirToAdd = fileName; //[fileName stringByDeletingLastPathComponent];
[manager fileExistsAtPath: fileName
isDirectory: &isDir];
if (isDir && [results containsObject: dirToAdd] == NO)
{
NSString *ext = [dirToAdd pathExtension];
if ([ext isEqualToString: @"app"] ||
[ext isEqualToString: @"xcassets"] ||
[ext isEqualToString: @"lproj"] ||
[dirToAdd containsString: @"build"] ||
[dirToAdd containsString: @"xcassets"] ||
[dirToAdd containsString: @"lproj"] ||
[dirToAdd containsString: @"xcodeproj"])
{
continue;
}
[results addObject: [dirToAdd stringByEscapingSpecialCharacters]];
NSDebugLog(@"adding dirToAdd = %@", dirToAdd);
}
}
NSDebugLog(@"results = %@", results);
RELEASE(manager);
return results;
}
- (NSString *) buildPath
{
PBXGroup *mainGroup = [[GSXCBuildContext sharedBuildContext] objectForKey: @"MAIN_GROUP"];
BOOL found = NO;
NSString *result = nil;
// Resolve path for the current file reference...
result = [self resolvePathFor: self
withGroup: mainGroup
found: &found];
return result;
}
- (BOOL) build
{
GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
BOOL targetInSubdir = [[context objectForKey:@"TARGET_IN_SUBDIR"] isEqualToString:@"YES"];
NSString *of = [context objectForKey: @"OUTPUT_FILES"];
NSString *modified = [context objectForKey: @"MODIFIED_FLAG"];
NSString *outputFiles = (of == nil)?@"":of;
int result = 0;
NSError *error = nil;
NSFileManager *manager = [NSFileManager defaultManager];
// NSDebugLog(@"*** %@", sourceTree);
if(modified == nil)
{
modified = @"NO";
[context setObject: @"NO"
forKey: @"MODIFIED_FLAG"];
}
if([lastKnownFileType isEqualToString: @"sourcecode.c.objc"] ||
[lastKnownFileType isEqualToString: @"sourcecode.c.c"] ||
[lastKnownFileType isEqualToString: @"sourcecode.cpp.cpp"] ||
[lastKnownFileType isEqualToString: @"sourcecode.cpp.objcpp"])
{
char *proj_root = getenv("PROJECT_ROOT");
if (proj_root == NULL ||
strcmp(proj_root, "") == 0)
{
proj_root = "";
}
char *cc = getenv("CC");
if (cc == NULL ||
strcmp(cc, "") == 0)
{
cc = "`gnustep-config --variable=CC`";
}
NSString *buildPath = [[NSString stringWithCString: proj_root]
stringByAppendingPathComponent:
[self buildPath]];
NSArray *localHeaderPathsArray = [self allSubdirsAtPath:@"."];
NSString *fileName = [path lastPathComponent];
NSString *buildDir = [NSString stringWithCString: getenv("TARGET_BUILD_DIR")];
NSString *additionalHeaderDirs = [context objectForKey:@"INCLUDE_DIRS"];
NSString *derivedSrcHeaderDir = [context objectForKey: @"DERIVED_SOURCE_HEADER_DIR"];
NSString *compiler = [NSString stringWithCString: cc];
NSString *headerSearchPaths = [[context objectForKey: @"HEADER_SEARCH_PATHS"]
implodeArrayWithSeparator: @" -I"];
NSString *warningCflags = [[context objectForKey: @"WARNING_CFLAGS"]
implodeArrayWithSeparator: @" "];
NSString *localHeaderPaths = [localHeaderPathsArray implodeArrayWithSeparator:@" -I"];
NSDebugLog(@"localHeaderPathsArray = %@, %@", localHeaderPathsArray, localHeaderPaths);
NSDebugLog(@"Build path = %@, %@", [self buildPath], [[self buildPath] stringByDeletingFirstPathComponent]);
// blank these out if they are not used...
if(headerSearchPaths == nil)
{
headerSearchPaths = @"";
}
if(localHeaderPaths == nil)
{
localHeaderPaths = @"";
}
if(warningCflags == nil)
{
warningCflags = @"";
}
// If we have derived sources, then get the header directory and add it to the search path....
if(derivedSrcHeaderDir != nil)
{
if([[derivedSrcHeaderDir pathComponents] count] > 1)
{
headerSearchPaths = [headerSearchPaths stringByAppendingString:
[NSString stringWithFormat: @" -I%@ ",
[derivedSrcHeaderDir stringByDeletingLastPathComponent]]];
}
}
// If we have additional header dirs specified... then add them.
if(additionalHeaderDirs != nil)
{
headerSearchPaths = [headerSearchPaths stringByAppendingString: additionalHeaderDirs];
headerSearchPaths = [headerSearchPaths stringByAppendingString: localHeaderPaths];
}
// If the target is in the subdirectory, then override the preprending of
// the project root.
if(targetInSubdir)
{
buildPath = [self path];
}
// Sometimes, for some incomprehensible reason, the buildpath doesn't
// need the project dir pre-pended. This could be due to differences
// in different version of xcode. It must be removed to successfully
// compile the application...
if([manager fileExistsAtPath:buildPath] == NO)
{
buildPath = [self path];
}
NSString *outputPath = [buildDir stringByAppendingPathComponent:
[fileName stringByAppendingString: @".o"]];
outputFiles = [[outputFiles stringByAppendingString: outputPath]
stringByAppendingString: @" "];
if([compiler isEqualToString: @""] ||
compiler == nil)
{
compiler = @"`gnustep-config --variable=CC` -DGNUSTEP";
}
NSString *objCflags = @"";
if([lastKnownFileType isEqualToString: @"sourcecode.c.objc"])
{
objCflags = @"-fgnu-runtime -fconstant-string-class=NSConstantString";
}
NSString *std = [NSString stringWithCString: getenv("GCC_C_LANGUAGE_STANDARD") != NULL ?
getenv("GCC_C_LANGUAGE_STANDARD") : "" ];
if ([std length] > 0)
{
if([std isEqualToString:@"compiler-default"] == YES)
{
std = @"gnu99";
}
objCflags = [NSString stringWithFormat: @"%@ -std=%@",
objCflags, std];
}
NSString *configString = [context objectForKey: @"CONFIG_STRING"];
NSString *buildTemplate = @"%@ %@ -c %@ %@ %@ -o %@";
NSDebugLog(@"*** %@ %@", path, buildPath);
NSString *compilePath = ([[[self buildPath] pathComponents] count] > 1)?
[[[self buildPath] stringByDeletingFirstPathComponent] stringByEscapingSpecialCharacters]:[self buildPath];
NSString *buildCommand = [NSString stringWithFormat: buildTemplate,
compiler,
compilePath,
objCflags,
configString,
headerSearchPaths,
[outputPath stringByEscapingSpecialCharacters]];
NSDictionary *buildPathAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath: buildPath
error: &error];
NSDictionary *outputPathAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath: outputPath
error: &error];
NSDate *buildPathDate = [buildPathAttributes fileModificationDate];
NSDate *outputPathDate = [outputPathAttributes fileModificationDate];
if(outputPathDate != nil)
{
if([buildPathDate compare: outputPathDate] == NSOrderedDescending)
{
// puts([[NSString stringWithFormat: @"\t** Rebuilding: %@",buildCommand] cString]);
result = system([buildCommand cString]);
if([modified isEqualToString: @"NO"])
{
modified = @"YES";
[context setObject: @"YES"
forKey: @"MODIFIED_FLAG"];
}
}
else
{
puts([[NSString stringWithFormat: @"\t** Already built: %@",buildPath] cString]);
}
}
else
{
NSDebugLog(@"\t%@",buildCommand);
result = system([buildCommand cString]);
if([modified isEqualToString: @"NO"])
{
modified = @"YES";
[context setObject: @"YES"
forKey: @"MODIFIED_FLAG"];
}
}
[context setObject: outputFiles forKey: @"OUTPUT_FILES"];
}
return (result == 0);
}
- (NSString *) description
{
NSString *s = [super description];
return [s stringByAppendingFormat: @" <%@, %@>", path, lastKnownFileType];
}
@end