我为 Bazel (2.0) 编写了一个 MSVC预编译头文件(PCH) 实现,并希望得到一些反馈,因为我对此不满意。
快速回顾一下让 PCH 在 MSVC 中工作需要做的事情:
- 用/Yc和/Fp编译PCH得到(1)
.pch
文件和(2).obj
文件。 - 使用 (1) 上的/Yu编译二进制文件,然后再次使用相同的/Fp选项。
.obj
使用文件 (2)链接二进制文件。
执行
我们定义了一个规则,它将pchsrc
(for /Yc ) 和pchhdr
(for /Fp ) 作为参数以及一些cc_*
规则参数(以获取定义和包含)。然后我们调用编译器来获取PCH(主要按照这里演示的方法)。一旦我们有了 PCH,我们通过CcInfo传播位置和链接器输入,用户需要调用cc_pch_copts
以获取/Yu和/Fp选项。
pch.bzl
load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES")
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
def cc_pch_copts(pchheader, pchtarget):
return [
"/Yu\"" + pchheader + "\"",
"/Fp\"$(location :" + pchtarget + ")\""
]
def _cc_pch(ctx):
""" Create a precompiled header """
cc_toolchain = find_cc_toolchain(ctx)
source_file = ctx.file.pchsrc
pch_file = ctx.outputs.pch
pch_obj_file = ctx.outputs.obj
# Obtain the includes of the dependencies
cc_infos = []
for dep in ctx.attr.deps:
if CcInfo in dep:
cc_infos.append(dep[CcInfo])
deps_cc_info = cc_common.merge_cc_infos(cc_infos=cc_infos)
# Flags to create the pch
pch_flags = [
"/Fp" + pch_file.path,
"/Yc" + ctx.attr.pchhdr,
]
# Prepare the compiler
feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
cc_compiler_path = cc_common.get_tool_for_action(
feature_configuration = feature_configuration,
action_name = ACTION_NAMES.cpp_compile,
)
deps_ctx = deps_cc_info.compilation_context
cc_compile_variables = cc_common.create_compile_variables(
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
user_compile_flags = ctx.fragments.cpp.copts + ctx.fragments.cpp.cxxopts + pch_flags + ctx.attr.copts,
source_file = source_file.path,
output_file = pch_obj_file.path,
preprocessor_defines = depset(deps_ctx.defines.to_list() + deps_ctx.local_defines.to_list() + ctx.attr.defines + ctx.attr.local_defines),
include_directories = deps_ctx.includes,
quote_include_directories = deps_ctx.quote_includes,
system_include_directories = depset(["."] + deps_ctx.system_includes.to_list()),
framework_include_directories = deps_ctx.framework_includes,
)
env = cc_common.get_environment_variables(
feature_configuration = feature_configuration,
action_name = ACTION_NAMES.cpp_compile,
variables = cc_compile_variables,
)
command_line = cc_common.get_memory_inefficient_command_line(
feature_configuration = feature_configuration,
action_name = ACTION_NAMES.cpp_compile,
variables = cc_compile_variables,
)
args = ctx.actions.args()
for cmd in command_line:
if cmd == "/showIncludes":
continue
args.add(cmd)
# Invoke the compiler
ctx.actions.run(
executable = cc_compiler_path,
arguments = [args],
env = env,
inputs = depset(
items = [source_file],
transitive = [cc_toolchain.all_files],
),
outputs = [pch_file, pch_obj_file],
progress_message = "Generating precompiled header {}".format(ctx.attr.pchhdr),
)
return [
DefaultInfo(files = depset(items = [pch_file])),
CcInfo(
compilation_context=cc_common.create_compilation_context(
includes=depset([pch_file.dirname]),
headers=depset([pch_file]),
),
linking_context=cc_common.create_linking_context(
user_link_flags = [pch_obj_file.path]
)
)
]
cc_pch = rule(
implementation = _cc_pch,
attrs = {
"pchsrc": attr.label(allow_single_file=True, mandatory=True),
"pchhdr": attr.string(mandatory=True),
"copts": attr.string_list(),
"local_defines": attr.string_list(),
"defines": attr.string_list(),
"deps": attr.label_list(allow_files = True),
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
},
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
fragments = ["cpp"],
outputs = {
"pch": "%{pchsrc}.pch",
"obj": "%{pchsrc}.pch.obj"
},
provides = [CcInfo],
)
我们会使用它:
构建.bzl
load(":pch.bzl", "cc_pch", "cc_pch_copts")
load("@rules_cc//cc:defs.bzl", "cc_binary")
def my_cc_binary(name, pchhdr, pchsrc, **kwargs):
pchtarget = name + "_pch"
cc_pch(
name = pchtarget,
pchsrc = pchsrc,
pchhdr = pchhdr,
defines = kwargs.get("defines", []),
deps = kwargs.get("deps", []),
local_defines = kwargs.get("local_defines", []),
copts = kwargs.get("copts", []),
)
kwargs["deps"] = kwargs.get("deps", []) + [":" + pchtarget])
kwargs["copts"] = kwargs.get("copts", []) + cc_pch_copts(pchhdr, pchtarget))
native.cc_binary(name=name, **kwargs)
my_cc_binary(
name = "main",
srcs = ["main.cpp", "common.h", "common.cpp"],
pchsrc = "common.cpp",
pchhdr = "common.h",
)
项目包含:
主文件
#include "common.h"
int main() { std::cout << "Hello world!" << std::endl; return 0; }
常见的.h
#include <iostream>
常见的.cpp
#include "common.h"
问题
实施工作。但是,我的讨论点是:
- 将附加编译标志传播到依赖目标的最佳方法是什么?我通过它解决它的方式
cc_pch_copts
似乎相当老套。我假设它涉及定义一个提供者,但我找不到一个允许我转发标志的提供者(CcToolChainConfigInfo在这个方向上有一些东西,但它似乎有点矫枉过正)。 - 除了我上面实现的方法之外,还有其他方法可以获取所有编译标志(定义、包含等)吗?它真的很冗长,而且大多数情况下都没有涵盖很多极端情况。是否有可能做一些事情,比如
empty.cpp
在规则中编译一个文件cc_pch
以获得一个可以直接访问所有标志的提供程序?
注意:我知道预编译头文件的缺点,但这是一个大型代码库,不幸的是不使用它不是一个选项。