From 23ecb1bfa44c01ccee346c0e9278d71be2302de1 Mon Sep 17 00:00:00 2001 From: Googler Date: Mon, 10 Aug 2020 11:37:45 -0700 Subject: [PATCH] Add include_prefix and strip_include_prefix to cc_common.compile This brings cc_common.compile a bit closer to parity with cc_library. Fixes #11856 RELNOTES[NEW]: cc_common.compile support for include_prefix/strip_include_prefix PiperOrigin-RevId: 325850902 --- .../lib/bazel/rules/cpp/BazelCcModule.java | 4 ++ .../build/lib/rules/cpp/CcModule.java | 8 +++ .../cpp/BazelCcModuleApi.java | 27 ++++++++ .../skydoc/fakebuildapi/cpp/FakeCcModule.java | 6 +- .../lib/rules/cpp/StarlarkCcCommonTest.java | 62 +++++++++++++++++++ 5 files changed, 105 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcModule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcModule.java index e55c5442635105..893af100d801bb 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcModule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcModule.java @@ -80,6 +80,8 @@ public Tuple compile( Sequence frameworkIncludes, // expected Sequence defines, // expected Sequence localDefines, // expected + String includePrefix, + String stripIncludePrefix, Sequence userCompileFlags, // expected Sequence ccCompilationContexts, // expected String name, @@ -101,6 +103,8 @@ public Tuple compile( frameworkIncludes, defines, localDefines, + includePrefix, + stripIncludePrefix, userCompileFlags, ccCompilationContexts, name, diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java index 46dfabcf2e7ddc..40d115a0f5f3ac 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java @@ -1725,6 +1725,8 @@ protected Tuple compile( Sequence frameworkIncludes, // expected Sequence defines, // expected Sequence localDefines, // expected + String includePrefix, + String stripIncludePrefix, Sequence userCompileFlags, // expected Sequence ccCompilationContexts, // expected String name, @@ -1829,6 +1831,12 @@ protected Tuple compile( helper.setGeneratePicAction(false); helper.setGenerateNoPicAction(true); } + if (!Strings.isNullOrEmpty(includePrefix)) { + helper.setIncludePrefix(includePrefix); + } + if (!Strings.isNullOrEmpty(stripIncludePrefix)) { + helper.setStripIncludePrefix(stripIncludePrefix); + } try { CompilationInfo compilationInfo = helper.compile(); return Tuple.of( diff --git a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/BazelCcModuleApi.java b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/BazelCcModuleApi.java index 5e3c210386a9ef..f18321f22b8305 100644 --- a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/BazelCcModuleApi.java +++ b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/BazelCcModuleApi.java @@ -172,6 +172,31 @@ public interface BazelCcModuleApi< named = true, defaultValue = "[]", type = Sequence.class), + @Param( + name = "include_prefix", + doc = + "The prefix to add to the paths of the headers of this rule. When set, the " + + "headers in the hdrs attribute of this rule are accessible at is the " + + "value of this attribute prepended to their repository-relative path. " + + "The prefix in the strip_include_prefix attribute is removed before this " + + "prefix is added.", + positional = false, + named = true, + defaultValue = "''", + type = String.class), + @Param( + name = "strip_include_prefix", + doc = + "The prefix to strip from the paths of the headers of this rule. When set, the" + + " headers in the hdrs attribute of this rule are accessible at their path" + + " with this prefix cut off. If it's a relative path, it's taken as a" + + " package-relative one. If it's an absolute one, it's understood as a" + + " repository-relative path. The prefix in the include_prefix attribute is" + + " added after this prefix is stripped.", + positional = false, + named = true, + defaultValue = "''", + type = String.class), @Param( name = "user_compile_flags", doc = "Additional list of compilation options.", @@ -229,6 +254,8 @@ Tuple compile( Sequence frameworkIncludes, // expected Sequence defines, // expected Sequence localDefines, // expected + String includePrefix, + String stripIncludePrefix, Sequence userCompileFlags, // expected Sequence ccCompilationContexts, // expected String name, diff --git a/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/cpp/FakeCcModule.java b/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/cpp/FakeCcModule.java index cf603536d44f62..917a80f6b9d3fc 100644 --- a/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/cpp/FakeCcModule.java +++ b/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/cpp/FakeCcModule.java @@ -241,10 +241,12 @@ public Tuple compile( Sequence privateHeaders, Sequence includes, Sequence quoteIncludes, - Sequence defines, - Sequence localDefines, Sequence systemIncludes, Sequence frameworkIncludes, + Sequence defines, + Sequence localDefines, + String includePrefix, + String stripIncludePrefix, Sequence userCompileFlags, Sequence ccCompilationContexts, String name, diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/StarlarkCcCommonTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/StarlarkCcCommonTest.java index 780662f123aa1a..c16167b41a1d8c 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/StarlarkCcCommonTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/StarlarkCcCommonTest.java @@ -5520,6 +5520,68 @@ public void testLocalDefines() throws Exception { assertThat(action.getArguments()).containsAtLeast("-DDEFINE1", "-DDEFINE2"); } + @Test + public void testIncludePrefix() throws Exception { + createFilesForTestingCompilation( + scratch, "third_party/tools/build_defs/foo", "include_prefix='prefix'"); + scratch.file( + "bar/BUILD", + "load('//third_party/tools/build_defs/foo:extension.bzl', 'cc_starlark_library')", + "cc_starlark_library(", + " name = 'starlark_lib',", + " srcs = ['starlark_lib.cc'],", + " public_hdrs = ['starlark_lib.h'],", + " private_hdrs = ['private_starlark_lib.h'],", + ")"); + ConfiguredTarget target = getConfiguredTarget("//bar:starlark_lib"); + assertThat(target).isNotNull(); + CcInfo ccInfo = target.get(CcInfo.PROVIDER); + assertThat(artifactsToStrings(ccInfo.getCcCompilationContext().getDirectPublicHdrs())) + .contains("bin bar/_virtual_includes/starlark_lib/prefix/starlark_lib.h"); + } + + @Test + public void testStripIncludePrefix() throws Exception { + createFilesForTestingCompilation( + scratch, "third_party/tools/build_defs/foo", "strip_include_prefix='v1'"); + scratch.file( + "bar/BUILD", + "load('//third_party/tools/build_defs/foo:extension.bzl', 'cc_starlark_library')", + "cc_starlark_library(", + " name = 'starlark_lib',", + " srcs = ['starlark_lib.cc'],", + " public_hdrs = ['v1/starlark_lib.h'],", + " private_hdrs = ['v1/private_starlark_lib.h'],", + ")"); + ConfiguredTarget target = getConfiguredTarget("//bar:starlark_lib"); + assertThat(target).isNotNull(); + CcInfo ccInfo = target.get(CcInfo.PROVIDER); + assertThat(artifactsToStrings(ccInfo.getCcCompilationContext().getDirectPublicHdrs())) + .contains("bin bar/_virtual_includes/starlark_lib/starlark_lib.h"); + } + + @Test + public void testStripIncludePrefixAndIncludePrefix() throws Exception { + createFilesForTestingCompilation( + scratch, + "third_party/tools/build_defs/foo", + "strip_include_prefix='v1', include_prefix='prefix'"); + scratch.file( + "bar/BUILD", + "load('//third_party/tools/build_defs/foo:extension.bzl', 'cc_starlark_library')", + "cc_starlark_library(", + " name = 'starlark_lib',", + " srcs = ['starlark_lib.cc'],", + " public_hdrs = ['v1/starlark_lib.h'],", + " private_hdrs = ['v1/private_starlark_lib.h'],", + ")"); + ConfiguredTarget target = getConfiguredTarget("//bar:starlark_lib"); + assertThat(target).isNotNull(); + CcInfo ccInfo = target.get(CcInfo.PROVIDER); + assertThat(artifactsToStrings(ccInfo.getCcCompilationContext().getDirectPublicHdrs())) + .contains("bin bar/_virtual_includes/starlark_lib/prefix/starlark_lib.h"); + } + @Test public void testHeaders() throws Exception { createFilesForTestingCompilation(