Skip to content

Commit 0e6b39c

Browse files
author
git apple-llvm automerger
committed
Merge commit 'dd508afc5f95' from swift/release/5.3 into swift/master
2 parents a60975d + dd508af commit 0e6b39c

File tree

10 files changed

+294
-87
lines changed

10 files changed

+294
-87
lines changed

Diff for: lldb/include/lldb/Core/Module.h

+5
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,11 @@ class Module : public std::enable_shared_from_this<Module>,
875875

876876
std::vector<lldb::DataBufferSP> GetASTData(lldb::LanguageType language);
877877

878+
/// Return the Xcode SDK this module was compiled against. This
879+
/// is computed by merging the SDKs from each compilation unit in
880+
/// the module.
881+
XcodeSDK GetXcodeSDK() const { return m_xcode_sdk; }
882+
878883
/// Update the ArchSpec to a more specific variant.
879884
bool MergeArchitecture(const ArchSpec &arch_spec);
880885

Diff for: lldb/include/lldb/Utility/XcodeSDK.h

+18-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class XcodeSDK {
2121

2222
public:
2323
XcodeSDK() = default;
24+
/// Initialize an XcodeSDK object with an SDK name. The SDK name is the last
25+
/// directory component of a path one would pass to clang's -isysroot
26+
/// parameter. For example, "MacOSX.10.14.sdk".
2427
XcodeSDK(std::string &&name) : m_name(std::move(name)) {}
2528
static XcodeSDK GetAnyMacOS() { return XcodeSDK("MacOSX.sdk"); }
2629

@@ -37,7 +40,6 @@ class XcodeSDK {
3740
numSDKTypes,
3841
unknown = -1
3942
};
40-
static llvm::StringRef GetNameForType(Type type);
4143

4244
/// The merge function follows a strict order to maintain monotonicity:
4345
/// 1. SDK with the higher SDKType wins.
@@ -47,15 +49,27 @@ class XcodeSDK {
4749
XcodeSDK &operator=(XcodeSDK other);
4850
bool operator==(XcodeSDK other);
4951

50-
/// Return parsed SDK number, and SDK version number.
51-
std::tuple<Type, llvm::VersionTuple> Parse() const;
52+
/// A parsed SDK directory name.
53+
struct Info {
54+
Type type = unknown;
55+
llvm::VersionTuple version;
56+
bool internal = false;
57+
58+
Info() = default;
59+
bool operator<(const Info &other) const;
60+
};
61+
62+
/// Return parsed SDK type and version number.
63+
Info Parse() const;
64+
bool IsAppleInternalSDK() const;
5265
llvm::VersionTuple GetVersion() const;
5366
Type GetType() const;
5467
llvm::StringRef GetString() const;
5568

5669
static bool SDKSupportsModules(Type type, llvm::VersionTuple version);
5770
static bool SDKSupportsModules(Type desired_type, const FileSpec &sdk_path);
58-
static llvm::StringRef GetSDKNameForType(Type type);
71+
/// Return the canonical SDK name, such as "macosx" for the macOS SDK.
72+
static std::string GetCanonicalName(Info info);
5973
};
6074

6175
} // namespace lldb_private

Diff for: lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm

+58-29
Original file line numberDiff line numberDiff line change
@@ -298,37 +298,66 @@ static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) {
298298
}
299299

300300
std::string HostInfoMacOSX::GetXcodeSDK(XcodeSDK sdk) {
301-
std::string xcrun_cmd = "xcrun --show-sdk-path --sdk " +
302-
XcodeSDK::GetSDKNameForType(sdk.GetType()).str();
303-
llvm::VersionTuple version = sdk.GetVersion();
304-
if (!version.empty())
305-
xcrun_cmd += version.getAsString();
306-
307-
int status = 0;
308-
int signo = 0;
309-
std::string output_str;
310-
lldb_private::Status error =
311-
Host::RunShellCommand(xcrun_cmd.c_str(), FileSpec(), &status, &signo,
312-
&output_str, std::chrono::seconds(15));
313-
314-
// Check that xcrun return something useful.
315-
if (status != 0 || output_str.empty())
316-
return {};
317-
318-
// Convert to a StringRef so we can manipulate the string without modifying
319-
// the underlying data.
320-
llvm::StringRef output(output_str);
321-
322-
// Remove any trailing newline characters.
323-
output = output.rtrim();
301+
XcodeSDK::Info info = sdk.Parse();
302+
std::string sdk_name = XcodeSDK::GetCanonicalName(info);
303+
auto find_sdk = [](std::string sdk_name) -> std::string {
304+
std::string xcrun_cmd = "xcrun --show-sdk-path --sdk " + sdk_name;
305+
int status = 0;
306+
int signo = 0;
307+
std::string output_str;
308+
lldb_private::Status error =
309+
Host::RunShellCommand(xcrun_cmd.c_str(), FileSpec(), &status, &signo,
310+
&output_str, std::chrono::seconds(15));
311+
312+
// Check that xcrun return something useful.
313+
if (status != 0 || output_str.empty())
314+
return {};
315+
316+
// Convert to a StringRef so we can manipulate the string without modifying
317+
// the underlying data.
318+
llvm::StringRef output(output_str);
319+
320+
// Remove any trailing newline characters.
321+
output = output.rtrim();
322+
323+
// Strip any leading newline characters and everything before them.
324+
const size_t last_newline = output.rfind('\n');
325+
if (last_newline != llvm::StringRef::npos)
326+
output = output.substr(last_newline + 1);
327+
328+
return output.str();
329+
};
330+
331+
std::string path = find_sdk(sdk_name);
332+
while (path.empty()) {
333+
// Try an alternate spelling of the name ("macosx10.9internal").
334+
if (info.type == XcodeSDK::Type::MacOSX && !info.version.empty() &&
335+
info.internal) {
336+
llvm::StringRef fixed(sdk_name);
337+
if (fixed.consume_back(".internal"))
338+
sdk_name = fixed.str() + "internal";
339+
path = find_sdk(sdk_name);
340+
if (!path.empty())
341+
break;
342+
}
343+
Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
344+
LLDB_LOGF(log, "Couldn't find SDK %s on host", sdk_name.c_str());
345+
346+
// Try without the version.
347+
if (!info.version.empty()) {
348+
info.version = {};
349+
sdk_name = XcodeSDK::GetCanonicalName(info);
350+
path = find_sdk(sdk_name);
351+
if (!path.empty())
352+
break;
353+
}
324354

325-
// Strip any leading newline characters and everything before them.
326-
const size_t last_newline = output.rfind('\n');
327-
if (last_newline != llvm::StringRef::npos)
328-
output = output.substr(last_newline + 1);
355+
LLDB_LOGF(log, "Couldn't find any matching SDK on host");
356+
return {};
357+
}
329358

330359
// Whatever is left in output should be a valid path.
331-
if (!FileSystem::Instance().Exists(output))
360+
if (!FileSystem::Instance().Exists(path))
332361
return {};
333-
return output.str();
362+
return path;
334363
}

Diff for: lldb/source/Symbol/SwiftASTContext.cpp

+23-19
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,9 @@ static SDKTypeMinVersion GetSDKType(const llvm::Triple &target,
998998
StringRef SwiftASTContext::GetSwiftStdlibOSDir(const llvm::Triple &target,
999999
const llvm::Triple &host) {
10001000
auto sdk = GetSDKType(target, host);
1001-
llvm::StringRef sdk_name = XcodeSDK::GetSDKNameForType(sdk.sdk_type);
1001+
XcodeSDK::Info sdk_info;
1002+
sdk_info.type = sdk.sdk_type;
1003+
llvm::StringRef sdk_name = XcodeSDK::GetCanonicalName(sdk_info);
10021004
if (!sdk_name.empty())
10031005
return sdk_name;
10041006
return target.getOSName();
@@ -1668,17 +1670,21 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
16681670
set_triple = true;
16691671
}
16701672

1673+
// SDK path setup.
16711674
llvm::StringRef serialized_sdk_path =
16721675
swift_ast_sp->GetCompilerInvocation().getSDKPath();
1673-
if (serialized_sdk_path.empty()) {
1676+
if (serialized_sdk_path.empty())
16741677
LOG_PRINTF(LIBLLDB_LOG_TYPES, "No serialized SDK path.");
1675-
} else {
1676-
LOG_PRINTF(LIBLLDB_LOG_TYPES, "Got serialized SDK path %s.",
1678+
else
1679+
LOG_PRINTF(LIBLLDB_LOG_TYPES, "Serialized SDK path is %s.",
16771680
serialized_sdk_path.str().c_str());
1678-
FileSpec sdk_spec(serialized_sdk_path.str().c_str());
1679-
if (FileSystem::Instance().Exists(sdk_spec)) {
1680-
swift_ast_sp->SetPlatformSDKPath(serialized_sdk_path);
1681-
}
1681+
XcodeSDK sdk = module.GetXcodeSDK();
1682+
PlatformSP platform =
1683+
Platform::GetPlatformForArchitecture(module.GetArchitecture(), nullptr);
1684+
std::string sdk_path = platform->GetSDKPath(sdk);
1685+
LOG_PRINTF(LIBLLDB_LOG_TYPES, "Host SDK path is %s.", sdk_path.c_str());
1686+
if (FileSystem::Instance().Exists(sdk_path)) {
1687+
swift_ast_sp->SetPlatformSDKPath(sdk_path);
16821688
}
16831689
}
16841690

@@ -2668,14 +2674,6 @@ void SwiftASTContext::InitializeSearchPathOptions(
26682674
// someone is passing this to us on the command line (e.g., for the
26692675
// REPL), they probably know what they're doing.
26702676

2671-
set_sdk = true;
2672-
}
2673-
} else if (!m_platform_sdk_path.empty()) {
2674-
FileSpec platform_sdk(m_platform_sdk_path.c_str());
2675-
2676-
if (FileSystem::Instance().Exists(platform_sdk) &&
2677-
SDKSupportsSwift(platform_sdk, XcodeSDK::Type::unknown)) {
2678-
invocation.setSDKPath(m_platform_sdk_path.c_str());
26792677
set_sdk = true;
26802678
}
26812679
}
@@ -2694,13 +2692,19 @@ void SwiftASTContext::InitializeSearchPathOptions(
26942692
auto sdk = GetSDKType(triple, HostInfo::GetArchitecture().GetTriple());
26952693
// Explicitly leave the SDKPath blank on other platforms.
26962694
if (sdk.sdk_type != XcodeSDK::Type::unknown) {
2697-
auto dir = GetSDKDirectory(sdk.sdk_type, sdk.min_version_major,
2698-
sdk.min_version_minor);
2695+
std::string sdk_path = m_platform_sdk_path;
2696+
if (sdk_path.empty() || !FileSystem::Instance().Exists(sdk_path) ||
2697+
!SDKSupportsSwift(FileSpec(sdk_path), sdk.sdk_type)) {
2698+
sdk_path = GetSDKDirectory(sdk.sdk_type, sdk.min_version_major,
2699+
sdk.min_version_minor)
2700+
.GetStringRef()
2701+
.str();
2702+
}
26992703
// Note that calling setSDKPath() also recomputes all paths that
27002704
// depend on the SDK path including the
27012705
// RuntimeLibraryImportPaths, which are *only* initialized
27022706
// through this mechanism.
2703-
invocation.setSDKPath(dir.AsCString(""));
2707+
invocation.setSDKPath(sdk_path);
27042708
}
27052709

27062710
std::vector<std::string> &lpaths =

Diff for: lldb/source/Utility/XcodeSDK.cpp

+61-20
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,24 @@ static llvm::VersionTuple ParseSDKVersion(llvm::StringRef &name) {
6464
return version;
6565
}
6666

67+
static bool ParseAppleInternalSDK(llvm::StringRef &name) {
68+
return name.consume_front("Internal.");
69+
}
70+
71+
XcodeSDK::Info XcodeSDK::Parse() const {
72+
XcodeSDK::Info info;
73+
llvm::StringRef input(m_name);
74+
info.type = ParseSDKName(input);
75+
info.version = ParseSDKVersion(input);
76+
info.internal = ParseAppleInternalSDK(input);
77+
return info;
78+
}
6779

68-
std::tuple<XcodeSDK::Type, llvm::VersionTuple> XcodeSDK::Parse() const {
80+
bool XcodeSDK::IsAppleInternalSDK() const {
6981
llvm::StringRef input(m_name);
70-
XcodeSDK::Type sdk = ParseSDKName(input);
71-
llvm::VersionTuple version = ParseSDKVersion(input);
72-
return std::make_tuple<XcodeSDK::Type, llvm::VersionTuple>(
73-
std::move(sdk), std::move(version));
82+
ParseSDKName(input);
83+
ParseSDKVersion(input);
84+
return ParseAppleInternalSDK(input);
7485
}
7586

7687
llvm::VersionTuple XcodeSDK::GetVersion() const {
@@ -86,37 +97,64 @@ XcodeSDK::Type XcodeSDK::GetType() const {
8697

8798
llvm::StringRef XcodeSDK::GetString() const { return m_name; }
8899

100+
bool XcodeSDK::Info::operator<(const Info &other) const {
101+
return std::tie(type, version, internal) <
102+
std::tie(other.type, other.version, other.internal);
103+
}
89104
void XcodeSDK::Merge(XcodeSDK other) {
90105
// The "bigger" SDK always wins.
91-
if (Parse() < other.Parse())
106+
auto l = Parse();
107+
auto r = other.Parse();
108+
if (l < r)
92109
*this = other;
110+
else {
111+
// The Internal flag always wins.
112+
if (llvm::StringRef(m_name).endswith(".sdk"))
113+
if (!l.internal && r.internal)
114+
m_name =
115+
m_name.substr(0, m_name.size() - 3) + std::string("Internal.sdk");
116+
}
93117
}
94118

95-
llvm::StringRef XcodeSDK::GetSDKNameForType(XcodeSDK::Type type) {
96-
switch (type) {
119+
std::string XcodeSDK::GetCanonicalName(XcodeSDK::Info info) {
120+
std::string name;
121+
switch (info.type) {
97122
case MacOSX:
98-
return "macosx";
123+
name = "macosx";
124+
break;
99125
case iPhoneSimulator:
100-
return "iphonesimulator";
126+
name = "iphonesimulator";
127+
break;
101128
case iPhoneOS:
102-
return "iphoneos";
129+
name = "iphoneos";
130+
break;
103131
case AppleTVSimulator:
104-
return "appletvsimulator";
132+
name = "appletvsimulator";
133+
break;
105134
case AppleTVOS:
106-
return "appletvos";
135+
name = "appletvos";
136+
break;
107137
case WatchSimulator:
108-
return "watchsimulator";
138+
name = "watchsimulator";
139+
break;
109140
case watchOS:
110-
return "watchos";
141+
name = "watchos";
142+
break;
111143
case bridgeOS:
112-
return "bridgeos";
144+
name = "bridgeos";
145+
break;
113146
case Linux:
114-
return "linux";
147+
name = "linux";
148+
break;
115149
case numSDKTypes:
116150
case unknown:
117-
return "";
151+
return {};
118152
}
119-
llvm_unreachable("unhandled switch case");
153+
if (!info.version.empty())
154+
name += info.version.getAsString();
155+
if (info.internal)
156+
name += ".internal";
157+
return name;
120158
}
121159

122160
bool XcodeSDK::SDKSupportsModules(XcodeSDK::Type sdk_type,
@@ -147,12 +185,15 @@ bool XcodeSDK::SDKSupportsModules(XcodeSDK::Type desired_type,
147185
const llvm::StringRef sdk_name = last_path_component.GetStringRef();
148186

149187
const std::string sdk_name_lower = sdk_name.lower();
150-
const llvm::StringRef sdk_string = GetSDKNameForType(desired_type);
188+
Info info;
189+
info.type = desired_type;
190+
const llvm::StringRef sdk_string = GetCanonicalName(info);
151191
if (!llvm::StringRef(sdk_name_lower).startswith(sdk_string))
152192
return false;
153193

154194
auto version_part = sdk_name.drop_front(sdk_string.size());
155195
version_part.consume_back(".sdk");
196+
version_part.consume_back(".Internal");
156197

157198
llvm::VersionTuple version;
158199
if (version.tryParse(version_part))

Diff for: lldb/test/API/lang/swift/xcode_sdk/Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SWIFT_SOURCES := main.swift
2+
SWIFTFLAGS_EXTRAS := -Xfrontend -no-serialize-debugging-options
3+
4+
include Makefile.rules

0 commit comments

Comments
 (0)