From 646e502de0d854cb3ecaca90ab52bebfe59a40cd Mon Sep 17 00:00:00 2001 From: Richard Howell Date: Wed, 4 May 2022 08:44:40 -0700 Subject: [PATCH] [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 --- clang/include/clang/Driver/Options.td | 4 ++++ clang/include/clang/Lex/HeaderSearchOptions.h | 12 +++++++++--- clang/lib/Serialization/ASTWriter.cpp | 17 +++++++++++++---- clang/test/Modules/module-file-home-is-cwd.m | 8 ++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 clang/test/Modules/module-file-home-is-cwd.m diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 8e840ede926a..6668ac4ca947 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -5619,6 +5619,10 @@ def fmodule_map_file_home_is_cwd : Flag<["-"], "fmodule-map-file-home-is-cwd">, HelpText<"Use the current working directory as the home directory of " "module maps specified by -fmodule-map-file=">, MarshallingInfoFlag>; +def fmodule_file_home_is_cwd : Flag<["-"], "fmodule-file-home-is-cwd">, + HelpText<"Use the current working directory as the base directory of " + "compiled module files.">, + MarshallingInfoFlag>; def fmodule_feature : Separate<["-"], "fmodule-feature">, MetaVarName<"">, HelpText<"Enable in module map requires declarations">, diff --git a/clang/include/clang/Lex/HeaderSearchOptions.h b/clang/include/clang/Lex/HeaderSearchOptions.h index 4efdfc26c3c6..6436a9b3bde2 100644 --- a/clang/include/clang/Lex/HeaderSearchOptions.h +++ b/clang/include/clang/Lex/HeaderSearchOptions.h @@ -143,6 +143,12 @@ public: /// file. unsigned ModuleMapFileHomeIsCwd : 1; + /// Set the base path of a built module file to be the current working + /// directory. This is useful for sharing module files across machines + /// that build with different paths without having to rewrite all + /// modulemap files to have working directory relative paths. + unsigned ModuleFileHomeIsCwd : 1; + /// Also search for prebuilt implicit modules in the prebuilt module cache /// path. unsigned EnablePrebuiltImplicitModules : 1; @@ -222,9 +228,9 @@ public: HeaderSearchOptions(StringRef _Sysroot = "/") : Sysroot(_Sysroot), ModuleFormat("raw"), DisableModuleHash(false), ImplicitModuleMaps(false), ModuleMapFileHomeIsCwd(false), - EnablePrebuiltImplicitModules(false), UseBuiltinIncludes(true), - UseStandardSystemIncludes(true), UseStandardCXXIncludes(true), - UseLibcxx(false), Verbose(false), + ModuleFileHomeIsCwd(false), EnablePrebuiltImplicitModules(false), + UseBuiltinIncludes(true), UseStandardSystemIncludes(true), + UseStandardCXXIncludes(true), UseLibcxx(false), Verbose(false), ModulesValidateOncePerBuildSession(false), ModulesValidateSystemHeaders(false), ValidateASTInputFilesContent(false), UseDebugInfo(false), diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index 228bd9aa08db..e42f41f8fbc2 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -1224,15 +1224,24 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context, } if (WritingModule && WritingModule->Directory) { - SmallString<128> BaseDir(WritingModule->Directory->getName()); + SmallString<128> BaseDir; + if (PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) { + // Use the current working directory as the base path for all inputs. + auto *CWD = + Context.getSourceManager().getFileManager().getDirectory(".").get(); + BaseDir.assign(CWD->getName()); + } else { + BaseDir.assign(WritingModule->Directory->getName()); + } cleanPathForOutput(Context.getSourceManager().getFileManager(), 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() - .ModuleMapFileHomeIsCwd || + if (!(PP.getHeaderSearchInfo() + .getHeaderSearchOpts() + .ModuleMapFileHomeIsCwd || + PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleFileHomeIsCwd) || WritingModule->Directory->getName() != StringRef(".")) { // Module directory. auto Abbrev = std::make_shared(); diff --git a/clang/test/Modules/module-file-home-is-cwd.m b/clang/test/Modules/module-file-home-is-cwd.m new file mode 100644 index 000000000000..c455d647da3f --- /dev/null +++ b/clang/test/Modules/module-file-home-is-cwd.m @@ -0,0 +1,8 @@ +// RUN: cd %S +// 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 +// RUN: llvm-bcanalyzer --dump --disable-histogram %t/mod.pcm | FileCheck %s + +// CHECK: blob data = 'Inputs{{/|\\}}normal-module-map{{/|\\}}a1.h' +// CHECK: blob data = 'Inputs{{/|\\}}normal-module-map{{/|\\}}a2.h' +// CHECK: blob data = 'Inputs{{/|\\}}normal-module-map{{/|\\}}module.map' +// CHECK-NOT: MODULE_DIRECTORY