[PATCH] D129446: [clang][driver] Find Apple default SDK path

Tobias Hieta via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 10 07:35:07 PDT 2022


thieta created this revision.
thieta added reviewers: egorzhdan, t.p.northover, dexonsmith, ldionne.
Herald added a project: All.
thieta requested review of this revision.
Herald added a subscriber: MaskRay.
Herald added a project: clang.

Currently if you download clang and install it on macOS it will
not be able to compile C++ applications out of the box. The
driver can't find the SDK path without help of SDKROOT or -isysroot.

For the Xcode toolchain this is always supplied with xcrun or
the wrapper binary that sets the correct SDKROOT.

But for new users this might be very confusing and since the
path for the SDKs is stable unless the user decide to install
Xcode to an alternative path we can try a naive search for it.

Currently this patch fails a bunch of tests that seems to assume
that no SDK is found and then the macosx-min stuff is set to
something very low. This changes when you have a real SDK.

I also haven't added any new tests to test this since I am didn't
want to assume that Xcode is installed on the system running the
tests. Does anyone have a good idea for testing this?

Tagged a bunch of apple people that have touched the driver
recently - but feel free to add more in here if needed.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129446

Files:
  clang/lib/Driver/ToolChains/Darwin.cpp


Index: clang/lib/Driver/ToolChains/Darwin.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Darwin.cpp
+++ clang/lib/Driver/ToolChains/Darwin.cpp
@@ -18,7 +18,9 @@
 #include "clang/Driver/DriverDiagnostic.h"
 #include "clang/Driver/Options.h"
 #include "clang/Driver/SanitizerArgs.h"
+#include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSwitch.h"
+#include "llvm/ADT/Triple.h"
 #include "llvm/Option/ArgList.h"
 #include "llvm/ProfileData/InstrProf.h"
 #include "llvm/Support/Path.h"
@@ -2039,6 +2041,30 @@
 
 } // namespace
 
+static const std::string guessSDKPath(const llvm::Triple::OSType OSType) {
+  std::string Platform;
+
+  if (OSType == llvm::Triple::OSType::MacOSX || OSType == llvm::Triple::OSType::Darwin) {
+    // Assume macOS when nothing specific is specified.
+    Platform = "MacOSX";
+  } else if (OSType == llvm::Triple::OSType::IOS) {
+    Platform = "IPhoneOS";
+  } else if (OSType == llvm::Triple::OSType::TvOS) {
+    Platform = "AppleTVOS";
+  } else if (OSType == llvm::Triple::OSType::WatchOS) {
+    Platform = "WatchOS";
+  }
+
+  std::string GuessPath = (
+    "/Applications/Xcode.app/Contents/Developer/Platforms/"
+    + Platform
+    + ".platform/Developer/SDKs/"
+    + Platform
+    + ".sdk");
+
+  return GuessPath;
+}
+
 void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
   const OptTable &Opts = getDriver().getOpts();
 
@@ -2058,6 +2084,14 @@
         Args.append(Args.MakeSeparateArg(
             nullptr, Opts.getOption(options::OPT_isysroot), env));
       }
+    } else {
+      // If the user doesn't pass -isysroot nor SDKROOT we will try to guess
+      // the standard path of the SDK (in /Applications/Xcode.app ...)
+      const std::string GuessPath = guessSDKPath(getTriple().getOS());
+      if (getVFS().exists(GuessPath)) {
+        Args.append(Args.MakeSeparateArg(
+          nullptr, Opts.getOption(options::OPT_isysroot), GuessPath));
+      }
     }
   }
 
@@ -2251,6 +2285,7 @@
     return DriverArgs.getLastArgValue(options::OPT_isysroot);
   if (!getDriver().SysRoot.empty())
     return getDriver().SysRoot;
+
   return "/";
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129446.443507.patch
Type: text/x-patch
Size: 2170 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220710/37697692/attachment.bin>


More information about the cfe-commits mailing list