Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: objc include_prefix/strip_include_prefix #5954

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,17 @@ public String getPurpose() {
* of all of its attributes.
*/
public Builder mergeDependentCcCompilationContext(
CcCompilationContext otherCcCompilationContext) {
CcCompilationContext otherCcCompilationContext, boolean mergeDelcaredIncludeSrcs) {
Preconditions.checkNotNull(otherCcCompilationContext);
compilationPrerequisites.addAll(
otherCcCompilationContext.getTransitiveCompilationPrerequisites());
includeDirs.addAll(otherCcCompilationContext.getIncludeDirs());
quoteIncludeDirs.addAll(otherCcCompilationContext.getQuoteIncludeDirs());
systemIncludeDirs.addAll(otherCcCompilationContext.getSystemIncludeDirs());
declaredIncludeDirs.addTransitive(otherCcCompilationContext.getDeclaredIncludeDirs());
declaredIncludeSrcs.addTransitive(otherCcCompilationContext.getDeclaredIncludeSrcs());
if (mergeDelcaredIncludeSrcs) {
declaredIncludeSrcs.addTransitive(otherCcCompilationContext.getDeclaredIncludeSrcs());
}
transitiveHeaderInfo.addTransitive(otherCcCompilationContext.transitiveHeaderInfos);
transitiveModules.addTransitive(otherCcCompilationContext.transitiveModules);
if (otherCcCompilationContext.headerInfo.headerModule != null) {
Expand Down Expand Up @@ -439,7 +441,7 @@ public Builder mergeDependentCcCompilationContext(
*/
public Builder mergeDependentCcCompilationContexts(Iterable<CcCompilationContext> targets) {
for (CcCompilationContext target : targets) {
mergeDependentCcCompilationContext(target);
mergeDependentCcCompilationContext(target, /* mergeDeclaredIncludeSrcs= */ true);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ private CcCompilationHelper addPrivateHeader(Artifact privateHeader, Label label
compilationUnitSources.put(
privateHeader, CppSource.create(privateHeader, label, CppSource.Type.HEADER));
}

this.privateHeaders.add(privateHeader);
return this;
}
Expand Down Expand Up @@ -2098,12 +2098,13 @@ private static void mergeToolchainDependentCcCompilationContext(
"Unable to merge the STL '" + stl.getLabel() + "' and toolchain contexts");
return;
}
ccCompilationContextBuilder.mergeDependentCcCompilationContext(ccCompilationContext);
ccCompilationContextBuilder.mergeDependentCcCompilationContext(ccCompilationContext,
/* mergeDeclaredIncludeSrcs= */ true);
}
}
if (toolchain != null) {
ccCompilationContextBuilder.mergeDependentCcCompilationContext(
toolchain.getCcCompilationContext());
toolchain.getCcCompilationContext(), /* mergeDeclaredIncludeSrcs= */ true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ static class Builder {
private final NestedSetBuilder<SdkFramework> weakSdkFrameworks = NestedSetBuilder.stableOrder();
private final NestedSetBuilder<String> sdkDylibs = NestedSetBuilder.stableOrder();
private Optional<PathFragment> packageFragment = Optional.absent();
private String includePrefix = null;
private String stripIncludePrefix = null;
private boolean enableModules;

/**
Expand Down Expand Up @@ -173,6 +175,22 @@ public Builder enableModules() {
return this;
}

/**
* Sets the include_prefix attribute.
*/
public Builder setIncludePrefix(String includePrefix) {
this.includePrefix = includePrefix;
return this;
}

/**
* Sets the strip_include_prefix attribute.
*/
public Builder setStripIncludePrefix(String stripIncludePrefix) {
this.stripIncludePrefix = stripIncludePrefix;
return this;
}

/**
* Builds a {@code CompilationAttributes} object.
*/
Expand All @@ -189,6 +207,8 @@ public CompilationAttributes build() {
this.copts.build(),
this.linkopts.build(),
this.moduleMapsForDirectDeps.build(),
this.includePrefix,
this.stripIncludePrefix,
this.enableModules);
}

Expand Down Expand Up @@ -224,6 +244,18 @@ private static void addIncludesFromRuleContext(Builder builder, RuleContext rule
PathFragment::create));
builder.addSdkIncludes(sdkIncludes.build());
}

if (ruleContext.getRule().isAttrDefined("include_prefix", Type.STRING)
&& ruleContext.attributes().isAttributeValueExplicitlySpecified("include_prefix")) {
builder.setIncludePrefix(
ruleContext.attributes().get("include_prefix", Type.STRING));
}

if (ruleContext.getRule().isAttrDefined("strip_include_prefix", Type.STRING)
&& ruleContext.attributes().isAttributeValueExplicitlySpecified("strip_include_prefix")) {
builder.setStripIncludePrefix(
ruleContext.attributes().get("strip_include_prefix", Type.STRING));
}
}

private static void addSdkAttributesFromRuleContext(Builder builder, RuleContext ruleContext) {
Expand Down Expand Up @@ -302,6 +334,8 @@ private static void addModuleOptionsFromRuleContext(Builder builder, RuleContext
private final ImmutableList<String> copts;
private final ImmutableList<String> linkopts;
private final NestedSet<CppModuleMap> moduleMapsForDirectDeps;
private final String includePrefix;
private final String stripIncludePrefix;
private final boolean enableModules;

private CompilationAttributes(
Expand All @@ -316,6 +350,8 @@ private CompilationAttributes(
ImmutableList<String> copts,
ImmutableList<String> linkopts,
NestedSet<CppModuleMap> moduleMapsForDirectDeps,
String includePrefix,
String stripIncludePrefix,
boolean enableModules) {
this.hdrs = hdrs;
this.textualHdrs = textualHdrs;
Expand All @@ -328,6 +364,8 @@ private CompilationAttributes(
this.copts = copts;
this.linkopts = linkopts;
this.moduleMapsForDirectDeps = moduleMapsForDirectDeps;
this.includePrefix = includePrefix;
this.stripIncludePrefix = stripIncludePrefix;
this.enableModules = enableModules;
}

Expand Down Expand Up @@ -424,6 +462,16 @@ public NestedSet<CppModuleMap> moduleMapsForDirectDeps() {
return this.moduleMapsForDirectDeps;
}

/**
* Returns the include_prefix attribute
*/
public String getIncludePrefix() { return this.includePrefix; }

/**
* Returns the strip_include_prefix attribute
*/
public String getStripIncludePrefix() { return this.stripIncludePrefix; }

/**
* Returns whether this target uses language features that require clang modules, such as
* {@literal @}import.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ private CompilationInfo compile(
Collection<Artifact> privateHdrs,
Collection<Artifact> publicHdrs,
Artifact pchHdr,
String includePrefix,
String stripIncludePrefix,
// TODO(b/70777494): Find out how deps get used and remove if not needed.
Iterable<? extends TransitiveInfoCollection> deps,
ObjcCppSemantics semantics,
Expand Down Expand Up @@ -341,6 +343,8 @@ private CompilationInfo compile(
.getCoptsForCompilationMode())
.addAll(extraCompileArgs)
.build())
.setIncludePrefix(includePrefix)
.setStripIncludePrefix(stripIncludePrefix)
.addIncludeDirs(priorityHeaders)
.addIncludeDirs(objcProvider.get(INCLUDE))
.addSystemIncludeDirs(objcProvider.get(INCLUDE_SYSTEM))
Expand Down Expand Up @@ -405,6 +409,8 @@ private CompilationInfo compile(
privateHdrs,
publicHdrs,
pchHdr,
attributes.getIncludePrefix(),
attributes.getStripIncludePrefix(),
deps,
semantics,
purpose);
Expand All @@ -424,6 +430,8 @@ private CompilationInfo compile(
privateHdrs,
publicHdrs,
pchHdr,
attributes.getIncludePrefix(),
attributes.getStripIncludePrefix(),
deps,
semantics,
purpose);
Expand Down Expand Up @@ -464,8 +472,10 @@ private CompilationInfo compile(
compilationOutputsBuilder.merge(objcArcCompilationInfo.getCcCompilationOutputs());
compilationOutputsBuilder.merge(nonObjcArcCompilationInfo.getCcCompilationOutputs());

CcCompilationContext mergedCcCompilationContext = ccCompilationContextBuilder.build();
LinkingInfo linkingInfo =
resultLink.link(compilationOutputsBuilder.build(), ccCompilationContextBuilder.build());
resultLink.link(compilationOutputsBuilder.build(), mergedCcCompilationContext);
ccCompilationContext = mergedCcCompilationContext;

Map<String, NestedSet<Artifact>> mergedOutputGroups =
CcCommon.mergeOutputGroups(
Expand Down Expand Up @@ -690,6 +700,7 @@ static Iterable<String> commonFrameworkNames(
private final Map<String, NestedSet<Artifact>> outputGroupCollector;
private final ImmutableList.Builder<Artifact> objectFilesCollector;
private final CcToolchainProvider toolchain;
private CcCompilationContext ccCompilationContext;
private final boolean isTestRule;
private final boolean usePch;

Expand Down Expand Up @@ -743,6 +754,8 @@ private CompilationSupport(
}
}

public CcCompilationContext getCcCompilationContext() { return this.ccCompilationContext; }

/** Builder for {@link CompilationSupport} */
public static class Builder {
private RuleContext ruleContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.devtools.build.lib.rules.cpp.LinkerInputs.LibraryToLink;
import com.google.devtools.build.lib.rules.objc.ObjcCommon.ResourceAttributes;
import com.google.devtools.build.lib.syntax.Type;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import java.util.Map;
import java.util.TreeMap;
Expand Down Expand Up @@ -96,6 +97,8 @@ public ConfiguredTarget create(RuleContext ruleContext)
J2ObjcEntryClassProvider j2ObjcEntryClassProvider = new J2ObjcEntryClassProvider.Builder()
.addTransitive(ruleContext.getPrerequisites("deps", Mode.TARGET,
J2ObjcEntryClassProvider.class)).build();
CcCompilationContext mergedCcCompilationContext = compilationSupport.getCcCompilationContext();

CcCompilationContext ccCompilationContext =
new CcCompilationContext.Builder(ruleContext)
.addDeclaredIncludeSrcs(
Expand All @@ -105,9 +108,11 @@ public ConfiguredTarget create(RuleContext ruleContext)
.toCollection())
.addTextualHdrs(common.getTextualHdrs())
.addDeclaredIncludeSrcs(common.getTextualHdrs())
.mergeDependentCcCompilationContext(mergedCcCompilationContext,
/* mergeDeclaredIncludeSrcs= */ false)
.build();

CcCompilationInfo.Builder ccCompilationInfoBuilder = CcCompilationInfo.Builder.create();
CcCompilationInfo.Builder ccCompilationInfoBuilder = CcCompilationInfo.Builder.create();
ccCompilationInfoBuilder.setCcCompilationContext(ccCompilationContext);

CcLinkParams ccLinkParams = buildCcLinkParams(common);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,31 @@ Enables clang module support (via -fmodules).
provided module map to the compiler.
<!-- #END_BLAZE_RULE.ATTRIBUTE -->*/
.add(attr("module_map", LABEL).allowedFileTypes(FileType.of(".modulemap")))

/* <!-- #BLAZE_RULE($objc_compiling_rule).ATTRIBUTE(strip_include_prefix) -->
The prefix to strip from the paths of the headers of this rule.

<p>When set, the headers in the <code>hdrs</code> attribute of this rule are accessible
at their path with this prefix cut off.

<p>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.

<p>The prefix in the <code>include_prefix</code> attribute is added after this prefix is
stripped.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("strip_include_prefix", STRING))
/* <!-- #BLAZE_RULE($objc_compiling_rule).ATTRIBUTE(include_prefix) -->
The prefix to add to the paths of the headers of this rule.

<p>When set, the headers in the <code>hdrs</code> attribute of this rule are accessible
at is the value of this attribute prepended to their repository-relative path.

<p>The prefix in the <code>strip_include_prefix</code> attribute is removed before this
prefix is added.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("include_prefix", STRING))

/* Provides the label for header_scanner tool that is used to scan inclusions for ObjC
sources and provide a list of required headers via a .header_list file.

Expand Down
57 changes: 57 additions & 0 deletions src/test/shell/bazel/apple/bazel_objc_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,61 @@ EOF
fail "Timestamp of contents of archive file should be zero"
}

function test_objc_library_include_prefix_external_repository() {
r="$TEST_TMPDIR/r"
mkdir -p "$TEST_TMPDIR/r/foo/v1"
touch "$TEST_TMPDIR/r/WORKSPACE"
echo "#define FOO 42" > "$TEST_TMPDIR/r/foo/v1/foo.h"
cat > "$TEST_TMPDIR/r/foo/BUILD" <<EOF
objc_library(
name = "foo",
hdrs = ["v1/foo.h"],
include_prefix = "foolib",
strip_include_prefix = "v1",
visibility = ["//visibility:public"],
)
EOF

cat > WORKSPACE <<EOF
local_repository(
name = "foo",
path = "$TEST_TMPDIR/r",
)
EOF

cat > BUILD <<EOF
objc_library(
name = "ok",
srcs = ["ok.m"],
deps = ["@foo//foo"],
)

objc_library(
name = "bad",
srcs = ["bad.m"],
deps = ["@foo//foo"],
)
EOF

cat > ok.m <<EOF
#include <stdio.h>
#include "foolib/foo.h"
int main() {
printf("FOO is %d\n", FOO);
}
EOF

cat > bad.m <<EOF
#include <stdio.h>
#include "foo/v1/foo.h"
int main() {
printf("FOO is %d\n", FOO);
}
EOF

bazel build --sandbox_debug --verbose_failures -s :bad && fail "Should not have found include at repository-relative path"
bazel build --sandbox_debug --verbose_failures -s :ok || fail "Should have found include at synthetic path"
}


run_suite "objc/ios test suite"