Debian Codegraph @

XCode/PBXFileReference.m

raw
1 /*
2 Copyright (C) 2018, 2019, 2020, 2021 Free Software Foundation, Inc.
3
4 Written by: Gregory John Casamento <[email protected]>
5 Date: 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 <stdlib.h>
26 // #import <unistd.h>
27
28 #import "PBXCommon.h"
29 #import "PBXFileReference.h"
30 #import "PBXGroup.h"
31 #import "GSXCBuildContext.h"
32 #import "NSArray+Additions.h"
33 #import "NSString+PBXAdditions.h"
34 #import "PBXNativeTarget.h"
35 #import "XCConfigurationList.h"
36 #import "XCBuildConfiguration.h"
37
38 extern char **environ;
39 static NSLock *lock = nil;
40
41 @implementation PBXFileReference
42
43 + (void) initialize
44 {
45 lock = [[NSLock alloc] init];
46 }
47
48 + (NSString *) fileTypeFromPath: (NSString *)path
49 {
50 NSString *result = @"compiled.mach-o.executable";
51 NSString *ext = [path pathExtension];
52
53 if ([ext isEqualToString: @"m"])
54 {
55 result = @"sourcecode.c.objc";
56 }
57 else if ([ext isEqualToString: @"c"])
58 {
59 result = @"sourcecode.c.c";
60 }
61 else if ([ext isEqualToString: @"cc"]
62 || [ext isEqualToString: @"cpp"]
63 || [ext isEqualToString: @"C"]
64 || [ext isEqualToString: @"cxx"])
65 {
66 result = @"sourcecode.cpp.cpp";
67 }
68 else if ([ext isEqualToString: @"mm"])
69 {
70 result = @"sourcecode.cpp.objcpp";
71 }
72 else if ([ext isEqualToString: @"lex.yy"])
73 {
74 result = @"sourcecode.lex";
75 }
76 else if ([ext isEqualToString: @"yy.tab"])
77 {
78 result = @"sourcecode.yacc";
79 }
80 else if ([ext isEqualToString: @"app"])
81 {
82 result = @"file.xib";
83 }
84 else if ([ext isEqualToString: @"nib"])
85 {
86 result = @"file.nib";
87 }
88 else if ([ext isEqualToString: @"gorm"])
89 {
90 result = @"file.gorm"; // GS specific
91 }
92 else if ([ext isEqualToString: @"entitlements"])
93 {
94 result = @"text.plist.entitlements";
95 }
96
97 return result;
98 }
99
100 + (NSString *) extForFileType: (NSString *)type
101 {
102 NSString *result = @"";
103
104 if ([type isEqualToString: @"sourcecode.c.objc"])
105 {
106 result = @"m";
107 }
108 else if ([type isEqualToString: @"sourcecode.c.c"])
109 {
110 result = @"c";
111 }
112 else if ([type isEqualToString: @"sourcecode.cpp.cpp"])
113 {
114 result = @"cc";
115 }
116 else if ([type isEqualToString: @"sourcecode.cpp.objcpp"])
117 {
118 result = @"mm";
119 }
120 else if ([type isEqualToString: @"sourcecode.lex"])
121 {
122 result = @"lex.yy";
123 }
124 else if ([type isEqualToString: @"sourcecode.yacc"])
125 {
126 result = @"yy.tab";
127 }
128 else if ([type isEqualToString: @"wrapper.application"])
129 {
130 result = @"app";
131 }
132 else if ([type isEqualToString: @"file.xib"])
133 {
134 result = @"xib";
135 }
136 else if ([type isEqualToString: @"file.nib"])
137 {
138 result = @"nib";
139 }
140 else if ([type isEqualToString: @"file.gorm"])
141 {
142 result = @"gorm";
143 }
144 else if ([type isEqualToString: @"text.plist.entitlements"])
145 {
146 result = @"entitlements";
147 }
148
149 return result;
150 }
151
152 - (instancetype) initWithPath: (NSString *)path
153 {
154 self = [super init];
155 if (self != nil)
156 {
157 NSString *fileType = [PBXFileReference fileTypeFromPath: [path lastPathComponent]];
158
159 if ([fileType isEqualToString: @"compiled.mach-o.executable"])
160 {
161 [self setIncludeInIndex: @"0"];
162 }
163
164 if ([fileType isEqualToString: @"wrapper.application"])
165 {
166 [self setExplicitFileType: fileType];
167 }
168 else
169 {
170 [self setLastKnownFileType: fileType];
171 }
172
173 ASSIGN(_path, path);
174 [self setSourceTree: @"<group>"];
175 }
176 return self;
177 }
178
179 - (void) dealloc
180 {
181 RELEASE(_sourceTree);
182 RELEASE(_lastKnownFileType);
183 RELEASE(_path);
184 RELEASE(_fileEncoding);
185 RELEASE(_explicitFileType);
186 RELEASE(_usesTabs);
187 RELEASE(_indentWidth);
188 RELEASE(_tabWidth);
189 RELEASE(_name);
190 RELEASE(_includeInIndex);
191 RELEASE(_comments);
192 RELEASE(_plistStructureDefinitionIdentifier);
193 RELEASE(_xcLanguageSpecificationIdentifier);
194 RELEASE(_lineEnding);
195 RELEASE(_wrapsLines);
196
197 [super dealloc];
198 }
199
200 - (void) setTotalFiles: (NSUInteger)t
201 {
202 _totalFiles = t;
203 }
204
205 - (void) setCurrentFile: (NSUInteger)n
206 {
207 _currentFile = n;
208 }
209
210 - (void) setWrapsLines: (NSString *)o
211 {
212 ASSIGN(_wrapsLines, o);
213 }
214
215 - (NSString *) sourceTree // getter
216 {
217 return _sourceTree;
218 }
219
220 - (void) setSourceTree: (NSString *)object; // setter
221 {
222 ASSIGN(_sourceTree,object);
223 }
224
225 - (NSString *) lastKnownFileType // getter
226 {
227 return _lastKnownFileType;
228 }
229
230 - (void) setLastKnownFileType: (NSString *)object; // setter
231 {
232 ASSIGN(_lastKnownFileType,object);
233 }
234
235 - (NSString *) path // getter
236 {
237 return _path;
238 }
239
240 - (void) setPath: (NSString *)object; // setter
241 {
242 ASSIGN(_path,object);
243 }
244
245 - (NSString *) fileEncoding // getter
246 {
247 return _fileEncoding;
248 }
249
250 - (void) setFileEncoding: (NSString *)object; // setter
251 {
252 ASSIGN(_fileEncoding,object);
253 }
254
255 - (NSString *) explicitFileType
256 {
257 return _explicitFileType;
258 }
259
260 - (void) setExplicitFileType: (NSString *)object
261 {
262 ASSIGN(_explicitFileType,object);
263 }
264
265 - (NSString *) name;
266 {
267 return _name;
268 }
269
270 - (void) setName: (NSString *)object
271 {
272 ASSIGN(_name,object);
273 }
274
275 - (NSString *) plistStructureDefinitionIdentifier
276 {
277 return _plistStructureDefinitionIdentifier;
278 }
279
280 - (void) setPlistStructureDefinitionIdentifier: (NSString *)object
281 {
282 ASSIGN(_plistStructureDefinitionIdentifier,object);
283 }
284
285 - (NSString *) xcLanguageSpecificationIdentifier
286 {
287 return _xcLanguageSpecificationIdentifier;
288 }
289
290 - (void) setXcLanguageSpecificationIdentifier: (NSString *)object
291 {
292 ASSIGN(_xcLanguageSpecificationIdentifier, object);
293 }
294
295 - (NSString *) lineEnding
296 {
297 return _lineEnding;
298 }
299
300 - (void) setLineEnding: (NSString *)object
301 {
302 ASSIGN(_lineEnding,object);
303 }
304
305 - (void) setTarget: (PBXNativeTarget *)t
306 {
307 _target = t;
308 }
309
310 - (NSString *) includeInIndex
311 {
312 return _includeInIndex;
313 }
314
315 - (void) setIncludeInIndex: (NSString *)includeInIndex
316 {
317 ASSIGN(_includeInIndex, includeInIndex);
318 }
319
320 - (NSString *) _resolvePathFor: (id)object
321 withGroup: (PBXGroup *)group
322 found: (BOOL *)found
323 {
324 NSString *result = @"";
325 NSArray *children = [group children];
326 NSEnumerator *en = [children objectEnumerator];
327 id file = nil;
328 while((file = [en nextObject]) != nil && *found == NO)
329 {
330 if([[file path] isEqualToString: [self path]]) // have we found ourselves??
331 {
332 NSString *filePath = ([file path] == nil)?@"":[file path];
333 result = filePath;
334 *found = YES;
335 break;
336 }
337 else if([file isKindOfClass: [PBXGroup class]])
338 {
339 NSString *filePath = ([file path] == nil)?@"":[file path];
340 result = [filePath stringByAppendingPathComponent:
341 [self _resolvePathFor: object
342 withGroup: file
343 found: found]];
344 }
345 }
346 return result;
347 }
348
349 - (NSArray *) _allSubdirsAtPath: (NSString *)apath
350 {
351 NSMutableArray *results = [NSMutableArray arrayWithCapacity:10];
352 NSFileManager *manager = [[NSFileManager alloc] init];
353 NSDirectoryEnumerator *en = [manager enumeratorAtPath:apath];
354 NSString *filename = nil;
355
356 NSDebugLog(@"apath = %@", apath);
357 NSDebugLog(@"cwd = %@", [manager currentDirectoryPath]);
358
359 while((filename = [en nextObject]) != nil)
360 {
361 BOOL isDir = NO;
362 NSString *dirToAdd = nil;
363
364 [manager fileExistsAtPath: filename
365 isDirectory: &isDir];
366
367 if (isDir == NO)
368 {
369 if ([filename isEqualToString: @""])
370 {
371 dirToAdd = @".";
372 }
373
374 if ([[filename pathComponents] count] > 2)
375 {
376 continue;
377 }
378
379 if ([[filename pathExtension] isEqualToString: @"h"])
380 {
381 NSDebugLog(@"filename = %@", filename);
382 dirToAdd = [filename stringByDeletingLastPathComponent];
383 }
384 }
385 else
386 {
387 NSDebugLog(@"dir = %@", filename);
388 continue;
389 }
390
391 if (dirToAdd == nil)
392 {
393 continue;
394 }
395
396 if ([results containsObject: dirToAdd] == NO) // [dirToAdd stringByAddingQuotationMarks]] == NO)
397 {
398 NSString *ext = [dirToAdd pathExtension];
399 if ([ext isEqualToString: @"app"] ||
400 [ext isEqualToString: @"xcassets"] ||
401 [ext isEqualToString: @"lproj"] ||
402 [dirToAdd containsString: @"build"] ||
403 [dirToAdd containsString: @"pbxbuild"] ||
404 [dirToAdd containsString: @"xcassets"] ||
405 [dirToAdd containsString: @"lproj"] ||
406 [dirToAdd containsString: @"xcodeproj"] ||
407 [dirToAdd containsString: @".git"])
408 {
409 continue;
410 }
411
412 if ([dirToAdd isEqualToString: @""] == NO)
413 {
414 [results addObject: dirToAdd]; // [dirToAdd stringByAddingQuotationMarks]];
415 NSDebugLog(@"adding dirToAdd = %@", dirToAdd);
416 }
417 }
418 }
419
420 // For some reason repeating the first -I directive helps resolve some issues with
421 // finding headers.
422 id o = [results count] > 1 ? [results objectAtIndex: 1] : @".";
423 o = [o isEqualToString: @""] ? @"." : o;
424
425 [results addObject: o];
426 results = [[results arrayByRemovingDuplicateEntries] mutableCopy];
427
428 NSDebugLog(@"results = %@", results);
429 RELEASE(manager);
430 return results;
431 }
432
433 - (NSString *) productName
434 {
435 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
436 NSDictionary *ctx = [context currentContext];
437 XCConfigurationList *xcl = [ctx objectForKey: @"buildConfig"];
438 XCBuildConfiguration *xbc = [xcl defaultConfiguration];
439 NSDictionary *bs = [xbc buildSettings];
440 NSString *productName = [bs objectForKey: @"PRODUCT_NAME"];
441
442 return productName;
443 }
444
445 - (NSString *) buildPath
446 {
447 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
448 PBXGroup *mainGroup = [context objectForKey: @"MAIN_GROUP"];
449 BOOL found = NO;
450 NSString *result = nil, *r = nil;
451 NSDictionary *plistFile = [context config];
452 NSDictionary *remappedSource = [plistFile objectForKey: @"remappedSource"];
453
454 // Resolve path for the current file reference...
455 result = [self _resolvePathFor: self
456 withGroup: mainGroup
457 found: &found];
458
459 if ((r = [remappedSource objectForKey: result]) != nil)
460 {
461 xcputs([[NSString stringWithFormat: @"\t+ Remapped %s%s%@%s to -> %s%s%@%s", BOLD, YELLOW, result, RESET, BOLD, GREEN, r, RESET] cString]);
462 result = r;
463 }
464
465 return result;
466 }
467
468 - (NSArray *) substituteSearchPaths: (NSArray *)array
469 buildPath: (NSString *)buildPath
470 {
471 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
472 NSMutableArray *result = [NSMutableArray arrayWithCapacity: [array count]];
473 NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity: [array count]];
474 XCConfigurationList *list = [context objectForKey: @"buildConfig"];
475 NSMutableArray *allHeaders = [NSMutableArray arrayWithArray: array];
476 NSDictionary *plistFile = [context config];
477 NSArray *headerPaths = [plistFile objectForKey: @"headerPaths"];
478
479 XCBuildConfiguration *config = [[list buildConfigurations] objectAtIndex: 0];
480 NSDictionary *buildSettings = [config buildSettings];
481 NSMutableArray *headers = [buildSettings objectForKey: @"HEADER_SEARCH_PATHS"];
482
483 if ([headers isKindOfClass: [NSArray class]] &&
484 headers != nil)
485 {
486 [allHeaders addObjectsFromArray: headers];
487 }
488
489 if ([headerPaths isKindOfClass: [NSArray class]] &&
490 headerPaths != nil)
491 {
492 [allHeaders prependObjectsFromArray: headerPaths];
493 }
494
495 // get environment variables...
496 char **env = NULL;
497 for (env = environ; *env != 0; env++)
498 {
499 char *thisEnv = *env;
500 NSString *envStr = [NSString stringWithCString: thisEnv
501 encoding: NSUTF8StringEncoding];
502 NSArray *components = [envStr componentsSeparatedByString: @"="];
503
504 [dict setObject: [[components lastObject] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]]
505 forKey: [[components firstObject] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]]];
506 }
507
508 // Get project root
509 NSString *projDir = [NSString stringForEnvironmentVariable: @"PROJECT_ROOT"
510 defaultValue: @"."];
511 NSEnumerator *en = [allHeaders objectEnumerator];
512 NSString *s = nil;
513
514 while ((s = [en nextObject]) != nil)
515 {
516 if ([s isEqualToString: @"$(inherited)"])
517 continue;
518
519 NSString *o = [s stringByReplacingOccurrencesOfString: @"$(PROJECT_DIR)"
520 withString: projDir];
521 o = [o stringByReplacingOccurrencesOfString: @"${PROJECT_DIR}"
522 withString: projDir];
523 NSString *q = [o stringByReplacingEnvironmentVariablesWithValues];
524 if ([result containsObject: o] == NO)
525 {
526 [result addObject: o];
527 [result addObject: q];
528 }
529 }
530
531 return result;
532 }
533
534 - (NSArray *) _addParentPath: (NSString *)parent toPaths: (NSArray *)paths
535 {
536 NSMutableArray *result = [NSMutableArray arrayWithCapacity: [paths count]];
537 NSEnumerator *en = [paths objectEnumerator];
538 NSString *p = nil;
539
540 [result addObject: parent];
541 while((p = [en nextObject]) != nil)
542 {
543 NSString *newPath = [parent stringByAppendingPathComponent: p];
544 [result addObject: newPath];
545 }
546
547 return result;
548 }
549
550 - (NSString *) _headerStringForPath: (NSString *)apath
551 {
552 BOOL isDir;
553 NSFileManager *manager = [NSFileManager defaultManager];
554 NSArray *result = [NSArray array];
555
556 [manager fileExistsAtPath:apath isDirectory:&isDir];
557 if (isDir)
558 {
559 result = [self _allSubdirsAtPath: apath];
560 result = [result arrayByAddingObjectsFromArray: [self _addParentPath: apath toPaths: result]];
561 result = [result arrayByAddingObject: apath];
562 result = [result arrayByRemovingDuplicateEntries];
563 result = [result arrayByAddingQuotationMarksToEntries];
564 }
565
566 return [result removeDuplicatesAndImplodeWithSeparator: @" -I"];
567 }
568
569 - (NSString *) _compiler
570 {
571 NSString *compiler = nil;
572
573 compiler = [NSString stringForEnvironmentVariable: @"CC"
574 defaultValue: @"`gnustep-config --variable=CC` "
575 @"`gnustep-config --objc-flags`"];
576
577 return compiler;
578 }
579
580 - (NSMutableArray *) _arrayForKey: (NSString *)keyName
581 {
582 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
583 NSMutableArray *result = [context objectForKey: keyName];
584
585 if (result == nil)
586 {
587 result = [NSMutableArray array];
588 [context setObject: result forKey: keyName];
589 }
590
591 return result;
592 }
593
594 - (BOOL) buildWithPath: (NSString *)bp
595 andFileType: (NSString *)ft
596 {
597 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
598 // NSString *of = [context objectForKey: @"OUTPUT_FILES"];
599 NSDictionary *config = [context config];
600 NSString *ctarget = [config objectForKey: @"target"];
601 NSString *additionalCFlags = [config objectForKey: @"additionalCFlags"];
602 NSString *modified = [context objectForKey: @"MODIFIED_FLAG"];
603 // NSString *outputFiles = (of == nil)?@"":of;
604 int result = 0;
605 NSError *error = nil;
606 NSFileManager *manager = [NSFileManager defaultManager];
607 NSDictionary *ctx = [context currentContext];
608 XCConfigurationList *xcl = [ctx objectForKey: @"buildConfig"];
609 XCBuildConfiguration *xbc = [xcl defaultConfiguration];
610 NSDictionary *bs = [xbc buildSettings];
611 NSDictionary *plistFile = [context config];
612 NSArray *skippedSource = [plistFile objectForKey:
613 @"skippedSource"];
614 NSString *buildType = [config objectForKey: @"buildType"];
615
616
617
618 if (additionalCFlags == nil)
619 {
620 additionalCFlags = @"";
621 }
622
623 if ([skippedSource containsObject: bp])
624 {
625 xcprintf("skipping file.\n");
626 return YES;
627 }
628
629 // Show the build percentage during linear build...
630 if ([buildType isEqualToString: @"linear"] || buildType == nil)
631 {
632 CGFloat perc = 100.0 * ((CGFloat)_currentFile / (CGFloat)_totalFiles);
633
634 xcprintf("%s",[[NSString stringWithFormat: @"\t* Building %s%s%@%s (%ld / %ld) - ( %3.2f%% )... ",
635 BOLD, MAGENTA, bp, RESET, (long)_currentFile, (long)_totalFiles, perc] cString]);
636 }
637
638 if(modified == nil)
639 {
640 modified = @"NO";
641 [context setObject: @"NO"
642 forKey: @"MODIFIED_FLAG"];
643 }
644
645 if([ft isEqualToString: @"sourcecode.c.objc"] ||
646 [ft isEqualToString: @"sourcecode.c.c"] ||
647 [ft isEqualToString: @"sourcecode.cpp.cpp"] ||
648 [ft isEqualToString: @"sourcecode.cpp.objcpp"])
649 {
650 NSString *proj_root = [bs objectForKey: @"PROJECT_ROOT"];
651
652 if (proj_root == nil ||
653 [proj_root isEqualToString: @""])
654 {
655 proj_root = @".";
656 }
657
658 if ([ft isEqualToString: @"sourcecode.cpp.cpp"] ||
659 [ft isEqualToString: @"sourcecode.cpp.objcpp"])
660 {
661 [context setObject: @"YES" forKey: @"LINK_WITH_CPP"];
662 }
663
664 NSString *compiler = [self _compiler];
665 NSString *buildPath = [proj_root stringByAppendingPathComponent: bp];
666 NSArray *localHeaderPathsArray = [self _allSubdirsAtPath:@"."];
667 NSString *fileName = [_path lastPathComponent];
668 NSString *buildDir = [NSString stringForEnvironmentVariable: @"TARGET_BUILD_DIR" defaultValue: @"build"];
669 NSString *additionalHeaderDirs = [context objectForKey:@"INCLUDE_DIRS"];
670 NSString *derivedSrcHeaderDir = [context objectForKey: @"DERIVED_SOURCE_HEADER_DIR"];
671 NSString *headerSearchPaths = [[self substituteSearchPaths: [context objectForKey: @"HEADER_SEARCH_PATHS"]
672 buildPath: buildPath]
673 removeDuplicatesAndImplodeWithSeparator: @" -I"];
674 NSString *warningCflags = [[context objectForKey: @"WARNING_CFLAGS"]
675 removeDuplicatesAndImplodeWithSeparator: @" "];
676 NSString *wsIncDirs = [context objectForKey: @"WORKSPACE_INCLUDE_LINE"];
677 NSString *usePCHFlag = [bs objectForKey: @"GCC_PRECOMPILE_PREFIX_HEADER"];
678 NSString *localHeaderPaths = [localHeaderPathsArray implodeArrayWithSeparator:@" -I"];
679
680 buildDir = [buildDir stringByAppendingPathComponent: [_target name]];
681
682 // blank these out if they are not used...
683 if(headerSearchPaths == nil)
684 {
685 headerSearchPaths = @"";
686 }
687
688 if(localHeaderPaths == nil)
689 {
690 localHeaderPaths = @"";
691 }
692
693 if(warningCflags == nil)
694 {
695 warningCflags = @"";
696 }
697
698 // if the workspace has added include dirs
699 if (wsIncDirs != nil)
700 {
701 headerSearchPaths = [headerSearchPaths stringByAppendingString: wsIncDirs];
702 NSDebugLog(@"\n\n\nheaders = %@\n\n\n", headerSearchPaths);
703 }
704
705 // If we have derived sources, then get the header directory and add it to the search path....
706 if(derivedSrcHeaderDir != nil)
707 {
708 if([[derivedSrcHeaderDir pathComponents] count] > 1)
709 {
710 headerSearchPaths = [headerSearchPaths stringByAppendingString:
711 [NSString stringWithFormat: @" -I'%@' ",
712 [derivedSrcHeaderDir stringByDeletingLastPathComponent]]];
713 }
714 }
715
716 // If we have additional header dirs specified... then add them.
717 if(additionalHeaderDirs != nil)
718 {
719 headerSearchPaths = [headerSearchPaths stringByAppendingString: additionalHeaderDirs];
720 headerSearchPaths = [headerSearchPaths stringByAppendingString: localHeaderPaths];
721 }
722
723 [lock lock];
724 NSString *outputPath = [buildDir stringByAppendingPathComponent:
725 [fileName stringByAppendingString: @".o"]];
726 [lock unlock];
727
728 NSString *objCflags = @"";
729 if([ft isEqualToString: @"sourcecode.c.objc"])
730 {
731 objCflags = @"";
732 }
733
734 NSString *std = [NSString stringForEnvironmentVariable: @"GCC_C_LANGUAGE_STANDARD"
735 defaultValue: @""];
736 if ([std length] > 0)
737 {
738 if([std isEqualToString:@"compiler-default"] == YES)
739 {
740 std = @"gnu99";
741 }
742 objCflags = [NSString stringWithFormat: @"%@ -std=%@",
743 objCflags, std];
744 }
745
746 // remove flags incompatible with gnustep...
747 objCflags = [objCflags stringByReplacingOccurrencesOfString: @"-std=gnu11" withString: @""];
748 headerSearchPaths = [headerSearchPaths stringByReplacingEnvironmentVariablesWithValues];
749
750 NSString *configString = [context objectForKey: @"CONFIG_STRING"];
751 NSString *buildTemplate = nil;
752
753 buildTemplate = @"%@ 2> %@ -c %@ %@ %@ %@ %@ %@ %@ -o %@";
754 if ([ctarget isEqualToString: @"msvc"])
755 {
756 buildTemplate = @"%@ 2> %@ -c %@ %@ %@ -D_CRT_SECURE_NO_WARNINGS %@ %@ %@ %@ -o %@";
757 }
758
759 if ([usePCHFlag isEqualToString: @"YES"])
760 {
761 NSString *pchFile = [bs objectForKey: @"GCC_PREFIX_HEADER"];
762 if (![pchFile isEqualToString: @""] && pchFile != nil)
763 {
764 buildTemplate = [buildTemplate stringByAppendingString: [NSString stringWithFormat: @" -include %@", pchFile]];
765 }
766 }
767
768 NSString *compilePath = bp;
769 NSString *subdirHeaderSearchPaths = [self _headerStringForPath: compilePath];
770 NSString *parentHeaderSearchPaths = [self _headerStringForPath: [[compilePath pathComponents] firstObject]];
771 NSString *errorOutPath = [outputPath stringByAppendingString: @".err"];
772 NSString *buildCommand = [NSString stringWithFormat: buildTemplate,
773 compiler,
774 [errorOutPath stringByAddingQuotationMarks],
775 [compilePath stringByAddingQuotationMarks],
776 objCflags,
777 additionalCFlags,
778 configString,
779 parentHeaderSearchPaths,
780 headerSearchPaths,
781 subdirHeaderSearchPaths,
782 [outputPath stringByAddingQuotationMarks]];
783 NSDictionary *buildPathAttributes = [manager attributesOfItemAtPath: buildPath
784 error: &error];
785 NSDictionary *outputPathAttributes = [manager attributesOfItemAtPath: outputPath
786 error: &error];
787 NSDate *buildPathDate = [buildPathAttributes fileModificationDate];
788 NSDate *outputPathDate = [outputPathAttributes fileModificationDate];
789
790 buildCommand = [buildCommand stringByReplacingOccurrencesOfString: @"$(inherited)"
791 withString: @"."];
792
793 NSDebugLog(@"%@", buildCommand);
794
795 if (outputPathDate != nil)
796 {
797 if([buildPathDate compare: outputPathDate] == NSOrderedDescending)
798 {
799 result = xcsystem(buildCommand);
800 if([modified isEqualToString: @"NO"])
801 {
802 modified = @"YES";
803 [context setObject: @"YES"
804 forKey: @"MODIFIED_FLAG"];
805 }
806
807 if (result == 0)
808 {
809 if ([buildType isEqualToString: @"linear"] || buildType == nil)
810 {
811 xcprintf("%srebuilt%s\n", GREEN, RESET);
812 }
813 }
814 }
815 else
816 {
817 if ([buildType isEqualToString: @"linear"] || buildType == nil)
818 {
819 xcprintf("%sexists%s\n", YELLOW, RESET);
820 }
821 }
822 }
823 else
824 {
825 result = xcsystem(buildCommand);
826 if([modified isEqualToString: @"NO"])
827 {
828 modified = @"YES";
829 [context setObject: @"YES"
830 forKey: @"MODIFIED_FLAG"];
831 }
832
833 if (result == 0)
834 {
835 if ([buildType isEqualToString: @"linear"] || buildType == nil)
836 {
837 xcprintf("%ssuccess%s\n", GREEN, RESET);
838 }
839 }
840 }
841
842 // If the result is not successful, show the error...
843 if (result != 0)
844 {
845 if ([buildType isEqualToString: @"linear"] || buildType == nil)
846 {
847 xcprintf("%serror%s\n\n", RED, RESET);
848 }
849
850 NSString *errorString = [NSString stringWithContentsOfFile: errorOutPath];
851 [NSException raise: NSGenericException
852 format: @"\n%sCommand:%s %@\n%sMessage:%s %@\n", YELLOW, RESET, buildCommand, RED, RESET, errorString];
853 }
854
855 // [context setObject: outputFiles forKey: @"OUTPUT_FILES"];
856 }
857 else if ([ft isEqualToString: @"sourcecode.lex"] || [_explicitFileType isEqualToString: @"sourcecode.lex"])
858 {
859 NSString *ex = [bp pathExtension];
860 NSString *nx = [ex substringFromIndex: [ex length] - 1];
861 NSString *op = [@"lex.yy" stringByAppendingPathExtension: nx];
862 NSString *fp = [[bp stringByDeletingLastPathComponent] stringByAppendingPathComponent: op];
863 NSString *command = [NSString stringWithFormat: @"flex > /dev/null 2> /dev/null -o %@ %@", fp, bp];
864 NSInteger f = 0;
865
866 xcprintf("%s%sprocessing%s\n", BOLD, YELLOW, RESET);
867 f = xcsystem(command);
868
869 if (f == 0)
870 {
871 return [self buildWithPath: fp
872 andFileType: @"sourcecode.c.objc"];
873 }
874 }
875 else if ([ft isEqualToString: @"sourcecode.yacc"] || [_explicitFileType isEqualToString: @"sourcecode.yacc"])
876 {
877 NSString *ex = [bp pathExtension];
878 NSString *nx = [ex substringFromIndex: [ex length] - 1];
879 NSString *op = [@"y.tab" stringByAppendingPathExtension: nx];
880 NSString *fp = [[bp stringByDeletingLastPathComponent] stringByAppendingPathComponent: op];
881 NSString *command = [NSString stringWithFormat: @"bison > /dev/null 2> /dev/null -o %@ %@", fp, bp];
882 NSInteger f = 0;
883
884 xcprintf("%s%sprocessing%s\n", BOLD, YELLOW, RESET);
885 f = xcsystem(command);
886
887 if (f == 0)
888 {
889 return [self buildWithPath: fp
890 andFileType: @"sourcecode.c.objc"];
891 }
892 }
893 else
894 {
895 NSLog(@"Unknown file type... %@", ft);
896 }
897
898 fflush(stdout);
899 return (result == 0);
900 }
901
902 - (BOOL) build
903 {
904 NSString *ft = (_explicitFileType == nil) ? _lastKnownFileType : _explicitFileType;
905 return [self buildWithPath: [self buildPath]
906 andFileType: ft];
907 }
908
909 - (BOOL) generate
910 {
911 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
912 NSMutableArray *objcFiles = [self _arrayForKey: @"OBJC_FILES"];
913 NSMutableArray *cFiles = [self _arrayForKey: @"C_FILES"];
914 NSMutableArray *cppFiles = [self _arrayForKey: @"CPP_FILES"];
915 // NSMutableArray *hFiles = [self _arrayForKey: @"HEADERS"];
916 NSMutableArray *objcppFiles = [self _arrayForKey: @"OBJCPP_FILES"];
917 NSMutableArray *addlIncDirs = [self _arrayForKey: @"ADDITIONAL_INCLUDE_DIRS"];
918 NSString *of = [context objectForKey: @"OUTPUT_FILES"];
919 NSString *modified = [context objectForKey: @"MODIFIED_FLAG"];
920 NSString *outputFiles = (of == nil)?@"":of;
921 int result = 0;
922 NSFileManager *manager = [NSFileManager defaultManager];
923
924 // NSDebugLog(@"*** %@", _sourceTree);
925 if(modified == nil)
926 {
927 modified = @"NO";
928 [context setObject: @"NO"
929 forKey: @"MODIFIED_FLAG"];
930 }
931
932 // Get project root
933 NSString *projDir = [NSString stringForEnvironmentVariable: @"PROJECT_ROOT"
934 defaultValue: @"."];
935
936 NSString *buildPath = [projDir stringByAppendingPathComponent:
937 [self buildPath]];
938
939
940 NSArray *localHeaderPathsArray = [self _allSubdirsAtPath: @"."];
941 NSString *buildDir = [NSString stringForEnvironmentVariable: @"TARGET_BUILD_DIR" defaultValue: @"build"];
942 buildDir = [buildDir stringByAppendingPathComponent: [self productName]];
943 NSString *additionalHeaderDirs = [context objectForKey:@"INCLUDE_DIRS"];
944 NSString *derivedSrcHeaderDir = [context objectForKey: @"DERIVED_SOURCE_HEADER_DIR"];
945 NSString *headerSearchPaths = [[context objectForKey: @"HEADER_SEARCH_PATHS"]
946 removeDuplicatesAndImplodeWithSeparator: @" -I"];
947 NSString *warningCflags = [[context objectForKey: @"WARNING_CFLAGS"]
948 removeDuplicatesAndImplodeWithSeparator: @" "];
949 NSString *localHeaderPaths = [localHeaderPathsArray removeDuplicatesAndImplodeWithSeparator:@" -I"];
950
951 NSDebugLog(@"localHeaderPathsArray = %@, %@", localHeaderPathsArray, localHeaderPaths);
952 NSDebugLog(@"Build path = %@, %@", [self buildPath], [[self buildPath] stringByDeletingFirstPathComponent]);
953
954 // blank these out if they are not used...
955 if(headerSearchPaths == nil)
956 {
957 headerSearchPaths = @"";
958 }
959
960 if(localHeaderPaths == nil)
961 {
962 localHeaderPaths = @"";
963 }
964
965 if(warningCflags == nil)
966 {
967 warningCflags = @"";
968 }
969
970 // If we have derived sources, then get the header directory and add it to the search path....
971 if(derivedSrcHeaderDir != nil)
972 {
973 if([[derivedSrcHeaderDir pathComponents] count] > 1)
974 {
975 headerSearchPaths = [headerSearchPaths stringByAppendingString:
976 [NSString stringWithFormat: @" -I%@ ",
977 [derivedSrcHeaderDir stringByDeletingLastPathComponent]]];
978 }
979 }
980
981 // If we have additional header dirs specified... then add them.
982 if(additionalHeaderDirs != nil)
983 {
984 headerSearchPaths = [headerSearchPaths stringByAppendingString: additionalHeaderDirs];
985 headerSearchPaths = [headerSearchPaths stringByAppendingString: localHeaderPaths];
986 }
987
988 // Sometimes, for some incomprehensible reason, the buildpath doesn't
989 // need the project dir pre-pended. This could be due to differences
990 // in different version of xcode. It must be removed to successfully
991 // compile the application...
992 if([manager fileExistsAtPath:buildPath] == NO)
993 {
994 buildPath = [self path];
995 }
996
997 NSString *objCflags = @"";
998 if([_lastKnownFileType isEqualToString: @"sourcecode.c.objc"])
999 {
1000 // objCflags = @"-fconstant-string-class=NSConstantString";
1001 objCflags = @"";
1002 }
1003
1004 if([_lastKnownFileType isEqualToString: @"sourcecode.cpp.objcpp"])
1005 {
1006 objCflags = [objCflags stringByReplacingOccurrencesOfString: @"-std=gnu99" withString: @""];
1007 }
1008
1009 // remove flags incompatible with gnustep...
1010 objCflags = [objCflags stringByReplacingOccurrencesOfString: @"-std=gnu11" withString: @""];
1011
1012 // get compile path
1013 BOOL exists = [manager fileExistsAtPath: [self buildPath]];
1014 NSString *compilePath = ([[[self buildPath] pathComponents] count] > 1 && !exists) ?
1015 [[[self buildPath] stringByDeletingFirstPathComponent] stringByEscapingSpecialCharacters] :
1016 [self buildPath];
1017
1018 [context setObject: outputFiles forKey: @"OUTPUT_FILES"];
1019
1020 if ([_lastKnownFileType isEqualToString: @"sourcecode.c.objc"])
1021 {
1022 [objcFiles addObject: compilePath];
1023 }
1024
1025 if ([_lastKnownFileType isEqualToString: @"sourcecode.c.c"])
1026 {
1027 [cFiles addObject: compilePath];
1028 }
1029
1030 if ([_lastKnownFileType isEqualToString: @"sourcecode.cpp.cpp"])
1031 {
1032 [cppFiles addObject: compilePath];
1033 }
1034
1035 if ([_lastKnownFileType isEqualToString: @"sourcecode.cpp.objcpp"])
1036 {
1037 [objcppFiles addObject: compilePath];
1038 }
1039
1040 if ([_lastKnownFileType isEqualToString: @"sourcexode.c.h"])
1041 {
1042
1043 }
1044
1045 NSString *includePath = [compilePath stringByDeletingLastPathComponent];
1046 NSDebugLog(@"%@", includePath);
1047 if (includePath != nil && [includePath isEqualToString: @""] == NO)
1048 {
1049 if ([addlIncDirs containsObject: includePath] == NO)
1050 {
1051 [addlIncDirs addObject: includePath];
1052 }
1053 }
1054 NSDebugLog(@"Additional includes %@", addlIncDirs);
1055
1056 return (result == 0);
1057 }
1058
1059 - (NSString *) description
1060 {
1061 NSString *s = [super description];
1062 return [s stringByAppendingFormat: @" <%@, %@>", _path, _lastKnownFileType];
1063 }
1064 @end