macOS: Work around CoreFoundation failing to resolve bundle resources

When a framework is loaded from a Samba share CoreFoundation will fail
to resolve its Resources directory, and hence its Info.plist, which
means we can't look up the bundle by id.

Until this has been fixed in CoreFoundation and/or the macOS Samba
implementation we work around it by manually looking for QtCore.

This fixes our particular use-case of finding QtCore so we can resolve
the relocatable prefix, but there's still a potential issue if any other
code tries to use CF for bundle lookups. We don't seem to have any of
those in Qt itself, but this should be kept in mind if we see similar
issues in the future.

Change-Id: I8fd471e44f6afe33a7459ce550f0fcec9acfefb4
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
This commit is contained in:
Tor Arne Vestbø 2020-01-21 14:13:03 +01:00
parent 92918e567a
commit d0e9e5a36e

View File

@ -535,8 +535,25 @@ static QString getRelocatablePrefix()
#if defined(QT_STATIC)
prefixPath = prefixFromAppDirHelper();
#elif defined(Q_OS_DARWIN) && QT_CONFIG(framework)
CFBundleRef qtCoreBundle = CFBundleGetBundleWithIdentifier(
CFSTR("org.qt-project.QtCore"));
auto qtCoreBundle = CFBundleGetBundleWithIdentifier(CFSTR("org.qt-project.QtCore"));
if (!qtCoreBundle) {
// When running Qt apps over Samba shares, CoreFoundation will fail to find
// the Resources directory inside the bundle, This directory is a symlink,
// and CF relies on readdir() and dtent.dt_type to detect symlinks, which
// does not work reliably for Samba shares. We work around it by manually
// looking for the QtCore bundle.
auto allBundles = CFBundleGetAllBundles();
auto bundleCount = CFArrayGetCount(allBundles);
for (int i = 0; i < bundleCount; ++i) {
auto bundle = CFBundleRef(CFArrayGetValueAtIndex(allBundles, i));
auto url = QCFType<CFURLRef>(CFBundleCopyBundleURL(bundle));
auto path = QCFType<CFStringRef>(CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle));
if (CFStringHasSuffix(path, CFSTR("/QtCore.framework"))) {
qtCoreBundle = bundle;
break;
}
}
}
Q_ASSERT(qtCoreBundle);
QCFType<CFURLRef> qtCorePath = CFBundleCopyBundleURL(qtCoreBundle);