Skip to content

Commit f99509f

Browse files
committed
Address feedback:
- CFBUNDLE_* feature flag is unrolled into its component parts - Enable this feature for all distributions of CF compiled for the Swift runtime (except Windows, because Windows does not heed the FHS and has its own structure). - Fix indentation and typos - _CFBundleSupportsFHSBundles() is only useful for tests and has been moved to ForSwiftFoundationOnly.h - Symbols are now consistently prefixed with _kCF… - Use kCFAllocatorSystemDefault rather than NULL where needed
1 parent fbdfc62 commit f99509f

File tree

6 files changed

+63
-46
lines changed

6 files changed

+63
-46
lines changed

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

+2
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ CF_EXPORT _Nullable CFErrorRef CFReadStreamCopyError(CFReadStreamRef stream);
327327

328328
CF_EXPORT _Nullable CFErrorRef CFWriteStreamCopyError(CFWriteStreamRef stream);
329329

330+
CF_SWIFT_EXPORT Boolean _CFBundleSupportsFHSBundles(void);
331+
330332
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
331333
// Version 0.8
332334

CoreFoundation/PlugIn.subproj/CFBundle.c

+12-10
Original file line numberDiff line numberDiff line change
@@ -137,33 +137,35 @@ static void _CFBundleEnsureBundlesExistForImagePaths(CFArrayRef imagePaths);
137137

138138
#pragma mark -
139139

140-
#if CFBUNDLE_ALLOW_FHS_BUNDLES
140+
#if !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
141+
142+
// Functions and constants for FHS bundles:
141143
#define _CFBundleFHSDirectory_share CFSTR("share")
142144

143-
CF_INLINE Boolean _CFBundleURLIsForFHSInstalledBundle(CFURLRef bundleURL) {
145+
static Boolean _CFBundleURLIsForFHSInstalledBundle(CFURLRef bundleURL) {
144146
// Paths of this form are FHS installed bundles:
145147
// <anywhere>/share/<name>.resources
146148

147149
CFStringRef extension = CFURLCopyPathExtension(bundleURL);
148-
CFURLRef parentURL = CFURLCreateCopyDeletingLastPathComponent(NULL, bundleURL);
150+
CFURLRef parentURL = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorSystemDefault, bundleURL);
149151
CFStringRef containingDirectoryName = parentURL ? CFURLCopyLastPathComponent(parentURL) : NULL;
150152

151153
Boolean isFHSBundle =
152-
extension &&
153-
containingDirectoryName &&
154-
CFEqual(extension, _CFBundleSiblingResourceDirectoryExtension) &&
155-
CFEqual(containingDirectoryName, _CFBundleFHSDirectory_share);
154+
extension &&
155+
containingDirectoryName &&
156+
CFEqual(extension, _CFBundleSiblingResourceDirectoryExtension) &&
157+
CFEqual(containingDirectoryName, _CFBundleFHSDirectory_share);
156158

157159
if (extension) CFRelease(extension);
158160
if (parentURL) CFRelease(parentURL);
159161
if (containingDirectoryName) CFRelease(containingDirectoryName);
160162

161163
return isFHSBundle;
162164
}
163-
#endif // CFBUNDLE_ALLOW_FHS_BUNDLES
165+
#endif // !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
164166

165167
Boolean _CFBundleSupportsFHSBundles() {
166-
#if CFBUNDLE_ALLOW_FHS_BUNDLES
168+
#if !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
167169
return true;
168170
#else
169171
return false;
@@ -712,7 +714,7 @@ static CFBundleRef _CFBundleCreate(CFAllocatorRef allocator, CFURLRef bundleURL,
712714

713715
bundle->_url = newURL;
714716

715-
#if CFBUNDLE_ALLOW_FHS_BUNDLES
717+
#if !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
716718
bundle->_isFHSInstalledBundle = _CFBundleURLIsForFHSInstalledBundle(newURL);
717719
#endif
718720

CoreFoundation/PlugIn.subproj/CFBundlePriv.h

-3
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,6 @@ CFBundleRefNum _CFBundleOpenBundleResourceFork(CFBundleRef bundle); // deprecate
302302
CF_EXPORT
303303
void _CFBundleCloseBundleResourceFork(CFBundleRef bundle); // deprecated in favor of CFBundleCloseBundleResourceMap
304304

305-
CF_EXPORT
306-
Boolean _CFBundleSupportsFHSBundles(void);
307-
308305
CF_EXTERN_C_END
309306

310307
#endif /* ! __COREFOUNDATION_CFBUNDLEPRIV__ */

CoreFoundation/PlugIn.subproj/CFBundle_Executable.c

+34-23
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,40 @@
1515
#include <dlfcn.h>
1616
#endif
1717

18-
#if CFBUNDLE_ALLOW_FHS_BUNDLES
19-
#if __LP64__
20-
#define _CFBundleFHSArchDirectorySuffix "64"
21-
#else // !__LP64__
22-
#define _CFBundleFHSArchDirectorySuffix "32"
23-
#endif // __LP64__
18+
#if !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
2419

25-
CONST_STRING_DECL(_CFBundleFHSDirectory_bin, "bin");
26-
CONST_STRING_DECL(_CFBundleFHSDirectory_sbin, "sbin");
27-
CONST_STRING_DECL(_CFBundleFHSDirectory_libWithArchSuffix, "lib" _CFBundleFHSArchDirectorySuffix);
28-
CONST_STRING_DECL(_CFBundleFHSDirectory_lib, "lib");
20+
#if DEPLOYMENT_TARGET_LINUX
21+
#if __LP64__
22+
#define _CFBundleFHSArchDirectorySuffix "64"
23+
#else // !__LP64__
24+
#define _CFBundleFHSArchDirectorySuffix "32"
25+
#endif // __LP64__
26+
#endif // DEPLOYMENT_TARGET_LINUX
2927

30-
#define _CFBundleFHSExecutablesDirectorySuffix CFSTR(".executables")
28+
CONST_STRING_DECL(_kCFBundleFHSDirectory_bin, "bin");
29+
CONST_STRING_DECL(_kCFBundleFHSDirectory_sbin, "sbin");
30+
CONST_STRING_DECL(_kCFBundleFHSDirectory_lib, "lib");
31+
#if DEPLOYMENT_TARGET_LINUX
32+
CONST_STRING_DECL(_kCFBundleFHSDirectory_libWithArchSuffix, "lib" _CFBundleFHSArchDirectorySuffix);
33+
#endif
3134

35+
#define _CFBundleFHSExecutablesDirectorySuffix CFSTR(".executables")
3236
#define _CFBundleFHSDirectoryCLiteral_libexec "libexec"
3337

38+
#if DEPLOYMENT_TARGET_LINUX
39+
#define _CFBundleFHSDirectoriesInExecutableSearchOrder \
40+
_kCFBundleFHSDirectory_bin, \
41+
_kCFBundleFHSDirectory_sbin, \
42+
_kCFBundleFHSDirectory_libWithArchSuffix, \
43+
_kCFBundleFHSDirectory_lib
44+
#else
3445
#define _CFBundleFHSDirectoriesInExecutableSearchOrder \
35-
_CFBundleFHSDirectory_bin, \
36-
_CFBundleFHSDirectory_sbin, \
37-
_CFBundleFHSDirectory_libWithArchSuffix, \
38-
_CFBundleFHSDirectory_lib
46+
_kCFBundleFHSDirectory_bin, \
47+
_kCFBundleFHSDirectory_sbin, \
48+
_kCFBundleFHSDirectory_lib
49+
#endif // DEPLOYMENT_TARGET_LINUX
3950

40-
#endif // CFBUNDLE_ALLOW_FHS_BUNDLES
51+
#endif // !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
4152

4253
// This is here because on iPhoneOS with the dyld shared cache, we remove binaries from their
4354
// original locations on disk, so checking whether a binary's path exists is no longer sufficient.
@@ -62,7 +73,7 @@ static CFURLRef _CFBundleCopyExecutableURLRaw(CFURLRef urlPath, CFStringRef exeN
6273
CFURLRef executableURL = NULL;
6374
if (!urlPath || !exeName) return NULL;
6475

65-
#if CFBUNDLE_ALLOW_FHS_BUNDLES
76+
#if !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
6677
if (!executableURL) {
6778
executableURL = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, exeName, kCFURLPOSIXPathStyle, false, urlPath);
6879
if (!_binaryLoadable(executableURL)) {
@@ -192,7 +203,7 @@ static CFURLRef _CFBundleCopyExecutableURLInDirectory2(CFBundleRef bundle, CFURL
192203
Boolean doExecSearch = true;
193204
#endif
194205

195-
#if CFBUNDLE_ALLOW_FHS_BUNDLES
206+
#if !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
196207
if (lookupMainExe && bundle && bundle->_isFHSInstalledBundle) {
197208
// For a FHS installed bundle, the URL points to share/Bundle.resources, and the binary is in:
198209

@@ -214,13 +225,13 @@ static CFURLRef _CFBundleCopyExecutableURLInDirectory2(CFBundleRef bundle, CFURL
214225

215226
CFRelease(prefix);
216227
}
217-
#endif // CFBUNDLE_ALLOW_FHS_BUNDLES
228+
#endif // !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
218229

219230
// Now, look for the executable inside the bundle.
220231
if (!foundIt && doExecSearch && 0 != version) {
221232
CFURLRef exeDirURL = NULL;
222233

223-
#if CFBUNDLE_ALLOW_FHS_BUNDLES
234+
#if !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
224235
if (bundle && bundle->_isFHSInstalledBundle) {
225236
CFURLRef withoutExtension = CFURLCreateCopyDeletingPathExtension(kCFAllocatorSystemDefault, url);
226237
CFStringRef lastPathComponent = CFURLCopyLastPathComponent(withoutExtension);
@@ -235,14 +246,14 @@ static CFURLRef _CFBundleCopyExecutableURLInDirectory2(CFBundleRef bundle, CFURL
235246
CFRelease(libexec);
236247
CFRelease(exeDirName);
237248
} else
238-
#endif // CFBUNDLE_ALLOW_FHS_BUNDLES
249+
#endif // !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
239250
if (1 == version) {
240251
exeDirURL = CFURLCreateWithString(kCFAllocatorSystemDefault, _CFBundleExecutablesURLFromBase1, url);
241252
} else if (2 == version) {
242253
exeDirURL = CFURLCreateWithString(kCFAllocatorSystemDefault, _CFBundleExecutablesURLFromBase2, url);
243254
} else {
244-
#if DEPLOYMENT_TARGET_WINDOWS || CFBUNDLE_ALLOW_FHS_BUNDLES
245-
// On Windows, Linyx if the bundle URL is foo.resources, then the executable is at the same level as the .resources directory
255+
#if DEPLOYMENT_TARGET_WINDOWS || !DEPLOYMENT_RUNTIME_OBJC
256+
// On Windows and on targets that support FHS bundles, if the bundle URL is foo.resources, then the executable is at the same level as the .resources directory
246257
CFStringRef extension = CFURLCopyPathExtension(url);
247258
if (extension && CFEqual(extension, _CFBundleSiblingResourceDirectoryExtension)) {
248259
exeDirURL = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorSystemDefault, url);

CoreFoundation/PlugIn.subproj/CFBundle_Internal.h

+8-9
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,20 @@ CF_EXTERN_C_BEGIN
3030
#define PLATFORM_PATH_STYLE kCFURLPOSIXPathStyle
3131
#endif
3232

33-
// For development use only:
34-
#define _CFBUNDLE_ALLOW_FHS_BUNDLES_ON_ALL_TARGETS 0
33+
// FHS bundles are supported on the Swift and C runtimes, except on Windows.
34+
#if !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
3535

36-
#define CFBUNDLE_ALLOW_FHS_BUNDLES (_CFBUNDLE_ALLOW_FHS_BUNDLES_ON_ALL_TARGETS || !(DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_WINDOWS))
37-
38-
#if CFBUNDLE_ALLOW_FHS_BUNDLES
39-
40-
#if DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD || _CFBUNDLE_ALLOW_FHS_BUNDLES_ON_ALL_TARGETS
36+
#if DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
4137
#define _CFBundleFHSSharedLibraryFilenamePrefix CFSTR("lib")
4238
#define _CFBundleFHSSharedLibraryFilenameSuffix CFSTR(".so")
39+
#elif DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
40+
#define _CFBundleFHSSharedLibraryFilenamePrefix CFSTR("lib")
41+
#define _CFBundleFHSSharedLibraryFilenameSuffix CFSTR(".dylib")
4342
#else // a non-covered DEPLOYMENT_TARGET…
4443
#error Disable FHS bundles or specify shared library prefixes and suffixes for this platform.
4544
#endif // DEPLOYMENT_TARGET_…
4645

47-
#endif // CFBUNDLE_ALLOW_FHS_BUNDLES
46+
#endif // !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
4847

4948
#define CFBundleExecutableNotFoundError 4
5049
#define CFBundleExecutableNotLoadableError 3584
@@ -78,7 +77,7 @@ struct __CFBundle {
7877

7978
CFURLRef _url;
8079

81-
#if CFBUNDLE_ALLOW_FHS_BUNDLES
80+
#if !DEPLOYMENT_RUNTIME_OBJC && !DEPLOYMENT_TARGET_WINDOWS
8281
Boolean _isFHSInstalledBundle;
8382
#endif
8483

TestFoundation/TestBundle.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,13 @@ class BundlePlayground {
137137
try FileManager.default.createDirectory(atPath: tempDir + "/lib", withIntermediateDirectories: false, attributes: nil)
138138

139139
// Make a main and an auxiliary executable:
140-
guard FileManager.default.createFile(atPath: tempDir + "/lib/lib" + bundleName + ".so", contents: nil) else { return false }
140+
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
141+
let pathExtension = "dylib"
142+
#else
143+
let pathExtension = "so"
144+
#endif
145+
146+
guard FileManager.default.createFile(atPath: tempDir + "/lib/lib" + bundleName + ".\(pathExtension)", contents: nil) else { return false }
141147

142148
let executables = tempDir + "/libexec/" + bundleName + ".executables"
143149
try FileManager.default.createDirectory(atPath: executables, withIntermediateDirectories: true, attributes: nil)

0 commit comments

Comments
 (0)