1 //===- tools/dsymutil/CFBundle.cpp - CFBundle helper ------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/Path.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include <CoreFoundation/CoreFoundation.h>
25 /// Deleter that calls CFRelease rather than deleting the pointer.
26 template <typename T
> struct CFDeleter
{
27 void operator()(T
*P
) {
33 /// This helper owns any CoreFoundation pointer and will call CFRelease() on
34 /// any valid pointer it owns unless that pointer is explicitly released using
35 /// the release() member function.
38 std::unique_ptr
<typename
std::remove_pointer
<T
>::type
,
39 CFDeleter
<typename
std::remove_pointer
<T
>::type
>>;
41 /// RAII wrapper around CFBundleRef.
42 class CFString
: public CFReleaser
<CFStringRef
> {
44 CFString(CFStringRef CFStr
= nullptr) : CFReleaser
<CFStringRef
>(CFStr
) {}
46 const char *UTF8(std::string
&Str
) const {
47 return CFString::UTF8(get(), Str
);
50 CFIndex
GetLength() const {
51 if (CFStringRef Str
= get())
52 return CFStringGetLength(Str
);
56 static const char *UTF8(CFStringRef CFStr
, std::string
&Str
);
59 /// Static function that puts a copy of the UTF-8 contents of CFStringRef into
60 /// std::string and returns the C string pointer that is contained in the
61 /// std::string when successful, nullptr otherwise.
63 /// This allows the std::string parameter to own the extracted string, and also
64 /// allows that string to be returned as a C string pointer that can be used.
65 const char *CFString::UTF8(CFStringRef CFStr
, std::string
&Str
) {
69 const CFStringEncoding Encoding
= kCFStringEncodingUTF8
;
70 CFIndex MaxUTF8StrLength
= CFStringGetLength(CFStr
);
72 CFStringGetMaximumSizeForEncoding(MaxUTF8StrLength
, Encoding
);
73 if (MaxUTF8StrLength
> 0) {
74 Str
.resize(MaxUTF8StrLength
);
76 CFStringGetCString(CFStr
, &Str
[0], Str
.size(), Encoding
)) {
77 Str
.resize(strlen(Str
.c_str()));
85 /// RAII wrapper around CFBundleRef.
86 class CFBundle
: public CFReleaser
<CFBundleRef
> {
88 CFBundle(StringRef Path
) : CFReleaser
<CFBundleRef
>() { SetFromPath(Path
); }
90 CFBundle(CFURLRef Url
)
91 : CFReleaser
<CFBundleRef
>(Url
? ::CFBundleCreate(nullptr, Url
)
94 /// Return the bundle identifier.
95 CFStringRef
GetIdentifier() const {
96 if (CFBundleRef bundle
= get())
97 return ::CFBundleGetIdentifier(bundle
);
101 /// Return value for key.
102 CFTypeRef
GetValueForInfoDictionaryKey(CFStringRef key
) const {
103 if (CFBundleRef bundle
= get())
104 return ::CFBundleGetValueForInfoDictionaryKey(bundle
, key
);
109 /// Helper to initialize this instance with a new bundle created from the
110 /// given path. This function will recursively remove components from the
111 /// path in its search for the nearest Info.plist.
112 void SetFromPath(StringRef Path
);
115 void CFBundle::SetFromPath(StringRef Path
) {
116 // Start from an empty/invalid CFBundle.
119 if (Path
.empty() || !sys::fs::exists(Path
))
122 SmallString
<256> RealPath
;
123 sys::fs::real_path(Path
, RealPath
, /*expand_tilde*/ true);
126 // Create a CFURL from the current path and use it to create a CFBundle.
127 CFReleaser
<CFURLRef
> BundleURL(::CFURLCreateFromFileSystemRepresentation(
128 kCFAllocatorDefault
, (const UInt8
*)RealPath
.data(), RealPath
.size(),
130 reset(::CFBundleCreate(kCFAllocatorDefault
, BundleURL
.get()));
132 // If we have a valid bundle and find its identifier we are done.
133 if (get() != nullptr) {
134 if (GetIdentifier() != nullptr)
139 // Remove the last component of the path and try again until there's
140 // nothing left but the root.
141 sys::path::remove_filename(RealPath
);
142 } while (RealPath
!= sys::path::root_name(RealPath
));
146 /// On Darwin, try and find the original executable's Info.plist to extract
147 /// information about the bundle. Return default values on other platforms.
148 CFBundleInfo
getBundleInfo(StringRef ExePath
) {
149 CFBundleInfo BundleInfo
;
152 auto PrintError
= [&](CFTypeID TypeID
) {
153 CFString
TypeIDCFStr(::CFCopyTypeIDDescription(TypeID
));
154 std::string TypeIDStr
;
155 errs() << "The Info.plist key \"CFBundleShortVersionString\" is"
156 << "a " << TypeIDCFStr
.UTF8(TypeIDStr
)
157 << ", but it should be a string in: " << ExePath
<< ".\n";
160 CFBundle
Bundle(ExePath
);
161 if (CFStringRef BundleID
= Bundle
.GetIdentifier()) {
162 CFString::UTF8(BundleID
, BundleInfo
.IDStr
);
163 if (CFTypeRef TypeRef
=
164 Bundle
.GetValueForInfoDictionaryKey(CFSTR("CFBundleVersion"))) {
165 CFTypeID TypeID
= ::CFGetTypeID(TypeRef
);
166 if (TypeID
== ::CFStringGetTypeID())
167 CFString::UTF8((CFStringRef
)TypeRef
, BundleInfo
.VersionStr
);
171 if (CFTypeRef TypeRef
= Bundle
.GetValueForInfoDictionaryKey(
172 CFSTR("CFBundleShortVersionString"))) {
173 CFTypeID TypeID
= ::CFGetTypeID(TypeRef
);
174 if (TypeID
== ::CFStringGetTypeID())
175 CFString::UTF8((CFStringRef
)TypeRef
, BundleInfo
.ShortVersionStr
);
185 } // end namespace dsymutil
186 } // end namespace llvm