|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#import <Foundation/Foundation.h> |
| 14 | + |
| 15 | +#include <objc/runtime.h> |
| 16 | + |
| 17 | +// This method is only used on "embedded" targets. It's not necessary on |
| 18 | +// Mac or simulators. |
| 19 | +#if TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR |
| 20 | + |
| 21 | +/// CoreFoundation SPI for finding the enclosing bundle. This is only |
| 22 | +/// ever called on older OSes, so there's no worry of running into |
| 23 | +/// trouble if the implementation is changed later on. |
| 24 | +extern "C" CFURLRef _CFBundleCopyBundleURLForExecutableURL(CFURLRef url); |
| 25 | + |
| 26 | +@implementation NSBundle (SwiftAdditions) |
| 27 | + |
| 28 | +/// Given an executable path as a C string, look up the corresponding |
| 29 | +/// NSBundle instance, if any. |
| 30 | ++ (NSBundle *)_swift_bundleWithExecutablePath: (const char *)path { |
| 31 | + NSString *nspath = [[NSFileManager defaultManager] |
| 32 | + stringWithFileSystemRepresentation:path length:strlen(path)]; |
| 33 | + NSURL *executableURL = [NSURL fileURLWithPath:nspath]; |
| 34 | + NSURL *bundleURL = |
| 35 | + (NSURL *)_CFBundleCopyBundleURLForExecutableURL((CFURLRef)executableURL); |
| 36 | + if (!bundleURL) |
| 37 | + return nil; |
| 38 | + |
| 39 | + NSBundle *bundle = [NSBundle bundleWithURL: bundleURL]; |
| 40 | + [bundleURL release]; |
| 41 | + return bundle; |
| 42 | +} |
| 43 | + |
| 44 | +@end |
| 45 | + |
| 46 | +#endif |
0 commit comments