Skip to content

Commit cb3f204

Browse files
committed
Reland "[clang][modules] Print library module manifest path."
This implements a way for the compiler to find the modules.json associated with the C++23 Standard library modules. This is based on a discussion in SG15. At the moment no Standard library installs this manifest. llvm#75741 adds this feature in libc++. This reverts commit 82f424f. Disables the tests on non-X86 platforms as suggested.
1 parent 8cd5e67 commit cb3f204

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

clang/include/clang/Driver/Driver.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,16 @@ class Driver {
611611
// FIXME: This should be in CompilationInfo.
612612
std::string GetProgramPath(StringRef Name, const ToolChain &TC) const;
613613

614+
/// Lookup the path to the Standard library module manifest.
615+
///
616+
/// \param C - The compilation.
617+
/// \param TC - The tool chain for additional information on
618+
/// directories to search.
619+
//
620+
// FIXME: This should be in CompilationInfo.
621+
std::string GetStdModuleManifestPath(const Compilation &C,
622+
const ToolChain &TC) const;
623+
614624
/// HandleAutocompletions - Handle --autocomplete by searching and printing
615625
/// possible flags, descriptions, and its arguments.
616626
void HandleAutocompletions(StringRef PassedFlags) const;

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5371,6 +5371,9 @@ def print_resource_dir : Flag<["-", "--"], "print-resource-dir">,
53715371
def print_search_dirs : Flag<["-", "--"], "print-search-dirs">,
53725372
HelpText<"Print the paths used for finding libraries and programs">,
53735373
Visibility<[ClangOption, CLOption]>;
5374+
def print_std_module_manifest_path : Flag<["-", "--"], "print-library-module-manifest-path">,
5375+
HelpText<"Print the path for the C++ Standard library module manifest">,
5376+
Visibility<[ClangOption, CLOption]>;
53745377
def print_targets : Flag<["-", "--"], "print-targets">,
53755378
HelpText<"Print the registered targets">,
53765379
Visibility<[ClangOption, CLOption]>;

clang/lib/Driver/Driver.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,12 @@ bool Driver::HandleImmediateArgs(const Compilation &C) {
21972197
return false;
21982198
}
21992199

2200+
if (C.getArgs().hasArg(options::OPT_print_std_module_manifest_path)) {
2201+
llvm::outs() << GetStdModuleManifestPath(C, C.getDefaultToolChain())
2202+
<< '\n';
2203+
return false;
2204+
}
2205+
22002206
if (C.getArgs().hasArg(options::OPT_print_runtime_dir)) {
22012207
if (std::optional<std::string> RuntimePath = TC.getRuntimePath())
22022208
llvm::outs() << *RuntimePath << '\n';
@@ -6183,6 +6189,44 @@ std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const {
61836189
return std::string(Name);
61846190
}
61856191

6192+
std::string Driver::GetStdModuleManifestPath(const Compilation &C,
6193+
const ToolChain &TC) const {
6194+
std::string error = "<NOT PRESENT>";
6195+
6196+
switch (TC.GetCXXStdlibType(C.getArgs())) {
6197+
case ToolChain::CST_Libcxx: {
6198+
std::string lib = GetFilePath("libc++.so", TC);
6199+
6200+
// Note when there are multiple flavours of libc++ the module json needs to
6201+
// look at the command-line arguments for the proper json.
6202+
// These flavours do not exist at the moment, but there are plans to
6203+
// provide a variant that is built with sanitizer instrumentation enabled.
6204+
6205+
// For example
6206+
// StringRef modules = [&] {
6207+
// const SanitizerArgs &Sanitize = TC.getSanitizerArgs(C.getArgs());
6208+
// if (Sanitize.needsAsanRt())
6209+
// return "modules-asan.json";
6210+
// return "modules.json";
6211+
// }();
6212+
6213+
SmallString<128> path(lib.begin(), lib.end());
6214+
llvm::sys::path::remove_filename(path);
6215+
llvm::sys::path::append(path, "modules.json");
6216+
if (TC.getVFS().exists(path))
6217+
return static_cast<std::string>(path);
6218+
6219+
return error;
6220+
}
6221+
6222+
case ToolChain::CST_Libstdcxx:
6223+
// libstdc++ does not provide Standard library modules yet.
6224+
return error;
6225+
}
6226+
6227+
return error;
6228+
}
6229+
61866230
std::string Driver::GetTemporaryPath(StringRef Prefix, StringRef Suffix) const {
61876231
SmallString<128> Path;
61886232
std::error_code EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, Path);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Test that -print-library-module-manifest-path finds the correct file.
2+
3+
// REQUIRES: x86-registered-target
4+
5+
// RUN: rm -rf %t && split-file %s %t && cd %t
6+
// RUN: mkdir -p %t/Inputs/usr/lib/x86_64-linux-gnu
7+
// RUN: touch %t/Inputs/usr/lib/x86_64-linux-gnu/libc++.so
8+
9+
// RUN: %clang -print-library-module-manifest-path \
10+
// RUN: -stdlib=libc++ \
11+
// RUN: --sysroot=%t/Inputs \
12+
// RUN: --target=x86_64-linux-gnu 2>&1 \
13+
// RUN: | FileCheck libcxx-no-module-json.cpp
14+
15+
// RUN: touch %t/Inputs/usr/lib/x86_64-linux-gnu/modules.json
16+
// RUN: %clang -print-library-module-manifest-path \
17+
// RUN: -stdlib=libc++ \
18+
// RUN: --sysroot=%t/Inputs \
19+
// RUN: --target=x86_64-linux-gnu 2>&1 \
20+
// RUN: | FileCheck libcxx.cpp
21+
22+
// RUN: %clang -print-library-module-manifest-path \
23+
// RUN: -stdlib=libstdc++ \
24+
// RUN: --sysroot=%t/Inputs \
25+
// RUN: --target=x86_64-linux-gnu 2>&1 \
26+
// RUN: | FileCheck libstdcxx.cpp
27+
28+
//--- libcxx-no-module-json.cpp
29+
30+
// CHECK: <NOT PRESENT>
31+
32+
//--- libcxx.cpp
33+
34+
// CHECK: {{.*}}/Inputs/usr/lib/x86_64-linux-gnu{{/|\\}}modules.json
35+
36+
//--- libstdcxx.cpp
37+
38+
// CHECK: <NOT PRESENT>

0 commit comments

Comments
 (0)