是否可以在 bazel 中使用具有自定义规则的 C++20 模块?

rni*_*ckb 7 c++ bazel c++20 c++-modules

Bazel 不直接支持模块(请参阅问题 #4005)。

但是,可以为 bazel 提供定制的 CROSSTOOL。

来自https://docs.bazel.build/versions/0.22.0/crosstool-reference.html

默认情况下,Bazel 会自动为您的构建配置 CROSSTOOL,但您可以选择手动配置它。

并且可以使用自定义规则扩展 bazel。

来自https://docs.bazel.build/versions/master/skylark/rules.html

Bazel 本身内置了一些规则。这些原生规则,例如 cc_library 和 java_binary,为某些语言提供了一些核心支持。通过定义自己的规则,您可以为 Bazel 本身不支持的语言和工具添加类似的支持。

关于Bazel 模块问题的评论表明,即使没有本机支持,您也可以使用自定义 CROSSTOOL 来支持模块:

有关模块(仅适用于 clang)的所有内容都已经开源。唯一缺少的部分是使用它们并提供所有必要功能的 CROSSTOOL。

任何人都可以展示如何为 clang 编写自定义 CROSSTOOL 以及如何使用它为模块编写自定义 C++ 规则(例如cc_module),以便您可以执行以下操作:

编写一个基本模块

// helloworld.cc
module;
#include <stdio.h>

export module helloworld;
export void hello();

module :private;
void hello() { puts("Hello world!"); }
Run Code Online (Sandbox Code Playgroud)

使用模块

// main.cc
import helloworld;
int main() { hello(); }
Run Code Online (Sandbox Code Playgroud)

将零件集成到构建系统中

cc_module(
   name = "helloworld",
   srcs = ["helloworld.cc"],
) # Compiles to a precomiled module file (PCM)

cc_binary(
  name = "main",
  srcs = [
    "main.cc",
  ],
  deps = [
     ":helloworld",
   ],
) # Compiles against the helloworld PCM
Run Code Online (Sandbox Code Playgroud)

rni*_*ckb 6

是的。以下是如何执行此操作的概述。

添加用于跟踪模块信息的自定义提供程序:

ModuleCompileInfo = provider(doc = "", fields = [
    "module_name", 
    "module_file",
    "module_dependencies",
])
Run Code Online (Sandbox Code Playgroud)

添加用于生成 C++20 模块的自定义规则cc_module。然后你可以写类似的东西

cc_module(
   name = "A",
   src = "a.cc", # a.cc exports the module A
   impl_srcs = [
     "a_impl1.cc", # optionally you can provide additional implementation sources for A
     "a_impl2.cc",
   ],
   deps = [
     ":B", # dependencies can be either other modules or other cc_libraries
     ":C",
   ],
)
Run Code Online (Sandbox Code Playgroud)

自定义规则将

  1. 为A的模块依赖创建模块映射
  2. 生成封装 A 对象的静态库和 A 模块的 cmi 文件
  3. 返回A 的CcInfo提供程序和 ModuleCompileInfo 提供程序以跟踪模块信息。

由于其他标准 bazel 规则(例如 cc_binary、cc_library)不支持 c++20 模块,因此您还需要提供自定义 cc_module_binary 和 cc_module_library 规则,以便可以将模块与其他 C++ 结构一起使用。例如,

cc_module_binary(
   name = "exe",
   srcs = ["main.cc"],  # main.cc can import A
   deps = [":A"],       # we can depend on modules
)
Run Code Online (Sandbox Code Playgroud)

请参阅https://github.com/rnburn/rules_cc_module了解提供 c++20 模块规则的项目。请参阅此处了解如何使用它的示例。