Debian Codegraph @

XCode/PBXFrameworksBuildPhase.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 "PBXCommon.h"
26 #import "PBXFrameworksBuildPhase.h"
27 #import "PBXFileReference.h"
28 #import "PBXBuildFile.h"
29 #import "GSXCBuildContext.h"
30 #import "NSArray+Additions.h"
31 #import "NSString+PBXAdditions.h"
32 #import "PBXTarget.h"
33
34 #import "GSXCCommon.h"
35
36 #import <Foundation/NSPathUtilities.h>
37
38 @implementation PBXFrameworksBuildPhase
39
40 - (NSString *) _gsConfigString
41 {
42 NSString *configString = nil;
43
44 configString = @"gnustep-config";
45
46 return configString;
47 }
48
49 - (NSString *) linkerForBuild
50 {
51 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
52 BOOL linkWithCpp = [[context objectForKey: @"LINK_WITH_CPP"] isEqualToString: @"YES"];
53 NSString *compiler = [NSString stringWithFormat: @"`%@ --variable=CC`", [self _gsConfigString]];
54
55 if (linkWithCpp)
56 {
57 compiler = [NSString stringWithFormat: @"`%@ --variable=CXX`", [self _gsConfigString]];
58 }
59
60 return compiler;
61 }
62
63 - (NSString *) processOutputFilesString
64 {
65 NSString *outputFiles = [[GSXCBuildContext sharedBuildContext] objectForKey:
66 @"OUTPUT_FILES"];
67 return outputFiles;
68 }
69
70 - (void) generateDummyClass
71 {
72 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
73 NSString *executableName = [context objectForKey: @"EXECUTABLE_NAME"];
74 NSString *outputDir = [[context objectForKey: @"PROJECT_ROOT"]
75 stringByAppendingPathComponent: @"derived_src"];
76 NSString *fileName = [NSString stringWithFormat: @"NSFramework_%@.m",executableName];
77 NSString *outputPath = [outputDir stringByAppendingPathComponent: fileName];
78 NSString *buildDir = [context objectForKey: @"TARGET_BUILD_DIR"];
79 NSString *objDir = [context objectForKey: @"BUILT_PRODUCTS_DIR"];
80 NSString *scriptPath = [[NSBundle bundleForClass: [self class]]
81 pathForResource: @"create-dummy-class" ofType: @"sh"];
82 NSString *of = [self processOutputFilesString];
83 NSString *outputFiles = (of == nil)?@"":of;
84 NSString *files = [outputFiles stringByReplacingOccurrencesOfString: @"'" withString: @""];
85
86 objDir = (objDir == nil) ? buildDir : objDir;
87
88 BOOL f = NO;
89 NSString *classesCommand = [NSString stringWithFormat: @"%@ '%@' '%@'", scriptPath, files, executableName];
90 // NSLog(@"classesCommand = %@\n", classesCommand); // [context currentContext]);
91 f = xcsystem(classesCommand) == 0;
92
93 if( f )
94 {
95 NSString *compiler = nil;
96 NSString *buildPath = outputPath;
97 NSString *objPath = [objDir stringByAppendingPathComponent: [fileName stringByAppendingString: @".o"]];
98 if([compiler isEqualToString: @""] || compiler == nil)
99 {
100 compiler = [self linkerForBuild];
101 }
102
103 NSString *configString = [context objectForKey: @"CONFIG_STRING"];
104 NSString *buildTemplate = @"%@ %@ -c %@ -o %@";
105 NSString *buildCommand = [NSString stringWithFormat: buildTemplate,
106 compiler,
107 [buildPath stringByEscapingSpecialCharacters],
108 configString,
109 [objPath stringByEscapingSpecialCharacters]];
110 NSString *of = [self processOutputFilesString];
111 NSString *outputFiles = (of == nil)?@"":of;
112 //NSLog(@"\t%@ %@",buildCommand, outputFiles);
113 BOOL success = xcsystem(buildCommand) == 0;
114 if (success)
115 {
116 outputFiles = [outputFiles stringByAppendingString: [NSString stringWithFormat: @" %@", objPath]];
117 [context setObject: outputFiles forKey: @"OUTPUT_FILES"];
118 }
119 else
120 {
121 NSLog(@"** build of framework dummy class failed.");
122 }
123 }
124 else
125 {
126 NSLog(@"** failed to run dummy class generation script");
127 }
128 }
129
130 - (NSString *) frameworkLinkString: (NSString*)framework
131 {
132 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
133 NSString *path = [[NSBundle bundleForClass: [self class]]
134 pathForResource: @"Framework-mapping" ofType: @"plist"];
135 NSDictionary *propList = [[NSString stringWithContentsOfFile: path] propertyList];
136 NSMutableArray *ignored = [[propList objectForKey: @"Ignored"] mutableCopy];
137 NSMutableDictionary *mapped = [[propList objectForKey: @"Mapped"] mutableCopy];
138 NSDictionary *configDict = [context configForTargetName: [[self target] name]];
139 NSString *result = nil;
140 NSString *fw = [framework copy];
141
142 if ([fw hasPrefix: @"lib"])
143 {
144 fw = [fw stringByReplacingCharactersInRange: NSMakeRange(0,3)
145 withString: @""];
146 }
147
148 NSDebugLog(@"\t* config = %@", configDict);
149 NSDebugLog(@"\t* target = %@", [[self target] name]);
150
151 if ([configDict objectForKey: @"mapped"] != nil)
152 {
153 [mapped addEntriesFromDictionary: [configDict objectForKey: @"mapped"]];
154 }
155
156 if ([configDict objectForKey: @"ignored"] != nil)
157 {
158 [ignored addObjectsFromArray: [configDict objectForKey: @"ignored"]];
159 }
160
161 NSDebugLog(@"path = %@", path);
162 NSDebugLog(@"%@", fw);
163
164 if ([ignored containsObject: fw])
165 {
166 xcprintf("\t- Ignored: %s\n",[fw cString]);
167 return @"";
168 }
169 else
170 {
171 NSDebugLog(@"%@ not found in %@", fw, ignored);
172 }
173
174 result = [mapped objectForKey: fw];
175 if (result == nil)
176 {
177 result = [NSString stringWithFormat: @"-l%@ ", fw];
178 xcprintf("\t* Linking: %s\n", [result cString]);
179 }
180 else
181 {
182 result = [result stringByAppendingString: @" "];
183 xcprintf("\t+ Remapped: %s -> %s\n", [fw cString], [result cString]);
184 }
185
186 return result;
187 }
188
189 - (NSString *) linkString
190 {
191 NSString *cfgString = [self _gsConfigString];
192 NSString *systemLibDir = [NSString stringWithFormat: @"`%@ --variable=GNUSTEP_SYSTEM_LIBRARIES`", cfgString];
193 NSString *localLibDir = [NSString stringWithFormat: @"`%@ --variable=GNUSTEP_LOCAL_LIBRARIES`", cfgString];
194 NSString *userLibDir = [NSString stringWithFormat: @"`%@ --variable=GNUSTEP_USER_LIBRARIES`", cfgString];
195 NSString *buildDir = [NSString stringForEnvironmentVariable: @"TARGET_BUILD_DIR" defaultValue: @"build"];
196 NSString *uninstalledProductsDir = [buildDir stringByAppendingPathComponent: @"Products"];
197 NSString *linkString = [NSString stringWithFormat: @"-L/usr/local/lib -L/opt/local/lib -L%@ -L%@ -L%@ ",
198 userLibDir,
199 localLibDir,
200 systemLibDir];;
201 NSFileManager *manager = [NSFileManager defaultManager];
202 NSDirectoryEnumerator *dirEnumerator = [manager enumeratorAtPath:uninstalledProductsDir];
203 NSEnumerator *en = [_files objectEnumerator];
204 id file = nil;
205 NSString *lpath = nil;
206 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
207 NSDictionary *configDict = [context configForTargetName: [[self target] name]];
208 NSArray *linkerPaths = [configDict objectForKey: @"linkerPaths"];
209 NSString *wsLink = [context objectForKey: @"WORKSPACE_LINK_LINE"];
210 NSString *wsLibs = [context objectForKey: @"WORKSPACE_LIBS_LINE"];
211
212 // If the workspace link string exists, add it...
213 if (wsLink != nil)
214 {
215 linkString = [linkString stringByAppendingString: wsLink];
216 NSDebugLog(@"linkString = %@", linkString);
217 }
218
219 if (wsLibs != nil)
220 {
221 linkString = [linkString stringByAppendingString: wsLibs];
222 xcprintf("\t* Linking from Workspace: %s\n", [wsLibs cString]);
223 NSDebugLog(@"linkString = %@", linkString);
224 }
225
226 en = [linkerPaths objectEnumerator];
227 while((lpath = [en nextObject]) != nil)
228 {
229 linkString = [linkString stringByAppendingString: [NSString stringWithFormat: @"-L%@ ", lpath]];
230 }
231
232 en = [_files objectEnumerator];
233 while((file = [en nextObject]) != nil)
234 {
235 PBXFileReference *fileRef = [file fileRef];
236 NSString *name = [[[fileRef path] lastPathComponent] stringByDeletingPathExtension];
237
238 linkString = [linkString stringByAppendingString: [self frameworkLinkString: name]];
239 }
240
241 // Find any frameworks and add them to the -L directive...
242 while((file = [dirEnumerator nextObject]) != nil)
243 {
244 NSString *ext = [file pathExtension];
245 if([ext isEqualToString:@"framework"])
246 {
247 NSString *headerDir = [file stringByAppendingPathComponent:@"Headers"];
248 linkString = [linkString stringByAppendingString:
249 [NSString stringWithFormat:@"-I%@ ",
250 [uninstalledProductsDir stringByAppendingPathComponent:headerDir]]];
251 linkString = [linkString stringByAppendingString:
252 [NSString stringWithFormat:@"-L%@ ",
253 [uninstalledProductsDir stringByAppendingPathComponent:file]]];
254 }
255 }
256
257 NSArray *otherLDFlags = [context objectForKey: @"OTHER_LDFLAGS"];
258 NSDebugLog(@"OTHER_LDFLAGS = %@", otherLDFlags);
259 en = [otherLDFlags objectEnumerator];
260 while((file = [en nextObject]) != nil)
261 {
262 if ([file isEqualToString: @"-framework"])
263 {
264 NSString *framework = [en nextObject];
265 linkString = [linkString stringByAppendingString: [self frameworkLinkString: framework]];
266 }
267 }
268
269 // linkString = [linkString stringByAppendingString: @" -lpthread -lobjc -lm "];
270 linkString = [linkString stringByAppendingString: @" -lobjc "];
271
272 // Do substitutions and additions for buildtool.plist...
273 NSDictionary *substitutionList = [configDict objectForKey: @"substitutions"];
274 NSArray *additionalFlags = [configDict objectForKey: @"additional"];
275 // NSNumber *flag = [configDict objectForKey: @"translateDylibs"];
276
277 NSDebugLog(@"%@",configDict);
278 NSDebugLog(@"%@", additionalFlags);
279 if (additionalFlags != nil)
280 {
281 [context setObject: additionalFlags forKey: @"ADDITIONAL_OBJC_LIBS"];
282 }
283
284 // Replace anything that needs substitution... not all libraries on macos map directly...
285 en = [[substitutionList allKeys] objectEnumerator];
286 id o = nil;
287 while ((o = [en nextObject]) != nil)
288 {
289 NSString *r = [substitutionList objectForKey: o];
290 linkString = [linkString stringByReplacingOccurrencesOfString: o
291 withString: r];
292 }
293
294 // Add any additional libs...
295 en = [additionalFlags objectEnumerator];
296 o = nil;
297 while ((o = [en nextObject]) != nil)
298 {
299 linkString = [linkString stringByAppendingFormat: @" %@ ", o];
300 xcprintf("\t+ Additional: %s\n",[o cString]);
301 }
302
303 return linkString;
304 }
305
306 - (BOOL) buildTool
307 {
308 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
309 NSString *cfgString = [self _gsConfigString];
310
311 xcputs([[NSString stringWithFormat: @"=== Executing Frameworks / Linking Build Phase (Tool)"] cString]);
312 NSString *compiler = [self linkerForBuild];
313 NSString *outputFiles = [self processOutputFilesString];
314 NSString *outputDir = [context objectForKey: @"PRODUCT_OUTPUT_DIR"];
315 NSString *executableName = [context objectForKey: @"EXECUTABLE_NAME"];
316 NSString *outputPath = [outputDir stringByAppendingPathComponent: executableName];
317 NSString *linkString = [self linkString];
318 linkString = [linkString stringByAppendingString: [NSString stringWithFormat:
319 @" `%@ --base-libs` `%@ --variable=LDFLAGS` -lgnustep-base ",
320 cfgString, cfgString]];
321
322 NSProcessInfo *pi = [NSProcessInfo processInfo];
323 NSUInteger os = [pi operatingSystem];
324 NSString *command = [NSString stringWithFormat:
325 @"%@ -rdynamic -shared-libgcc -fgnu-runtime -o \"%@\" %@ %@",
326 compiler,
327 outputPath,
328 outputFiles,
329 linkString];
330
331 if (os == NSWindowsNTOperatingSystem || os == NSWindows95OperatingSystem)
332 {
333 outputPath = [outputPath stringByAppendingPathExtension: @"exe"];
334 command = [NSString stringWithFormat:
335 @"%@ -shared-libgcc -fgnu-runtime -o \"%@\" %@ %@",
336 compiler,
337 outputPath,
338 outputFiles,
339 linkString];
340 }
341
342 NSDebugLog(@"command = %@", command);
343 NSString *modified = [context objectForKey: @"MODIFIED_FLAG"];
344 int result = 0;
345 if([modified isEqualToString: @"YES"])
346 {
347 result = xcsystem(command);
348 if (result != 0)
349 {
350 NSLog(@"%sReturn Value:%s %d", RED, RESET, result);
351 NSLog(@"%sCommand:%s %s%@%s", RED, RESET, GREEN, command, RESET);
352 }
353 }
354 else
355 {
356 xcputs([[NSString stringWithFormat: @"\t** Nothing to be done for %@, no modifications.",outputPath] cString]);
357 }
358
359 xcputs("=== Frameworks / Linking Build Phase Completed");
360 return (result == 0);
361 }
362
363 - (BOOL) buildApp
364 {
365 xcputs("=== Executing Frameworks / Linking Build Phase (Application)");
366 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
367 NSString *compiler = [self linkerForBuild];
368 NSString *outputFiles = [self processOutputFilesString];
369 NSString *outputDir = [context objectForKey: @"PRODUCT_OUTPUT_DIR"];
370 // NSString *errorPath = [outputDir stringByAppendingPathComponent: @"linker.err"];
371 NSString *executableName = [context objectForKey: @"EXECUTABLE_NAME"];
372 NSString *outputPath = [outputDir stringByAppendingPathComponent: executableName];
373 NSString *linkString = [self linkString];
374 NSString *cfgString = [self _gsConfigString];
375 NSProcessInfo *pi = [NSProcessInfo processInfo];
376 NSUInteger os = [pi operatingSystem];
377
378 NSDebugLog(@"Output files = %@", outputFiles);
379
380 if (outputFiles == nil)
381 {
382 xcputs("\t++++ No object files found. Nothing to link ++++\n");
383 return YES;
384 }
385
386 linkString = [NSString stringWithFormat: [linkString stringByAppendingString: @" `%@ --objc-flags --objc-libs " \
387 @"--base-libs --gui-libs` `%@ --variable=LDFLAGS` " \
388 @"-lgnustep-base -lgnustep-gui "], cfgString, cfgString];
389 NSDebugLog(@"LINK: %@", linkString);
390
391 NSString *command = nil;
392 if (os == NSWindowsNTOperatingSystem || os == NSWindows95OperatingSystem)
393 {
394 outputPath = [outputPath stringByAppendingPathExtension: @"exe"];
395 command = [NSString stringWithFormat:
396 @"%@ -shared-libgcc -fgnu-runtime -o \"%@\" %@ %@",
397 compiler,
398 outputPath,
399 outputFiles,
400 linkString];
401 }
402 else
403 {
404 command = [NSString stringWithFormat:
405 @"%@ -rdynamic -shared-libgcc -fgnu-runtime -o \"%@\" %@ %@",
406 compiler,
407 outputPath,
408 outputFiles,
409 linkString];
410 }
411
412 // NSLog(@"Link command = %@", command);
413 // NSString *modified = [context objectForKey: @"MODIFIED_FLAG"];
414 int result = 0;
415 if(YES) // [modified isEqualToString: @"YES"])
416 {
417 xcputs([[NSString stringWithFormat: @"\t* Linking \"%@\"",outputPath] cString]);
418 result = xcsystem(command);
419 }
420 else
421 {
422 xcputs([[NSString stringWithFormat: @"\t** Nothing to be done for \"%@\", no modifications.",outputPath] cString]);
423 }
424
425 xcputs("=== Frameworks / Linking Build Phase Completed");
426 fflush(stdout);
427
428 return (result == 0);
429 }
430
431 - (NSString *) _execName
432 {
433 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
434 NSString *name = [context objectForKey: @"EXECUTABLE_NAME"];
435
436 if( name == nil )
437 {
438 name = [context objectForKey: @"PRODUCT_NAME"];
439 if ( name == nil )
440 {
441 name = [context objectForKey: @"TARGET_NAME"];
442 }
443 }
444
445 return name;
446 }
447
448 - (NSString *) _productOutputDir
449 {
450 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
451 NSString *outputDir = [context objectForKey: @"PRODUCT_OUTPUT_DIR"];
452
453 if( outputDir == nil )
454 {
455 outputDir = [@"./build" stringByAppendingPathComponent: [self _execName]];
456 outputDir = [outputDir stringByAppendingPathComponent: @"Products"];
457 }
458
459 return outputDir;
460 }
461
462 - (BOOL) buildStaticLib
463 {
464 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
465 // NSDictionary *config = [context config];
466 // NSString *ctarget = [config objectForKey: @"target"];
467 // NSString *libext = [ctarget isEqualToString: @"msvc"] ? @"lib" : @"a";
468 // NSString *libpfx = [ctarget isEqualToString: @"msvc"] ? @"" : @"lib";
469 NSString *outputFiles = [self processOutputFilesString];
470 NSString *outputDir = [self _productOutputDir];
471 NSString *executableName = [[[_target productReference] path] lastPathComponent];
472 // [[NSString stringWithFormat: @"%@%@", libpfx,[self _execName]]
473 // stringByReplacingPathExtensionWith: libext];
474 NSString *outputPath = [outputDir stringByAppendingPathComponent: executableName];
475 NSString *commandTemplate = @"ar rc %@ %@; ranlib %@";
476 NSString *command = [NSString stringWithFormat: commandTemplate,
477 outputPath,
478 outputFiles,
479 outputPath];
480 NSString *modified = [context objectForKey: @"MODIFIED_FLAG"];
481 int result = 0;
482
483 xcputs("=== Executing Frameworks / Archiving Build Phase (Static Library)");
484 if([modified isEqualToString: @"YES"])
485 {
486 xcputs([[NSString stringWithFormat: @"\t* Linking %@",outputPath] cString]);
487 result = xcsystem(command);
488 }
489 else
490 {
491 xcputs([[NSString stringWithFormat: @"\t** Nothing to be done for %@, no modifications.",outputPath] cString]);
492 }
493
494 xcputs("=== Frameworks / Linking Build Phase Completed");
495
496 return (result == 0);
497 }
498
499 - (BOOL) buildDynamicLib
500 {
501 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
502 NSDictionary *config = [context config];
503 NSString *ctarget = [config objectForKey: @"target"];
504 NSString *outputFiles = [self processOutputFilesString];
505 NSString *modified = [context objectForKey: @"MODIFIED_FLAG"];
506 NSString *outputDir = [context objectForKey: @"PRODUCT_OUTPUT_DIR"];
507 NSString *executableName = [context objectForKey: @"EXECUTABLE_NAME"];
508 NSString *executableNameStripped = [executableName stringByDeletingPathExtension];
509 NSString *libName = [NSString stringWithFormat: @"%@.so",executableNameStripped];
510 NSString *outputPath = [outputDir stringByAppendingPathComponent: libName];
511
512 NSString *libraryPath = [outputDir stringByAppendingPathComponent: libName];
513 NSString *cfgString = [self _gsConfigString];
514 NSString *systemLibDir = [NSString stringWithFormat: @"`%@ --variable=GNUSTEP_SYSTEM_LIBRARIES`", cfgString];
515 NSString *localLibDir = [NSString stringWithFormat: @"`%@ --variable=GNUSTEP_LOCAL_LIBRARIES`", cfgString];
516 NSString *userLibDir = [NSString stringWithFormat: @"`%@ --variable=GNUSTEP_USER_LIBRARIES`", cfgString];
517 NSProcessInfo *pi = [NSProcessInfo processInfo];
518 NSUInteger os = [pi operatingSystem];
519 NSString *compiler = [self linkerForBuild];
520 NSString *command = nil;
521 NSString *commandTemplate = nil;
522 NSInteger result = 0;
523
524 xcputs("=== Executing Frameworks / Linking Build Phase (Dynamic Library");
525 if (os == NSWindowsNTOperatingSystem || os == NSWindows95OperatingSystem)
526 {
527 if ([ctarget containsString: @"msvc"])
528 {
529 outputPath = [outputPath stringByReplacingOccurrencesOfString: @"lib" withString: @""];
530
531 NSString *msvcLibname = [outputPath stringByAppendingPathExtension: @"lib"];
532 NSString *dllLibname = [libraryPath stringByReplacingPathExtensionWith: @"dll"];
533
534 commandTemplate = @"%@ -g -Wl,-dll -Wl,implib:%@ -o %@ %@ `gnustep-config --base-libs` "
535 @"-L%@ -L%@ -L%@";
536 libraryPath = [outputDir stringByAppendingPathComponent: msvcLibname];
537
538
539 command = [NSString stringWithFormat: commandTemplate,
540 compiler,
541 libraryPath,
542 dllLibname,
543 outputFiles,
544 userLibDir,
545 localLibDir,
546 systemLibDir];
547
548 }
549 else
550 {
551 commandTemplate = @"%@ -shared -Wl,-soname,lib%@.so "
552 @"-shared-libgcc -o %@ %@ "
553 @"-L%@ -L%@ -L%@";
554
555
556 command = [NSString stringWithFormat: commandTemplate,
557 compiler,
558 executableName,
559 libraryPath,
560 outputFiles,
561 userLibDir,
562 localLibDir,
563 systemLibDir];
564 }
565 }
566 else
567 {
568 commandTemplate = @"%@ -shared -Wl,-soname,lib%@.so -rdynamic "
569 @"-shared-libgcc -o %@ %@ "
570 @"-L%@ -L%@ -L%@";
571
572 command = [NSString stringWithFormat: commandTemplate,
573 compiler,
574 executableName,
575 libraryPath,
576 outputFiles,
577 userLibDir,
578 localLibDir,
579 systemLibDir];
580 }
581
582 if([modified isEqualToString: @"YES"])
583 {
584 xcputs([[NSString stringWithFormat: @"\t* Linking %@",outputPath] cString]);
585 result = xcsystem(command);
586 }
587 else
588 {
589 xcputs([[NSString stringWithFormat: @"\t** Nothing to be done for %@, no modifications.",outputPath] cString]);
590 }
591
592 xcputs("=== Frameworks / Linking Build Phase Completed");
593 return (result == 0);
594 }
595
596 - (BOOL) buildFramework
597 {
598 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
599 NSDictionary *config = [context config];
600 NSString *ctarget = [config objectForKey: @"target"];
601 NSInteger result = 0;
602
603 xcputs("=== Executing Frameworks / Linking Build Phase (Framework)");
604 [self generateDummyClass];
605
606 NSString *outputFiles = [self processOutputFilesString];
607 NSString *modified = [context objectForKey: @"MODIFIED_FLAG"];
608 NSString *outputDir = [context objectForKey: @"PRODUCT_OUTPUT_DIR"];
609 NSString *executableName = [context objectForKey: @"EXECUTABLE_NAME"];
610 NSString *outputPath = [outputDir stringByAppendingPathComponent: executableName];
611 NSString *frameworkVersion = [NSString stringForEnvironmentVariable: "FRAMEWORK_VERSION"];
612 if (frameworkVersion == nil)
613 {
614 frameworkVersion = @"0";
615 }
616 NSString *libNameWithVersion = [NSString stringWithFormat: @"lib%@.so.%@",
617 executableName,frameworkVersion];
618 NSString *libName = [NSString stringWithFormat: @"lib%@.so",executableName];
619
620 NSString *libraryPath = [outputDir stringByAppendingPathComponent: libNameWithVersion];
621 NSString *libraryPathNoVersion = [outputDir stringByAppendingPathComponent: libName];
622 NSString *cfgString = [self _gsConfigString];
623 NSString *systemLibDir = [NSString stringWithFormat: @"`%@ --variable=GNUSTEP_SYSTEM_LIBRARIES`", cfgString];
624 NSString *localLibDir = [NSString stringWithFormat: @"`%@ --variable=GNUSTEP_LOCAL_LIBRARIES`", cfgString];
625 NSString *userLibDir = [NSString stringWithFormat: @"`%@ --variable=GNUSTEP_USER_LIBRARIES`", cfgString];
626 NSString *frameworkRoot = [context objectForKey: @"FRAMEWORK_DIR"];
627 NSString *libraryLink = [frameworkRoot stringByAppendingPathComponent: libName];
628 NSString *execLink = [frameworkRoot stringByAppendingPathComponent: executableName];
629 NSProcessInfo *pi = [NSProcessInfo processInfo];
630 NSUInteger os = [pi operatingSystem];
631 NSString *compiler = [self linkerForBuild];
632 NSString *command = nil;
633 NSString *commandTemplate = nil;
634
635 if (os == NSWindowsNTOperatingSystem || os == NSWindows95OperatingSystem)
636 {
637 if ([ctarget containsString: @"msvc"])
638 {
639 NSString *msvcLibname = [outputPath stringByAppendingPathExtension: @"lib"];
640 NSString *dllLibname = [libraryPathNoVersion stringByReplacingPathExtensionWith: @"dll"];
641
642 commandTemplate = @"%@ -g -Wl,-dll -Wl,implib:%@ -o %@ %@ "
643 @"-L%@ -L%@ -L%@ "
644 @"`gnustep-config --gui-libs` ";
645 libraryPath = msvcLibname;
646
647
648 command = [NSString stringWithFormat: commandTemplate,
649 compiler,
650 libraryPath,
651 dllLibname,
652 outputFiles,
653 userLibDir,
654 localLibDir,
655 systemLibDir];
656
657 }
658 else
659 {
660 commandTemplate = @"%@ -shared -Wl,-soname,lib%@.so "
661 @"-shared-libgcc -o %@ %@ "
662 @"-L%@ -L%@ -L%@ "
663 @"`gnustep-config --gui-libs` ";
664
665 command = [NSString stringWithFormat: commandTemplate,
666 compiler,
667 executableName,
668 libraryPath,
669 outputFiles,
670 userLibDir,
671 localLibDir,
672 systemLibDir];
673 }
674 }
675 else
676 {
677 commandTemplate = @"%@ -shared -Wl,-soname,lib%@.so.%@ -rdynamic "
678 @"-shared-libgcc -o %@ %@ "
679 @"-L%@ -L%@ -L%@";
680
681 command = [NSString stringWithFormat: commandTemplate,
682 compiler,
683 executableName,
684 frameworkVersion,
685 libraryPath,
686 outputFiles,
687 userLibDir,
688 localLibDir,
689 systemLibDir];
690 }
691
692
693 // Create link to library...
694 [[NSFileManager defaultManager] createSymbolicLinkAtPath: outputPath
695 pathContent: libName];
696
697 [[NSFileManager defaultManager] createSymbolicLinkAtPath: libraryPathNoVersion
698 pathContent: libNameWithVersion];
699
700 [[NSFileManager defaultManager] createSymbolicLinkAtPath: libraryLink
701 pathContent:
702 [NSString stringWithFormat: @"Versions/Current/%@",libName]];
703
704 [[NSFileManager defaultManager] createSymbolicLinkAtPath: execLink
705 pathContent:
706 [NSString stringWithFormat: @"Versions/Current/%@",executableName]];
707
708
709 // NSLog(@"Link command = %@", command);
710 if([modified isEqualToString: @"YES"])
711 {
712 xcputs([[NSString stringWithFormat: @"\t* Linking %@",outputPath] cString]);
713 result = xcsystem(command);
714 }
715 else
716 {
717 xcputs([[NSString stringWithFormat: @"\t** Nothing to be done for %@, no modifications.",outputPath] cString]);
718 }
719
720 xcputs("=== Frameworks / Linking Build Phase Completed");
721 return (result == 0);
722 }
723
724 - (BOOL) buildBundle
725 {
726 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
727 xcputs("=== Executing Frameworks / Linking Build Phase (Bundle)");
728 NSString *compiler = [self linkerForBuild];
729 NSString *outputFiles = [self processOutputFilesString];
730 NSString *outputDir = [NSString stringWithCString: getenv("PRODUCT_OUTPUT_DIR")];
731 NSString *executableName = [NSString stringWithCString: getenv("EXECUTABLE_NAME")];
732 NSString *outputPath = [outputDir stringByAppendingPathComponent: executableName];
733 NSString *linkString = [self linkString];
734 NSProcessInfo *pi = [NSProcessInfo processInfo];
735 NSUInteger os = [pi operatingSystem];
736
737 NSString *command = [NSString stringWithFormat:
738 @"%@ -rdynamic -shared -o \"%@\" %@ %@",
739 compiler,
740 outputPath,
741 outputFiles,
742 linkString];
743
744 if (os == NSWindowsNTOperatingSystem || os == NSWindows95OperatingSystem)
745 {
746 command = [NSString stringWithFormat:
747 @"%@ -shared -o \"%@\" %@ %@",
748 compiler,
749 outputPath,
750 outputFiles,
751 linkString];
752 }
753
754 NSString *modified = [context objectForKey: @"MODIFIED_FLAG"];
755 int result = 0;
756 if([modified isEqualToString: @"YES"])
757 {
758 xcputs([[NSString stringWithFormat: @"\t* Linking %@",outputPath] cString]);
759 result = xcsystem(command);
760 }
761 else
762 {
763 xcputs([[NSString stringWithFormat: @"\t** Nothing to be done for %@, no modifications.",outputPath] cString]);
764 }
765
766 xcputs("=== Frameworks / Linking Build Phase Completed");
767 return (result == 0);
768 }
769
770 - (BOOL) buildTest
771 {
772 xcputs("=== Build tests... currently unsupported...");
773 return YES;
774 }
775
776 - (BOOL) build
777 {
778 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
779 NSString *productType = [context objectForKey: @"PRODUCT_TYPE"];
780 if([productType isEqualToString: APPLICATION_TYPE])
781 {
782 return [self buildApp];
783 }
784 else if([productType isEqualToString: TOOL_TYPE])
785 {
786 return [self buildTool];
787 }
788 else if([productType isEqualToString: LIBRARY_TYPE])
789 {
790 return [self buildStaticLib];
791 }
792 else if([productType isEqualToString: DYNAMIC_LIBRARY_TYPE])
793 {
794 return [self buildDynamicLib];
795 }
796 else if([productType isEqualToString: FRAMEWORK_TYPE])
797 {
798 return [self buildFramework];
799 }
800 else if([productType isEqualToString: BUNDLE_TYPE])
801 {
802 return [self buildBundle];
803 }
804 else if([productType isEqualToString: TEST_TYPE])
805 {
806 return [self buildTest];
807 }
808 else
809 {
810 xcputs([[NSString stringWithFormat: @"***** ERROR: Unknown product type: %@",productType] cString]);
811 }
812 return NO;
813 }
814
815 - (BOOL) generate
816 {
817 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
818 NSString *productType = [context objectForKey: @"PRODUCT_TYPE"];
819 NSDictionary *configDict = [context configForTargetName: [[self target] name]];
820 NSArray *additionalFlags = [configDict objectForKey: @"additional"];
821
822 NSDebugLog(@"%@", additionalFlags);
823 if (additionalFlags != nil)
824 {
825 [context setObject: additionalFlags forKey: @"ADDITIONAL_OBJC_LIBS"];
826 }
827
828 xcprintf("\t* Adding product type entry: %s\n", [productType cStringUsingEncoding: NSUTF8StringEncoding]);
829
830 if([productType isEqualToString: APPLICATION_TYPE])
831 {
832 [context setObject: @"application"
833 forKey: @"PROJECT_TYPE"];
834 }
835 else if([productType isEqualToString: TOOL_TYPE])
836 {
837 [context setObject: @"tool"
838 forKey: @"PROJECT_TYPE"];
839 }
840 else if([productType isEqualToString: LIBRARY_TYPE])
841 {
842 [context setObject: @"library"
843 forKey: @"PROJECT_TYPE"];
844 }
845 else if([productType isEqualToString: DYNAMIC_LIBRARY_TYPE])
846 {
847 [context setObject: @"library"
848 forKey: @"PROJECT_TYPE"];
849 }
850 else if([productType isEqualToString: FRAMEWORK_TYPE])
851 {
852 [context setObject: @"framework"
853 forKey: @"PROJECT_TYPE"];
854 }
855 else if([productType isEqualToString: BUNDLE_TYPE])
856 {
857 [context setObject: @"bundle"
858 forKey: @"PROJECT_TYPE"];
859 }
860 else if([productType isEqualToString: TEST_TYPE])
861 {
862 [context setObject: @"test"
863 forKey: @"PROJECT_TYPE"];
864 }
865 else
866 {
867 xcputs([[NSString stringWithFormat: @"***** ERROR: Unknown product type: %@",productType] cString]);
868 }
869
870 return YES;
871 }
872
873 - (BOOL) link
874 {
875 GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext];
876
877 [context setObject: @"YES" forKey: @"MODIFIED_FLAG"];
878
879 return [self build];
880 }
881
882 @end