aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/objc/IosFramework.java
blob: d823621b203c99cb14606c8d58169e6089e3e710 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2014 Google Inc. 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.common.base.Preconditions.checkNotNull;
import static com.google.devtools.build.lib.rules.objc.ObjcCommon.uniqueContainers;
import static com.google.devtools.build.lib.rules.objc.ObjcProvider.FLAG;
import static com.google.devtools.build.lib.rules.objc.ObjcProvider.FRAMEWORK_DIR;
import static com.google.devtools.build.lib.rules.objc.ObjcProvider.FRAMEWORK_FILE;
import static com.google.devtools.build.lib.rules.objc.ObjcProvider.Flag.USES_FRAMEWORKS;
import static com.google.devtools.build.lib.rules.objc.ObjcProvider.MERGE_ZIP;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.actions.SymlinkAction;
import com.google.devtools.build.lib.rules.cpp.CcCommon;
import com.google.devtools.build.lib.rules.objc.ReleaseBundlingSupport.SplitArchTransition.ConfigurationDistinguisher;
import com.google.devtools.build.lib.vfs.PathFragment;

/**
 * Implementation for {@code ios_framework}.
 */
public class IosFramework extends ReleaseBundlingTargetFactory {

  @VisibleForTesting static final String MINIMUM_OS_VERSION = "8.0";

  public IosFramework() {
    super(
        ReleaseBundlingSupport.FRAMEWORK_BUNDLE_DIR_FORMAT,
        XcodeProductType.FRAMEWORK,
        ImmutableSet.of(new Attribute("binary", Mode.SPLIT)),
        ConfigurationDistinguisher.FRAMEWORK);
  }

  @Override
  protected String bundleName(RuleContext ruleContext) {
    String frameworkName = null;

    for (IosFrameworkProvider provider :
        ruleContext.getPrerequisites("binary", Mode.SPLIT, IosFrameworkProvider.class)) {
      frameworkName = provider.getFrameworkName();
    }


    return checkNotNull(frameworkName);
  }

  @Override
  protected String bundleMinimumOsVersion(RuleContext ruleContext) {
    String flagValue = ObjcRuleClasses.objcConfiguration(ruleContext).getMinimumOs();

    return String.valueOf(
        Math.max(Double.parseDouble(MINIMUM_OS_VERSION), Double.parseDouble(flagValue)));
  }

  /**
   * Returns a map of original {@code Artifact} to symlinked {@code Artifact} inside framework
   * bundle.
   */
  private ImmutableMap<Artifact, Artifact> getExtraArtifacts(RuleContext ruleContext) {
    IntermediateArtifacts intermediateArtifacts =
        ObjcRuleClasses.intermediateArtifacts(ruleContext);

    ImmutableList<Artifact> headers = ImmutableList.copyOf(CcCommon.getHeaders(ruleContext));

    ImmutableMap.Builder<Artifact, Artifact> builder = new ImmutableMap.Builder<>();

    // Create framework binary
    Artifact frameworkBinary =
        outputArtifact(ruleContext, new PathFragment(bundleName(ruleContext)));
    builder.put(intermediateArtifacts.combinedArchitectureBinary(), frameworkBinary);

    // Create framework headers
    for (Artifact header : headers) {
      Artifact frameworkHeader =
          outputArtifact(ruleContext, new PathFragment("Headers/" + header.getFilename()));

      builder.put(header, frameworkHeader);
    }

    return builder.build();
  }

  @Override
  protected ObjcProvider exposedObjcProvider(RuleContext ruleContext) throws InterruptedException {
    // Assemble framework binary and headers in the label-scoped location, so that it's possible to
    // pass -F X.framework to the compiler and -framework X to the linker. This mimics usage of
    // frameworks when built from Xcode.

    // To do this, we symlink all required artifacts into destination and pass them to
    // FRAMEWORK_IMPORTS list, thus utilizing ObjcFramework rule to do the work of propagating
    // them correctly.
    Iterable<Artifact> frameworkImports = getExtraArtifacts(ruleContext).values();

    ObjcProvider frameworkProvider =
        new ObjcProvider.Builder()
            .add(MERGE_ZIP, ruleContext.getImplicitOutputArtifact(ReleaseBundlingSupport.IPA))
            .add(FLAG, USES_FRAMEWORKS)
            .addAll(FRAMEWORK_FILE, frameworkImports)
            .addAll(
                FRAMEWORK_DIR,
                uniqueContainers(frameworkImports, ObjcCommon.FRAMEWORK_CONTAINER_TYPE))
            .build();

    return frameworkProvider;
  }

  @Override
  protected void configureTarget(
      RuleConfiguredTargetBuilder target,
      RuleContext ruleContext,
      ReleaseBundlingSupport releaseBundlingSupport) {
    // Create generating actions for framework artifacts
    for (ImmutableMap.Entry<Artifact, Artifact> entry : getExtraArtifacts(ruleContext).entrySet()) {
      ruleContext.registerAction(
          new SymlinkAction(
              ruleContext.getActionOwner(),
              entry.getKey(),
              entry.getValue(),
              "Symlinking framework artifact"));
    }
  }

  /**
   * Returns an artifact at given path under "package/_frameworks/bundleName.framework" directory.
   */
  private Artifact outputArtifact(RuleContext ruleContext, PathFragment path) {
    PathFragment frameworkRoot =
        new PathFragment(
            new PathFragment("_frameworks"),
            new PathFragment(bundleName(ruleContext) + ".framework"),
            path);

    return ruleContext.getPackageRelativeArtifact(
        frameworkRoot, ruleContext.getBinOrGenfilesDirectory());
  }
}