xref: /freebsd-src/contrib/llvm-project/llvm/include/llvm/DWARFLinker/Utils.h (revision 297eecfb02bb25902531dbb5c3b9a88caf8adf29)
1*297eecfbSDimitry Andric //===- Utils.h --------------------------------------------------*- C++ -*-===//
2*297eecfbSDimitry Andric //
3*297eecfbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*297eecfbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*297eecfbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*297eecfbSDimitry Andric //
7*297eecfbSDimitry Andric //===----------------------------------------------------------------------===//
8*297eecfbSDimitry Andric 
9*297eecfbSDimitry Andric #ifndef LLVM_DWARFLINKER_UTILS_H
10*297eecfbSDimitry Andric #define LLVM_DWARFLINKER_UTILS_H
11*297eecfbSDimitry Andric 
12*297eecfbSDimitry Andric #include "llvm/ADT/SmallString.h"
13*297eecfbSDimitry Andric #include "llvm/ADT/Twine.h"
14*297eecfbSDimitry Andric #include "llvm/Support/Error.h"
15*297eecfbSDimitry Andric #include "llvm/Support/FileSystem.h"
16*297eecfbSDimitry Andric #include "llvm/Support/Path.h"
17*297eecfbSDimitry Andric 
18*297eecfbSDimitry Andric namespace llvm {
19*297eecfbSDimitry Andric namespace dwarf_linker {
20*297eecfbSDimitry Andric 
21*297eecfbSDimitry Andric /// This function calls \p Iteration() until it returns false.
22*297eecfbSDimitry Andric /// If number of iterations exceeds \p MaxCounter then an Error is returned.
23*297eecfbSDimitry Andric /// This function should be used for loops which assumed to have number of
24*297eecfbSDimitry Andric /// iterations significantly smaller than \p MaxCounter to avoid infinite
25*297eecfbSDimitry Andric /// looping in error cases.
26*297eecfbSDimitry Andric inline Error finiteLoop(function_ref<Expected<bool>()> Iteration,
27*297eecfbSDimitry Andric                         size_t MaxCounter = 100000) {
28*297eecfbSDimitry Andric   size_t iterationsCounter = 0;
29*297eecfbSDimitry Andric   while (iterationsCounter++ < MaxCounter) {
30*297eecfbSDimitry Andric     Expected<bool> IterationResultOrError = Iteration();
31*297eecfbSDimitry Andric     if (!IterationResultOrError)
32*297eecfbSDimitry Andric       return IterationResultOrError.takeError();
33*297eecfbSDimitry Andric     if (!IterationResultOrError.get())
34*297eecfbSDimitry Andric       return Error::success();
35*297eecfbSDimitry Andric   }
36*297eecfbSDimitry Andric   return createStringError(std::errc::invalid_argument, "Infinite recursion");
37*297eecfbSDimitry Andric }
38*297eecfbSDimitry Andric 
39*297eecfbSDimitry Andric /// Make a best effort to guess the
40*297eecfbSDimitry Andric /// Xcode.app/Contents/Developer/Toolchains/ path from an SDK path.
41*297eecfbSDimitry Andric inline SmallString<128> guessToolchainBaseDir(StringRef SysRoot) {
42*297eecfbSDimitry Andric   SmallString<128> Result;
43*297eecfbSDimitry Andric   // Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
44*297eecfbSDimitry Andric   StringRef Base = sys::path::parent_path(SysRoot);
45*297eecfbSDimitry Andric   if (sys::path::filename(Base) != "SDKs")
46*297eecfbSDimitry Andric     return Result;
47*297eecfbSDimitry Andric   Base = sys::path::parent_path(Base);
48*297eecfbSDimitry Andric   Result = Base;
49*297eecfbSDimitry Andric   Result += "/Toolchains";
50*297eecfbSDimitry Andric   return Result;
51*297eecfbSDimitry Andric }
52*297eecfbSDimitry Andric 
53*297eecfbSDimitry Andric inline bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
54*297eecfbSDimitry Andric   // Debug info can contain paths from any OS, not necessarily
55*297eecfbSDimitry Andric   // an OS we're currently running on. Moreover different compilation units can
56*297eecfbSDimitry Andric   // be compiled on different operating systems and linked together later.
57*297eecfbSDimitry Andric   return sys::path::is_absolute(Path, sys::path::Style::posix) ||
58*297eecfbSDimitry Andric          sys::path::is_absolute(Path, sys::path::Style::windows);
59*297eecfbSDimitry Andric }
60*297eecfbSDimitry Andric 
61*297eecfbSDimitry Andric } // end of namespace dwarf_linker
62*297eecfbSDimitry Andric } // end of namespace llvm
63*297eecfbSDimitry Andric 
64*297eecfbSDimitry Andric #endif // LLVM_DWARFLINKER_UTILS_H
65