Skip to content

Commit c6f29db

Browse files
authored
[readtapi] Setup simple stubify support (llvm#76075)
Stubify broadly takes either tbd files or binary dylibs and turns them into tbd files. In future patches, stubify will also allow additional information to be embedded into the final TBD output too. Add Util APIs to TextAPI for common operations used by readtapi for now.
1 parent 8773c9b commit c6f29db

File tree

16 files changed

+6022
-85
lines changed

16 files changed

+6022
-85
lines changed

llvm/include/llvm/TextAPI/Utils.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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/FileSystem.h"
18+
#include "llvm/Support/Path.h"
19+
20+
#if !defined(PATH_MAX)
21+
#define PATH_MAX 1024
22+
#endif
23+
24+
namespace llvm::MachO {
25+
26+
using PathSeq = std::vector<std::string>;
27+
28+
/// Replace extension considering frameworks.
29+
///
30+
/// \param Path Location of file.
31+
/// \param Extension File extension to update with.
32+
void replace_extension(SmallVectorImpl<char> &Path, const Twine &Extension);
33+
} // namespace llvm::MachO
34+
#endif // LLVM_TEXTAPI_UTILS_H

llvm/lib/TextAPI/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_llvm_component_library(LLVMTextAPI
1313
TextAPIError.cpp
1414
TextStub.cpp
1515
TextStubCommon.cpp
16+
Utils.cpp
1617

1718
ADDITIONAL_HEADER_DIRS
1819
"${LLVM_MAIN_INCLUDE_DIR}/llvm/TextAPI"

llvm/lib/TextAPI/Utils.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===- Utils.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+
// Implements utility functions for TextAPI Darwin operations.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "llvm/TextAPI/Utils.h"
14+
15+
using namespace llvm;
16+
using namespace llvm::MachO;
17+
18+
void llvm::MachO::replace_extension(SmallVectorImpl<char> &Path,
19+
const Twine &Extension) {
20+
StringRef P(Path.begin(), Path.size());
21+
auto ParentPath = sys::path::parent_path(P);
22+
auto Filename = sys::path::filename(P);
23+
24+
if (!ParentPath.ends_with(Filename.str() + ".framework")) {
25+
sys::path::replace_extension(Path, Extension);
26+
return;
27+
}
28+
// Framework dylibs do not have a file extension, in those cases the new
29+
// extension is appended. e.g. given Path: "Foo.framework/Foo" and Extension:
30+
// "tbd", the result is "Foo.framework/Foo.tbd".
31+
SmallString<8> Storage;
32+
StringRef Ext = Extension.toStringRef(Storage);
33+
34+
// Append '.' if needed.
35+
if (!Ext.empty() && Ext[0] != '.')
36+
Path.push_back('.');
37+
38+
// Append extension.
39+
Path.append(Ext.begin(), Ext.end());
40+
}

0 commit comments

Comments
 (0)