Skip to content

Commit

Permalink
Add include_prefix and strip_include_prefix to cc_common.compile
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Googler authored and Copybara-Service committed Aug 10, 2020
1 parent 4984d0a commit b37c51c
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public Tuple<Object> compile(
Sequence<?> frameworkIncludes, // <String> expected
Sequence<?> defines, // <String> expected
Sequence<?> localDefines, // <String> expected
String includePrefix,
String stripIncludePrefix,
Sequence<?> userCompileFlags, // <String> expected
Sequence<?> ccCompilationContexts, // <CcCompilationContext> expected
String name,
Expand All @@ -101,6 +103,8 @@ public Tuple<Object> compile(
frameworkIncludes,
defines,
localDefines,
includePrefix,
stripIncludePrefix,
userCompileFlags,
ccCompilationContexts,
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,8 @@ protected Tuple<Object> compile(
Sequence<?> frameworkIncludes, // <String> expected
Sequence<?> defines, // <String> expected
Sequence<?> localDefines, // <String> expected
String includePrefix,
String stripIncludePrefix,
Sequence<?> userCompileFlags, // <String> expected
Sequence<?> ccCompilationContexts, // <CcCompilationContext> expected
String name,
Expand Down Expand Up @@ -1829,6 +1831,12 @@ protected Tuple<Object> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -229,6 +254,8 @@ Tuple<Object> compile(
Sequence<?> frameworkIncludes, // <String> expected
Sequence<?> defines, // <String> expected
Sequence<?> localDefines, // <String> expected
String includePrefix,
String stripIncludePrefix,
Sequence<?> userCompileFlags, // <String> expected
Sequence<?> ccCompilationContexts, // <CompilationContextT> expected
String name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,12 @@ public Tuple<Object> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit b37c51c

Please sign in to comment.