簡體   English   中英

Bazel C++ 預編譯頭文件實現

[英]Bazel C++ precompiled headers implementation

我已經為 Bazel (2.0) 編寫了一個 MSVC 預編譯頭文件(PCH) 實現,並希望得到一些反饋,因為我對它不滿意。

快速回顧一下需要做什么才能讓 PCH 在 MSVC 中工作:

  1. 使用/Yc/Fp編譯 PCH 以獲取 (1) .pch文件和 (2) .obj文件。
  2. 使用 (1) 上的/Yu和相同的/Fp選項編譯二進制文件。
  3. 使用.obj文件 (2) 鏈接二進制文件。

執行

我們定義了一個規則,它將pchsrc (用於/Yc )和pchhdr (用於/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; }

通用文件

#include <iostream>

通用文件

#include "common.h"

問題

實施工作 但是,我的討論要點是:

  • 將附加編譯標志傳播到相關目標的最佳方法是什么? 我通過cc_pch_copts解決它的方式似乎相當cc_pch_copts 我認為它涉及定義一個提供者,但我找不到一個允許我轉發標志的提供者( CcToolChainConfigInfo在這個方向上有一些東西,但似乎有點矯枉過正)。
  • 除了我上面實現的方法之外,還有其他方法可以獲取所有編譯標志(定義、包含等)嗎? 真的很冗長,而且大部分都沒有涵蓋很多極端情況。 是否有可能在cc_pch規則中編譯一個empty.cpp文件來獲得一個可以直接訪問所有標志的提供者?

注意:我知道預編譯頭文件的缺點,但這是一個很大的代碼庫,不幸的是,不使用它不是一種選擇。

也許可以通過生成一個dummy cpp來簡化,只是為了觸發pch文件的生成,不需要鏈接生成的obj。 (就像在 qmake 中):您只需定義 precomp 頭的名稱,它就會生成一個虛擬的 precomp.h.cpp 並使用它來觸發 pch 文件的生成。

在 VS/msbuild 中,也可以僅從 precomp.h 文件生成 pch(但需要更改源): - 將標頭的項目類型更改為“C/C++ compile” - 將 /Yc 選項設置為這 - 在 precomp.h 的末尾添加一個 hdrstop 指令,如

#pragma once
#include <windows.h>
#pragma hdrstop("precomp.h") 

感謝分享您的 bzl 文件,我也在研究這個(帶有 precomp 標頭的大型代碼庫)。

據我所知,預編譯頭文件對於框架開發人員進行大量模板元編程並擁有可觀的代碼庫特別有用。 如果您仍在開發框架,它不會加速編譯。 如果代碼設計不佳並且每個依賴項都按順序出現,它不會加快編譯時間。 你這里的文件只是 VC++ 的配置文件,實際工作還沒有開始,預編譯頭是字節碼。盡可能使用並行構建。

此外,由此產生的標題是巨大的!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM