Skip to content

Commit 2152094

Browse files
authored
[clang] Improves -print-library-module-manifest-path. (#85943)
This adds a libc++ to modules.json as is currently used by libc++. When libc++.so is not found the function will search for libc++.a as fallback.
1 parent a11d9b4 commit 2152094

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6203,28 +6203,35 @@ std::string Driver::GetStdModuleManifestPath(const Compilation &C,
62036203

62046204
switch (TC.GetCXXStdlibType(C.getArgs())) {
62056205
case ToolChain::CST_Libcxx: {
6206-
std::string lib = GetFilePath("libc++.so", TC);
6207-
6208-
// Note when there are multiple flavours of libc++ the module json needs to
6209-
// look at the command-line arguments for the proper json.
6210-
// These flavours do not exist at the moment, but there are plans to
6211-
// provide a variant that is built with sanitizer instrumentation enabled.
6212-
6213-
// For example
6214-
// StringRef modules = [&] {
6215-
// const SanitizerArgs &Sanitize = TC.getSanitizerArgs(C.getArgs());
6216-
// if (Sanitize.needsAsanRt())
6217-
// return "modules-asan.json";
6218-
// return "modules.json";
6219-
// }();
6220-
6221-
SmallString<128> path(lib.begin(), lib.end());
6222-
llvm::sys::path::remove_filename(path);
6223-
llvm::sys::path::append(path, "modules.json");
6224-
if (TC.getVFS().exists(path))
6225-
return static_cast<std::string>(path);
6206+
auto evaluate = [&](const char *library) -> std::optional<std::string> {
6207+
std::string lib = GetFilePath(library, TC);
6208+
6209+
// Note when there are multiple flavours of libc++ the module json needs
6210+
// to look at the command-line arguments for the proper json. These
6211+
// flavours do not exist at the moment, but there are plans to provide a
6212+
// variant that is built with sanitizer instrumentation enabled.
6213+
6214+
// For example
6215+
// StringRef modules = [&] {
6216+
// const SanitizerArgs &Sanitize = TC.getSanitizerArgs(C.getArgs());
6217+
// if (Sanitize.needsAsanRt())
6218+
// return "libc++.modules-asan.json";
6219+
// return "libc++.modules.json";
6220+
// }();
6221+
6222+
SmallString<128> path(lib.begin(), lib.end());
6223+
llvm::sys::path::remove_filename(path);
6224+
llvm::sys::path::append(path, "libc++.modules.json");
6225+
if (TC.getVFS().exists(path))
6226+
return static_cast<std::string>(path);
6227+
6228+
return {};
6229+
};
62266230

6227-
return error;
6231+
if (std::optional<std::string> result = evaluate("libc++.so"); result)
6232+
return *result;
6233+
6234+
return evaluate("libc++.a").value_or(error);
62286235
}
62296236

62306237
case ToolChain::CST_Libstdcxx:

clang/test/Driver/modules-print-library-module-manifest-path.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,29 @@
33
// RUN: rm -rf %t && split-file %s %t && cd %t
44
// RUN: mkdir -p %t/Inputs/usr/lib/x86_64-linux-gnu
55
// RUN: touch %t/Inputs/usr/lib/x86_64-linux-gnu/libc++.so
6+
// RUN: touch %t/Inputs/usr/lib/x86_64-linux-gnu/libc++.a
67

78
// RUN: %clang -print-library-module-manifest-path \
89
// RUN: -stdlib=libc++ \
910
// RUN: -resource-dir=%t/Inputs/usr/lib/x86_64-linux-gnu \
1011
// RUN: --target=x86_64-linux-gnu 2>&1 \
1112
// RUN: | FileCheck libcxx-no-module-json.cpp
1213

13-
// RUN: touch %t/Inputs/usr/lib/x86_64-linux-gnu/modules.json
14+
// RUN: touch %t/Inputs/usr/lib/x86_64-linux-gnu/libc++.modules.json
1415
// RUN: %clang -print-library-module-manifest-path \
1516
// RUN: -stdlib=libc++ \
1617
// RUN: -resource-dir=%t/Inputs/usr/lib/x86_64-linux-gnu \
1718
// RUN: --target=x86_64-linux-gnu 2>&1 \
1819
// RUN: | FileCheck libcxx.cpp
1920

21+
// RUN: rm %t/Inputs/usr/lib/x86_64-linux-gnu/libc++.so
22+
// RUN: touch %t/Inputs/usr/lib/x86_64-linux-gnu/libc++.a
23+
// RUN: %clang -print-library-module-manifest-path \
24+
// RUN: -stdlib=libc++ \
25+
// RUN: -resource-dir=%t/Inputs/usr/lib/x86_64-linux-gnu \
26+
// RUN: --target=x86_64-linux-gnu 2>&1 \
27+
// RUN: | FileCheck libcxx-no-shared-lib.cpp
28+
2029
// RUN: %clang -print-library-module-manifest-path \
2130
// RUN: -stdlib=libstdc++ \
2231
// RUN: -resource-dir=%t/Inputs/usr/lib/x86_64-linux-gnu \
@@ -29,7 +38,13 @@
2938

3039
//--- libcxx.cpp
3140

32-
// CHECK: {{.*}}/Inputs/usr/lib/x86_64-linux-gnu{{/|\\}}modules.json
41+
// CHECK: {{.*}}/Inputs/usr/lib/x86_64-linux-gnu{{/|\\}}libc++.modules.json
42+
43+
//--- libcxx-no-shared-lib.cpp
44+
45+
// Note this might find a different path depending whether search path
46+
// contains a different libc++.so.
47+
// CHECK: {{.*}}libc++.modules.json
3348

3449
//--- libstdcxx.cpp
3550

0 commit comments

Comments
 (0)