[Lldb-commits] [PATCH] D81289: Factor out GetEnvDeveloperDir() (NFC)

Adrian Prantl via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Fri Jun 5 11:45:26 PDT 2020


aprantl created this revision.
aprantl added a reviewer: JDevlieghere.

https://reviews.llvm.org/D81289

Files:
  lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm


Index: lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm
===================================================================
--- lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm
+++ lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm
@@ -297,6 +297,19 @@
   }
 }
 
+/// Return and cache $DEVELOPER_DIR if it is set and exists.
+static std::string GetEnvDeveloperDir() {
+  static std::string g_env_developer_dir;
+  static std::once_flag g_once_flag;
+  std::call_once(g_once_flag, [&]() {
+    if (const char *developer_dir_env_var = getenv("DEVELOPER_DIR")) {
+      FileSpec fspec(developer_dir_env_var);
+      if (FileSystem::Instance().Exists(fspec))
+        g_env_developer_dir = fspec.GetPath();
+    }});
+  return g_env_developer_dir;
+}
+
 FileSpec HostInfoMacOSX::GetXcodeContentsDirectory() {
   static FileSpec g_xcode_contents_path;
   static std::once_flag g_once_flag;
@@ -313,16 +326,14 @@
       }
     }
 
-    if (const char *developer_dir_env_var = getenv("DEVELOPER_DIR")) {
-      FileSpec fspec(developer_dir_env_var);
-      if (FileSystem::Instance().Exists(fspec)) {
-        // FIXME: This looks like it couldn't possibly work!
-        std::string xcode_contents_dir =
-            XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath());
-        if (!xcode_contents_dir.empty()) {
-          g_xcode_contents_path = FileSpec(xcode_contents_dir);
-          return;
-        }
+    std::string env_developer_dir = GetEnvDeveloperDir();
+    if (!env_developer_dir.empty()) {
+      // FIXME: This looks like it couldn't possibly work!
+      std::string xcode_contents_dir =
+          XcodeSDK::FindXcodeContentsDirectoryInPath(env_developer_dir);
+      if (!xcode_contents_dir.empty()) {
+        g_xcode_contents_path = FileSpec(xcode_contents_dir);
+        return;
       }
     }
 
@@ -359,8 +370,7 @@
   std::string sdk_name = XcodeSDK::GetCanonicalName(info);
   auto find_sdk = [](std::string sdk_name) -> std::string {
     std::string xcrun_cmd;
-    Environment env = Host::GetEnvironment();
-    std::string developer_dir = env.lookup("DEVELOPER_DIR");
+    std::string developer_dir = GetEnvDeveloperDir();
     if (developer_dir.empty())
       if (FileSpec fspec = HostInfo::GetShlibDir())
         if (FileSystem::Instance().Exists(fspec)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81289.268892.patch
Type: text/x-patch
Size: 2292 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20200605/51e66d8f/attachment.bin>


More information about the lldb-commits mailing list