1 //===- DriverUtils.cpp ----------------------------------------------------===//
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 #include "DriverUtils.h"
10 #include "InputFiles.h"
11 
12 #include "lld/Common/ErrorHandler.h"
13 #include "lld/Common/Memory.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/TextAPI/MachO/TextAPIReader.h"
16 
17 using namespace llvm;
18 using namespace llvm::MachO;
19 using namespace llvm::sys;
20 using namespace lld;
21 using namespace lld::macho;
22 
23 Optional<std::string> macho::resolveDylibPath(StringRef path) {
24   // TODO: if a tbd and dylib are both present, we should check to make sure
25   // they are consistent.
26   if (fs::exists(path))
27     return std::string(path);
28 
29   SmallString<261> location = path;
30   path::replace_extension(location, ".tbd");
31   if (fs::exists(location))
32     return std::string(location);
33 
34   return {};
35 }
36 
37 Optional<DylibFile *> macho::makeDylibFromTAPI(MemoryBufferRef mbref,
38                                                DylibFile *umbrella) {
39   Expected<std::unique_ptr<InterfaceFile>> result = TextAPIReader::get(mbref);
40   if (!result) {
41     error("could not load TAPI file at " + mbref.getBufferIdentifier() + ": " +
42           toString(result.takeError()));
43     return {};
44   }
45   return make<DylibFile>(**result, umbrella);
46 }
47