// Copyright 2015 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 static com.google.devtools.build.lib.packages.ImplicitOutputsFunction.fromTemplates; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.common.io.ByteSource; 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.BinaryFileWriteAction; import com.google.devtools.build.lib.analysis.actions.SpawnAction; import com.google.devtools.build.lib.analysis.actions.SymlinkAction; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; import com.google.devtools.build.lib.packages.ImplicitOutputsFunction.SafeImplicitOutputsFunction; import com.google.devtools.build.lib.rules.apple.AppleConfiguration; import com.google.devtools.build.lib.rules.apple.AppleConfiguration.ConfigurationDistinguisher; import com.google.devtools.build.lib.rules.apple.Platform.PlatformType; import com.google.devtools.build.lib.rules.objc.XcodeProvider.Builder; import com.google.devtools.build.lib.rules.objc.XcodeProvider.Project; import com.google.devtools.build.xcode.xcodegen.proto.XcodeGenProtos; import com.google.devtools.build.xcode.xcodegen.proto.XcodeGenProtos.XcodeprojBuildSetting; import java.io.InputStream; import java.util.List; /** * Support for Objc rule types that export an Xcode provider or generate xcode project files. * *
Methods on this class can be called in any order without impacting the result. * *
These objects should not outlast the analysis phase. Do not pass them to {@link Action}
* objects or other persistent objects. There are internal tests to ensure that XcodeSupport objects
* are not persisted that check the name of this class, so update those tests if you change this
* class's name.
*/
public final class XcodeSupport {
/** Template for a target's xcode project. */
private static final SafeImplicitOutputsFunction PBXPROJ =
fromTemplates("%{name}.xcodeproj/project.pbxproj");
private final RuleContext ruleContext;
private final IntermediateArtifacts intermediateArtifacts;
private final Label xcodeTargetLabel;
/**
* Creates a new xcode support for the given context.
*/
XcodeSupport(RuleContext ruleContext) {
this(ruleContext, ObjcRuleClasses.intermediateArtifacts(ruleContext), ruleContext.getLabel());
}
/**
* Creates a new xcode support for the given context and {@link IntermediateArtifacts} with given
* target label.
*/
public XcodeSupport(
RuleContext ruleContext, IntermediateArtifacts intermediateArtifacts,
Label xcodeTargetLabel) {
this.ruleContext = ruleContext;
this.intermediateArtifacts = intermediateArtifacts;
this.xcodeTargetLabel = xcodeTargetLabel;
}
/**
* Adds xcode project files to the given builder.
*
* @return this xcode support
* @throws InterruptedException
*/
XcodeSupport addFilesToBuild(NestedSetBuilder A non-propagated dependency will not be linked into the final app bundle and can only serve
* as a compile-only dependency for its direct dependent.
*
* @return this xcode support
*/
XcodeSupport addNonPropagatedDependencies(Builder xcodeProviderBuilder, Attribute attribute) {
xcodeProviderBuilder.addNonPropagatedDependencies(
ruleContext.getPrerequisites(
attribute.getName(), attribute.getAccessMode(), XcodeProvider.class));
return this;
}
/**
* Adds J2ObjC JRE dependencies to the given provider builder from the given attribute.
*
* @return this xcode support
*/
XcodeSupport addJreDependencies(Builder xcodeProviderBuilder) {
xcodeProviderBuilder.addJreDependencies(
ruleContext.getPrerequisites("jre_deps", Mode.TARGET, XcodeProvider.class));
return this;
}
/**
* Generates an extra {@link XcodeProductType#LIBRARY_STATIC} Xcode target with the same
* compilation artifacts as the main Xcode target associated with this Xcode support. The extra
* Xcode library target, instead of the main Xcode target, will act as a dependency for all
* dependent Xcode targets.
*
* This is needed to build the Xcode binary target generated by ios_application in XCode.
* Currently there is an Xcode target dependency between the binary target from ios_application
* and the binary target from objc_binary. But Xcode does not link in compiled artifacts from
* binary dependencies, so any sources specified on objc_binary rules will not be compiled and
* linked into the app bundle in dependent binary targets associated with ios_application in
* XCode.
*/
// TODO(bazel-team): Remove this when the binary rule types and bundling rule types are merged.
XcodeSupport generateCompanionLibXcodeTarget(Builder xcodeProviderBuilder) {
xcodeProviderBuilder.generateCompanionLibTarget();
return this;
}
private void registerXcodegenActions(XcodeProvider.Project project) throws InterruptedException {
Artifact controlFile = intermediateArtifacts.pbxprojControlArtifact();
ruleContext.registerAction(new BinaryFileWriteAction(
ruleContext.getActionOwner(),
controlFile,
xcodegenControlFileBytes(project),
/*makeExecutable=*/false));
ruleContext.registerAction(new SpawnAction.Builder()
.setMnemonic("GenerateXcodeproj")
.setExecutable(ruleContext.getExecutablePrerequisite("$xcodegen", Mode.HOST))
.addArgument("--control")
.addInputArgument(controlFile)
.addOutput(ruleContext.getImplicitOutputArtifact(XcodeSupport.PBXPROJ))
.addTransitiveInputs(project.getInputsToXcodegen())
.addTransitiveInputs(project.getAdditionalSources())
.build(ruleContext));
}
/**
* Static class to avoid keeping references to configurations and this XcodeSupport object during
* execution.
*/
private static class XcodegenControlFileBytes extends ByteSource {
private final XcodeProvider.Project project;
private final Artifact pbxproj;
private final String workspaceRoot;
private final List