xref: /freebsd/contrib/llvm-project/llvm/include/llvm/TextAPI/Utils.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===- llvm/TextAPI/Utils.h - TAPI Utils -----------------------*- 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 // Helper functionality used for Darwin specific operations.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_TEXTAPI_UTILS_H
14 #define LLVM_TEXTAPI_UTILS_H
15 
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Support/Error.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/Regex.h"
22 #include "llvm/TextAPI/Symbol.h"
23 #include <map>
24 
25 #if !defined(PATH_MAX)
26 #define PATH_MAX 1024
27 #endif // !defined(PATH_MAX)
28 
29 #define MACCATALYST_PREFIX_PATH "/System/iOSSupport"
30 #define DRIVERKIT_PREFIX_PATH "/System/DriverKit"
31 
32 namespace llvm::MachO {
33 
34 using PathSeq = std::vector<std::string>;
35 using PathToPlatform = std::pair<std::string, std::optional<PlatformType>>;
36 using PathToPlatformSeq = std::vector<PathToPlatform>;
37 
38 // Defines simple struct for storing symbolic links.
39 struct SymLink {
40   std::string SrcPath;
41   std::string LinkContent;
42 
SymLinkSymLink43   SymLink(std::string Path, std::string Link)
44       : SrcPath(std::move(Path)), LinkContent(std::move(Link)) {}
45 
SymLinkSymLink46   SymLink(StringRef Path, StringRef Link)
47       : SrcPath(std::string(Path)), LinkContent(std::string(Link)) {}
48 };
49 
50 /// Replace extension considering frameworks.
51 ///
52 /// \param Path Location of file.
53 /// \param Extension File extension to update with.
54 void replace_extension(SmallVectorImpl<char> &Path, const Twine &Extension);
55 
56 /// Determine whether to skip over symlink due to either too many symlink levels
57 /// or is cyclic.
58 ///
59 /// \param Path Location to symlink.
60 /// \param Result Holds whether to skip over Path.
61 std::error_code shouldSkipSymLink(const Twine &Path, bool &Result);
62 
63 /// Turn absolute symlink into relative.
64 ///
65 /// \param From The symlink.
66 /// \param To What the symlink points to.
67 /// \param RelativePath Path location to update what the symlink points to.
68 std::error_code make_relative(StringRef From, StringRef To,
69                               SmallVectorImpl<char> &RelativePath);
70 
71 /// Determine if library is private by parsing file path.
72 /// It does not touch the file system.
73 ///
74 /// \param Path File path for library.
75 /// \param IsSymLink Whether path points to a symlink.
76 bool isPrivateLibrary(StringRef Path, bool IsSymLink = false);
77 
78 /// Create a regex rule from provided glob string.
79 /// \param Glob String that represents glob input.
80 /// \return The equivalent regex rule.
81 llvm::Expected<llvm::Regex> createRegexFromGlob(llvm::StringRef Glob);
82 
83 using AliasEntry = std::pair<std::string, EncodeKind>;
84 using AliasMap = std::map<AliasEntry, AliasEntry>;
85 
86 /// Parse input list and capture symbols and their alias.
87 ///
88 /// \param Buffer Data contents of file for the alias list.
89 /// \return Lookup table of alias to their base symbol.
90 Expected<AliasMap> parseAliasList(std::unique_ptr<llvm::MemoryBuffer> &Buffer);
91 
92 /// Pickup active paths for a given platform.
93 ///
94 /// \param Paths File or search paths to pick up.
95 /// \param Platform Platform to collect paths for.
96 PathSeq getPathsForPlatform(const PathToPlatformSeq &Paths,
97                             PlatformType Platform);
98 
99 } // namespace llvm::MachO
100 #endif // LLVM_TEXTAPI_UTILS_H
101