xref: /freebsd/contrib/llvm-project/lld/MachO/Driver.h (revision ccb59683b98360afaf5b5bb641a68fea22c68d0b)
1 //===- Driver.h -------------------------------------------------*- 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 #ifndef LLD_MACHO_DRIVER_H
10 #define LLD_MACHO_DRIVER_H
11 
12 #include "lld/Common/LLVM.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/SetVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/BinaryFormat/MachO.h"
17 #include "llvm/Option/OptTable.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 
20 #include <set>
21 #include <type_traits>
22 
23 namespace lld {
24 namespace macho {
25 
26 class DylibFile;
27 class InputFile;
28 
29 class MachOOptTable : public llvm::opt::OptTable {
30 public:
31   MachOOptTable();
32   llvm::opt::InputArgList parse(ArrayRef<const char *> argv);
33   void printHelp(const char *argv0, bool showHidden) const;
34 };
35 
36 // Create enum with OPT_xxx values for each option in Options.td
37 enum {
38   OPT_INVALID = 0,
39 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
40 #include "Options.inc"
41 #undef OPTION
42 };
43 
44 void parseLCLinkerOption(InputFile *, unsigned argc, StringRef data);
45 
46 std::string createResponseFile(const llvm::opt::InputArgList &args);
47 
48 // Check for both libfoo.dylib and libfoo.tbd (in that order).
49 llvm::Optional<StringRef> resolveDylibPath(llvm::StringRef path);
50 
51 DylibFile *loadDylib(llvm::MemoryBufferRef mbref, DylibFile *umbrella = nullptr,
52                      bool isBundleLoader = false,
53                      bool explicitlyLinked = false);
54 void resetLoadedDylibs();
55 
56 // Search for all possible combinations of `{root}/{name}.{extension}`.
57 // If \p extensions are not specified, then just search for `{root}/{name}`.
58 llvm::Optional<llvm::StringRef>
59 findPathCombination(const llvm::Twine &name,
60                     const std::vector<llvm::StringRef> &roots,
61                     ArrayRef<llvm::StringRef> extensions = {""});
62 
63 // If -syslibroot is specified, absolute paths to non-object files may be
64 // rerooted.
65 llvm::StringRef rerootPath(llvm::StringRef path);
66 
67 uint32_t getModTime(llvm::StringRef path);
68 
69 void printArchiveMemberLoad(StringRef reason, const InputFile *);
70 
71 // Map simulator platforms to their underlying device platform.
72 llvm::MachO::PlatformType removeSimulator(llvm::MachO::PlatformType platform);
73 
74 // Helper class to export dependency info.
75 class DependencyTracker {
76 public:
77   explicit DependencyTracker(llvm::StringRef path);
78 
79   // Adds the given path to the set of not-found files.
80   inline void logFileNotFound(const Twine &path) {
81     if (active)
82       notFounds.insert(path.str());
83   }
84 
85   // Writes the dependencies to specified path. The content is first sorted by
86   // OpCode and then by the filename (in alphabetical order).
87   void write(llvm::StringRef version,
88              const llvm::SetVector<InputFile *> &inputs,
89              llvm::StringRef output);
90 
91 private:
92   enum DepOpCode : uint8_t {
93     // Denotes the linker version.
94     Version = 0x00,
95     // Denotes the input files.
96     Input = 0x10,
97     // Denotes the files that do not exist(?)
98     NotFound = 0x11,
99     // Denotes the output files.
100     Output = 0x40,
101   };
102 
103   const llvm::StringRef path;
104   bool active;
105 
106   // The paths need to be alphabetically ordered.
107   // We need to own the paths because some of them are temporarily
108   // constructed.
109   std::set<std::string> notFounds;
110 };
111 
112 extern std::unique_ptr<DependencyTracker> depTracker;
113 
114 } // namespace macho
115 } // namespace lld
116 
117 #endif
118