xref: /freebsd/contrib/llvm-project/lldb/include/lldb/Utility/XcodeSDK.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===-- XcodeSDK.h ----------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_UTILITY_SDK_H
10 #define LLDB_UTILITY_SDK_H
11 
12 #include "lldb/Utility/FileSpec.h"
13 #include "lldb/lldb-forward.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/VersionTuple.h"
16 #include <tuple>
17 
18 namespace llvm {
19 class Triple;
20 }
21 
22 namespace lldb_private {
23 
24 /// An abstraction for Xcode-style SDKs that works like \ref ArchSpec.
25 class XcodeSDK {
26   std::string m_name;
27   FileSpec m_sysroot;
28 
29 public:
30   /// Different types of Xcode SDKs.
31   enum Type : int {
32     MacOSX = 0,
33     iPhoneSimulator,
34     iPhoneOS,
35     AppleTVSimulator,
36     AppleTVOS,
37     WatchSimulator,
38     watchOS,
39     XRSimulator,
40     XROS,
41     bridgeOS,
42     Linux,
43     unknown = -1
44   };
45   static constexpr int numSDKTypes = Linux + 1;
46 
47   /// A parsed SDK directory name.
48   struct Info {
49     Type type = unknown;
50     llvm::VersionTuple version;
51     bool internal = false;
52 
53     Info() = default;
54     bool operator<(const Info &other) const;
55     bool operator==(const Info &other) const;
56   };
57 
58 
59   /// Default constructor, constructs an empty string.
60   XcodeSDK() = default;
61   /// Construct an XcodeSDK object from a specification.
62   XcodeSDK(Info info);
63   /// Initialize an XcodeSDK object with an SDK name. The SDK name is the last
64   /// directory component of a path one would pass to clang's -isysroot
65   /// parameter. For example, "MacOSX.10.14.sdk".
XcodeSDK(std::string && name)66   XcodeSDK(std::string &&name) : m_name(std::move(name)) {}
XcodeSDK(std::string name,FileSpec sysroot)67   XcodeSDK(std::string name, FileSpec sysroot)
68       : m_name(std::move(name)), m_sysroot(std::move(sysroot)) {
69     assert(!m_sysroot || m_name == m_sysroot.GetFilename().GetStringRef());
70   }
GetAnyMacOS()71   static XcodeSDK GetAnyMacOS() { return XcodeSDK("MacOSX.sdk"); }
72 
73   /// The merge function follows a strict order to maintain monotonicity:
74   /// 1. SDK with the higher SDKType wins.
75   /// 2. The newer SDK wins.
76   void Merge(const XcodeSDK &other);
77 
78   XcodeSDK &operator=(const XcodeSDK &other);
79   XcodeSDK(const XcodeSDK&) = default;
80   bool operator==(const XcodeSDK &other) const;
81 
82   /// Return parsed SDK type and version number.
83   Info Parse() const;
84   bool IsAppleInternalSDK() const;
85   llvm::VersionTuple GetVersion() const;
86   Type GetType() const;
87   llvm::StringRef GetString() const;
88   const FileSpec &GetSysroot() const;
89   /// Whether this Xcode SDK supports Swift.
90   bool SupportsSwift() const;
91 
92   /// Whether LLDB feels confident importing Clang modules from this SDK.
93   static bool SDKSupportsModules(Type type, llvm::VersionTuple version);
94   static bool SDKSupportsModules(Type desired_type, const FileSpec &sdk_path);
95 
96   /// Returns true if the SDK for the specified triple supports
97   /// builtin modules in system headers.
98   ///
99   /// NOTE: should be kept in sync with sdkSupportsBuiltinModules in
100   /// Toolchains/Darwin.cpp
101   ///
102   /// FIXME: this function will be removed once LLDB's ClangExpressionParser
103   /// constructs the compiler instance through the driver/toolchain. See \ref
104   /// SetupImportStdModuleLangOpts
105   ///
106   static bool SDKSupportsBuiltinModules(const llvm::Triple &target_triple,
107                                         llvm::VersionTuple sdk_version);
108 
109   /// Return the canonical SDK name, such as "macosx" for the macOS SDK.
110   static std::string GetCanonicalName(Info info);
111   /// Return the best-matching SDK type for a specific triple.
112   static XcodeSDK::Type GetSDKTypeForTriple(const llvm::Triple &triple);
113 
114   static std::string FindXcodeContentsDirectoryInPath(llvm::StringRef path);
115 };
116 
117 } // namespace lldb_private
118 
119 #endif
120