Skip to content

Support '-fmodule-file-home-is-cwd' for C++ modules. #135147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 36 additions & 33 deletions clang/lib/Serialization/ASTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1493,42 +1493,45 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, StringRef isysroot) {
unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
RecordData::value_type Record[] = {MODULE_NAME};
Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
}

if (WritingModule && WritingModule->Directory) {
SmallString<128> BaseDir;
if (PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) {
// Use the current working directory as the base path for all inputs.
auto CWD = FileMgr.getOptionalDirectoryRef(".");
BaseDir.assign(CWD->getName());
} else {
BaseDir.assign(WritingModule->Directory->getName());
}
cleanPathForOutput(FileMgr, BaseDir);

// If the home of the module is the current working directory, then we
// want to pick up the cwd of the build process loading the module, not
// our cwd, when we load this module.
if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd &&
(!PP.getHeaderSearchInfo()
.getHeaderSearchOpts()
.ModuleMapFileHomeIsCwd ||
WritingModule->Directory->getName() != ".")) {
// Module directory.
auto Abbrev = std::make_shared<BitCodeAbbrev>();
Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
auto BaseDir = [&]() -> std::optional<SmallString<128>> {
if (PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) {
// Use the current working directory as the base path for all inputs.
auto CWD = FileMgr.getOptionalDirectoryRef(".");
return CWD->getName();
}
if (WritingModule->Directory) {
return WritingModule->Directory->getName();
}
return std::nullopt;
}();
if (BaseDir) {
cleanPathForOutput(FileMgr, *BaseDir);

// If the home of the module is the current working directory, then we
// want to pick up the cwd of the build process loading the module, not
// our cwd, when we load this module.
if (!PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd &&
(!PP.getHeaderSearchInfo()
.getHeaderSearchOpts()
.ModuleMapFileHomeIsCwd ||
WritingModule->Directory->getName() != ".")) {
// Module directory.
auto Abbrev = std::make_shared<BitCodeAbbrev>();
Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));

RecordData::value_type Record[] = {MODULE_DIRECTORY};
Stream.EmitRecordWithBlob(AbbrevCode, Record, *BaseDir);
}

RecordData::value_type Record[] = {MODULE_DIRECTORY};
Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
// Write out all other paths relative to the base directory if possible.
BaseDirectory.assign(BaseDir->begin(), BaseDir->end());
} else if (!isysroot.empty()) {
// Write out paths relative to the sysroot if possible.
BaseDirectory = std::string(isysroot);
}

// Write out all other paths relative to the base directory if possible.
BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
} else if (!isysroot.empty()) {
// Write out paths relative to the sysroot if possible.
BaseDirectory = std::string(isysroot);
}

// Module map file
Expand Down
54 changes: 54 additions & 0 deletions clang/test/Modules/relocatable-modules.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: split-file %s %t
// RUN: cd %t

// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header hu-01.h \
// RUN: -fmodule-name=hu-01 -o hu-01.pcm

// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header hu-02.h \
// RUN: -Wno-experimental-header-units -fmodule-file=hu-01.pcm -o hu-02-abs.pcm

// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header hu-02.h \
// RUN: -Wno-experimental-header-units -fmodule-file=hu-01.pcm -o hu-02-rel.pcm \
// RUN: -fmodule-file-home-is-cwd

// RUN: %clang -module-file-info hu-02-abs.pcm | FileCheck %s --check-prefix=IMPORT-ABS -DPREFIX=%t
// IMPORT-ABS: Imports module 'hu-01': [[PREFIX]]{{/|\\}}hu-01.pcm

// RUN: %clang -module-file-info hu-02-rel.pcm | FileCheck %s --check-prefix=IMPORT-REL
// IMPORT-REL: Imports module 'hu-01': hu-01.pcm

// RUN: llvm-bcanalyzer --dump --disable-histogram %t/hu-02-abs.pcm \
// RUN: | FileCheck %s --check-prefix=INPUT-ABS -DPREFIX=%t
// INPUT-ABS: <INPUT_FILE {{.*}}/> blob data = '[[PREFIX]]{{/|\\}}hu-02.h'

// RUN: llvm-bcanalyzer --dump --disable-histogram %t/hu-02-rel.pcm \
// RUN: | FileCheck %s --check-prefix=INPUT-REL
// INPUT-REL: <INPUT_FILE {{.*}}/> blob data = 'hu-02.h'

//--- hu-01.h
inline void f() {}

//--- hu-02.h
import "hu-01.h";

inline void g() {
f();
}

// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/a.cppm -o %t/a-abs.pcm

// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/a.cppm -o %t/a-rel.pcm \
// RUN: -fmodule-file-home-is-cwd

// RUN: llvm-bcanalyzer --dump --disable-histogram %t/a-abs.pcm \
// RUN: | FileCheck %s --check-prefix=M-INPUT-ABS -DPREFIX=%t
// M-INPUT-ABS: <INPUT_FILE {{.*}}/> blob data = '[[PREFIX]]{{/|\\}}a.cppm'

// RUN: llvm-bcanalyzer --dump --disable-histogram %t/a-rel.pcm \
// RUN: | FileCheck %s --check-prefix=M-INPUT-REL
// M-INPUT-REL: <INPUT_FILE {{.*}}/> blob data = 'a.cppm'

//--- a.cppm
export module a;