@@ -366,7 +366,39 @@ namespace {
366366 }
367367
368368 #ifdef CLING_OSX_SYSROOT
369- sArguments .addArgument (" -isysroot" , CLING_OSX_SYSROOT );
369+ std::string cling_osx_sysroot = CLING_OSX_SYSROOT ;
370+ if (!llvm::sys::fs::exists (cling_osx_sysroot)) {
371+ // Fallback: try to find the correct SDK directory
372+ std::array<char , 128 > buffer;
373+ std::string cling_osx_sysroot_str;
374+ const std::string xcrun_cmd = " xcrun --show-sdk-path" ;
375+ std::unique_ptr<FILE , decltype (&pclose)> pipe (
376+ ::popen (xcrun_cmd.c_str(), "r"), pclose);
377+ if (!pipe) {
378+ cling::errs () << " The sysroot directory set at build time, "
379+ << cling_osx_sysroot
380+ << " , could not be found. The command " << xcrun_cmd
381+ << " was tried to figure out the path of the SDK on "
382+ " the local system, however, the invocation through "
383+ " popen() failed.\n " ;
384+ } else {
385+ // Read the output block by block
386+ while (fgets (buffer.data (), buffer.size (), pipe.get ()) != nullptr ) {
387+ cling_osx_sysroot_str += buffer.data ();
388+ }
389+ if (!cling_osx_sysroot_str.empty ()) {
390+ cling_osx_sysroot_str.pop_back ();
391+ cling_osx_sysroot = cling_osx_sysroot_str;
392+ } else {
393+ cling::errs () << " The sysroot directory set at build time, "
394+ << cling_osx_sysroot
395+ << " , could not be found. The command " << xcrun_cmd
396+ << " was tried to figure out the path of the SDK on "
397+ " the local system, however, no valid path was returned.\n " ;
398+ }
399+ }
400+ }
401+ sArguments .addArgument (" -isysroot" , cling_osx_sysroot);
370402 #endif
371403
372404#endif // _MSC_VER
0 commit comments