[clang] [clang][driver][clang-cl] Support `--precompile` and `-fmodule-*` options in Clang-CL (PR #98761)

Sharadh Rajaraman via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 22 12:51:42 PDT 2024


https://github.com/sharadhr updated https://github.com/llvm/llvm-project/pull/98761

>From 1fed92a00f0d732a2575861c2bf6a6d053407255 Mon Sep 17 00:00:00 2001
From: Sharadh Rajaraman <r.sharadh at outlook.sg>
Date: Sat, 13 Jul 2024 19:25:47 +0100
Subject: [PATCH 1/6] Allow `--precompile` and `-fprebuilt-module-path` to be
 passed directly into CL driver without `/clang:` prefix

---
 clang/include/clang/Driver/Options.td | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 4ab8638175dd3..ca7cfef8453a0 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3085,7 +3085,7 @@ def fmodules_user_build_path : Separate<["-"], "fmodules-user-build-path">, Grou
   HelpText<"Specify the module user build path">,
   MarshallingInfoString<HeaderSearchOpts<"ModuleUserBuildPath">>;
 def fprebuilt_module_path : Joined<["-"], "fprebuilt-module-path=">, Group<i_Group>,
-  Flags<[]>, Visibility<[ClangOption, CC1Option]>,
+  Flags<[]>, Visibility<[ClangOption, CLOption, CC1Option]>,
   MetaVarName<"<directory>">,
   HelpText<"Specify the prebuilt module path">;
 defm prebuilt_implicit_modules : BoolFOption<"prebuilt-implicit-modules",
@@ -5874,6 +5874,7 @@ def _output : Separate<["--"], "output">, Alias<o>;
 def _param : Separate<["--"], "param">, Group<CompileOnly_Group>;
 def _param_EQ : Joined<["--"], "param=">, Alias<_param>;
 def _precompile : Flag<["--"], "precompile">, Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CLOption]>,
   Group<Action_Group>, HelpText<"Only precompile the input">;
 def _prefix_EQ : Joined<["--"], "prefix=">, Alias<B>;
 def _prefix : Separate<["--"], "prefix">, Alias<B>;

>From eda96d01d1e698b8f246cbbdb481356f67797260 Mon Sep 17 00:00:00 2001
From: Sharadh Rajaraman <r.sharadh at outlook.sg>
Date: Sun, 14 Jul 2024 16:28:46 +0100
Subject: [PATCH 2/6] Support more `fmodule-*` options from CL driver

---
 clang/include/clang/Driver/Options.td | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index ca7cfef8453a0..bfe369a6284fe 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3094,11 +3094,11 @@ defm prebuilt_implicit_modules : BoolFOption<"prebuilt-implicit-modules",
   NegFlag<SetFalse>, BothFlags<[], [ClangOption, CC1Option]>>;
 
 def fmodule_output_EQ : Joined<["-"], "fmodule-output=">,
-  Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option]>,
+  Flags<[NoXarchOption]>, Visibility<[ClangOption, CLOption, CC1Option]>,
   MarshallingInfoString<FrontendOpts<"ModuleOutputPath">>,
   HelpText<"Save intermediate module file results when compiling a standard C++ module unit.">;
 def fmodule_output : Flag<["-"], "fmodule-output">, Flags<[NoXarchOption]>,
-  Visibility<[ClangOption, CC1Option]>,
+  Visibility<[ClangOption, CLOption, CC1Option]>,
   HelpText<"Save intermediate module file results when compiling a standard C++ module unit.">;
 
 defm skip_odr_check_in_gmf : BoolOption<"f", "skip-odr-check-in-gmf",
@@ -3278,8 +3278,10 @@ def fretain_comments_from_system_headers : Flag<["-"], "fretain-comments-from-sy
   Visibility<[ClangOption, CC1Option]>,
   MarshallingInfoFlag<LangOpts<"RetainCommentsFromSystemHeaders">>;
 def fmodule_header : Flag <["-"], "fmodule-header">, Group<f_Group>,
+  Visibility<[ClangOption, CLOption]>,
   HelpText<"Build a C++20 Header Unit from a header">;
 def fmodule_header_EQ : Joined<["-"], "fmodule-header=">, Group<f_Group>,
+  Visibility<[ClangOption, CLOption]>,
   MetaVarName<"<kind>">,
   HelpText<"Build a C++20 Header Unit from a header that should be found in the user (fmodule-header=user) or system (fmodule-header=system) search path.">;
 

>From 994ca23255888b14fe14c8abddb2d37eaa578ce3 Mon Sep 17 00:00:00 2001
From: Sharadh Rajaraman <r.sharadh at petagene.com>
Date: Tue, 16 Jul 2024 23:49:08 +0100
Subject: [PATCH 3/6] `clang-cl` C++20 modules compile test

---
 clang/test/Driver/cl-cxx20-modules.cppm | 65 +++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
 create mode 100644 clang/test/Driver/cl-cxx20-modules.cppm

diff --git a/clang/test/Driver/cl-cxx20-modules.cppm b/clang/test/Driver/cl-cxx20-modules.cppm
new file mode 100644
index 0000000000000..84807324b4cbc
--- /dev/null
+++ b/clang/test/Driver/cl-cxx20-modules.cppm
@@ -0,0 +1,65 @@
+// REQUIRES: system-windows
+
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+
+// RUN: %clang_cl /std:c++20 --precompile "%/t/Hello.cppm" "/Fo%/t/Hello.pcm"
+// RUN: %clang_cl /std:c++20 %/t/use.cpp -fmodule-file=Hello=%/t/Hello.pcm %/t/Hello.pcm /Fo%/t/SimpleHelloWorld.exe 2>&1
+
+// RUN: %/t/SimpleHelloWorld.exe
+// CHECK: Simple Hello World!
+
+//--- Hello.cppm
+module;
+#include <iostream>
+export module Hello;
+export void hello() {
+  std::cout << "Simple Hello World!\n";
+}
+
+//--- use.cpp
+import Hello;
+int main() {
+  hello();
+  return 0;
+}
+
+//--- M.cppm
+export module M;
+export import :interface_part;
+import :impl_part;
+export void Hello();
+
+//--- interface_part.cpp
+export module M:interface_part;
+export void World();
+
+//--- impl_part.cppm
+module;
+#include <iostream>
+#include <string>
+module M:impl_part;
+import :interface_part;
+
+std::string W = "World!";
+void World() {
+  std::cout << W << std::endl;
+}
+
+//--- Impl.cpp
+module;
+#include <iostream>
+module M;
+void Hello() {
+  std::cout << "Complex Hello ";
+}
+
+//--- User.cpp
+import M;
+int main() {
+  Hello();
+  World();
+  return 0;
+}
+

>From d1ef402c3258871d541e4acb21c5019c94636afb Mon Sep 17 00:00:00 2001
From: Sharadh Rajaraman <r.sharadh at petagene.com>
Date: Wed, 17 Jul 2024 00:08:29 +0100
Subject: [PATCH 4/6] Document the new options

---
 clang/docs/StandardCPlusPlusModules.rst | 17 ++++++++++++-----
 clang/docs/UsersManual.rst              |  6 ++++++
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/clang/docs/StandardCPlusPlusModules.rst b/clang/docs/StandardCPlusPlusModules.rst
index 1c3c4d319c0e1..cfa8a027c9d26 100644
--- a/clang/docs/StandardCPlusPlusModules.rst
+++ b/clang/docs/StandardCPlusPlusModules.rst
@@ -398,6 +398,13 @@ BMIs cannot be shipped in an archive to create a module library. Instead, the
 BMIs(``*.pcm``) are compiled into object files(``*.o``) and those object files
 are added to the archive instead.
 
+clang-cl
+~~~~~~~~
+
+``clang-cl`` supports the same options as ``clang++`` for modules as detailed above;
+there is no need to prefix these options with ``/clang:``. Note that ``cl.exe``
+options to emit `IFC files <https://devblogs.microsoft.com/cppblog/using-cpp-modules-in-msvc-from-the-command-line-part-1/>` are *not* supported. The precompiled modules are also not compatible for use with ``cl.exe``.
+
 Consistency Requirements
 ~~~~~~~~~~~~~~~~~~~~~~~~
 
@@ -1176,12 +1183,12 @@ have ``.cppm`` (or ``.ccm``, ``.cxxm``, ``.c++m``) as the file extension.
 However, the behavior is inconsistent with other compilers. This is tracked by
 `#57416 <https://github.com/llvm/llvm-project/issues/57416>`_.
 
-clang-cl is not compatible with standard C++ modules
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.. clang-cl is not compatible with standard C++ modules
+.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-``/clang:-fmodule-file`` and ``/clang:-fprebuilt-module-path`` cannot be used
-to specify the BMI with ``clang-cl.exe``. This is tracked by
-`#64118 <https://github.com/llvm/llvm-project/issues/64118>`_.
+.. ``/clang:-fmodule-file`` and ``/clang:-fprebuilt-module-path`` cannot be used
+.. to specify the BMI with ``clang-cl.exe``. This is tracked by
+.. `#64118 <https://github.com/llvm/llvm-project/issues/64118>`_.
 
 Incorrect ODR violation diagnostics
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst
index 8e01ea15064ba..51bc871ebbe13 100644
--- a/clang/docs/UsersManual.rst
+++ b/clang/docs/UsersManual.rst
@@ -4732,6 +4732,12 @@ Execute ``clang-cl /?`` to see a list of supported options:
       -flto=<value>           Set LTO mode to either 'full' or 'thin'
       -flto                   Enable LTO in 'full' mode
       -fmerge-all-constants   Allow merging of constants
+      -fmodule-file=<module_name>=<module-file>
+                              Use the specified module file that provides the module <module_name>
+      -fmodule-header=<header>
+                              Build <header> as a C++20 header unit
+      -fmodule-output=<path>
+                              Save intermediate module file results when compiling a standard C++ module unit.
       -fms-compatibility-version=<value>
                               Dot-separated value representing the Microsoft compiler version
                               number to report in _MSC_VER (0 = don't define it; default is same value as installed cl.exe, or 1933)

>From e6386de2ef7dcb4650073c81e2da99bbd3446c99 Mon Sep 17 00:00:00 2001
From: Sharadh Rajaraman <r.sharadh at outlook.sg>
Date: Wed, 17 Jul 2024 10:58:02 +0100
Subject: [PATCH 5/6] Remove commented-out docs - Also add a note for build
 system authors to use `clang++` options

---
 clang/docs/StandardCPlusPlusModules.rst | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/clang/docs/StandardCPlusPlusModules.rst b/clang/docs/StandardCPlusPlusModules.rst
index cfa8a027c9d26..2e5af67e53cb7 100644
--- a/clang/docs/StandardCPlusPlusModules.rst
+++ b/clang/docs/StandardCPlusPlusModules.rst
@@ -403,7 +403,10 @@ clang-cl
 
 ``clang-cl`` supports the same options as ``clang++`` for modules as detailed above;
 there is no need to prefix these options with ``/clang:``. Note that ``cl.exe``
-options to emit `IFC files <https://devblogs.microsoft.com/cppblog/using-cpp-modules-in-msvc-from-the-command-line-part-1/>` are *not* supported. The precompiled modules are also not compatible for use with ``cl.exe``.
+`options to emit/consume IFC files <https://devblogs.microsoft.com/cppblog/using-cpp-modules-in-msvc-from-the-command-line-part-1/>` are *not* supported.
+The resultant precompiled modules are also not compatible for use with ``cl.exe``.
+
+We recommend that build system authors use the above-mentioned ``clang++`` options  with ``clang-cl`` to build modules.
 
 Consistency Requirements
 ~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1183,13 +1186,6 @@ have ``.cppm`` (or ``.ccm``, ``.cxxm``, ``.c++m``) as the file extension.
 However, the behavior is inconsistent with other compilers. This is tracked by
 `#57416 <https://github.com/llvm/llvm-project/issues/57416>`_.
 
-.. clang-cl is not compatible with standard C++ modules
-.. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-.. ``/clang:-fmodule-file`` and ``/clang:-fprebuilt-module-path`` cannot be used
-.. to specify the BMI with ``clang-cl.exe``. This is tracked by
-.. `#64118 <https://github.com/llvm/llvm-project/issues/64118>`_.
-
 Incorrect ODR violation diagnostics
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

>From 42ff7de7363b36378eb028368c44b6155452199b Mon Sep 17 00:00:00 2001
From: Sharadh Rajaraman <r.sharadh at petagene.com>
Date: Mon, 22 Jul 2024 20:51:24 +0100
Subject: [PATCH 6/6] Attempt to rewrite tests for -fmodule* args to clang-cl

---
 clang/test/Driver/cl-cxx20-modules.cppm | 69 +++----------------------
 1 file changed, 6 insertions(+), 63 deletions(-)

diff --git a/clang/test/Driver/cl-cxx20-modules.cppm b/clang/test/Driver/cl-cxx20-modules.cppm
index 84807324b4cbc..82b3120b5077b 100644
--- a/clang/test/Driver/cl-cxx20-modules.cppm
+++ b/clang/test/Driver/cl-cxx20-modules.cppm
@@ -1,65 +1,8 @@
-// REQUIRES: system-windows
+// RUN: %clang_cl /std:c++20 --precompile -### -- %s 2>&1 | FileCheck  %s
+// CHECK: --precompile
 
-// RUN: rm -rf %t
-// RUN: mkdir -p %t
-// RUN: split-file %s %t
-
-// RUN: %clang_cl /std:c++20 --precompile "%/t/Hello.cppm" "/Fo%/t/Hello.pcm"
-// RUN: %clang_cl /std:c++20 %/t/use.cpp -fmodule-file=Hello=%/t/Hello.pcm %/t/Hello.pcm /Fo%/t/SimpleHelloWorld.exe 2>&1
-
-// RUN: %/t/SimpleHelloWorld.exe
-// CHECK: Simple Hello World!
-
-//--- Hello.cppm
-module;
-#include <iostream>
-export module Hello;
-export void hello() {
-  std::cout << "Simple Hello World!\n";
-}
-
-//--- use.cpp
-import Hello;
-int main() {
-  hello();
-  return 0;
-}
-
-//--- M.cppm
-export module M;
-export import :interface_part;
-import :impl_part;
-export void Hello();
-
-//--- interface_part.cpp
-export module M:interface_part;
-export void World();
-
-//--- impl_part.cppm
-module;
-#include <iostream>
-#include <string>
-module M:impl_part;
-import :interface_part;
-
-std::string W = "World!";
-void World() {
-  std::cout << W << std::endl;
-}
-
-//--- Impl.cpp
-module;
-#include <iostream>
-module M;
-void Hello() {
-  std::cout << "Complex Hello ";
-}
-
-//--- User.cpp
-import M;
-int main() {
-  Hello();
-  World();
-  return 0;
-}
+// RUN: %clang_cl /std:c++20 --fmodule-file=Foo=Foo.pcm -### -- %s 2>&1 | FileCheck  %s
+// CHECK: -fmodule-file=Foo=Foo.pcm
 
+// RUN: %clang_cl /std:c++20 --fprebuilt-module-path=. -### -- %s 2>&1 | FileCheck  %s
+// CHECK: -fprebuilt-module-path=.
\ No newline at end of file



More information about the cfe-commits mailing list