@@ -373,26 +373,19 @@ static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) {
373373static std::string GetXcodeSDK (XcodeSDK sdk) {
374374 XcodeSDK::Info info = sdk.Parse ();
375375 std::string sdk_name = XcodeSDK::GetCanonicalName (info);
376- auto find_sdk = [](std::string sdk_name) -> std::string {
377- std::string xcrun_cmd;
378- std::string developer_dir = GetEnvDeveloperDir ();
379- if (developer_dir.empty ())
380- if (FileSpec fspec = HostInfo::GetShlibDir ())
381- if (FileSystem::Instance ().Exists (fspec)) {
382- FileSpec path (
383- XcodeSDK::FindXcodeContentsDirectoryInPath (fspec.GetPath ()));
384- if (path.RemoveLastPathComponent ())
385- developer_dir = path.GetPath ();
386- }
376+
377+ auto xcrun = [](const std::string &sdk,
378+ llvm::StringRef developer_dir = " " ) -> std::string {
379+ std::string xcrun_cmd = " xcrun --show-sdk-path --sdk " + sdk;
387380 if (!developer_dir.empty ())
388- xcrun_cmd = " /usr/bin/env DEVELOPER_DIR=\" " + developer_dir + " \" " ;
389- xcrun_cmd += " xcrun --show-sdk-path --sdk " + sdk_name ;
381+ xcrun_cmd = " /usr/bin/env DEVELOPER_DIR=\" " + developer_dir. str () +
382+ " \" " + xcrun_cmd ;
390383
391384 int status = 0 ;
392385 int signo = 0 ;
393386 std::string output_str;
394387 lldb_private::Status error =
395- Host::RunShellCommand (xcrun_cmd. c_str () , FileSpec (), &status, &signo,
388+ Host::RunShellCommand (xcrun_cmd, FileSpec (), &status, &signo,
396389 &output_str, std::chrono::seconds (15 ));
397390
398391 // Check that xcrun return something useful.
@@ -414,6 +407,33 @@ FileSpec path(
414407 return output.str ();
415408 };
416409
410+ auto find_sdk = [&xcrun](const std::string &sdk_name) -> std::string {
411+ // Invoke xcrun with the developer dir specified in the environment.
412+ std::string developer_dir = GetEnvDeveloperDir ();
413+ if (!developer_dir.empty ()) {
414+ // Don't fallback if DEVELOPER_DIR was set.
415+ return xcrun (sdk_name, developer_dir);
416+ }
417+
418+ // Invoke xcrun with the shlib dir.
419+ if (FileSpec fspec = HostInfo::GetShlibDir ()) {
420+ if (FileSystem::Instance ().Exists (fspec)) {
421+ std::string contents_dir =
422+ XcodeSDK::FindXcodeContentsDirectoryInPath (fspec.GetPath ());
423+ llvm::StringRef shlib_developer_dir =
424+ llvm::sys::path::parent_path (contents_dir);
425+ if (!shlib_developer_dir.empty ()) {
426+ std::string sdk = xcrun (sdk_name, std::move (shlib_developer_dir));
427+ if (!sdk.empty ())
428+ return sdk;
429+ }
430+ }
431+ }
432+
433+ // Invoke xcrun without a developer dir as a last resort.
434+ return xcrun (sdk_name);
435+ };
436+
417437 std::string path = find_sdk (sdk_name);
418438 while (path.empty ()) {
419439 // Try an alternate spelling of the name ("macosx10.9internal").
0 commit comments