Skip to content

Commit 646e502

Browse files
committed
[clang] add -fmodule-file-home-is-cwd
This diff adds a new frontend flag `-fmodule-file-home-is-cwd`. The behavior of this flag is similar to `-fmodule-map-file-home-is-cwd` but does not require the module map files to be modified to have inputs relative to the cwd. Instead the output modules will have their `BaseDirectory` set to the cwd and will try and resolve paths relative to that. The motiviation for this change is to support relocatable pcm files that are built on different machines with different paths without having to alter module map files, which is sometimes not possible as they are provided by 3rd parties. Reviewed By: urnathan Differential Revision: https://reviews.llvm.org/D124874
1 parent 40d3a0b commit 646e502

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5619,6 +5619,10 @@ def fmodule_map_file_home_is_cwd : Flag<["-"], "fmodule-map-file-home-is-cwd">,
56195619
HelpText<"Use the current working directory as the home directory of "
56205620
"module maps specified by -fmodule-map-file=<FILE>">,
56215621
MarshallingInfoFlag<HeaderSearchOpts<"ModuleMapFileHomeIsCwd">>;
5622+
def fmodule_file_home_is_cwd : Flag<["-"], "fmodule-file-home-is-cwd">,
5623+
HelpText<"Use the current working directory as the base directory of "
5624+
"compiled module files.">,
5625+
MarshallingInfoFlag<HeaderSearchOpts<"ModuleFileHomeIsCwd">>;
56225626
def fmodule_feature : Separate<["-"], "fmodule-feature">,
56235627
MetaVarName<"<feature>">,
56245628
HelpText<"Enable <feature> in module map requires declarations">,

clang/include/clang/Lex/HeaderSearchOptions.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ class HeaderSearchOptions {
143143
/// file.
144144
unsigned ModuleMapFileHomeIsCwd : 1;
145145

146+
/// Set the base path of a built module file to be the current working
147+
/// directory. This is useful for sharing module files across machines
148+
/// that build with different paths without having to rewrite all
149+
/// modulemap files to have working directory relative paths.
150+
unsigned ModuleFileHomeIsCwd : 1;
151+
146152
/// Also search for prebuilt implicit modules in the prebuilt module cache
147153
/// path.
148154
unsigned EnablePrebuiltImplicitModules : 1;
@@ -222,9 +228,9 @@ class HeaderSearchOptions {
222228
HeaderSearchOptions(StringRef _Sysroot = "/")
223229
: Sysroot(_Sysroot), ModuleFormat("raw"), DisableModuleHash(false),
224230
ImplicitModuleMaps(false), ModuleMapFileHomeIsCwd(false),
225-
EnablePrebuiltImplicitModules(false), UseBuiltinIncludes(true),
226-
UseStandardSystemIncludes(true), UseStandardCXXIncludes(true),
227-
UseLibcxx(false), Verbose(false),
231+
ModuleFileHomeIsCwd(false), EnablePrebuiltImplicitModules(false),
232+
UseBuiltinIncludes(true), UseStandardSystemIncludes(true),
233+
UseStandardCXXIncludes(true), UseLibcxx(false), Verbose(false),
228234
ModulesValidateOncePerBuildSession(false),
229235
ModulesValidateSystemHeaders(false),
230236
ValidateASTInputFilesContent(false), UseDebugInfo(false),

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,15 +1224,24 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
12241224
}
12251225

12261226
if (WritingModule && WritingModule->Directory) {
1227-
SmallString<128> BaseDir(WritingModule->Directory->getName());
1227+
SmallString<128> BaseDir;
1228+
if (PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) {
1229+
// Use the current working directory as the base path for all inputs.
1230+
auto *CWD =
1231+
Context.getSourceManager().getFileManager().getDirectory(".").get();
1232+
BaseDir.assign(CWD->getName());
1233+
} else {
1234+
BaseDir.assign(WritingModule->Directory->getName());
1235+
}
12281236
cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
12291237

12301238
// If the home of the module is the current working directory, then we
12311239
// want to pick up the cwd of the build process loading the module, not
12321240
// our cwd, when we load this module.
1233-
if (!PP.getHeaderSearchInfo()
1234-
.getHeaderSearchOpts()
1235-
.ModuleMapFileHomeIsCwd ||
1241+
if (!(PP.getHeaderSearchInfo()
1242+
.getHeaderSearchOpts()
1243+
.ModuleMapFileHomeIsCwd ||
1244+
PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) ||
12361245
WritingModule->Directory->getName() != StringRef(".")) {
12371246
// Module directory.
12381247
auto Abbrev = std::make_shared<BitCodeAbbrev>();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: cd %S
2+
// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fmodule-file-home-is-cwd -fmodule-name=libA -emit-module Inputs/normal-module-map/module.map -o %t/mod.pcm
3+
// RUN: llvm-bcanalyzer --dump --disable-histogram %t/mod.pcm | FileCheck %s
4+
5+
// CHECK: <INPUT_FILE {{.*}}/> blob data = 'Inputs{{/|\\}}normal-module-map{{/|\\}}a1.h'
6+
// CHECK: <INPUT_FILE {{.*}}/> blob data = 'Inputs{{/|\\}}normal-module-map{{/|\\}}a2.h'
7+
// CHECK: <INPUT_FILE {{.*}}/> blob data = 'Inputs{{/|\\}}normal-module-map{{/|\\}}module.map'
8+
// CHECK-NOT: MODULE_DIRECTORY

0 commit comments

Comments
 (0)