Skip to content

Commit a301fb1

Browse files
authored
[clang][modules] Print library module manifest path. (#76451)
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. #75741 adds this feature in libc++.
1 parent 4207ad5 commit a301fb1

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

clang/include/clang/Driver/Driver.h

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

605+
/// Lookup the path to the Standard library module manifest.
606+
///
607+
/// \param C - The compilation.
608+
/// \param TC - The tool chain for additional information on
609+
/// directories to search.
610+
//
611+
// FIXME: This should be in CompilationInfo.
612+
std::string GetStdModuleManifestPath(const Compilation &C,
613+
const ToolChain &TC) const;
614+
605615
/// HandleAutocompletions - Handle --autocomplete by searching and printing
606616
/// possible flags, descriptions, and its arguments.
607617
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
@@ -5306,6 +5306,9 @@ def print_resource_dir : Flag<["-", "--"], "print-resource-dir">,
53065306
def print_search_dirs : Flag<["-", "--"], "print-search-dirs">,
53075307
HelpText<"Print the paths used for finding libraries and programs">,
53085308
Visibility<[ClangOption, CLOption]>;
5309+
def print_std_module_manifest_path : Flag<["-", "--"], "print-library-module-manifest-path">,
5310+
HelpText<"Print the path for the C++ Standard library module manifest">,
5311+
Visibility<[ClangOption, CLOption]>;
53095312
def print_targets : Flag<["-", "--"], "print-targets">,
53105313
HelpText<"Print the registered targets">,
53115314
Visibility<[ClangOption, CLOption]>;

clang/lib/Driver/Driver.cpp

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

2197+
if (C.getArgs().hasArg(options::OPT_print_std_module_manifest_path)) {
2198+
llvm::outs() << GetStdModuleManifestPath(C, C.getDefaultToolChain())
2199+
<< '\n';
2200+
return false;
2201+
}
2202+
21972203
if (C.getArgs().hasArg(options::OPT_print_runtime_dir)) {
21982204
if (std::optional<std::string> RuntimePath = TC.getRuntimePath())
21992205
llvm::outs() << *RuntimePath << '\n';
@@ -6165,6 +6171,44 @@ std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const {
61656171
return std::string(Name);
61666172
}
61676173

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

0 commit comments

Comments
 (0)