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 
36 // Defines simple struct for storing symbolic links.
37 struct SymLink {
38   std::string SrcPath;
39   std::string LinkContent;
40 
SymLinkSymLink41   SymLink(std::string Path, std::string Link)
42       : SrcPath(std::move(Path)), LinkContent(std::move(Link)) {}
43 
SymLinkSymLink44   SymLink(StringRef Path, StringRef Link)
45       : SrcPath(std::string(Path)), LinkContent(std::string(Link)) {}
46 };
47 
48 /// Replace extension considering frameworks.
49 ///
50 /// \param Path Location of file.
51 /// \param Extension File extension to update with.
52 void replace_extension(SmallVectorImpl<char> &Path, const Twine &Extension);
53 
54 /// Determine whether to skip over symlink due to either too many symlink levels
55 /// or is cyclic.
56 ///
57 /// \param Path Location to symlink.
58 /// \param Result Holds whether to skip over Path.
59 std::error_code shouldSkipSymLink(const Twine &Path, bool &Result);
60 
61 /// Turn absolute symlink into relative.
62 ///
63 /// \param From The symlink.
64 /// \param To What the symlink points to.
65 /// \param RelativePath Path location to update what the symlink points to.
66 std::error_code make_relative(StringRef From, StringRef To,
67                               SmallVectorImpl<char> &RelativePath);
68 
69 /// Determine if library is private by parsing file path.
70 /// It does not touch the file system.
71 ///
72 /// \param Path File path for library.
73 /// \param IsSymLink Whether path points to a symlink.
74 bool isPrivateLibrary(StringRef Path, bool IsSymLink = false);
75 
76 /// Create a regex rule from provided glob string.
77 /// \param Glob String that represents glob input.
78 /// \return The equivalent regex rule.
79 llvm::Expected<llvm::Regex> createRegexFromGlob(llvm::StringRef Glob);
80 
81 using AliasEntry = std::pair<std::string, EncodeKind>;
82 using AliasMap = std::map<AliasEntry, AliasEntry>;
83 
84 /// Parse input list and capture symbols and their alias.
85 ///
86 /// \param Buffer Data contents of file for the alias list.
87 /// \return Lookup table of alias to their base symbol.
88 Expected<AliasMap> parseAliasList(std::unique_ptr<llvm::MemoryBuffer> &Buffer);
89 
90 } // namespace llvm::MachO
91 #endif // LLVM_TEXTAPI_UTILS_H
92