Bazel 不直接支持模块(参见 Issue #4005)。
但是,可以为 bazel 提供自定义 CROSSTOOL。
来自 https://docs.bazel.build/versions/0.22.0/crosstool-reference.html :
By default, Bazel automatically configures CROSSTOOL for your build, but you have the option to configure it manually.
并且可以使用自定义规则扩展 bazel。
来自 https://docs.bazel.build/versions/master/skylark/rules.html :
A few rules are built into Bazel itself. These native rules, such as cc_library and java_binary, provide some core support for certain languages. By defining your own rules, you can add similar support for languages and tools that Bazel does not support natively.
还有这个comment关于 Bazel 的模块问题,建议即使没有 native 支持,您也可以使用自定义 CROSSTOOL 来支持模块:
everything regarding modules (only for clang) is open sourced already. The only missing piece is a CROSSTOOL that makes use of them and provides all necessary features.
谁能展示如何为 clang 编写自定义 CROSSTOOL 以及如何使用它为模块编写自定义 C++ 规则(例如
cc_module
),以便您可以执行以下操作:编写一个基本模块
// helloworld.cc
module;
#include <stdio.h>
export module helloworld;
export void hello();
module :private;
void hello() { puts("Hello world!"); }
使用模块// main.cc
import helloworld;
int main() { hello(); }
将部件集成到构建系统中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
最佳答案
是的。以下是如何做到这一点的概述。
添加用于跟踪模块信息的自定义提供程序:
ModuleCompileInfo = provider(doc = "", fields = [
"module_name",
"module_file",
"module_dependencies",
])
添加自定义规则 cc_module
用于生成 C++20 模块。然后你可以写类似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",
],
)
自定义规则将由于其他标准 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
)
见 https://github.com/rnburn/rules_cc_module对于提供 c++20 模块规则的项目。见here有关如何使用它的示例。
关于c++ - 是否可以在带有自定义规则的 bazel 中使用 C++20 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64406726/