Bazel C ++预编译头文件的实现

问题描述 投票:0回答:1

[我已经为Bazel(2.0)编写了MSVC Precompiled Header Files(PCH)实现,并希望获得一些反馈,因为我对此不满意。

快速回顾使PCH在MSVC中工作需要做的事情:

  1. /Yc/Fp编译PCH以获得(1).pch文件和(2).obj文件。
  2. 使用(1)上的/Yu并再次使用相同的/Fp选项编译二进制文件。
  3. 使用.obj文件(2)链接二进制文件。

实施

我们定义了一个规则,该规则以pchsrc(对于/Yc)和pchhdr(对于/Fp)为参数,以及某些cc_*规则参数(以获取定义并包括) 。然后,我们调用编译器来获取PCH(主要是按照here演示的方法)。一旦有了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],
)

我们将使用它:

BUILD.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",
)

其中包含以下项目:

main.cpp

#include "common.h"
int main() { std::cout << "Hello world!" << std::endl; return 0; }

common.h

#include <iostream>

common.cpp

#include "common.h"

问题

实施工作。但是,我的讨论重点是:

  • 将附加编译标志传播到相关目标的最佳方法是什么?我通过cc_pch_copts解决该问题的方法似乎很拙劣。我以为它涉及定义一个提供程序,但是我找不到一个允许我转发标志的提供程序(CcToolChainConfigInfo在此方向上有某些功能,但似乎有点过头)。
  • 除了上面实现的方法以外,还有另一种获取所有编译标志(定义,包含等)的方法吗? really冗长,大多数情况下并没有涉及很多极端情况。是否可以像在empty.cpp规则中编译cc_pch文件那样来获得直接访问所有标志的提供程序?

注:我知道预编译头文件的缺点,但这是一个很大的代码库,不幸的是,不使用它不是一种选择。

c++ visual-c++ bazel precompiled-headers starlark
1个回答
0
投票

据我所知,预编译头对于进行大量模板元编程并拥有可观代码库的框架开发人员特别有用。如果您仍在开发框架,则无意于加快编译速度。如果代码设计不当且每个依赖项都按顺序出现,则不会加快编译时间。您的文件仅是VC ++的配置文件,实际作业甚至尚未启动,并且预编译的标头是字节码。请尽可能使用并行构建。

而且,结果头是巨大的!

© www.soinside.com 2019 - 2024. All rights reserved.