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