-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSBundle.m
More file actions
3404 lines (3019 loc) · 92.9 KB
/
NSBundle.m
File metadata and controls
3404 lines (3019 loc) · 92.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** Implementation of NSBundle class
Copyright (C) 1993-2002 Free Software Foundation, Inc.
Written by: Adam Fedor <fedor@boulder.colorado.edu>
Date: May 1993
Author: Mirko Viviani <mirko.viviani@rccr.cremona.it>
Date: October 2000 Added frameworks support
Author: Nicola Pero <nicola@brainstorm.co.uk>
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
<title>NSBundle class reference</title>
$Date$ $Revision$
*/
#define EXPOSE_NSBundle_IVARS 1
#import "common.h"
#include "objc-load.h"
#import "Foundation/NSBundle.h"
#import "Foundation/NSException.h"
#import "Foundation/NSArray.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSEnumerator.h"
#import "Foundation/NSNull.h"
#import "Foundation/NSProcessInfo.h"
#import "Foundation/NSUserDefaults.h"
#import "Foundation/NSNotification.h"
#import "Foundation/NSLock.h"
#import "Foundation/NSMapTable.h"
#import "Foundation/NSAutoreleasePool.h"
#import "Foundation/NSFileManager.h"
#import "Foundation/NSPathUtilities.h"
#import "Foundation/NSData.h"
#import "Foundation/NSURL.h"
#import "Foundation/NSValue.h"
#import "Foundation/NSSet.h"
#import "GNUstepBase/NSString+GNUstepBase.h"
#import "GNUstepBase/NSTask+GNUstepBase.h"
#import "GSPrivate.h"
static NSFileManager *
manager()
{
static NSFileManager *mgr = nil;
if (mgr == nil)
{
mgr = RETAIN([NSFileManager defaultManager]);
[[NSObject leakAt: &mgr] release];
}
return mgr;
}
static NSDictionary *langAliases = nil;
static NSDictionary *langCanonical = nil;
/* Map a language name to any alternative versions. This function should
* return an array of alternative language/localisation directory names in
* the preferred order of precedence (ie resources in the directories named
* earlier in the array are to be preferred to those in directories named
* later).
* We should support regional language specifications (such as en-GB)
* as our first priority, and then fall back to the more general names.
* NB. Also handle the form like en-GB_US (English language, British dialect,
* in the United States region).
*/
static NSArray *
altLang(NSString *full)
{
NSMutableArray *a = nil;
if (nil != full)
{
NSString *alias = nil;
NSString *canon = nil;
NSString *lang = nil;
NSString *dialect = nil;
NSString *region = nil;
NSRange r;
alias = [langAliases objectForKey: full];
if (nil == alias)
{
canon = [langCanonical objectForKey: full];
if (nil != canon)
{
alias = [langAliases objectForKey: canon];
}
if (nil == alias)
{
alias = full;
}
}
canon = [langCanonical objectForKey: alias];
if (nil == canon)
{
canon = [langCanonical objectForKey: full];
if (nil == canon)
{
canon = full;
}
}
if ((r = [canon rangeOfString: @"-"]).length > 1)
{
dialect = [canon substringFromIndex: NSMaxRange(r)];
lang = [canon substringToIndex: r.location];
if ((r = [dialect rangeOfString: @"_"]).length > 1)
{
region = [dialect substringFromIndex: NSMaxRange(r)];
dialect = [dialect substringToIndex: r.location];
}
}
else if ((r = [canon rangeOfString: @"_"]).length > 1)
{
region = [canon substringFromIndex: NSMaxRange(r)];
lang = [canon substringToIndex: r.location];
}
else
{
lang = canon;
}
a = [NSMutableArray arrayWithCapacity: 5];
if (nil != dialect && nil != region)
{
[a addObject: [NSString stringWithFormat: @"%@-%@_%@",
lang, dialect, region]];
}
if (nil != dialect)
{
[a addObject: [NSString stringWithFormat: @"%@-%@",
lang, dialect]];
}
if (nil != region)
{
[a addObject: [NSString stringWithFormat: @"%@_%@",
lang, region]];
}
[a addObject: lang];
if (NO == [a containsObject: alias])
{
[a addObject: alias];
}
}
return a;
}
static NSLock *pathCacheLock = nil;
static NSMutableDictionary *pathCache = nil;
@interface NSObject (PrivateFrameworks)
+ (NSString*) frameworkVersion;
+ (NSString**) frameworkClasses;
@end
typedef enum {
NSBUNDLE_BUNDLE = 1,
NSBUNDLE_APPLICATION,
NSBUNDLE_FRAMEWORK,
NSBUNDLE_LIBRARY
} bundle_t;
/* Class variables - We keep track of all the bundles */
static NSBundle *_mainBundle = nil;
static NSMapTable *_bundles = NULL;
static NSMapTable *_byClass = NULL;
static NSMapTable *_byIdentifier = NULL;
/* Store the working directory at startup */
static NSString *_launchDirectory = nil;
static NSString *_base_version
= OBJC_STRINGIFY(GNUSTEP_BASE_MAJOR_VERSION.GNUSTEP_BASE_MINOR_VERSION);
/*
* An empty strings file table for use when localization files can't be found.
*/
static NSDictionary *_emptyTable = nil;
/* When we are linking in an object file, GSPrivateLoadModule calls our
callback routine for every Class and Category loaded. The following
variable stores the bundle that is currently doing the loading so we know
where to store the class names.
*/
static NSBundle *_loadingBundle = nil;
static NSBundle *_gnustep_bundle = nil;
static NSRecursiveLock *load_lock = nil;
static BOOL _strip_after_loading = NO;
/* List of framework linked in the _loadingBundle */
static NSMutableArray *_loadingFrameworks = nil;
static NSString *_currentFrameworkName = nil;
static NSString *gnustep_target_dir =
#ifdef GNUSTEP_TARGET_DIR
@GNUSTEP_TARGET_DIR;
#else
nil;
#endif
static NSString *gnustep_target_cpu =
#ifdef GNUSTEP_TARGET_CPU
@GNUSTEP_TARGET_CPU;
#else
nil;
#endif
static NSString *gnustep_target_os =
#ifdef GNUSTEP_TARGET_OS
@GNUSTEP_TARGET_OS;
#else
nil;
#endif
static NSString *library_combo =
#ifdef LIBRARY_COMBO
@LIBRARY_COMBO;
#else
nil;
#endif
#ifdef __ANDROID__
static jobject _jassetManager = NULL;
static AAssetManager *_assetManager = NULL;
#endif
/*
* Try to find the absolute path of an executable.
* Search all the directoried in the PATH.
* The atLaunch flag determines whether '.' is considered to be
* the current working directory or the working directory at the
* time when the program was launched (technically the directory
* at the point when NSBundle was first used ... so programs must
* use NSBundle *before* changing their working directories).
*/
static NSString*
AbsolutePathOfExecutable(NSString *path, BOOL atLaunch)
{
if (NO == [path isAbsolutePath])
{
NSFileManager *mgr = manager();
NSDictionary *env;
NSString *pathlist;
NSString *prefix;
id patharr;
NSString *result = nil;
env = [[NSProcessInfo processInfo] environment];
pathlist = [env objectForKey: @"PATH"];
/* Windows 2000 and perhaps others have "Path" not "PATH" */
if (pathlist == nil)
{
pathlist = [env objectForKey: @"Path"];
}
#if defined(_WIN32)
patharr = [pathlist componentsSeparatedByString: @";"];
#else
patharr = [pathlist componentsSeparatedByString: @":"];
#endif
/* Add . if not already in path */
if ([patharr indexOfObject: @"."] == NSNotFound)
{
patharr = AUTORELEASE([patharr mutableCopy]);
[patharr addObject: @"."];
}
patharr = [patharr objectEnumerator];
while (nil != (prefix = [patharr nextObject]))
{
if ([prefix isEqual: @"."])
{
if (atLaunch == YES)
{
prefix = _launchDirectory;
}
else
{
prefix = [mgr currentDirectoryPath];
}
}
prefix = [prefix stringByAppendingPathComponent: path];
if ([mgr isExecutableFileAtPath: prefix])
{
result = [prefix stringByStandardizingPath];
break;
}
#if defined(_WIN32)
else
{
NSString *extension = [path pathExtension];
/* Also try adding any executable extensions on windows
*/
if ([extension length] == 0)
{
static NSSet *executable = nil;
NSString *wpath;
NSEnumerator *e;
NSString *s;
if (nil == executable)
{
executable = [[NSTask executableExtensions] copy];
}
e = [executable objectEnumerator];
while (nil != (s = [e nextObject]))
{
wpath = [prefix stringByAppendingPathExtension: s];
if ([mgr isExecutableFileAtPath: wpath])
{
result = [wpath stringByStandardizingPath];
break;
}
}
}
}
#endif
}
path = result;
}
path = [path stringByResolvingSymlinksInPath];
path = [path stringByStandardizingPath];
return path;
}
/*
* Return the path to this executable.
*/
NSString *
GSPrivateExecutablePath()
{
static NSString *executablePath = nil;
static BOOL beenHere = NO;
if (beenHere == NO)
{
[load_lock lock];
if (beenHere == NO)
{
#if defined(PROCFS_EXE_LINK)
executablePath = [manager()
pathContentOfSymbolicLinkAtPath:
[NSString stringWithUTF8String: PROCFS_EXE_LINK]];
/*
On some systems, the link is of the form "[device]:inode", which
can be used to open the executable, but is useless if you want
the path to it. Thus we check that the path is an actual absolute
path. (Using '/' here is safe; it isn't the path separator
everywhere, but it is on all systems that have PROCFS_EXE_LINK.)
*/
if ([executablePath length] > 0
&& [executablePath characterAtIndex: 0] != '/')
{
executablePath = nil;
}
#endif
if (executablePath == nil || [executablePath length] == 0)
{
executablePath
= [[[NSProcessInfo processInfo] arguments] objectAtIndex: 0];
}
if (NO == [executablePath isAbsolutePath])
{
executablePath = AbsolutePathOfExecutable(executablePath, YES);
}
else
{
executablePath = [executablePath stringByResolvingSymlinksInPath];
executablePath = [executablePath stringByStandardizingPath];
}
IF_NO_GC([executablePath retain];)
beenHere = YES;
}
[load_lock unlock];
NSCAssert(executablePath != nil, NSInternalInconsistencyException);
}
return executablePath;
}
static NSArray *
bundle_directory_readable(NSString *path)
{
id found;
[pathCacheLock lock];
found = [[[pathCache objectForKey: path] retain] autorelease];
[pathCacheLock unlock];
if (nil == found)
{
NSFileManager *mgr = manager();
found = [mgr directoryContentsAtPath: path];
if (nil == found)
{
found = [NSNull null];
}
[pathCacheLock lock];
[pathCache setObject: found forKey: path];
[pathCacheLock unlock];
}
if ((id)[NSNull null] == found)
{
found = nil;
}
return (NSArray*)found;
}
/* Get the object file that should be located in the bundle of the same name */
static NSString *
bundle_object_name(NSString *path, NSString* executable)
{
NSFileManager *mgr = manager();
NSString *name, *path0, *path1, *path2;
if (executable)
{
NSString *exepath;
name = [executable lastPathComponent];
exepath = [executable stringByDeletingLastPathComponent];
if ([exepath isEqualToString: @""] == NO)
{
if ([exepath isAbsolutePath] == YES)
path = exepath;
else
path = [path stringByAppendingPathComponent: exepath];
}
}
else
{
name = [[path lastPathComponent] stringByDeletingPathExtension];
path = [path stringByDeletingLastPathComponent];
}
path0 = [path stringByAppendingPathComponent: name];
path = [path stringByAppendingPathComponent: gnustep_target_dir];
path1 = [path stringByAppendingPathComponent: name];
path = [path stringByAppendingPathComponent: library_combo];
path2 = [path stringByAppendingPathComponent: name];
if ([mgr isReadableFileAtPath: path2] == YES)
return path2;
else if ([mgr isReadableFileAtPath: path1] == YES)
return path1;
else if ([mgr isReadableFileAtPath: path0] == YES)
return path0;
#if defined(_WIN32)
/* If we couldn't find the binary, and we are on windows, and the name
* has no path extension, then let's try looking for a dll.
*/
if ([name pathExtension] == nil)
{
if ([mgr isReadableFileAtPath:
[path2 stringByAppendingPathExtension: @"dll"]] == YES)
return [path2 stringByAppendingPathExtension: @"dll"];
else if ([mgr isReadableFileAtPath:
[path1 stringByAppendingPathExtension: @"dll"]] == YES)
return [path1 stringByAppendingPathExtension: @"dll"];
else if ([mgr isReadableFileAtPath:
[path0 stringByAppendingPathExtension: @"dll"]] == YES)
return [path0 stringByAppendingPathExtension: @"dll"];
}
#endif
return path0;
}
/* Construct a path from components */
static void
addBundlePath(NSMutableArray *list, NSArray *contents,
NSString *path, NSString *subdir, NSString *lang)
{
if (nil == contents)
{
return;
}
if (nil != subdir)
{
NSEnumerator *e = [[subdir pathComponents] objectEnumerator];
NSString *subdirComponent;
while ((subdirComponent = [e nextObject]) != nil)
{
if (NO == [contents containsObject: subdirComponent])
{
return;
}
path = [path stringByAppendingPathComponent: subdirComponent];
if (nil == (contents = bundle_directory_readable(path)))
{
return;
}
}
}
if (nil == lang)
{
[list addObject: path];
}
else
{
NSEnumerator *enumerator = [altLang(lang) objectEnumerator];
NSString *alt;
/* Add each language specific subdirectory in order.
*/
while (nil != (alt = [enumerator nextObject]))
{
alt = [alt stringByAppendingPathExtension: @"lproj"];
if (YES == [contents containsObject: alt])
{
alt = [path stringByAppendingPathComponent: alt];
if (nil != (contents = bundle_directory_readable(alt)))
{
[list addObject: alt];
}
}
}
}
}
/* Try to locate name framework in standard places
which are like /Library/Frameworks/(name).framework */
static inline NSString *
_find_framework(NSString *name)
{
NSArray *paths;
NSFileManager *file_mgr = manager();
NSString *file_name;
NSString *file_path;
NSString *path;
NSEnumerator *enumerator;
NSCParameterAssert(name != nil);
file_name = [name stringByAppendingPathExtension: @"framework"];
paths = NSSearchPathForDirectoriesInDomains(GSFrameworksDirectory,
NSAllDomainsMask,YES);
enumerator = [paths objectEnumerator];
while ((path = [enumerator nextObject]))
{
file_path = [path stringByAppendingPathComponent: file_name];
if ([file_mgr fileExistsAtPath: file_path] == YES)
{
return file_path; // Found it!
}
}
return nil;
}
/* Try to locate resources for tool name (which is this tool) in
* standard places like xxx/Library/Tools/Resources/name */
/* This could be converted into a public +bundleForTool:
* method. At the moment it's only used privately
* to locate the main bundle for this tool.
*/
static inline NSString *
_find_main_bundle_for_tool(NSString *toolName)
{
NSArray *paths;
NSEnumerator *enumerator;
NSString *path;
NSString *tail;
NSFileManager *fm = manager();
/*
* Eliminate any base path or extensions.
*/
toolName = [toolName lastPathComponent];
do
{
toolName = [toolName stringByDeletingPathExtension];
}
while ([[toolName pathExtension] length] > 0);
if ([toolName length] == 0)
{
return nil;
}
tail = [@"Tools" stringByAppendingPathComponent:
[@"Resources" stringByAppendingPathComponent:
toolName]];
paths = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory,
NSAllDomainsMask, YES);
enumerator = [paths objectEnumerator];
while ((path = [enumerator nextObject]))
{
BOOL isDir;
path = [path stringByAppendingPathComponent: tail];
if ([fm fileExistsAtPath: path isDirectory: &isDir] && isDir)
{
return path;
}
}
return nil;
}
@implementation NSBundle (Private)
+ (NSString *) _absolutePathOfExecutable: (NSString *)path
{
return AbsolutePathOfExecutable(path, NO);
}
/* Nicola & Mirko:
Frameworks can be used in an application in two different ways:
() the framework is dynamically/manually loaded, as if it were a
bundle. This is the easier case, because we already have the
bundle setup with the correct path (it's the programmer's
responsibility to find the framework bundle on disk); we get all
information from the bundle dictionary, such as the version; we
also create the class list when loading the bundle, as for any
other bundle.
() the framework was linked into the application. This is much
more difficult, because without using tricks, we have no way of
knowing where the framework bundle (needed eg for resources) is on
disk, and we have no way of knowing what the class list is, or the
version. So the trick we use in this case to work around those
problems is that gnustep-make generates a 'NSFramework_xxx' class
and compiles it into each framework. By asking to the class, we
can get the version information and the list of classes which were
compiled into the framework. To get the location of the framework
on disk, we try using advanced dynamic linker features to get the
shared object file on disk from which the NSFramework_xxx class was
loaded. If that doesn't work, because the dynamic linker can't
provide this information on this platform (or maybe because the
framework was statically linked into the application), we have a
fallback trick :-) We look for the framework in the standard
locations and in the main bundle. This might fail if the framework
is not in a standard location or there is more than one installed
framework of the same name (and different versions?).
So at startup, we scan all classes which were compiled into the
application. For each NSFramework_ class, we call the following
function, which records the name of the framework, the version,
the classes belonging to it, and tries to determine the path
on disk to the framework bundle.
Bundles (and frameworks if dynamically loaded as bundles) could
depend on other frameworks (linked togheter on platform that
supports this behaviour) so whenever we dynamically load a bundle,
we need to spot out any additional NSFramework_* classes which are
loaded, and call this method (exactly as for frameworks linked into
the main application) to record them, and try finding the path on
disk to those framework bundles.
*/
+ (NSBundle*) _addFrameworkFromClass: (Class)frameworkClass
{
NSBundle *bundle = nil;
NSString **fmClasses;
NSString *bundlePath = nil;
unsigned int len;
const char *frameworkClassName;
if (frameworkClass == Nil)
{
return nil;
}
frameworkClassName = class_getName(frameworkClass);
len = strlen (frameworkClassName);
if (len > 12 * sizeof(char)
&& !strncmp ("NSFramework_", frameworkClassName, 12))
{
/* The name of the framework. */
NSString *name;
/* If the bundle for this framework class is already loaded,
* simply return it. The lookup will return an NSNull object
* if the framework class has no known bundle.
*/
bundle = (id)NSMapGet(_byClass, frameworkClass);
if (nil != bundle)
{
if ((id)bundle == (id)[NSNull null])
{
bundle = nil;
}
return bundle;
}
name = [NSString stringWithUTF8String: &frameworkClassName[12]];
/* Important - gnustep-make mangles framework names to encode
* them as ObjC class names. Here we need to demangle them. We
* apply the reverse transformations in the reverse order.
*/
name = [name stringByReplacingString: @"_1" withString: @"+"];
name = [name stringByReplacingString: @"_0" withString: @"-"];
name = [name stringByReplacingString: @"__" withString: @"_"];
/* Try getting the path to the framework using the dynamic
* linker. When it works it's really cool :-) This is the only
* really universal way of getting the framework path ... we can
* locate the framework no matter where it is on disk!
*/
bundlePath = GSPrivateSymbolPath(frameworkClass);
if ([bundlePath isEqualToString: GSPrivateExecutablePath()])
{
/* Oops ... the NSFramework_xxx class is linked in the main
* executable. Maybe the framework was statically linked
* into the application ... resort to searching the
* framework bundle on the filesystem manually.
*/
bundlePath = nil;
}
if (bundlePath != nil)
{
NSString *pathComponent;
/* bundlePath should really be an absolute path; we
* recommend you use only absolute paths in LD_LIBRARY_PATH.
*
* If it isn't, we try to survive the situation; we assume
* it's relative to the launch directory. That's how the
* dynamic linker would have found it after all. This is
* fragile though, so please use absolute paths.
*/
if ([bundlePath isAbsolutePath] == NO)
{
bundlePath = [_launchDirectory
stringByAppendingPathComponent: bundlePath];
}
/* Dereference symlinks, and standardize path. This will
* only work properly if the original bundlePath is
* absolute.
*/
bundlePath = [bundlePath stringByStandardizingPath];
/* We now have the location of the shared library object
* file inside the framework directory. We need to walk up
* the directory tree up to the top of the framework. To do
* so, we need to chop off the extra subdirectories, the
* library combo and the target cpu/os if they exist. The
* framework and this library should match so we can use the
* compiled-in settings.
*/
/* library name */
bundlePath = [bundlePath stringByDeletingLastPathComponent];
/* library combo */
pathComponent = [bundlePath lastPathComponent];
if ([pathComponent isEqual: library_combo])
{
bundlePath = [bundlePath stringByDeletingLastPathComponent];
}
/* target directory */
pathComponent = [bundlePath lastPathComponent];
if ([pathComponent isEqual: gnustep_target_dir])
{
bundlePath = [bundlePath stringByDeletingLastPathComponent];
}
#if defined(_WIN32)
/* On windows, the library (dll) is in the Tools area rather than
* in the framework, so we can adjust the path here.
*/
if ([[bundlePath lastPathComponent] isEqual: @"Tools"])
{
bundlePath = [bundlePath stringByDeletingLastPathComponent];
bundlePath
= [bundlePath stringByAppendingPathComponent: @"Library"];
bundlePath
= [bundlePath stringByAppendingPathComponent: @"Frameworks"];
bundlePath = [bundlePath stringByAppendingPathComponent:
[NSString stringWithFormat: @"%@%@", name, @".framework"]];
}
#else
/* There are no Versions on MinGW. So the version check is only
* done on non-MinGW. */
/* version name */
bundlePath = [bundlePath stringByDeletingLastPathComponent];
pathComponent = [bundlePath lastPathComponent];
if ([pathComponent isEqual: @"Versions"])
{
bundlePath = [bundlePath stringByDeletingLastPathComponent];
#endif
pathComponent = [bundlePath lastPathComponent];
if ([pathComponent isEqualToString:
[NSString stringWithFormat: @"%@%@",
name, @".framework"]])
{
/* Try creating the bundle. */
if (bundlePath)
bundle = [[self alloc] initWithPath: bundlePath];
}
#if !defined(_WIN32)
}
#endif
/* Failed - buu - try the fallback trick. */
if (bundle == nil)
{
bundlePath = nil;
}
}
if (bundlePath == nil)
{
/* NICOLA: In an ideal world, the following is just a hack
* for when GSPrivateSymbolPath() fails! But in real life
* GSPrivateSymbolPath() is risky (some platforms don't
* have it at all!), so this hack might be used a lot! It
* must be quite robust. We try to look for the framework
* in the standard GNUstep installation dirs and in the main
* bundle. This should be reasonably safe if the user is
* not being too clever ... :-)
*/
bundlePath = _find_framework(name);
if (bundlePath == nil)
{
bundlePath = [[NSBundle mainBundle] pathForResource: name
ofType: @"framework"
inDirectory: @"Frameworks"];
}
/* Try creating the bundle. */
if (bundlePath != nil)
{
bundle = [[self alloc] initWithPath: bundlePath];
}
}
[load_lock lock];
if (bundle == nil)
{
NSMapInsert(_byClass, frameworkClass, [NSNull null]);
[load_lock unlock];
NSWarnMLog (@"Could not find framework %@ in any standard location",
name);
return nil;
}
else
{
bundle->_principalClass = frameworkClass;
NSMapInsert(_byClass, frameworkClass, bundle);
[load_lock unlock];
}
bundle->_bundleType = NSBUNDLE_FRAMEWORK;
bundle->_codeLoaded = YES;
/* frameworkVersion is something like 'A'. */
bundle->_frameworkVersion = RETAIN([frameworkClass frameworkVersion]);
bundle->_bundleClasses = RETAIN([NSMutableArray arrayWithCapacity: 2]);
/* A NULL terminated list of class names - the classes contained
in the framework. */
fmClasses = [frameworkClass frameworkClasses];
while (*fmClasses != NULL)
{
NSValue *value;
Class class = NSClassFromString(*fmClasses);
NSMapInsert(_byClass, class, bundle);
value = [NSValue valueWithPointer: (void*)class];
[bundle->_bundleClasses addObject: value];
fmClasses++;
}
/* If _loadingBundle is not nil, it means we reached this point
* while loading a bundle. This can happen if the framework is
* linked into the bundle (then, the dynamic linker
* automatically drags in the framework when the bundle is
* loaded). But then, the classes in the framework should be
* removed from the list of classes in the bundle. Check that
* _loadingBundle != bundle which happens on Windows machines when
* loading in Frameworks.
*/
if (_loadingBundle != nil && _loadingBundle != bundle)
{
int i, j;
id b = bundle->_bundleClasses;
id l = _loadingBundle->_bundleClasses;
/* The following essentially does:
*
* [_loadingBundle->_bundleClasses
* removeObjectsInArray: bundle->_bundleClasses];
*
* The problem with that code is isEqual: gets
* sent to the classes, which will cause them to be
* initialized (which should not happen.)
*/
for (i = 0; i < [b count]; i++)
{
for (j = 0; j < [l count]; j++)
{
if ([[l objectAtIndex: j] pointerValue]
== [[b objectAtIndex: i] pointerValue])
{
[l removeObjectAtIndex: j];
}
}
}
}
}
return bundle;
}
+ (NSMutableArray*) _addFrameworks
{
int i;
int numClasses = 0;
int newNumClasses;
Class *classes = NULL;
NSMutableArray *added = nil;
newNumClasses = objc_getClassList(NULL, 0);
while (numClasses < newNumClasses)
{
numClasses = newNumClasses;
classes = realloc(classes, sizeof(Class) * numClasses);
newNumClasses = objc_getClassList(classes, numClasses);
}
for (i = 0; i < numClasses; i++)
{
NSBundle *bundle = [self _addFrameworkFromClass: classes[i]];
if (nil != bundle)
{
if (nil == added)
{
added = [NSMutableArray arrayWithCapacity: 100];
}
[added addObject: bundle];
}
}
free(classes);
return added;
}
+ (NSString*) _gnustep_target_cpu
{
return gnustep_target_cpu;
}
+ (NSString*) _gnustep_target_dir
{
return gnustep_target_dir;
}
+ (NSString*) _gnustep_target_os
{
return gnustep_target_os;
}
+ (NSString*) _library_combo
{
return library_combo;
}
@end
/*
Mirko:
The gnu-runtime calls the +load method of each class before the
_bundle_load_callback() is called and we can't provide the list of classes
ready for this method.
*/
static void