xref: /freebsd/contrib/llvm-project/llvm/include/llvm/DWARFLinker/Utils.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===- Utils.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 LLVM_DWARFLINKER_UTILS_H
10 #define LLVM_DWARFLINKER_UTILS_H
11 
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/Support/Error.h"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Support/Path.h"
17 
18 namespace llvm {
19 namespace dwarf_linker {
20 
21 /// This function calls \p Iteration() until it returns false.
22 /// If number of iterations exceeds \p MaxCounter then an Error is returned.
23 /// This function should be used for loops which assumed to have number of
24 /// iterations significantly smaller than \p MaxCounter to avoid infinite
25 /// looping in error cases.
26 inline Error finiteLoop(function_ref<Expected<bool>()> Iteration,
27                         size_t MaxCounter = 100000) {
28   size_t iterationsCounter = 0;
29   while (iterationsCounter++ < MaxCounter) {
30     Expected<bool> IterationResultOrError = Iteration();
31     if (!IterationResultOrError)
32       return IterationResultOrError.takeError();
33     if (!IterationResultOrError.get())
34       return Error::success();
35   }
36   return createStringError(std::errc::invalid_argument, "Infinite recursion");
37 }
38 
39 /// Make a best effort to guess the
40 /// Xcode.app/Contents/Developer path from an SDK path.
guessDeveloperDir(StringRef SysRoot)41 inline StringRef guessDeveloperDir(StringRef SysRoot) {
42   // Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
43   auto it = sys::path::rbegin(SysRoot);
44   auto end = sys::path::rend(SysRoot);
45   if (it == end || !it->ends_with(".sdk"))
46     return {};
47   ++it;
48   // Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs
49   if (it == end || *it != "SDKs")
50     return {};
51   auto developerEnd = it;
52   ++it;
53   while (it != end) {
54     // Contents/Developer/Platforms/MacOSX.platform/Developer
55     if (*it != "Developer")
56       return {};
57     ++it;
58     if (it == end)
59       return {};
60     if (*it == "Contents")
61       return StringRef(SysRoot.data(),
62                        developerEnd - sys::path::rend(SysRoot) - 1);
63     // Contents/Developer/Platforms/MacOSX.platform
64     if (!it->ends_with(".platform"))
65       return {};
66     ++it;
67     // Contents/Developer/Platforms
68     if (it == end || *it != "Platforms")
69       return {};
70     developerEnd = it;
71     ++it;
72   }
73   return {};
74 }
75 
76 /// Make a best effort to determine whether Path is inside a toolchain.
isInToolchainDir(StringRef Path)77 inline bool isInToolchainDir(StringRef Path) {
78   // Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-05-15-a.xctoolchain/usr/lib/swift/macosx/_StringProcessing.swiftmodule/arm64-apple-macos.private.swiftinterface
79   for (auto it = sys::path::rbegin(Path), end = sys::path::rend(Path);
80        it != end; ++it) {
81     if (it->ends_with(".xctoolchain")) {
82       ++it;
83       if (it == end)
84         return false;
85       if (*it != "Toolchains")
86         return false;
87       ++it;
88       if (it == end)
89         return false;
90       if (*it != "Developer")
91         return false;
92       return true;
93     }
94   }
95   return false;
96 }
97 
isPathAbsoluteOnWindowsOrPosix(const Twine & Path)98 inline bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
99   // Debug info can contain paths from any OS, not necessarily
100   // an OS we're currently running on. Moreover different compilation units can
101   // be compiled on different operating systems and linked together later.
102   return sys::path::is_absolute(Path, sys::path::Style::posix) ||
103          sys::path::is_absolute(Path, sys::path::Style::windows);
104 }
105 
106 } // end of namespace dwarf_linker
107 } // end of namespace llvm
108 
109 #endif // LLVM_DWARFLINKER_UTILS_H
110