Skip to content

Commit

Permalink
Allow custom Package serialization logic to be injected. Also fix inc…
Browse files Browse the repository at this point in the history
…orrect comment in PackageSerializer.

--
MOS_MIGRATED_REVID=103693274
  • Loading branch information
haxorz authored and lberki committed Sep 23, 2015
1 parent a28d69c commit 2012b4e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ protected Package(PackageIdentifier packageId, String runfilesPrefix) {

private void writeObject(ObjectOutputStream out) {
try {
PackageSerializer.DEFAULT.serialize(this, out);
new PackageSerializer().serialize(this, out);
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
import static com.google.devtools.build.lib.syntax.Type.STRING_LIST;
import static com.google.devtools.build.lib.syntax.Type.STRING_LIST_DICT;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.packages.License.DistributionType;
import com.google.devtools.build.lib.packages.MakeEnvironment.Binding;
import com.google.devtools.build.lib.query2.proto.proto2api.Build;
import com.google.devtools.build.lib.query2.proto.proto2api.Build.Rule.Builder;
import com.google.devtools.build.lib.syntax.GlobCriteria;
import com.google.devtools.build.lib.syntax.GlobList;

Expand All @@ -56,8 +58,34 @@
* Functionality to serialize loaded packages.
*/
public class PackageSerializer {
/** Allows custom serialization logic to be injected. */
public interface PackageSerializationEnvironment {
/**
* Called right before the given builder's {@link Build.Rule.Builder#build} method is called.
* Implementations can use this hook to serialize additional data in the proto.
*/
void maybeSerializeAdditionalDataForRule(Rule rule, Build.Rule.Builder builder);
}

// Workaround for Java serialization making it tough to pass in a serialization environment
// manually.
// volatile is needed to ensure that the objects are published safely.
public static volatile PackageSerializationEnvironment defaultPackageSerializationEnvironment =
new PackageSerializationEnvironment() {
@Override
public void maybeSerializeAdditionalDataForRule(Rule rule, Builder builder) {
}
};

public static final PackageSerializer DEFAULT = new PackageSerializer();
private final PackageSerializationEnvironment env;

public PackageSerializer() {
this(defaultPackageSerializationEnvironment);
}

public PackageSerializer(PackageSerializationEnvironment env) {
this.env = Preconditions.checkNotNull(env);
}

/**
* Get protocol buffer representation of the specified attribute.
Expand All @@ -69,7 +97,8 @@ public class PackageSerializer {
*/
public static Build.Attribute getAttributeProto(Attribute attr, Iterable<Object> values,
Boolean explicitlySpecified) {
return DEFAULT.serializeAttribute(attr, values, explicitlySpecified, /*includeGlobs=*/ false);
return new PackageSerializer().serializeAttribute(attr, values, explicitlySpecified,
/*includeGlobs=*/ false);
}

/**
Expand Down Expand Up @@ -446,6 +475,7 @@ private Build.Target serializeRule(Rule rule) {
serializeAttribute(attribute, getAttributeValues(rule, attribute),
rule.isAttributeValueExplicitlySpecified(attribute), /*includeGlobs=*/ true));
}
env.maybeSerializeAdditionalDataForRule(rule, builder);

return Build.Target.newBuilder()
.setType(Build.Target.Discriminator.RULE)
Expand Down Expand Up @@ -516,7 +546,7 @@ private void emitTargets(Collection<Target> targets, OutputStream out) throws IO
if (target instanceof InputFile) {
emitTarget(serializeInputFile((InputFile) target), out);
} else if (target instanceof OutputFile) {
// Output files are ignored; they are recorded in rules.
// Output files are not serialized; they are recreated by the RuleClass on deserialization.
} else if (target instanceof PackageGroup) {
emitTarget(serializePackageGroup((PackageGroup) target), out);
} else if (target instanceof Rule) {
Expand Down

0 comments on commit 2012b4e

Please sign in to comment.