[llvm] [gn] Add libcxx_enable_explicit_modules arg (PR #126017)

Takuto Ikuta via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 5 23:04:34 PST 2025


https://github.com/atetubou created https://github.com/llvm/llvm-project/pull/126017

This is to enable Clang modules build for libc++.

Currently, there are many errors due to missing includes when chromium tries to use explicit Clang modules build. e.g.
https://ci.chromium.org/ui/p/chromium/builders/ci/linux-modules-compile-fyi-rel/5368/overview

To find and fix them easily, I'd like to have GN args enabling explicit Clang modules build for libc++ in this repository too.

Some more context is in https://crbug.com/40263312.

>From c04228f2cf58dec27ed57a4b8569174e714b9683 Mon Sep 17 00:00:00 2001
From: Takuto Ikuta <tikuta at google.com>
Date: Thu, 6 Feb 2025 15:56:51 +0900
Subject: [PATCH] [gn] Add libcxx_enable_explicit_modules arg

This is to enable Clang modules build for libc++.

Currently, there are many errors due to missing includes when
chromium tries to use explicit Clang modules build.
e.g.
https://ci.chromium.org/ui/p/chromium/builders/ci/linux-modules-compile-fyi-rel/5368/overview

To find and fix them easily, I'd like to have GN args enabling
explicit Clang modules build for libc++ in this repository too.
---
 llvm/utils/gn/build/toolchain/BUILD.gn          | 10 +++++++++-
 llvm/utils/gn/secondary/libcxx/config.gni       |  5 +++++
 llvm/utils/gn/secondary/libcxx/include/BUILD.gn |  9 +++++++++
 3 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/llvm/utils/gn/build/toolchain/BUILD.gn b/llvm/utils/gn/build/toolchain/BUILD.gn
index ce2e6df1b69f552..3b377444a81463e 100644
--- a/llvm/utils/gn/build/toolchain/BUILD.gn
+++ b/llvm/utils/gn/build/toolchain/BUILD.gn
@@ -34,12 +34,20 @@ template("unix_toolchain") {
 
     tool("cxx") {
       depfile = "{{output}}.d"
-      command = "$cxx -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}"
+      command = "$cxx -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} {{module_deps_no_self}}"
       depsformat = "gcc"
       description = "CXX {{output}}"
       outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.o" ]
     }
 
+    tool("cxx_module") {
+      depfile = "{{output}}.d"
+      command = "$cxx -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} {{module_deps_no_self}} -fmodule-name={{label_name}} -x c++ -Xclang -emit-module"
+      depsformat = "gcc"
+      description = "CXX_MODULE {{output}}"
+      outputs = [ "{{source_out_dir}}/{{label_name}}.{{source_name_part}}.o" ]
+    }
+
     tool("objcxx") {
       depfile = "{{output}}.d"
       command = "$cxx -MMD -MF $depfile -o {{output}} -c {{source}} {{defines}} {{include_dirs}} {{cflags}} {{cflags_objcc}}"
diff --git a/llvm/utils/gn/secondary/libcxx/config.gni b/llvm/utils/gn/secondary/libcxx/config.gni
index e5bdf545da4fb41..775cd1b3fab2d16 100644
--- a/llvm/utils/gn/secondary/libcxx/config.gni
+++ b/llvm/utils/gn/secondary/libcxx/config.gni
@@ -7,4 +7,9 @@ declare_args() {
 
   # Build timezone database as part of libc++experimental.
   libcxx_enable_time_zone_database = target_os == "linux"
+
+  # Whether to enable Clang modules build for libc++.
+  # See https://clang.llvm.org/docs/Modules.html for more details about Clang
+  # Modules.
+  libcxx_enable_explicit_modules = false
 }
diff --git a/llvm/utils/gn/secondary/libcxx/include/BUILD.gn b/llvm/utils/gn/secondary/libcxx/include/BUILD.gn
index f118d22c472d89a..433ebfff5dd3bd6 100644
--- a/llvm/utils/gn/secondary/libcxx/include/BUILD.gn
+++ b/llvm/utils/gn/secondary/libcxx/include/BUILD.gn
@@ -2044,6 +2044,15 @@ if (current_toolchain == default_toolchain) {
 
 config("include_config") {
   include_dirs = [ libcxx_generated_include_dir ]
+
+  if (libcxx_enable_explicit_modules) {
+    cflags_cc = [
+      "-fmodules",
+      "-fmodule-map-file=" +
+          rebase_path("//libcxx/include/module.modulemap", root_build_dir),
+      "-fno-implicit-module-maps",
+    ]
+  }
 }
 
 group("include") {



More information about the llvm-commits mailing list