Skip to content

Commit f3f0db6

Browse files
committed
[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++.
1 parent 8cf6bcf commit f3f0db6

File tree

7 files changed

+77
-0
lines changed

7 files changed

+77
-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+
/// GetModuleManifestPath - Lookup the name of the Standard library 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 GetModuleManifestPath(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
@@ -5280,6 +5280,9 @@ def print_resource_dir : Flag<["-", "--"], "print-resource-dir">,
52805280
def print_search_dirs : Flag<["-", "--"], "print-search-dirs">,
52815281
HelpText<"Print the paths used for finding libraries and programs">,
52825282
Visibility<[ClangOption, CLOption]>;
5283+
def print_library_module_manifest_path : Flag<["-", "--"], "print-library-module-manifest-path">,
5284+
HelpText<"Print the path for the C++ Standard library module manifest">,
5285+
Visibility<[ClangOption, CLOption]>;
52835286
def print_targets : Flag<["-", "--"], "print-targets">,
52845287
HelpText<"Print the registered targets">,
52855288
Visibility<[ClangOption, CLOption]>;

clang/lib/Driver/Driver.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,6 +2164,12 @@ bool Driver::HandleImmediateArgs(const Compilation &C) {
21642164
return false;
21652165
}
21662166

2167+
if (C.getArgs().hasArg(options::OPT_print_library_module_manifest_path)) {
2168+
llvm::outs() << "module: ="
2169+
<< GetModuleManifestPath(C, C.getDefaultToolChain()) << '\n';
2170+
return false;
2171+
}
2172+
21672173
if (C.getArgs().hasArg(options::OPT_print_runtime_dir)) {
21682174
if (std::optional<std::string> RuntimePath = TC.getRuntimePath())
21692175
llvm::outs() << *RuntimePath << '\n';
@@ -6135,6 +6141,40 @@ std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const {
61356141
return std::string(Name);
61366142
}
61376143

6144+
std::string Driver::GetModuleManifestPath(const Compilation &C,
6145+
const ToolChain &TC) const {
6146+
6147+
switch (TC.GetCXXStdlibType(C.getArgs())) {
6148+
case ToolChain::CST_Libcxx: {
6149+
std::string lib = "libc++.so";
6150+
std::string path = GetFilePath(lib, TC);
6151+
6152+
// Note when there are multiple flavours of libc++ the module json needs to
6153+
// look at the command-line arguments for the proper json.
6154+
6155+
// For example
6156+
/*
6157+
const SanitizerArgs &Sanitize = TC.getSanitizerArgs(C.getArgs());
6158+
if (Sanitize.needsAsanRt())
6159+
return path.replace(path.size() - lib.size(), lib.size(),
6160+
"modules-asan.json");
6161+
*/
6162+
6163+
path = path.replace(path.size() - lib.size(), lib.size(), "modules.json");
6164+
if (TC.getVFS().exists(path))
6165+
return path;
6166+
6167+
return "";
6168+
}
6169+
6170+
case ToolChain::CST_Libstdcxx:
6171+
// libstdc++ does not provide Standard library modules yet.
6172+
return "";
6173+
}
6174+
6175+
return "";
6176+
}
6177+
61386178
std::string Driver::GetTemporaryPath(StringRef Prefix, StringRef Suffix) const {
61396179
SmallString<128> Path;
61406180
std::error_code EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, Path);

clang/test/Driver/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/libc++.so

Whitespace-only changes.

clang/test/Driver/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/modules.json

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Test that -print-library-module-manifest-path finds the correct file.
2+
3+
// RUN: %clang -print-library-module-manifest-path \
4+
// RUN: -stdlib=libc++ \
5+
// RUN: --sysroot=%S/Inputs/cxx23_modules \
6+
// RUN: --target=x86_64-linux-gnu 2>&1 \
7+
// RUN: | FileCheck --check-prefix=CHECK-LIBCXX %s
8+
// CHECK-LIBCXX: module: ={{.*}}/Inputs/cxx23_modules/usr/lib/x86_64-linux-gnu/modules.json
9+
10+
// RUN: %clang -print-library-module-manifest-path \
11+
// RUN: -stdlib=libstdc++ \
12+
// RUN: --sysroot=%S/Inputs/cxx23_modules \
13+
// RUN: --target=x86_64-linux-gnu 2>&1 \
14+
// RUN: | FileCheck --check-prefix=CHECK-LIBSTDCXX %s
15+
// CHECK-LIBSTDCXX: module: =
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Test that -print-library-module-manifest-path finds the correct file.
2+
//
3+
// Note this file is currently not available on Apple platforms
4+
5+
// RUN: %clang -print-library-module-manifest-path \
6+
// RUN: -resource-dir=%S/Inputs/resource_dir \
7+
// RUN: --target=x86_64-unknown-linux-gnu 2>&1 \
8+
// RUN: | FileCheck %s
9+
// CHECK: module: =

0 commit comments

Comments
 (0)