File indexing completed on 2026-05-10 08:43:49
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_DWARFLINKER_UTILS_H
0010 #define LLVM_DWARFLINKER_UTILS_H
0011
0012 #include "llvm/ADT/SmallString.h"
0013 #include "llvm/ADT/Twine.h"
0014 #include "llvm/Support/Error.h"
0015 #include "llvm/Support/FileSystem.h"
0016 #include "llvm/Support/Path.h"
0017
0018 namespace llvm {
0019 namespace dwarf_linker {
0020
0021
0022
0023
0024
0025
0026 inline Error finiteLoop(function_ref<Expected<bool>()> Iteration,
0027 size_t MaxCounter = 100000) {
0028 size_t iterationsCounter = 0;
0029 while (iterationsCounter++ < MaxCounter) {
0030 Expected<bool> IterationResultOrError = Iteration();
0031 if (!IterationResultOrError)
0032 return IterationResultOrError.takeError();
0033 if (!IterationResultOrError.get())
0034 return Error::success();
0035 }
0036 return createStringError(std::errc::invalid_argument, "Infinite recursion");
0037 }
0038
0039
0040
0041 inline StringRef guessDeveloperDir(StringRef SysRoot) {
0042
0043 auto it = sys::path::rbegin(SysRoot);
0044 auto end = sys::path::rend(SysRoot);
0045 if (it == end || !it->ends_with(".sdk"))
0046 return {};
0047 ++it;
0048
0049 if (it == end || *it != "SDKs")
0050 return {};
0051 auto developerEnd = it;
0052 ++it;
0053 while (it != end) {
0054
0055 if (*it != "Developer")
0056 return {};
0057 ++it;
0058 if (it == end)
0059 return {};
0060 if (*it == "Contents")
0061 return StringRef(SysRoot.data(),
0062 developerEnd - sys::path::rend(SysRoot) - 1);
0063
0064 if (!it->ends_with(".platform"))
0065 return {};
0066 ++it;
0067
0068 if (it == end || *it != "Platforms")
0069 return {};
0070 developerEnd = it;
0071 ++it;
0072 }
0073 return {};
0074 }
0075
0076
0077 inline bool isInToolchainDir(StringRef Path) {
0078
0079 for (auto it = sys::path::rbegin(Path), end = sys::path::rend(Path);
0080 it != end; ++it) {
0081 if (it->ends_with(".xctoolchain")) {
0082 ++it;
0083 if (it == end)
0084 return false;
0085 if (*it != "Toolchains")
0086 return false;
0087 ++it;
0088 if (it == end)
0089 return false;
0090 if (*it != "Developer")
0091 return false;
0092 return true;
0093 }
0094 }
0095 return false;
0096 }
0097
0098 inline bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
0099
0100
0101
0102 return sys::path::is_absolute(Path, sys::path::Style::posix) ||
0103 sys::path::is_absolute(Path, sys::path::Style::windows);
0104 }
0105
0106 }
0107 }
0108
0109 #endif