Skip to content

Commit

Permalink
[clang] Improves -print-library-module-manifest-path. (#85943)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mordante committed Mar 21, 2024
1 parent a11d9b4 commit 2152094
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
49 changes: 28 additions & 21 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6203,28 +6203,35 @@ std::string Driver::GetStdModuleManifestPath(const Compilation &C,

switch (TC.GetCXXStdlibType(C.getArgs())) {
case ToolChain::CST_Libcxx: {
std::string lib = GetFilePath("libc++.so", TC);

// Note when there are multiple flavours of libc++ the module json needs to
// look at the command-line arguments for the proper json.
// These flavours do not exist at the moment, but there are plans to
// provide a variant that is built with sanitizer instrumentation enabled.

// For example
// StringRef modules = [&] {
// const SanitizerArgs &Sanitize = TC.getSanitizerArgs(C.getArgs());
// if (Sanitize.needsAsanRt())
// return "modules-asan.json";
// return "modules.json";
// }();

SmallString<128> path(lib.begin(), lib.end());
llvm::sys::path::remove_filename(path);
llvm::sys::path::append(path, "modules.json");
if (TC.getVFS().exists(path))
return static_cast<std::string>(path);
auto evaluate = [&](const char *library) -> std::optional<std::string> {
std::string lib = GetFilePath(library, TC);

// Note when there are multiple flavours of libc++ the module json needs
// to look at the command-line arguments for the proper json. These
// flavours do not exist at the moment, but there are plans to provide a
// variant that is built with sanitizer instrumentation enabled.

// For example
// StringRef modules = [&] {
// const SanitizerArgs &Sanitize = TC.getSanitizerArgs(C.getArgs());
// if (Sanitize.needsAsanRt())
// return "libc++.modules-asan.json";
// return "libc++.modules.json";
// }();

SmallString<128> path(lib.begin(), lib.end());
llvm::sys::path::remove_filename(path);
llvm::sys::path::append(path, "libc++.modules.json");
if (TC.getVFS().exists(path))
return static_cast<std::string>(path);

return {};
};

return error;
if (std::optional<std::string> result = evaluate("libc++.so"); result)
return *result;

return evaluate("libc++.a").value_or(error);
}

case ToolChain::CST_Libstdcxx:
Expand Down
19 changes: 17 additions & 2 deletions clang/test/Driver/modules-print-library-module-manifest-path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@
// RUN: rm -rf %t && split-file %s %t && cd %t
// RUN: mkdir -p %t/Inputs/usr/lib/x86_64-linux-gnu
// RUN: touch %t/Inputs/usr/lib/x86_64-linux-gnu/libc++.so
// RUN: touch %t/Inputs/usr/lib/x86_64-linux-gnu/libc++.a

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

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

// RUN: rm %t/Inputs/usr/lib/x86_64-linux-gnu/libc++.so
// RUN: touch %t/Inputs/usr/lib/x86_64-linux-gnu/libc++.a
// RUN: %clang -print-library-module-manifest-path \
// RUN: -stdlib=libc++ \
// RUN: -resource-dir=%t/Inputs/usr/lib/x86_64-linux-gnu \
// RUN: --target=x86_64-linux-gnu 2>&1 \
// RUN: | FileCheck libcxx-no-shared-lib.cpp

// RUN: %clang -print-library-module-manifest-path \
// RUN: -stdlib=libstdc++ \
// RUN: -resource-dir=%t/Inputs/usr/lib/x86_64-linux-gnu \
Expand All @@ -29,7 +38,13 @@

//--- libcxx.cpp

// CHECK: {{.*}}/Inputs/usr/lib/x86_64-linux-gnu{{/|\\}}modules.json
// CHECK: {{.*}}/Inputs/usr/lib/x86_64-linux-gnu{{/|\\}}libc++.modules.json

//--- libcxx-no-shared-lib.cpp

// Note this might find a different path depending whether search path
// contains a different libc++.so.
// CHECK: {{.*}}libc++.modules.json

//--- libstdcxx.cpp

Expand Down

0 comments on commit 2152094

Please sign in to comment.