// Copyright 2016 The Bazel Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.devtools.build.lib.rules.objc; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.collect.Ordering; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode; import com.google.devtools.build.lib.analysis.RuleContext; import com.google.devtools.build.lib.analysis.actions.CustomCommandLine; import com.google.devtools.build.lib.analysis.actions.FileWriteAction; import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException; import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.PathFragment; /** * Support for generating Objective C proto static libraries that registers actions which generate * and compile the Objective C protos by using the deprecated ProtocolBuffers2 library and compiler. * *
Methods on this class can be called in any order without impacting the result.
*/
final class ProtocolBuffers2Support {
private static final String UNIQUE_DIRECTORY_NAME = "_generated_protos";
private final RuleContext ruleContext;
private final ProtoAttributes attributes;
/**
* Creates a new proto support for the ProtocolBuffers2 library.
*
* @param ruleContext context this proto library is constructed in
*/
public ProtocolBuffers2Support(RuleContext ruleContext) {
this.ruleContext = ruleContext;
this.attributes = new ProtoAttributes(ruleContext);
}
/**
* Register the proto generation actions. These actions generate the ObjC/CPP code to be compiled
* by this rule.
*/
public ProtocolBuffers2Support registerGenerationActions() throws InterruptedException {
ruleContext.registerAction(
FileWriteAction.create(
ruleContext,
getProtoInputsFile(),
getProtoInputsFileContents(
attributes.filterWellKnownProtos(attributes.getProtoFiles())),
false));
ruleContext.registerAction(
ObjcRuleClasses.spawnOnDarwinActionBuilder()
.setMnemonic("GenObjcPB2Protos")
.addInput(attributes.getProtoCompiler())
.addInputs(attributes.getProtoCompilerSupport())
.addInput(getProtoInputsFile())
.addTransitiveInputs(attributes.getProtoFiles())
.addInputs(attributes.getOptionsFile().asSet())
.addOutputs(getGeneratedProtoOutputs(getHeaderExtension()))
.addOutputs(getGeneratedProtoOutputs(getSourceExtension()))
.setExecutable(PathFragment.create("/usr/bin/python"))
.setCommandLine(getGenerationCommandLine())
.build(ruleContext));
return this;
}
/**
* Registers the actions that will compile the generated code.
*/
public ProtocolBuffers2Support registerCompilationActions()
throws RuleErrorException, InterruptedException {
CompilationSupport compilationSupport =
new CompilationSupport.Builder()
.setRuleContext(ruleContext)
.doNotUseDeps()
.doNotUsePch()
.build();
compilationSupport.registerCompileAndArchiveActions(getCommon());
return this;
}
/** Adds the generated files to the set of files to be output when this rule is built. */
public ProtocolBuffers2Support addFilesToBuild(NestedSetBuilder