erenon / rules_pch

Precompiled header rule for Bazel

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Precompiled Header (PCH) support for Bazel + GCC

Requires GCC and Bazel 3.1. Clang support needs slight modification.

Usage

  • Copy precompiled_headers.bzl to your workspace

  • Create a header file that is going to be the first include in your source files, e.g: Common.hpp or stdafx.h

  • Add common includes to this main header file - these are going to be precompiled

  • Add a precompiled_headers rule, add cc_library dependencies that provide the header files included by the main header file:

     load("//:precompiled_headers.bzl", "precompiled_headers")
    
     precompiled_headers(
       name = "pch",
       main = "Common.hpp",
       deps = [":libA", ":libB"],
     )
    
  • Depend on the created target, copts must include -fpch-deps:

     cc_binary(
       name = "app",
       copts = ["-Winvalid-pch", "-Werror", "-fpch-deps"],
       srcs = ["app.cpp"],
       deps = [":pch"],
     )
    

If -pch-deps is not specified, bazel will not rebuild app if pch changes. This is a GCC bug, see:

Test

$ bazel build test/...

License

Public domain.

About

Precompiled header rule for Bazel


Languages

Language:Starlark 87.3%Language:C++ 12.7%