是否可以在带有自定义规则的 bazel 中使用 C++20 模块?
Is it possible to use C++20 modules in bazel with custom rules?
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.
关于 Bazel 模块问题的 comment 建议即使没有本机支持也可以使用自定义 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",
])
添加用于生成 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",
],
)
自定义规则将
- 为 A 的模块依赖关系创建模块映射
- 同时生成打包 A 对象的静态库和 A 模块的 cmi 文件
- Return 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
)
有关如何使用它的示例,请参阅 https://github.com/rnburn/rules_cc_module for a project that provides c++20 module rules. And see here。
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.
关于 Bazel 模块问题的 comment 建议即使没有本机支持也可以使用自定义 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",
])
添加用于生成 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",
],
)
自定义规则将
- 为 A 的模块依赖关系创建模块映射
- 同时生成打包 A 对象的静态库和 A 模块的 cmi 文件
- Return 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
)
有关如何使用它的示例,请参阅 https://github.com/rnburn/rules_cc_module for a project that provides c++20 module rules. And see here。