| 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 <Foundation/NSOperation.h> |
| 26 |
|
| 27 |
#import "PBXCommon.h" |
| 28 |
#import "PBXSourcesBuildPhase.h" |
| 29 |
#import "PBXFileReference.h" |
| 30 |
#import "PBXBuildFile.h" |
| 31 |
#import "GSXCBuildOperation.h" |
| 32 |
#import "GSXCBuildDatabase.h" |
| 33 |
|
| 34 |
@implementation PBXSourcesBuildPhase |
| 35 |
|
| 36 |
- (instancetype) init |
| 37 |
{ |
| 38 |
if ((self = [super init]) != nil) |
| 39 |
{ |
| 40 |
_cpus = [[NSProcessInfo processInfo] processorCount]; |
| 41 |
_queue = [[NSOperationQueue alloc] init]; |
| 42 |
[_queue setMaxConcurrentOperationCount: _cpus]; |
| 43 |
} |
| 44 |
return self; |
| 45 |
} |
| 46 |
|
| 47 |
- (void) dealloc |
| 48 |
{ |
| 49 |
RELEASE(_queue); |
| 50 |
[super dealloc]; |
| 51 |
} |
| 52 |
|
| 53 |
- (BOOL) build |
| 54 |
{ |
| 55 |
GSXCBuildContext *ctx = [GSXCBuildContext sharedBuildContext]; |
| 56 |
NSDictionary *config = [ctx config]; |
| 57 |
GSXCBuildDatabase *db = [_target database]; |
| 58 |
NSArray *files = _files; |
| 59 |
id file = nil; |
| 60 |
BOOL result = YES; |
| 61 |
NSUInteger i = 1; |
| 62 |
NSMutableArray *ops = [NSMutableArray array]; |
| 63 |
NSEnumerator *en = nil; |
| 64 |
NSString *buildType = [config objectForKey: @"buildType"]; |
| 65 |
|
| 66 |
NSDebugLog(@"config = %@", config); |
| 67 |
if ([buildType isEqualToString: @"linear"] == YES || buildType == nil) // linear is the default |
| 68 |
{ |
| 69 |
[_queue setMaxConcurrentOperationCount: 1]; |
| 70 |
} |
| 71 |
else if ([buildType isEqualToString: @"parallel"] == YES) |
| 72 |
{ |
| 73 |
NSString *mct = [config objectForKey: @"maxConcurrentOperationCount"]; |
| 74 |
|
| 75 |
if (mct != nil) |
| 76 |
{ |
| 77 |
NSUInteger t = [mct intValue]; |
| 78 |
|
| 79 |
if (t > 0) |
| 80 |
{ |
| 81 |
_cpus = t; |
| 82 |
[_queue setMaxConcurrentOperationCount: _cpus]; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
xcprintf("\t* Parallel build using %ld CPUs...\n", _cpus); |
| 87 |
} |
| 88 |
|
| 89 |
NSArray *synchronizedFiles = [_target synchronizedSources]; |
| 90 |
files = [files arrayByAddingObjectsFromArray: synchronizedFiles]; |
| 91 |
// NSLog(@"files = %@", files); |
| 92 |
|
| 93 |
// if the database is present use it's list of files... |
| 94 |
if (db != nil && [synchronizedFiles count] == 0) |
| 95 |
{ |
| 96 |
if ([db isEmpty]) |
| 97 |
{ |
| 98 |
xcputs("\t++++ No files modified, nothing to build. ++++\n"); |
| 99 |
return YES; |
| 100 |
} |
| 101 |
|
| 102 |
files = [db files]; |
| 103 |
} |
| 104 |
else |
| 105 |
{ |
| 106 |
xcputs([[NSString stringWithFormat: @"%s+++ Using synchronized group...%s", YELLOW, RESET] cString]); |
| 107 |
} |
| 108 |
|
| 109 |
en = [files objectEnumerator]; |
| 110 |
xcputs("=== Executing Sources Build Phase"); |
| 111 |
while((file = [en nextObject]) != nil && result) |
| 112 |
{ |
| 113 |
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init]; |
| 114 |
GSXCBuildOperation *op = [GSXCBuildOperation operationWithFile: file]; |
| 115 |
|
| 116 |
[file setTarget: _target]; |
| 117 |
[file setTotalFiles: [files count]]; |
| 118 |
[file setCurrentFile: i]; |
| 119 |
[ops addObject: op]; |
| 120 |
|
| 121 |
i++; |
| 122 |
|
| 123 |
RELEASE(p); |
| 124 |
} |
| 125 |
|
| 126 |
// Handle the error... |
| 127 |
NS_DURING |
| 128 |
{ |
| 129 |
[_queue addOperations: ops waitUntilFinished: YES]; |
| 130 |
} |
| 131 |
NS_HANDLER |
| 132 |
{ |
| 133 |
[_queue cancelAllOperations]; |
| 134 |
NSLog(@"Compilation halted."); |
| 135 |
} |
| 136 |
NS_ENDHANDLER; |
| 137 |
|
| 138 |
xcputs("=== Sources Build Phase Completed"); |
| 139 |
|
| 140 |
// if (db != nil) |
| 141 |
{ |
| 142 |
[self link]; // generate the rest of the output file entries |
| 143 |
} |
| 144 |
|
| 145 |
return result; |
| 146 |
} |
| 147 |
|
| 148 |
- (BOOL) generate |
| 149 |
{ |
| 150 |
xcputs("=== Generating using Sources Build Phase"); |
| 151 |
NSEnumerator *en = [_files objectEnumerator]; |
| 152 |
id file = nil; |
| 153 |
BOOL result = YES; |
| 154 |
while((file = [en nextObject]) != nil && result) |
| 155 |
{ |
| 156 |
[file setTarget: _target]; |
| 157 |
result = [file generate]; |
| 158 |
} |
| 159 |
xcputs("=== Sources Build Phase generation completed"); |
| 160 |
|
| 161 |
return result; |
| 162 |
} |
| 163 |
|
| 164 |
- (BOOL) link |
| 165 |
{ |
| 166 |
GSXCBuildDatabase *db = [_target database]; |
| 167 |
GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext]; |
| 168 |
NSString *of = (db != nil) ? nil : [context objectForKey: @"OUTPUT_FILES"]; |
| 169 |
NSString *outputFiles = (of == nil) ? @"" : of; |
| 170 |
PBXBuildFile *file = nil; |
| 171 |
BOOL result = YES; |
| 172 |
NSString *buildDir = @"./build"; |
| 173 |
NSEnumerator *en = nil; // [_files objectEnumerator]; |
| 174 |
NSFileManager *mgr = [NSFileManager defaultManager]; |
| 175 |
|
| 176 |
buildDir = [buildDir stringByAppendingPathComponent: [_target name]]; |
| 177 |
|
| 178 |
NSArray *synchronizedFiles = [_target synchronizedSources]; |
| 179 |
NSArray *files = [_files arrayByAddingObjectsFromArray: synchronizedFiles]; |
| 180 |
en = [files objectEnumerator]; |
| 181 |
|
| 182 |
xcputs("=== Executing Sources Build Phase (LINK)"); |
| 183 |
while((file = [en nextObject]) != nil && result) |
| 184 |
{ |
| 185 |
PBXFileReference *fr = [file fileRef]; |
| 186 |
NSString *fileName = [[fr path] lastPathComponent]; |
| 187 |
NSString *outputPath = [buildDir stringByAppendingPathComponent: |
| 188 |
[fileName stringByAppendingString: @".o"]]; |
| 189 |
|
| 190 |
outputFiles = [[outputFiles stringByAppendingString: [NSString stringWithFormat: @"'%@'",outputPath]] |
| 191 |
stringByAppendingString: @" "]; |
| 192 |
|
| 193 |
// NSLog(@"name = %@", [fr path]); |
| 194 |
xcprintf("\t+ Collecting %s%s%s%s ... ", BOLD, MAGENTA, [outputPath cString], RESET); |
| 195 |
|
| 196 |
if ([mgr fileExistsAtPath: outputPath] == YES) |
| 197 |
{ |
| 198 |
xcprintf("%s%sfound%s\n", BOLD, GREEN, RESET); |
| 199 |
} |
| 200 |
else |
| 201 |
{ |
| 202 |
xcprintf("%s%smissing%s\n", BOLD, RED, RESET); |
| 203 |
result = NO; |
| 204 |
break; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
[context setObject: outputFiles forKey: @"OUTPUT_FILES"]; |
| 209 |
// NSLog(@"Output files %@", outputFiles); |
| 210 |
xcputs("=== Sources Build Phase Completed (LINK)"); |
| 211 |
|
| 212 |
return result; |
| 213 |
} |
| 214 |
|
| 215 |
@end |