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