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