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
|
//===- tools/dsymutil/CFBundle.cpp - CFBundle helper ------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "CFBundle.h"
#ifdef __APPLE__
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include <CoreFoundation/CoreFoundation.h>
#include <assert.h>
#include <glob.h>
#include <memory>
#endif
namespace llvm {
namespace dsymutil {
#ifdef __APPLE__
/// Deleter that calls CFRelease rather than deleting the pointer.
template <typename T> struct CFDeleter {
void operator()(T *P) {
if (P)
::CFRelease(P);
}
};
/// This helper owns any CoreFoundation pointer and will call CFRelease() on
/// any valid pointer it owns unless that pointer is explicitly released using
/// the release() member function.
template <typename T>
using CFReleaser =
std::unique_ptr<typename std::remove_pointer<T>::type,
CFDeleter<typename std::remove_pointer<T>::type>>;
/// RAII wrapper around CFBundleRef.
class CFString : public CFReleaser<CFStringRef> {
public:
CFString(CFStringRef CFStr = nullptr) : CFReleaser<CFStringRef>(CFStr) {}
const char *UTF8(std::string &Str) const {
return CFString::UTF8(get(), Str);
}
CFIndex GetLength() const {
if (CFStringRef Str = get())
return CFStringGetLength(Str);
return 0;
}
static const char *UTF8(CFStringRef CFStr, std::string &Str);
};
/// Static function that puts a copy of the UTF8 contents of CFStringRef into
/// std::string and returns the C string pointer that is contained in the
/// std::string when successful, nullptr otherwise.
///
/// This allows the std::string parameter to own the extracted string, and also
/// allows that string to be returned as a C string pointer that can be used.
const char *CFString::UTF8(CFStringRef CFStr, std::string &Str) {
if (!CFStr)
return nullptr;
const CFStringEncoding Encoding = kCFStringEncodingUTF8;
CFIndex MaxUTF8StrLength = CFStringGetLength(CFStr);
MaxUTF8StrLength =
CFStringGetMaximumSizeForEncoding(MaxUTF8StrLength, Encoding);
if (MaxUTF8StrLength > 0) {
Str.resize(MaxUTF8StrLength);
if (!Str.empty() &&
CFStringGetCString(CFStr, &Str[0], Str.size(), Encoding)) {
Str.resize(strlen(Str.c_str()));
return Str.c_str();
}
}
return nullptr;
}
/// RAII wrapper around CFBundleRef.
class CFBundle : public CFReleaser<CFBundleRef> {
public:
CFBundle(const char *Path = nullptr) : CFReleaser<CFBundleRef>() {
if (Path && Path[0])
SetFromPath(Path);
}
CFBundle(CFURLRef url)
: CFReleaser<CFBundleRef>(url ? ::CFBundleCreate(nullptr, url)
: nullptr) {}
/// Return the bundle identifier.
CFStringRef GetIdentifier() const {
if (CFBundleRef bundle = get())
return ::CFBundleGetIdentifier(bundle);
return nullptr;
}
/// Return value for key.
CFTypeRef GetValueForInfoDictionaryKey(CFStringRef key) const {
if (CFBundleRef bundle = get())
return ::CFBundleGetValueForInfoDictionaryKey(bundle, key);
return nullptr;
}
private:
/// Update this instance with a new bundle created from the given path.
bool SetFromPath(const char *Path);
};
bool CFBundle::SetFromPath(const char *InPath) {
// Release our old bundle and URL.
reset();
if (InPath && InPath[0]) {
char ResolvedPath[PATH_MAX];
const char *Path = ::realpath(InPath, ResolvedPath);
if (Path == nullptr)
Path = InPath;
CFAllocatorRef Allocator = kCFAllocatorDefault;
// Make our Bundle URL.
CFReleaser<CFURLRef> BundleURL(::CFURLCreateFromFileSystemRepresentation(
Allocator, (const UInt8 *)Path, strlen(Path), false));
if (BundleURL.get()) {
CFIndex LastLength = LONG_MAX;
while (BundleURL.get() != nullptr) {
// Check the Path range and make sure we didn't make it to just "/",
// ".", or "..".
CFRange rangeIncludingSeparators;
CFRange range = ::CFURLGetByteRangeForComponent(
BundleURL.get(), kCFURLComponentPath, &rangeIncludingSeparators);
if (range.length > LastLength)
break;
reset(::CFBundleCreate(Allocator, BundleURL.get()));
if (get() != nullptr) {
if (GetIdentifier() != nullptr)
break;
reset();
}
BundleURL.reset(::CFURLCreateCopyDeletingLastPathComponent(
Allocator, BundleURL.get()));
LastLength = range.length;
}
}
}
return get() != nullptr;
}
#endif
/// On Darwin, try and find the original executable's Info.plist information
/// using CoreFoundation calls by creating a URL for the executable and
/// chopping off the last Path component. The CFBundle can then get the
/// identifier and grab any needed information from it directly. Return default
/// CFBundleInfo on other platforms.
CFBundleInfo getBundleInfo(StringRef ExePath) {
CFBundleInfo BundleInfo;
#ifdef __APPLE__
if (ExePath.empty() || !sys::fs::exists(ExePath))
return BundleInfo;
auto PrintError = [&](CFTypeID TypeID) {
CFString TypeIDCFStr(::CFCopyTypeIDDescription(TypeID));
std::string TypeIDStr;
errs() << "The Info.plist key \"CFBundleShortVersionString\" is"
<< "a " << TypeIDCFStr.UTF8(TypeIDStr)
<< ", but it should be a string in: " << ExePath << ".\n";
};
CFBundle Bundle(ExePath.data());
if (CFStringRef BundleID = Bundle.GetIdentifier()) {
CFString::UTF8(BundleID, BundleInfo.IDStr);
if (CFTypeRef TypeRef =
Bundle.GetValueForInfoDictionaryKey(CFSTR("CFBundleVersion"))) {
CFTypeID TypeID = ::CFGetTypeID(TypeRef);
if (TypeID == ::CFStringGetTypeID())
CFString::UTF8((CFStringRef)TypeRef, BundleInfo.VersionStr);
else
PrintError(TypeID);
}
if (CFTypeRef TypeRef = Bundle.GetValueForInfoDictionaryKey(
CFSTR("CFBundleShortVersionString"))) {
CFTypeID TypeID = ::CFGetTypeID(TypeRef);
if (TypeID == ::CFStringGetTypeID())
CFString::UTF8((CFStringRef)TypeRef, BundleInfo.ShortVersionStr);
else
PrintError(TypeID);
}
}
#endif
return BundleInfo;
}
} // end namespace dsymutil
} // end namespace llvm
|