Debian Codegraph @

XCode/PBXNativeTarget.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 "PBXCommon.h"
27 #import "PBXNativeTarget.h"
28 #import "PBXProject.h"
29 #import "PBXContainer.h"
30
31 #import "GSXCCommon.h"
32 #import "GSXCBuildContext.h"
33 #import "GSXCGenerator.h"
34
35 #import "NSString+PBXAdditions.h"
36
37 #ifdef _WIN32
38 #import "setenv.h"
39 #endif
40
41 @implementation PBXNativeTarget
42
43 - (void) dealloc
44 {
45 RELEASE(_productReference);
46 RELEASE(_productInstallPath);
47 RELEASE(_buildRules);
48 RELEASE(_comments);
49 RELEASE(_productSettingsXML);
50
51 [super dealloc];
52 }
53
54 // Methods....
55 - (PBXFileReference *) productReference // getter
56 {
57 return _productReference;
58 }
59
60 - (void) setProductReference: (PBXFileReference *)object; // setter
61 {
62 ASSIGN(_productReference,object);
63 }
64
65 - (NSString *) productInstallPath // getter
66 {
67 return _productInstallPath;
68 }
69
70 - (void) setProductInstallPath: (NSString *)object; // setter
71 {
72 ASSIGN(_productInstallPath,object);
73 }
74
75 - (NSMutableArray *) buildRules // getter
76 {
77 return _buildRules;
78 }
79
80 - (void) setBuildRules: (NSMutableArray *)object; // setter
81 {
82 ASSIGN(_buildRules,object);
83 }
84
85 - (NSString *) productSettingsXML // getter
86 {
87 return _productSettingsXML;
88 }
89
90 - (void) setProductSettingsXML: (NSString *)object // setter
91 {
92 ASSIGN(_productSettingsXML,object);
93 }
94
95 /*
96 - (XCConfigurationList *) buildConfigurationList
97 {
98 return xcConfigurationList;
99 }
100
101 - (void) setBuildConfigurationList: (XCConfigurationList *)list
102 {
103 NSLog(@"build config list = %@",list);
104 ASSIGN(xcConfigurationList, list);
105 }
106 */
107
108 - (void) _productWrapper
109 {
110 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
111 NSString *buildDir = @"./build";
112 NSString *aname = [self name];
113 buildDir = [buildDir stringByAppendingPathComponent: aname];
114 NSString *uninstalledProductsDir = [buildDir stringByAppendingPathComponent: @"Products"];
115 NSString *fullPath = [[buildDir stringByAppendingPathComponent: @"Products"]
116 stringByAppendingPathComponent: [_productReference path]];
117
118 NSError *error = nil;
119
120 // Set up some target specific vars, based on the build dir...
121 [context setObject: buildDir
122 forKey: @"TARGET_BUILD_DIR"];
123 [context setObject: aname
124 forKey: @"TARGET_NAME"];
125 [context setObject: uninstalledProductsDir
126 forKey: @"BUILT_PRODUCTS_DIR"];
127
128 // Create directories...
129 [[NSFileManager defaultManager] createDirectoryAtPath:uninstalledProductsDir
130 withIntermediateDirectories:YES
131 attributes:nil
132 error:&error];
133
134 setenv("inherited","",1); // probably from a parent project or target..
135 if([_productType isEqualToString: BUNDLE_TYPE] ||
136 [_productType isEqualToString: APPLICATION_TYPE])
137 {
138 NSString *execName = [[fullPath lastPathComponent] stringByDeletingPathExtension];
139
140 // Create directories...
141 [[NSFileManager defaultManager] createDirectoryAtPath:fullPath
142 withIntermediateDirectories:YES
143 attributes:nil
144 error:&error];
145
146 setenv("PRODUCT_OUTPUT_DIR",[fullPath cString],1);
147 setenv("PRODUCT_NAME",[execName cString],1);
148 setenv("EXECUTABLE_NAME",[execName cString],1);
149
150 [context setObject: fullPath forKey: @"PRODUCT_OUTPUT_DIR"];
151 [context setObject: execName forKey: @"PRODUCT_NAME"];
152 [context setObject: execName forKey: @"EXECUTABLE_NAME"];
153 }
154 else if([_productType isEqualToString: FRAMEWORK_TYPE])
155 {
156 NSString *dir = [NSString stringForEnvironmentVariable: @"PROJECT_ROOT"
157 defaultValue: @"./"];
158 NSString *derivedSourceDir = [dir stringByAppendingPathComponent: @"derived_src"];
159 NSString *execName = [[fullPath lastPathComponent] stringByDeletingPathExtension];
160 NSString *derivedSourceHeaderDir = [derivedSourceDir stringByAppendingPathComponent: execName];
161 NSString *frameworkVersion = [NSString stringForEnvironmentVariable: @"FRAMEWORK_VERSION"
162 defaultValue: @"0"];
163 [context setObject: [NSString stringWithString: fullPath]
164 forKey: @"FRAMEWORK_DIR"];
165
166 // Above "Versions"
167 NSString *headersLink = [fullPath stringByAppendingPathComponent: @"Headers"];
168 NSString *resourcesLink = [fullPath stringByAppendingPathComponent: @"Resources"];
169
170 // Below "Versions"
171 fullPath = [fullPath stringByAppendingPathComponent: @"Versions"];
172 NSString *currentLink = [fullPath stringByAppendingPathComponent: @"Current"];
173 fullPath = [fullPath stringByAppendingPathComponent: frameworkVersion];
174 NSString *headerDir = [fullPath stringByAppendingPathComponent: @"Headers"];
175 NSString *resourceDir = [fullPath stringByAppendingPathComponent: @"Resources"];
176
177 // Create directories...
178 [[NSFileManager defaultManager] createDirectoryAtPath:fullPath
179 withIntermediateDirectories:YES
180 attributes:nil
181 error:&error];
182
183 [[NSFileManager defaultManager] createDirectoryAtPath:derivedSourceHeaderDir
184 withIntermediateDirectories:YES
185 attributes:nil
186 error:&error];
187
188 [[NSFileManager defaultManager] createDirectoryAtPath:headerDir
189 withIntermediateDirectories:YES
190 attributes:nil
191 error:&error];
192 // Create links....
193 [[NSFileManager defaultManager] createSymbolicLinkAtPath: currentLink
194 pathContent: frameworkVersion];
195
196 [[NSFileManager defaultManager] createSymbolicLinkAtPath: headersLink
197 pathContent: @"Versions/Current/Headers"];
198
199 [[NSFileManager defaultManager] createSymbolicLinkAtPath: resourcesLink
200 pathContent: @"Versions/Current/Resources"];
201
202 // Things to pass on to the next phase...
203 [context setObject: headerDir forKey: @"HEADER_DIR"];
204 [context setObject: resourceDir forKey: @"RESOURCE_DIR"];
205 [context setObject: derivedSourceHeaderDir forKey: @"DERIVED_SOURCE_HEADER_DIR"];
206 [context setObject: derivedSourceDir forKey: @"DERIVED_SOURCES_DIR"];
207
208 setenv("PRODUCT_OUTPUT_DIR",[fullPath cString],1);
209 setenv("PRODUCT_NAME",[execName cString],1);
210 setenv("EXECUTABLE_NAME",[execName cString],1);
211
212 [context setObject: fullPath forKey: @"PRODUCT_OUTPUT_DIR"];
213 [context setObject: execName forKey: @"PRODUCT_NAME"];
214 [context setObject: execName forKey: @"EXECUTABLE_NAME"];
215 }
216 else if([_productType isEqualToString: LIBRARY_TYPE])
217 {
218 NSString *fileName = [fullPath lastPathComponent];
219 NSString *path = [fullPath stringByDeletingLastPathComponent];
220 NSString *dir = [NSString stringForEnvironmentVariable: @"PROJECT_ROOT"
221 defaultValue: @"./"];
222 NSString *derivedSourceDir = [dir stringByAppendingPathComponent: @"derived_src"];
223 NSString *derivedSourceHeaderDir = derivedSourceDir;
224
225 setenv("PRODUCT_OUTPUT_DIR",[path cString],1);
226 setenv("PRODUCT_NAME",[fileName cString],1);
227 setenv("EXECUTABLE_NAME",[fileName cString],1);
228
229 [context setObject: derivedSourceHeaderDir forKey: @"DERIVED_SOURCE_HEADER_DIR"];
230 [context setObject: derivedSourceDir forKey: @"DERIVED_SOURCES_DIR"];
231
232 [[NSFileManager defaultManager] createDirectoryAtPath:derivedSourceHeaderDir
233 withIntermediateDirectories:YES
234 attributes:nil
235 error:&error];
236 }
237 else // For non bundled packages
238 {
239 NSString *fileName = [fullPath lastPathComponent];
240 NSString *path = [fullPath stringByDeletingLastPathComponent];
241 NSString *dir = [NSString stringForEnvironmentVariable: @"PROJECT_ROOT"
242 defaultValue: @"./"];
243 NSString *derivedSourceDir = [dir stringByAppendingPathComponent: @"derived_src"];
244 NSString *derivedSourceHeaderDir = derivedSourceDir;
245
246 setenv("PRODUCT_OUTPUT_DIR",[path cString],1);
247 setenv("PRODUCT_NAME",[fileName cString],1);
248 setenv("EXECUTABLE_NAME",[fileName cString],1);
249
250 [context setObject: derivedSourceHeaderDir forKey: @"DERIVED_SOURCE_HEADER_DIR"];
251 [context setObject: derivedSourceDir forKey: @"DERIVED_SOURCES_DIR"];
252
253 [context setObject: path forKey: @"PRODUCT_OUTPUT_DIR"];
254 [context setObject: fileName forKey: @"PRODUCT_NAME"];
255 [context setObject: fileName forKey: @"EXECUTABLE_NAME"];
256 }
257 }
258
259 - (BOOL) build
260 {
261 BOOL result = YES;
262 NSEnumerator *en = nil;
263 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
264 NSDictionary *plistFile = [NSDictionary dictionaryWithContentsOfFile:
265 @"buildtool.plist"];
266 NSArray *skippedTarget = [plistFile objectForKey:
267 @"skippedTarget"];
268
269 // call super to set up common data structures...
270 if(![super build])
271 {
272 return NO;
273 }
274
275 if ([skippedTarget containsObject: [self name]])
276 {
277 xcputs([[NSString stringWithFormat: @"=== Skipping Target %s%@%s", YELLOW, _name, RESET] cString]);
278 return YES;
279 }
280
281 xcputs([[NSString stringWithFormat: @"=== Building Target %s%@%s", GREEN, _name, RESET] cString]);
282 [_buildConfigurationList applyDefaultConfiguration];
283 [context setObject: _buildConfigurationList
284 forKey: @"buildConfig"];
285 [context setObject: _productType
286 forKey: @"PRODUCT_TYPE"];
287 if(_productSettingsXML != nil)
288 {
289 [context setObject: _productSettingsXML
290 forKey: @"PRODUCT_SETTINGS_XML"];
291 }
292 xcputs([[NSString stringWithFormat: @"=== Checking Dependencies"] cString]);
293 id dependency = nil;
294 en = [_dependencies objectEnumerator];
295 while((dependency = [en nextObject]) != nil && result)
296 {
297 result = [dependency build];
298 }
299 xcputs([[NSString stringWithFormat: @"=== Done."] cString]);
300
301 xcputs([[NSString stringWithFormat: @"=== Executing build phases..."] cString]);
302
303 [self _productWrapper];
304 id phase = nil;
305 en = [_buildPhases objectEnumerator];
306 while((phase = [en nextObject]) != nil && result)
307 {
308 NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
309
310 [phase setTarget: self];
311 result = [phase build];
312 if(NO == result)
313 {
314 xcputs([[NSString stringWithFormat: @"*** Failed build phase: %@",phase] cString]);
315 }
316
317 RELEASE(p);
318 }
319 xcputs([[NSString stringWithFormat: @"=== Done..."] cString]);
320 xcputs([[NSString stringWithFormat: @"=== Completed Executing Target %@", _name] cString]);
321
322 return result;
323 }
324
325 - (BOOL) clean
326 {
327 xcputs([[NSString stringWithFormat: @"=== Cleaning Target %@",_name] cString]);
328 NSString *buildDir = @"./build";
329 buildDir = [buildDir stringByAppendingPathComponent: [self name]];
330 NSString *command = [NSString stringWithFormat: @"rm -rf \"%@\"",buildDir];
331
332 xcputs([[NSString stringWithFormat: @"Cleaning build directory"] cString]);
333 int result = xcsystem(command);
334
335 if(result == 0)
336 {
337 if([[NSFileManager defaultManager] fileExistsAtPath: @"derived_src"])
338 {
339 command = @"rm -rf derived_src";
340 xcputs([[NSString stringWithFormat: @"Cleaning derived_src directory"] cString]);
341 result = xcsystem(command);
342 }
343 }
344
345 xcputs([[NSString stringWithFormat: @"=== Completed Cleaning Target %@",_name] cString]);
346 return (result == 0);
347 }
348
349 - (BOOL) installTool
350 {
351 NSString *buildDir = @"./build";
352 NSString *outputDir = [buildDir stringByAppendingPathComponent: [self name]];
353 NSString *uninstalledProductsDir = [outputDir stringByAppendingPathComponent: @"Products"];
354 NSString *fullPath = [uninstalledProductsDir stringByAppendingPathComponent: [_productReference path]];
355 NSString *fileName = [fullPath lastPathComponent];
356 NSFileManager *fileManager = [NSFileManager defaultManager];
357 NSError *error = nil;
358
359 NSString *installDir = [NSString stringForCommand: @"gnustep-config --variable=GNUSTEP_LOCAL_TOOLS"];
360 NSString *installDest = [installDir stringByAppendingPathComponent: fileName];
361
362 NSLog(@"installDest = %@, fullPath = %@", installDest, fullPath);
363 return [fileManager copyItemAtPath: fullPath
364 toPath: installDest
365 error: &error];
366 }
367
368 - (BOOL) installApp
369 {
370 NSString *buildDir = @"./build";
371 NSString *outputDir = [buildDir stringByAppendingPathComponent: [self name]];
372 NSString *uninstalledProductsDir = [outputDir stringByAppendingPathComponent: @"Products"];
373 NSString *fullPath = [uninstalledProductsDir stringByAppendingPathComponent: [_productReference path]];
374 NSString *fileName = [fullPath lastPathComponent];
375 // NSString *execName = [fileName stringByDeletingPathExtension];
376 NSFileManager *fileManager = [NSFileManager defaultManager];
377 NSError *error = nil;
378 NSString *cwd = [fileManager currentDirectoryPath];
379
380 NSLog(@"******* CWD = %@", cwd);
381 NSString *installDir = [NSString stringForCommand: @"gnustep-config --variable=GNUSTEP_LOCAL_APPS"];
382 NSString *installDest = [installDir stringByAppendingPathComponent: fileName];
383 return [fileManager copyItemAtPath: fullPath
384 toPath: installDest
385 error: &error];
386 }
387
388 - (BOOL) installFramework
389 {
390 NSString *buildDir = @"./build";
391 NSString *outputDir = [buildDir stringByAppendingPathComponent: [self name]];
392 NSString *installDir = [NSString stringForCommand: @"gnustep-config --variable=GNUSTEP_LOCAL_LIBRARY"];
393 NSString *libraryPath = installDir; // ([paths firstObject] != nil ? [paths firstObject] : @"");
394 NSString *frameworkPath = [libraryPath stringByAppendingPathComponent: @"Frameworks"];
395 NSString *installDest = frameworkPath;
396 NSString *productDir = [installDest stringByAppendingPathComponent: [_productReference path]];
397 NSString *headersDir = [libraryPath stringByAppendingPathComponent: @"Headers"];
398 NSString *librariesDir = [libraryPath stringByAppendingPathComponent: @"Libraries"];
399 NSString *frameworksLinkDir = [[[@"../Frameworks" stringByAppendingPathComponent: [_productReference path]]
400 stringByAppendingPathComponent:@"Versions"]
401 stringByAppendingPathComponent:@"Current"];
402 NSString *headersLinkDir = [[[@"../Frameworks" stringByAppendingPathComponent:
403 [_productReference path]] stringByAppendingPathComponent:@"Versions"]
404 stringByAppendingPathComponent:@"Current"];
405 NSString *uninstalledProductsDir = [outputDir stringByAppendingPathComponent: @"Products"];
406 NSString *fullPath = [uninstalledProductsDir stringByAppendingPathComponent: [_productReference path]];
407 NSError *error = nil;
408 NSFileManager *fileManager = [NSFileManager defaultManager];
409 NSString *fileName = [fullPath lastPathComponent];
410 NSString *execName = [fileName stringByDeletingPathExtension];
411
412 // Copy
413 [fileManager removeItemAtPath: productDir error: NULL];
414 [fileManager copyItemAtPath: fullPath
415 toPath: productDir
416 error: &error];
417
418 // Create links...
419 [fileManager removeItemAtPath: [headersDir stringByAppendingPathComponent: execName]
420 error: NULL];
421
422 BOOL flag = [fileManager createSymbolicLinkAtPath: [headersDir stringByAppendingPathComponent: execName]
423 pathContent: [headersLinkDir stringByAppendingPathComponent: @"Headers"]];
424 if(!flag)
425 {
426 xcputs([[NSString stringWithFormat: @"Error creating symbolic link..."] cString]);
427 }
428
429 [fileManager removeItemAtPath: [librariesDir stringByAppendingPathComponent:
430 [NSString stringWithFormat: @"lib%@.so",execName]]
431 error:NULL];
432 flag = [fileManager createSymbolicLinkAtPath: [librariesDir stringByAppendingPathComponent:
433 [NSString stringWithFormat: @"lib%@.so",execName]]
434 pathContent: [frameworksLinkDir stringByAppendingPathComponent:
435 [NSString stringWithFormat: @"lib%@.so",execName]]];
436 if(!flag)
437 {
438 xcputs([[NSString stringWithFormat: @"Error creating symbolic link..."] cString]);
439 }
440
441 return flag;
442 }
443
444
445 - (BOOL) installLibrary
446 {
447 NSString *buildDir = @"./build";
448 NSString *outputDir = [buildDir stringByAppendingPathComponent: [self name]];
449 NSString *installDir = [NSString stringForCommand: @"gnustep-config --variable=GNUSTEP_LOCAL_LIBRARIES"];
450 NSString *libraryPath = installDir; // ([paths firstObject] != nil ? [paths firstObject] : @"");
451 NSString *headersDir = [NSString stringForCommand: @"gnustep-config --variable=GNUSTEP_LOCAL_HEADERS"]; //[libraryPath stringByAppendingPathComponent: @"Headers"];
452 NSString *derivedSrcDir = @"derived_src";
453 NSString *derivedSrcHeaderDir = derivedSrcDir;
454 NSString *libName = [[_productReference path] lastPathComponent];
455 NSString *destPath = [libraryPath stringByAppendingPathComponent: libName];
456 NSError *error = nil;
457 NSFileManager *fileManager = [NSFileManager defaultManager];
458 NSString *uninstalledProductsDir = [outputDir stringByAppendingPathComponent: @"Products"];
459 NSString *fullPath = [uninstalledProductsDir stringByAppendingPathComponent: libName];
460
461 NSString *cwd = [fileManager currentDirectoryPath];
462
463 NSLog(@"******* CWD = %@", cwd);
464 BOOL f = NO;
465 f = [fileManager fileExistsAtPath: fullPath];
466 if (f == YES)
467 {
468 xcputs([[NSString stringWithFormat: @"\tLibrary exists %@...", fullPath] cString]);
469 }
470
471 f = [fileManager fileExistsAtPath: destPath];
472 if (f == YES)
473 {
474 xcputs([[NSString stringWithFormat: @"\tLibrary already exists at path %@...", destPath] cString]);
475 }
476 else
477 {
478 xcputs([[NSString stringWithFormat: @"\tCopy static library %@ -> %@", fullPath, destPath] cString]);
479 [fileManager copyItemAtPath: fullPath
480 toPath: destPath
481 error: &error];
482 if (error != nil)
483 {
484 xcputs([[NSString stringWithFormat: @"Error while copying library: %@", error] cString]);
485 //return NO;
486 }
487 }
488
489 // NSString *libName = [fullPath lastPathComponent];
490 NSString *libHeaderDir = [libName stringByReplacingOccurrencesOfString: @"lib" withString: @""];
491 libHeaderDir = [libHeaderDir stringByReplacingOccurrencesOfString: @".a" withString: @""];
492 libHeaderDir = [libHeaderDir stringByReplacingOccurrencesOfString: @".so" withString: @""];
493 NSString *libHeadersPath = [headersDir stringByAppendingPathComponent: libHeaderDir];
494
495 NSLog(@"headers path = %@", libHeadersPath);
496 [fileManager createDirectoryAtPath: libHeadersPath
497 withIntermediateDirectories: YES
498 attributes: nil
499 error: &error];
500 if (error != nil)
501 {
502 xcputs([[NSString stringWithFormat: @"Error while creating directory %@ : %@", libHeadersPath, error] cString]);
503 return NO;
504 }
505
506 NSEnumerator *en = [fileManager enumeratorAtPath: derivedSrcHeaderDir];
507 id file = nil;
508 while((file = [en nextObject]) != nil)
509 {
510 NSString *srcFile = [libHeaderDir stringByAppendingPathComponent: file];
511 NSString *destFile = [libHeadersPath stringByAppendingPathComponent: file];
512 xcputs([[NSString stringWithFormat: @"\tCopy %@ -> %@",srcFile,destFile] cString]);
513 [fileManager copyItemAtPath: srcFile
514 toPath: destFile
515 error: &error];
516 if (error != nil)
517 {
518 xcputs([[NSString stringWithFormat: @"Error while copying header: %@", error] cString]);
519 return NO;
520 }
521 }
522
523 return YES;
524 }
525
526 - (BOOL) installDynamicLibrary
527 {
528 NSString *buildDir = @"./build";
529 NSString *outputDir = [buildDir stringByAppendingPathComponent: [self name]];
530 NSString *installDir = [NSString stringForCommand: @"gnustep-config --variable=GNUSTEP_LOCAL_LIBRARIES"];
531 NSString *libraryPath = installDir;
532 NSString *librariesDir = [libraryPath stringByAppendingPathComponent: @"Libraries"];
533 NSString *headersDir = [NSString stringForCommand: @"gnustep-config --variable=GNUSTEP_LOCAL_HEADERS"]; //[libraryPath stringByAppendingPathComponent: @"Headers"];
534 NSString *derivedSrcDir = @"derived_src";
535 NSString *derivedSrcHeaderDir = derivedSrcDir;
536 NSString *destPath = [librariesDir stringByAppendingPathComponent: [_productReference path]];
537 NSError *error = nil;
538 NSFileManager *fileManager = [NSFileManager defaultManager];
539 NSString *uninstalledProductsDir = [outputDir stringByAppendingPathComponent: @"Products"];
540 NSString *fullPath = [uninstalledProductsDir stringByAppendingPathComponent: [_productReference path]];
541
542 xcputs([[NSString stringWithFormat: @"\tCopy dynamic library %@ -> %@",fullPath,destPath] cString]);
543 [fileManager copyItemAtPath: fullPath
544 toPath: destPath
545 error: &error];
546 if (error != nil)
547 {
548 xcputs([[NSString stringWithFormat: @"Error while copying: (%@)", error] cString]);
549 return NO;
550 }
551
552 NSString *libName = [fullPath lastPathComponent];
553 NSString *libHeaderDir = [libName stringByReplacingOccurrencesOfString: @"lib" withString: @""];
554 libHeaderDir = [libHeaderDir stringByReplacingOccurrencesOfString: @".a" withString: @""];
555 libHeaderDir = [libHeaderDir stringByReplacingOccurrencesOfString: @".so" withString: @""];
556 NSString *libHeadersPath = [headersDir stringByAppendingPathComponent: libHeaderDir];
557
558 [fileManager createDirectoryAtPath: libHeadersPath
559 withIntermediateDirectories: YES
560 attributes: nil
561 error: &error];
562 if (error != nil)
563 {
564 xcputs([[NSString stringWithFormat: @"Error while creating directory %@ : (%@)",libHeadersPath, error] cString]);
565 return NO;
566 }
567
568 NSEnumerator *en = [fileManager enumeratorAtPath: derivedSrcHeaderDir];
569 id file = nil;
570 while((file = [en nextObject]) != nil)
571 {
572 NSString *srcFile = [libHeaderDir stringByAppendingPathComponent: file];
573 NSString *destFile = [libHeadersPath stringByAppendingPathComponent: file];
574 xcputs([[NSString stringWithFormat: @"\tCopy %@ -> %@",srcFile,destFile] cString]);
575 [fileManager copyItemAtPath: srcFile
576 toPath: destFile
577 error: &error];
578 if (error != nil)
579 {
580 xcputs([[NSString stringWithFormat: @"Error while copying: (%@)", error] cString]);
581 return NO;
582 }
583 }
584
585 return YES;
586 }
587
588 - (BOOL) install
589 {
590 BOOL f = NO;
591
592 xcputs([[NSString stringWithFormat: @"=== Installing Target %@",_name] cString]);
593
594 if([_productType isEqualToString: TOOL_TYPE])
595 {
596 f = [self installTool];
597 }
598 else if([_productType isEqualToString: APPLICATION_TYPE])
599 {
600 f = [self installApp];
601 }
602 else if([_productType isEqualToString: FRAMEWORK_TYPE])
603 {
604 f = [self installFramework];
605 }
606 else if([_productType isEqualToString: LIBRARY_TYPE])
607 {
608 f = [self installLibrary];
609 }
610 else if([_productType isEqualToString: DYNAMIC_LIBRARY_TYPE])
611 {
612 f = [self installDynamicLibrary];
613 }
614
615 xcputs([[NSString stringWithFormat: @"=== Completed Installing Target %@",_name] cString]);
616
617 return f;
618 }
619
620 - (GSXCGenerator *) _loadGeneratorBundleFromDirectory: (NSString *)dir
621 withName: (NSString *)bname
622 {
623 NSString *bundleName = [bname stringByAppendingPathExtension: @"generator"];
624 NSString *bundlePath = [dir stringByAppendingPathComponent: bundleName];
625 NSBundle *generatorBundle = [NSBundle bundleWithPath: bundlePath];
626
627 if (generatorBundle != nil)
628 {
629 NSString *className = [[generatorBundle infoDictionary] objectForKey: @"NSPrincipalClass"];
630
631 if (className != nil)
632 {
633 Class cls = [generatorBundle classNamed: className];
634
635 if (cls != nil)
636 {
637 GSXCGenerator *generator = [[cls alloc] initWithTarget: self];
638
639 if (generator != nil)
640 {
641 return generator;
642 }
643 else
644 {
645 NSLog(@"Could not instantiate generator: %@", className);
646 }
647 }
648 else
649 {
650 NSLog(@"Could not build class from string: %@", className);
651 }
652 }
653 else
654 {
655 NSLog(@"NSPrincipalClass not specified in plist for bundle: %@", bundlePath);
656 }
657 }
658
659 return nil;
660 }
661
662
663 - (BOOL) invokeGeneratorBundle
664 {
665 NSString *generatorName = [[_project container] parameter]; // @"Makefile"; // default if not specified...
666 NSString *bundlesDir = nil;
667 GSXCGenerator *generator = nil;
668
669 bundlesDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
670 bundlesDir = [bundlesDir stringByAppendingPathComponent: @"Bundles"];
671 generator = [self _loadGeneratorBundleFromDirectory: bundlesDir
672 withName: generatorName];
673 if (generator) return [generator generate];
674
675 bundlesDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSLocalDomainMask, YES) lastObject];
676 bundlesDir = [bundlesDir stringByAppendingPathComponent: @"Bundles"];
677 generator = [self _loadGeneratorBundleFromDirectory: bundlesDir
678 withName: generatorName];
679 if (generator) return [generator generate];
680
681 bundlesDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSSystemDomainMask, YES) lastObject];
682 bundlesDir = [bundlesDir stringByAppendingPathComponent: @"Bundles"];
683 generator = [self _loadGeneratorBundleFromDirectory: bundlesDir
684 withName: generatorName];
685 if (generator) return [generator generate];
686
687 return NO;
688 }
689
690
691 - (BOOL) generate
692 {
693 BOOL result = YES;
694 NSEnumerator *en = nil;
695 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
696 id o = nil;
697
698 xcputs([[NSString stringWithFormat: @"=== Generating Target: %@", _name] cString]);
699 [[self buildConfigurationList] applyDefaultConfiguration];
700 [context setObject: _productType
701 forKey: @"PRODUCT_TYPE"];
702 [context setObject: [self buildConfigurationList]
703 forKey: @"buildConfig"];
704 if(_productSettingsXML != nil)
705 {
706 [context setObject: _productSettingsXML
707 forKey: @"PRODUCT_SETTINGS_XML"];
708 }
709
710 // Iterate over dependecies...
711 xcputs([[NSString stringWithFormat: @"=== Checking Dependencies"] cString]);
712 en = [[self dependencies] objectEnumerator];
713 while((o = [en nextObject]) != nil && result)
714 {
715 result = [o generate];
716 if(NO == result)
717 {
718 xcputs([[NSString stringWithFormat: @"*** Failed to generate dependency: %@", o] cString]);
719 }
720
721 }
722 xcputs([[NSString stringWithFormat: @"=== Done."] cString]);
723
724 // Interate over phases...
725 xcputs([[NSString stringWithFormat: @"=== Interpreting build phases..."] cString]);
726 en = [[self buildPhases] objectEnumerator];
727 while((o = [en nextObject]) != nil && result)
728 {
729 NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
730
731 NSDebugLog(@"Phase = %@", o);
732
733 [o setTarget: self];
734 result = [o generate];
735 if(NO == result)
736 {
737 xcputs([[NSString stringWithFormat: @"*** Failed to generate build phase: %@", o] cString]);
738 }
739
740 RELEASE(p);
741 }
742 xcputs([[NSString stringWithFormat: @"=== Done..."] cString]);
743
744 return [self invokeGeneratorBundle];
745 }
746
747 - (BOOL) link
748 {
749 BOOL result = YES;
750 NSEnumerator *en = nil;
751 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
752 NSDictionary *plistFile = [NSDictionary dictionaryWithContentsOfFile:
753 @"buildtool.plist"];
754 NSArray *skippedTarget = [plistFile objectForKey:
755 @"skippedTarget"];
756
757 if ([skippedTarget containsObject: [self name]])
758 {
759 xcputs([[NSString stringWithFormat: @"=== Skipping Target %s%@%s", YELLOW, _name, RESET] cString]);
760 return YES;
761 }
762
763 xcputs([[NSString stringWithFormat: @"=== Building Target %s%@%s", GREEN, _name, RESET] cString]);
764 [_buildConfigurationList applyDefaultConfiguration];
765 [context setObject: _buildConfigurationList
766 forKey: @"buildConfig"];
767 [context setObject: _productType
768 forKey: @"PRODUCT_TYPE"];
769 if(_productSettingsXML != nil)
770 {
771 [context setObject: _productSettingsXML
772 forKey: @"PRODUCT_SETTINGS_XML"];
773 }
774 xcputs([[NSString stringWithFormat: @"=== Checking Dependencies"] cString]);
775 id dependency = nil;
776 en = [_dependencies objectEnumerator];
777 while((dependency = [en nextObject]) != nil && result)
778 {
779 result = [dependency build];
780 }
781 xcputs([[NSString stringWithFormat: @"=== Done."] cString]);
782
783 xcputs([[NSString stringWithFormat: @"=== Executing build phases..."] cString]);
784
785 [self _productWrapper];
786 id phase = nil;
787 en = [_buildPhases objectEnumerator];
788 while((phase = [en nextObject]) != nil && result)
789 {
790 NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
791
792 [phase setTarget: self];
793 result = [phase link];
794 if(NO == result)
795 {
796 xcputs([[NSString stringWithFormat: @"*** Failed build phase: %@",phase] cString]);
797 }
798
799 RELEASE(p);
800 }
801 xcputs([[NSString stringWithFormat: @"=== Done..."] cString]);
802 xcputs([[NSString stringWithFormat: @"=== Completed Executing Target %@", _name] cString]);
803
804 return result;
805 }
806
807 - (NSString *) description
808 {
809 return [NSString stringWithFormat: @"%@-%@", [super description], [self name]];
810 }
811 @end