From d8018c76b2a0e4017c1c56da870f0a470f769fd9 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Tue, 27 Oct 2020 16:40:24 -0700 Subject: [PATCH 01/12] bazel: Add pre-compiled clang header poc This adds a custom rule for producing clang pre-compiled headers. The hope is that by using these we reduce significant duplication in some larger headers that get recompiled by many dependees. This works by fetching the normal C++ configuration from bazel's crosstool, and adding custom flags to compile our pch in the right mode. This currently has a few limitations / rough edges: 1. It only supports clang 2. There's no way with the CcInfo provider to propagate compiler flags up the dependency tree. Because of this we instead add the required `-include-pch` flag to all macros. 3. Pch files are timestamp sensitive, this means if you add a include to the pch, build, then remove it and rebuild, it will currently fail. LLVM master has a flag to ignore this timestamp, but it isn't currently available in the release version of Xcode on macOS. 4. The logic to disable isn't implemented yet. Signed-off-by: Keith Smiley --- bazel/envoy_build_system.bzl | 2 + bazel/envoy_library.bzl | 5 +- bazel/envoy_pch.bzl | 46 +++++++++++++++ bazel/envoy_test.bzl | 11 ++-- bazel/pch.bzl | 109 +++++++++++++++++++++++++++++++++++ source/common/common/BUILD | 14 +++++ test/BUILD | 14 +++++ 7 files changed, 195 insertions(+), 6 deletions(-) create mode 100644 bazel/envoy_pch.bzl create mode 100644 bazel/pch.bzl diff --git a/bazel/envoy_build_system.bzl b/bazel/envoy_build_system.bzl index c312d1f6f2e5..a0463ab770d0 100644 --- a/bazel/envoy_build_system.bzl +++ b/bazel/envoy_build_system.bzl @@ -14,6 +14,7 @@ load( _envoy_cc_win32_library = "envoy_cc_win32_library", _envoy_proto_library = "envoy_proto_library", ) +load(":envoy_pch.bzl", _envoy_pch_library = "envoy_pch_library") load( ":envoy_select.bzl", _envoy_select_boringssl = "envoy_select_boringssl", @@ -225,6 +226,7 @@ envoy_cc_posix_library = _envoy_cc_posix_library envoy_cc_posix_without_linux_library = _envoy_cc_posix_without_linux_library envoy_cc_win32_library = _envoy_cc_win32_library envoy_proto_library = _envoy_proto_library +envoy_pch_library = _envoy_pch_library # Test wrappers (from envoy_test.bzl) envoy_cc_fuzz_test = _envoy_cc_fuzz_test diff --git a/bazel/envoy_library.bzl b/bazel/envoy_library.bzl index a6e77598f912..9b04b7ebe96c 100644 --- a/bazel/envoy_library.bzl +++ b/bazel/envoy_library.bzl @@ -8,6 +8,7 @@ load( "envoy_external_dep_path", "envoy_linkstatic", ) +load(":envoy_pch.bzl", "envoy_pch_copts") load("@envoy_api//bazel:api_build_system.bzl", "api_cc_py_proto_library") load( "@envoy_build_config//:extensions_build_config.bzl", @@ -92,17 +93,17 @@ def envoy_cc_library( name = name, srcs = srcs, hdrs = hdrs, - copts = envoy_copts(repository) + copts, + copts = envoy_copts(repository) + envoy_pch_copts(repository, "//source/common/common:common_pch") + copts, visibility = visibility, tags = tags, textual_hdrs = textual_hdrs, deps = deps + [envoy_external_dep_path(dep) for dep in external_deps] + [ repository + "//envoy/common:base_includes", repository + "//source/common/common:fmt_lib", + repository + "//source/common/common:common_pch", envoy_external_dep_path("abseil_flat_hash_map"), envoy_external_dep_path("abseil_flat_hash_set"), envoy_external_dep_path("abseil_strings"), - envoy_external_dep_path("spdlog"), envoy_external_dep_path("fmtlib"), ], alwayslink = 1, diff --git a/bazel/envoy_pch.bzl b/bazel/envoy_pch.bzl new file mode 100644 index 000000000000..89ab74a39ff8 --- /dev/null +++ b/bazel/envoy_pch.bzl @@ -0,0 +1,46 @@ +load("@rules_cc//cc:defs.bzl", "cc_library") + +# DO NOT LOAD THIS FILE. Load envoy_build_system.bzl instead. +# Envoy library targets +load( + ":envoy_internal.bzl", + "envoy_copts", + "envoy_external_dep_path", + "envoy_linkstatic", +) +load(":pch.bzl", "pch") + +def envoy_pch_copts(repository, target): + # TODO: enable only sometimes, same as below + return select({ + "//conditions:default": [ + "-include-pch", + "$(location {}{})".format(repository, target), + ], + }) + +def envoy_pch_library( + name, + includes, + deps, + external_deps, + visibility, + testonly = False, + repository = ""): + cc_library( + name = name + "_libs", + visibility = ["//visibility:private"], + copts = envoy_copts(repository), + deps = deps + [envoy_external_dep_path(dep) for dep in external_deps], + alwayslink = 1, + testonly = testonly, + linkstatic = envoy_linkstatic(), + ) + + pch( + name = name, + deps = [name + "_libs"], + includes = includes, + visibility = visibility, + testonly = testonly, + ) diff --git a/bazel/envoy_test.bzl b/bazel/envoy_test.bzl index 48f11942cf5a..4adee3a2b7b0 100644 --- a/bazel/envoy_test.bzl +++ b/bazel/envoy_test.bzl @@ -5,6 +5,7 @@ load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test") load("@rules_fuzzing//fuzzing:cc_defs.bzl", "fuzzing_decoration") load(":envoy_binary.bzl", "envoy_cc_binary") load(":envoy_library.bzl", "tcmalloc_external_deps") +load(":envoy_pch.bzl", "envoy_pch_copts") load( ":envoy_internal.bzl", "envoy_copts", @@ -37,10 +38,10 @@ def _envoy_cc_test_infrastructure_library( srcs = srcs, hdrs = hdrs, data = data, - copts = envoy_copts(repository, test = True) + copts, + copts = envoy_copts(repository, test = True) + copts + envoy_pch_copts(repository, "//test:test_pch"), testonly = 1, deps = deps + [envoy_external_dep_path(dep) for dep in external_deps] + [ - envoy_external_dep_path("googletest"), + repository + "//test:test_pch", ], tags = tags, include_prefix = include_prefix, @@ -115,6 +116,7 @@ def envoy_cc_fuzz_test( data = [corpus_name], # No fuzzing on macOS or Windows deps = select({ + # TODO remove for incompatible target skipping "@envoy//bazel:apple": [repository + "//test:dummy_main"], "@envoy//bazel:windows_x86_64": [repository + "//test:dummy_main"], "//conditions:default": [ @@ -159,11 +161,12 @@ def envoy_cc_test( name = name, srcs = srcs, data = data, - copts = envoy_copts(repository, test = True) + copts, + copts = envoy_copts(repository, test = True) + copts + envoy_pch_copts(repository, "//test:test_pch"), linkopts = _envoy_test_linkopts(), linkstatic = envoy_linkstatic(), malloc = tcmalloc_external_dep(repository), - deps = envoy_stdlib_deps() + deps + [envoy_external_dep_path(dep) for dep in external_deps + ["googletest"]] + [ + deps = envoy_stdlib_deps() + deps + [envoy_external_dep_path(dep) for dep in external_deps] + [ + repository + "//test:test_pch", repository + "//test:main", repository + "//test/test_common:test_version_linkstamp", ], diff --git a/bazel/pch.bzl b/bazel/pch.bzl new file mode 100644 index 000000000000..570470755fe8 --- /dev/null +++ b/bazel/pch.bzl @@ -0,0 +1,109 @@ +load( + "@bazel_tools//tools/build_defs/cc:action_names.bzl", + "CPP_COMPILE_ACTION_NAME", +) + +def _pch(ctx): + cc_toolchain = ctx.attr._cc_toolchain[cc_common.CcToolchainInfo] + 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 = CPP_COMPILE_ACTION_NAME, + ) + + deps_cc_info = cc_common.merge_cc_infos( + cc_infos = [dep[CcInfo] for dep in ctx.attr.deps], + ) + + # TODO: define somewhere to allow disables + enable_pch = True + if "clang" not in cc_compiler_path or not enable_pch: + return [deps_cc_info] + + generated_header_file = ctx.actions.declare_file(ctx.label.name + ".h") + ctx.actions.write( + generated_header_file, + "\n".join(["#include \"{}\"".format(include) for include in ctx.attr.includes]) + "\n", + ) + + pch_file = ctx.actions.declare_file(ctx.label.name + ".pch") + + # TODO: -fno-pch-timestamp / invalidation in that case doesn't work + pch_flags = ["-x", "c++-header"] + + 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, + source_file = generated_header_file.path, + output_file = pch_file.path, + preprocessor_defines = depset(deps_ctx.defines.to_list() + deps_ctx.local_defines.to_list()), + include_directories = deps_ctx.includes, + quote_include_directories = deps_ctx.quote_includes, + system_include_directories = deps_ctx.system_includes, + framework_include_directories = deps_ctx.framework_includes, + ) + + env = cc_common.get_environment_variables( + feature_configuration = feature_configuration, + action_name = CPP_COMPILE_ACTION_NAME, + variables = cc_compile_variables, + ) + + command_line = cc_common.get_memory_inefficient_command_line( + feature_configuration = feature_configuration, + action_name = CPP_COMPILE_ACTION_NAME, + variables = cc_compile_variables, + ) + + ctx.actions.run( + executable = cc_compiler_path, + arguments = command_line, + env = env, + inputs = depset( + items = [generated_header_file], + transitive = [cc_toolchain.all_files], + ), + outputs = [pch_file], + ) + + return [ + DefaultInfo(files = depset(items = [pch_file])), + cc_common.merge_cc_infos( + direct_cc_infos = [ + CcInfo( + compilation_context = cc_common.create_compilation_context( + includes = depset([pch_file.dirname]), + headers = depset([pch_file, generated_header_file]), + ), + ), + ], + cc_infos = [deps_cc_info], + ), + ] + +pch = rule( + attrs = dict( + includes = attr.string_list( + mandatory = True, + allow_empty = False, + ), + deps = attr.label_list( + mandatory = True, + allow_empty = False, + providers = [CcInfo], + ), + _cc_toolchain = attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), + ), + fragments = ["cpp"], + provides = [CcInfo], + toolchains = ["@bazel_tools//tools/cpp:toolchain_type"], + implementation = _pch, +) diff --git a/source/common/common/BUILD b/source/common/common/BUILD index 15067b74e8e0..c870a3c73313 100644 --- a/source/common/common/BUILD +++ b/source/common/common/BUILD @@ -6,6 +6,7 @@ load( "envoy_cc_posix_library", "envoy_cc_win32_library", "envoy_package", + "envoy_pch_library", ) licenses(["notice"]) # Apache 2 @@ -469,3 +470,16 @@ envoy_cc_library( "@com_google_absl//absl/status:statusor", ], ) + +envoy_pch_library( + name = "common_pch", + external_deps = [ + "spdlog", + ], + includes = [ + "spdlog/sinks/android_sink.h", + "spdlog/spdlog.h", + ], + visibility = ["//visibility:public"], + deps = [], +) diff --git a/test/BUILD b/test/BUILD index ab4a56a7d42e..6dcf77b890fe 100644 --- a/test/BUILD +++ b/test/BUILD @@ -2,6 +2,7 @@ load( "//bazel:envoy_build_system.bzl", "envoy_cc_test_library", "envoy_package", + "envoy_pch_library", ) licenses(["notice"]) # Apache 2 @@ -36,3 +37,16 @@ envoy_cc_test_library( "//test/test_common:printers_lib", ], ) + +envoy_pch_library( + name = "test_pch", + external_deps = [ + "googletest", + ], + includes = [ + "gmock/gmock.h", + "gtest/gtest.h", + ], + visibility = ["//test:__subpackages__"], + deps = [], +) From c8a49bd9c540156ce4bc5f2041c370e1ff983ab9 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Tue, 27 Oct 2020 17:08:24 -0700 Subject: [PATCH 02/12] Allow enabling / disabling Signed-off-by: Keith Smiley --- .bazelrc | 4 ++++ bazel/BUILD | 5 +++++ bazel/envoy_pch.bzl | 8 ++++++-- bazel/pch.bzl | 10 +++++++--- test/BUILD | 1 + 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.bazelrc b/.bazelrc index 21d0db6bb1a6..225246fbea40 100644 --- a/.bazelrc +++ b/.bazelrc @@ -46,6 +46,10 @@ build:sanitizer --test_tag_filters=-no_san build:clang --action_env=BAZEL_COMPILER=clang build:clang --linkopt=-fuse-ld=lld +# Flags for Clang + PCH +build:clang-pch --spawn_strategy=local +build:clang-pch --define=ENVOY_CLANG_PCH=1 + # Basic ASAN/UBSAN that works for gcc build:asan --action_env=ENVOY_ASAN=1 build:asan --config=sanitizer diff --git a/bazel/BUILD b/bazel/BUILD index 0dc6f348425a..3d65037c2792 100644 --- a/bazel/BUILD +++ b/bazel/BUILD @@ -169,6 +169,11 @@ config_setting( }, ) +config_setting( + name = "clang_pch_build", + values = {"define": "ENVOY_CLANG_PCH=1"}, +) + config_setting( name = "gcc_build_gcc", flag_values = { diff --git a/bazel/envoy_pch.bzl b/bazel/envoy_pch.bzl index 89ab74a39ff8..eb778da9aa14 100644 --- a/bazel/envoy_pch.bzl +++ b/bazel/envoy_pch.bzl @@ -11,12 +11,12 @@ load( load(":pch.bzl", "pch") def envoy_pch_copts(repository, target): - # TODO: enable only sometimes, same as below return select({ - "//conditions:default": [ + repository + "//bazel:clang_pch_build": [ "-include-pch", "$(location {}{})".format(repository, target), ], + "//conditions:default": [], }) def envoy_pch_library( @@ -43,4 +43,8 @@ def envoy_pch_library( includes = includes, visibility = visibility, testonly = testonly, + enabled = select({ + repository + "//bazel:clang_pch_build": True, + "//conditions:default": False, + }), ) diff --git a/bazel/pch.bzl b/bazel/pch.bzl index 570470755fe8..eba76c88a8fc 100644 --- a/bazel/pch.bzl +++ b/bazel/pch.bzl @@ -21,11 +21,12 @@ def _pch(ctx): cc_infos = [dep[CcInfo] for dep in ctx.attr.deps], ) - # TODO: define somewhere to allow disables - enable_pch = True - if "clang" not in cc_compiler_path or not enable_pch: + if not ctx.attr.enabled: return [deps_cc_info] + if "clang" not in cc_compiler_path: + fail("error: attempting to use clang PCH without clang: {}".format(cc_compiler_path)) + generated_header_file = ctx.actions.declare_file(ctx.label.name + ".h") ctx.actions.write( generated_header_file, @@ -100,6 +101,9 @@ pch = rule( allow_empty = False, providers = [CcInfo], ), + enabled = attr.bool( + mandatory = True, + ), _cc_toolchain = attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), ), fragments = ["cpp"], diff --git a/test/BUILD b/test/BUILD index 6dcf77b890fe..e139abfc6770 100644 --- a/test/BUILD +++ b/test/BUILD @@ -40,6 +40,7 @@ envoy_cc_test_library( envoy_pch_library( name = "test_pch", + testonly = True, external_deps = [ "googletest", ], From 0225dc9adcd76fe9edc25802183292785963b699 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Tue, 27 Oct 2020 17:11:59 -0700 Subject: [PATCH 03/12] Remove unrelated comment Signed-off-by: Keith Smiley --- bazel/envoy_test.bzl | 1 - 1 file changed, 1 deletion(-) diff --git a/bazel/envoy_test.bzl b/bazel/envoy_test.bzl index 4adee3a2b7b0..e636a9c8298e 100644 --- a/bazel/envoy_test.bzl +++ b/bazel/envoy_test.bzl @@ -116,7 +116,6 @@ def envoy_cc_fuzz_test( data = [corpus_name], # No fuzzing on macOS or Windows deps = select({ - # TODO remove for incompatible target skipping "@envoy//bazel:apple": [repository + "//test:dummy_main"], "@envoy//bazel:windows_x86_64": [repository + "//test:dummy_main"], "//conditions:default": [ From b575c292ea8734b5342e25945cc662e76678aa1f Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Tue, 27 Oct 2020 17:43:52 -0700 Subject: [PATCH 04/12] Move unnecessary work later Signed-off-by: Keith Smiley --- bazel/pch.bzl | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/bazel/pch.bzl b/bazel/pch.bzl index eba76c88a8fc..ec7ac75a5ee9 100644 --- a/bazel/pch.bzl +++ b/bazel/pch.bzl @@ -4,6 +4,13 @@ load( ) def _pch(ctx): + deps_cc_info = cc_common.merge_cc_infos( + cc_infos = [dep[CcInfo] for dep in ctx.attr.deps], + ) + + if not ctx.attr.enabled: + return [deps_cc_info] + cc_toolchain = ctx.attr._cc_toolchain[cc_common.CcToolchainInfo] feature_configuration = cc_common.configure_features( ctx = ctx, @@ -17,13 +24,6 @@ def _pch(ctx): action_name = CPP_COMPILE_ACTION_NAME, ) - deps_cc_info = cc_common.merge_cc_infos( - cc_infos = [dep[CcInfo] for dep in ctx.attr.deps], - ) - - if not ctx.attr.enabled: - return [deps_cc_info] - if "clang" not in cc_compiler_path: fail("error: attempting to use clang PCH without clang: {}".format(cc_compiler_path)) @@ -33,10 +33,9 @@ def _pch(ctx): "\n".join(["#include \"{}\"".format(include) for include in ctx.attr.includes]) + "\n", ) - pch_file = ctx.actions.declare_file(ctx.label.name + ".pch") - # TODO: -fno-pch-timestamp / invalidation in that case doesn't work pch_flags = ["-x", "c++-header"] + pch_file = ctx.actions.declare_file(ctx.label.name + ".pch") deps_ctx = deps_cc_info.compilation_context cc_compile_variables = cc_common.create_compile_variables( @@ -81,8 +80,7 @@ def _pch(ctx): direct_cc_infos = [ CcInfo( compilation_context = cc_common.create_compilation_context( - includes = depset([pch_file.dirname]), - headers = depset([pch_file, generated_header_file]), + headers = depset([generated_header_file]), ), ), ], From 8d1f918084bfd78bc97a751ea61b853867c774a4 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Fri, 30 Oct 2020 13:17:37 -0700 Subject: [PATCH 05/12] Fix rebuilds Removing this from headers stopped it from being built, but it worked locally since sandboxing was disabled. Signed-off-by: Keith Smiley --- bazel/envoy_pch.bzl | 1 + bazel/pch.bzl | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/bazel/envoy_pch.bzl b/bazel/envoy_pch.bzl index eb778da9aa14..81305b0d24b8 100644 --- a/bazel/envoy_pch.bzl +++ b/bazel/envoy_pch.bzl @@ -43,6 +43,7 @@ def envoy_pch_library( includes = includes, visibility = visibility, testonly = testonly, + tags = ["no-cache", "no-remote"], enabled = select({ repository + "//bazel:clang_pch_build": True, "//conditions:default": False, diff --git a/bazel/pch.bzl b/bazel/pch.bzl index ec7ac75a5ee9..dca1530c0a77 100644 --- a/bazel/pch.bzl +++ b/bazel/pch.bzl @@ -80,7 +80,7 @@ def _pch(ctx): direct_cc_infos = [ CcInfo( compilation_context = cc_common.create_compilation_context( - headers = depset([generated_header_file]), + headers = depset([pch_file, generated_header_file]), ), ), ], From 0b5e2e1611db6d932b9c2ee7cf28378a31670c3f Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Fri, 30 Oct 2020 13:18:44 -0700 Subject: [PATCH 06/12] Remove visibility of test pch This was overly aggressive. `testonly` being set should be good enough Signed-off-by: Keith Smiley --- test/BUILD | 1 - 1 file changed, 1 deletion(-) diff --git a/test/BUILD b/test/BUILD index e139abfc6770..c5ae51d58f3f 100644 --- a/test/BUILD +++ b/test/BUILD @@ -48,6 +48,5 @@ envoy_pch_library( "gmock/gmock.h", "gtest/gtest.h", ], - visibility = ["//test:__subpackages__"], deps = [], ) From 4edefd1134ec36a2fb9d0e17380d718ac15e560d Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Fri, 30 Oct 2020 14:47:35 -0700 Subject: [PATCH 07/12] Attempt to remove no-cache Signed-off-by: Keith Smiley --- bazel/envoy_pch.bzl | 2 +- test/BUILD | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/bazel/envoy_pch.bzl b/bazel/envoy_pch.bzl index 81305b0d24b8..8d1418596dd2 100644 --- a/bazel/envoy_pch.bzl +++ b/bazel/envoy_pch.bzl @@ -43,7 +43,7 @@ def envoy_pch_library( includes = includes, visibility = visibility, testonly = testonly, - tags = ["no-cache", "no-remote"], + tags = ["no-remote"], enabled = select({ repository + "//bazel:clang_pch_build": True, "//conditions:default": False, diff --git a/test/BUILD b/test/BUILD index c5ae51d58f3f..31561f058237 100644 --- a/test/BUILD +++ b/test/BUILD @@ -48,5 +48,6 @@ envoy_pch_library( "gmock/gmock.h", "gtest/gtest.h", ], + visibility = ["//visibililty:public"], deps = [], ) From 702210be8070324a3a83daa4bfaea42e3f59a0d7 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Fri, 4 Dec 2020 09:33:47 -0800 Subject: [PATCH 08/12] Remove pch dep from all envoy_cc_mocks Signed-off-by: Keith Smiley --- bazel/envoy_test.bzl | 10 ++++++---- test/BUILD | 6 ++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/bazel/envoy_test.bzl b/bazel/envoy_test.bzl index e636a9c8298e..ac5c97bc54ba 100644 --- a/bazel/envoy_test.bzl +++ b/bazel/envoy_test.bzl @@ -30,9 +30,13 @@ def _envoy_cc_test_infrastructure_library( include_prefix = None, copts = [], alwayslink = 1, + exclude_pch_dep = False, **kargs): # Add implicit tcmalloc external dependency(if available) in order to enable CPU and heap profiling in tests. deps += tcmalloc_external_deps(repository) + pch_deps = [] + if not exclude_pch_dep: + pch_deps = [repository + "//test:test_pch"] cc_library( name = name, srcs = srcs, @@ -40,9 +44,7 @@ def _envoy_cc_test_infrastructure_library( data = data, copts = envoy_copts(repository, test = True) + copts + envoy_pch_copts(repository, "//test:test_pch"), testonly = 1, - deps = deps + [envoy_external_dep_path(dep) for dep in external_deps] + [ - repository + "//test:test_pch", - ], + deps = deps + [envoy_external_dep_path(dep) for dep in external_deps] + pch_deps, tags = tags, include_prefix = include_prefix, alwayslink = alwayslink, @@ -285,7 +287,7 @@ def envoy_py_test( # Envoy C++ mock targets should be specified with this function. def envoy_cc_mock(name, **kargs): - envoy_cc_test_library(name = name, **kargs) + envoy_cc_test_library(name = name, exclude_pch_dep = True, **kargs) # Envoy shell tests that need to be included in coverage run should be specified with this function. def envoy_sh_test( diff --git a/test/BUILD b/test/BUILD index 31561f058237..270a56ff44b1 100644 --- a/test/BUILD +++ b/test/BUILD @@ -48,6 +48,8 @@ envoy_pch_library( "gmock/gmock.h", "gtest/gtest.h", ], - visibility = ["//visibililty:public"], - deps = [], + visibility = ["//visibility:public"], + deps = [ + "//test/mocks/access_log:access_log_mocks", + ], ) From bf2b2f5d8819d34e9224e2c1d17aa0ffb88143a1 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Tue, 26 Jan 2021 14:09:42 -0800 Subject: [PATCH 09/12] Update pch with fixes Signed-off-by: Keith Smiley --- bazel/envoy_test.bzl | 9 +++++++-- bazel/pch.bzl | 5 ++++- source/common/common/BUILD | 16 +++++++++++++++- test/BUILD | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/bazel/envoy_test.bzl b/bazel/envoy_test.bzl index ac5c97bc54ba..d9540bfb0336 100644 --- a/bazel/envoy_test.bzl +++ b/bazel/envoy_test.bzl @@ -35,14 +35,17 @@ def _envoy_cc_test_infrastructure_library( # Add implicit tcmalloc external dependency(if available) in order to enable CPU and heap profiling in tests. deps += tcmalloc_external_deps(repository) pch_deps = [] + pch_copts = [] if not exclude_pch_dep: pch_deps = [repository + "//test:test_pch"] + pch_copts = envoy_pch_copts(repository, "//test:test_pch") + cc_library( name = name, srcs = srcs, hdrs = hdrs, data = data, - copts = envoy_copts(repository, test = True) + copts + envoy_pch_copts(repository, "//test:test_pch"), + copts = envoy_copts(repository, test = True) + copts + pch_copts, testonly = 1, deps = deps + [envoy_external_dep_path(dep) for dep in external_deps] + pch_deps, tags = tags, @@ -166,7 +169,7 @@ def envoy_cc_test( linkopts = _envoy_test_linkopts(), linkstatic = envoy_linkstatic(), malloc = tcmalloc_external_dep(repository), - deps = envoy_stdlib_deps() + deps + [envoy_external_dep_path(dep) for dep in external_deps] + [ + deps = envoy_stdlib_deps() + deps + [envoy_external_dep_path(dep) for dep in external_deps + ["googletest"]] + [ repository + "//test:test_pch", repository + "//test:main", repository + "//test/test_common:test_version_linkstamp", @@ -196,6 +199,7 @@ def envoy_cc_test_library( copts = [], alwayslink = 1, **kargs): + exclude_pch_dep = kargs.pop("exclude_pch_dep", True) _envoy_cc_test_infrastructure_library( name, srcs, @@ -209,6 +213,7 @@ def envoy_cc_test_library( copts, visibility = ["//visibility:public"], alwayslink = alwayslink, + exclude_pch_dep = exclude_pch_dep, **kargs ) diff --git a/bazel/pch.bzl b/bazel/pch.bzl index dca1530c0a77..da5fce914408 100644 --- a/bazel/pch.bzl +++ b/bazel/pch.bzl @@ -63,13 +63,16 @@ def _pch(ctx): variables = cc_compile_variables, ) + transitive_headers = [] + for dep in ctx.attr.deps: + transitive_headers.append(dep[CcInfo].compilation_context.headers) ctx.actions.run( executable = cc_compiler_path, arguments = command_line, env = env, inputs = depset( items = [generated_header_file], - transitive = [cc_toolchain.all_files], + transitive = [cc_toolchain.all_files] + transitive_headers, ), outputs = [pch_file], ) diff --git a/source/common/common/BUILD b/source/common/common/BUILD index c870a3c73313..6fd4fd29cce6 100644 --- a/source/common/common/BUILD +++ b/source/common/common/BUILD @@ -477,9 +477,23 @@ envoy_pch_library( "spdlog", ], includes = [ + "envoy/config/bootstrap/v3/bootstrap.pb.h", + "envoy/config/cluster/v3/cluster.pb.h", + "envoy/config/core/v3/base.pb.h", + "envoy/config/core/v3/config_source.pb.h", + "envoy/config/route/v3/route_components.pb.h", + "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h", + "envoy/service/discovery/v3/discovery.pb.h", "spdlog/sinks/android_sink.h", "spdlog/spdlog.h", ], visibility = ["//visibility:public"], - deps = [], + deps = [ + "@envoy_api//envoy/config/bootstrap/v3:pkg_cc_proto", + "@envoy_api//envoy/config/cluster/v3:pkg_cc_proto", + "@envoy_api//envoy/config/core/v3:pkg_cc_proto", + "@envoy_api//envoy/config/route/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/filters/network/http_connection_manager/v3:pkg_cc_proto", + "@envoy_api//envoy/service/discovery/v3:pkg_cc_proto", + ], ) diff --git a/test/BUILD b/test/BUILD index 270a56ff44b1..572e27eb653f 100644 --- a/test/BUILD +++ b/test/BUILD @@ -43,13 +43,46 @@ envoy_pch_library( testonly = True, external_deps = [ "googletest", + "spdlog", ], includes = [ + "envoy/config/bootstrap/v3/bootstrap.pb.h", + "envoy/config/cluster/v3/cluster.pb.h", + "envoy/config/core/v3/base.pb.h", + "envoy/config/core/v3/config_source.pb.h", + "envoy/config/route/v3/route_components.pb.h", + "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h", + "envoy/service/discovery/v3/discovery.pb.h", "gmock/gmock.h", "gtest/gtest.h", + "spdlog/sinks/android_sink.h", + "spdlog/spdlog.h", + "test/mocks/access_log/mocks.h", + "test/mocks/common.h", + "test/mocks/event/mocks.h", + "test/mocks/http/mocks.h", + "test/mocks/network/mocks.h", + "test/mocks/runtime/mocks.h", + "test/mocks/server/factory_context.h", + "test/mocks/server/instance.h", + "test/mocks/stats/mocks.h", ], visibility = ["//visibility:public"], deps = [ + "//test/mocks:common_lib", "//test/mocks/access_log:access_log_mocks", + "//test/mocks/event:event_mocks", + "//test/mocks/http:http_mocks", + "//test/mocks/network:network_mocks", + "//test/mocks/runtime:runtime_mocks", + "//test/mocks/server:factory_context_mocks", + "//test/mocks/server:instance_mocks", + "//test/mocks/stats:stats_mocks", + "@envoy_api//envoy/config/bootstrap/v3:pkg_cc_proto", + "@envoy_api//envoy/config/cluster/v3:pkg_cc_proto", + "@envoy_api//envoy/config/core/v3:pkg_cc_proto", + "@envoy_api//envoy/config/route/v3:pkg_cc_proto", + "@envoy_api//envoy/extensions/filters/network/http_connection_manager/v3:pkg_cc_proto", + "@envoy_api//envoy/service/discovery/v3:pkg_cc_proto", ], ) From 6222c18c360d2a4a7d05d48298c4533f4005bf2b Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Tue, 26 Jan 2021 14:18:33 -0800 Subject: [PATCH 10/12] Ignore envoy_pch_library deps formatting Signed-off-by: Keith Smiley --- tools/code_format/envoy_build_fixer.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/code_format/envoy_build_fixer.py b/tools/code_format/envoy_build_fixer.py index 378a1af63fdd..2c6654888c15 100755 --- a/tools/code_format/envoy_build_fixer.py +++ b/tools/code_format/envoy_build_fixer.py @@ -150,6 +150,8 @@ def fix_api_deps(path, contents): kind, name, srcs, hdrs, deps = match.groups() if not name: continue + if kind == "envoy_pch_library": + continue source_paths = [] if srcs != 'missing': source_paths.extend( From b8cb37a3e8d33b9a035ad5de896c4a36cc7e8a00 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Fri, 29 Jan 2021 10:45:56 -0800 Subject: [PATCH 11/12] Add googletest back when pch is disabled Signed-off-by: Keith Smiley --- bazel/envoy_test.bzl | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/bazel/envoy_test.bzl b/bazel/envoy_test.bzl index d9540bfb0336..f74eda3e8393 100644 --- a/bazel/envoy_test.bzl +++ b/bazel/envoy_test.bzl @@ -30,14 +30,16 @@ def _envoy_cc_test_infrastructure_library( include_prefix = None, copts = [], alwayslink = 1, - exclude_pch_dep = False, + disable_pch = False, **kargs): # Add implicit tcmalloc external dependency(if available) in order to enable CPU and heap profiling in tests. deps += tcmalloc_external_deps(repository) - pch_deps = [] + extra_deps = [] pch_copts = [] - if not exclude_pch_dep: - pch_deps = [repository + "//test:test_pch"] + if disable_pch: + extra_deps = [envoy_external_dep_path("googletest")] + else: + extra_deps = [repository + "//test:test_pch"] pch_copts = envoy_pch_copts(repository, "//test:test_pch") cc_library( @@ -47,7 +49,7 @@ def _envoy_cc_test_infrastructure_library( data = data, copts = envoy_copts(repository, test = True) + copts + pch_copts, testonly = 1, - deps = deps + [envoy_external_dep_path(dep) for dep in external_deps] + pch_deps, + deps = deps + [envoy_external_dep_path(dep) for dep in external_deps] + extra_deps, tags = tags, include_prefix = include_prefix, alwayslink = alwayslink, @@ -199,7 +201,7 @@ def envoy_cc_test_library( copts = [], alwayslink = 1, **kargs): - exclude_pch_dep = kargs.pop("exclude_pch_dep", True) + disable_pch = kargs.pop("disable_pch", True) _envoy_cc_test_infrastructure_library( name, srcs, @@ -213,7 +215,7 @@ def envoy_cc_test_library( copts, visibility = ["//visibility:public"], alwayslink = alwayslink, - exclude_pch_dep = exclude_pch_dep, + disable_pch = disable_pch, **kargs ) @@ -292,7 +294,7 @@ def envoy_py_test( # Envoy C++ mock targets should be specified with this function. def envoy_cc_mock(name, **kargs): - envoy_cc_test_library(name = name, exclude_pch_dep = True, **kargs) + envoy_cc_test_library(name = name, disable_pch = True, **kargs) # Envoy shell tests that need to be included in coverage run should be specified with this function. def envoy_sh_test( From 93555c9e90ef5eb6bf768c325c421eadcc206bcd Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Fri, 4 Jun 2021 15:24:18 -0700 Subject: [PATCH 12/12] Update envoy_build_fixer.py Signed-off-by: Keith Smiley --- tools/code_format/envoy_build_fixer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/code_format/envoy_build_fixer.py b/tools/code_format/envoy_build_fixer.py index 2c6654888c15..cfdbf59d24a9 100755 --- a/tools/code_format/envoy_build_fixer.py +++ b/tools/code_format/envoy_build_fixer.py @@ -151,7 +151,7 @@ def fix_api_deps(path, contents): if not name: continue if kind == "envoy_pch_library": - continue + continue source_paths = [] if srcs != 'missing': source_paths.extend(