aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/android/AndroidResourceParsingActionBuilder.java
blob: 1b7125be7d942adce66f8e34a60a15bea99c7788 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// 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.android;

import static java.util.stream.Collectors.joining;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Streams;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.ParamFileInfo;
import com.google.devtools.build.lib.actions.ParameterFile.ParameterFileType;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.actions.ActionConstructionContext;
import com.google.devtools.build.lib.analysis.actions.CustomCommandLine;
import com.google.devtools.build.lib.analysis.actions.SpawnAction;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;

/** Builder for creating $android_resource_parser action. */
public class AndroidResourceParsingActionBuilder {

  private final RuleContext ruleContext;
  private final AndroidSdkProvider sdk;

  // These are only needed when parsing resources with data binding
  @Nullable private Artifact manifest;
  @Nullable private String javaPackage;

  private AndroidResources resources = AndroidResources.empty();
  private AndroidAssets assets = AndroidAssets.empty();
  private Artifact output;

  private Artifact compiledSymbols;
  private Artifact dataBindingInfoZip;

  /** @param ruleContext The RuleContext that was used to create the SpawnAction.Builder. */
  public AndroidResourceParsingActionBuilder(RuleContext ruleContext) {
    this.ruleContext = ruleContext;
    this.sdk = AndroidSdkProvider.fromRuleContext(ruleContext);
  }

  /** Set the artifact location for the output protobuf. */
  public AndroidResourceParsingActionBuilder setOutput(Artifact output) {
    this.output = output;
    return this;
  }

  /** Sets the manifest. Will be ignored except when parsing resources with data binding. */
  public AndroidResourceParsingActionBuilder setManifest(@Nullable Artifact manifest) {
    this.manifest = manifest;
    return this;
  }

  /** Sets the Java package. Will be ignored except when parsing resources with data binding. */
  public AndroidResourceParsingActionBuilder setJavaPackage(@Nullable String javaPackage) {
    this.javaPackage = javaPackage;
    return this;
  }

  public AndroidResourceParsingActionBuilder setResources(AndroidResources resources) {
    this.resources = resources;
    return this;
  }

  public AndroidResourceParsingActionBuilder setAssets(AndroidAssets assets) {
    this.assets = assets;
    return this;
  }

  public AndroidResourceParsingActionBuilder setCompiledSymbolsOutput(
      @Nullable Artifact compiledSymbols) {
    this.compiledSymbols = compiledSymbols;
    return this;
  }

  public AndroidResourceParsingActionBuilder setDataBindingInfoZip(Artifact dataBindingInfoZip) {
    this.dataBindingInfoZip = dataBindingInfoZip;
    return this;
  }

  private static String convertRoots(Iterable<PathFragment> roots) {
    return Streams.stream(roots).map(Object::toString).collect(joining("#"));
  }

  private void build(ActionConstructionContext context) {
    CustomCommandLine.Builder builder = new CustomCommandLine.Builder();

    // Set the busybox tool.
    builder.add("--tool").add("PARSE").add("--");

    NestedSetBuilder<Artifact> inputs = NestedSetBuilder.naiveLinkOrder();

    String resourceDirectories =
        convertRoots(resources.getResourceRoots()) + ":" + convertRoots(assets.getAssetRoots());
    builder.add("--primaryData", resourceDirectories);
    inputs.addTransitive(
        NestedSetBuilder.<Artifact>naiveLinkOrder()
            .addAll(assets.getAssets())
            .addAll(resources.getResources())
            .build());

    Preconditions.checkNotNull(output);
    builder.addExecPath("--output", output);

    SpawnAction.Builder spawnActionBuilder = new SpawnAction.Builder();
    ParamFileInfo.Builder paramFileInfo = ParamFileInfo.builder(ParameterFileType.SHELL_QUOTED);
    // Some flags (e.g. --mainData) may specify lists (or lists of lists) separated by special
    // characters (colon, semicolon, hashmark, ampersand) that don't work on Windows, and quoting
    // semantics are very complicated (more so than in Bash), so let's just always use a parameter
    // file.
    // TODO(laszlocsomor), TODO(corysmith): restructure the Android BusyBux's flags by deprecating
    // list-type and list-of-list-type flags that use such problematic separators in favor of
    // multi-value flags (to remove one level of listing) and by changing all list separators to a
    // platform-safe character (= comma).
    paramFileInfo.setUseAlways(OS.getCurrent() == OS.WINDOWS);

    // Create the spawn action.
    ruleContext.registerAction(
        spawnActionBuilder
            .useDefaultShellEnvironment()
            .addTransitiveInputs(inputs.build())
            .addOutputs(ImmutableList.of(output))
            .addCommandLine(builder.build(), paramFileInfo.build())
            .setExecutable(
                ruleContext.getExecutablePrerequisite("$android_resources_busybox", Mode.HOST))
            .setProgressMessage("Parsing Android resources for %s", ruleContext.getLabel())
            .setMnemonic("AndroidResourceParser")
            .build(context));

    if (compiledSymbols != null) {
      List<Artifact> outs = new ArrayList<>();
      CustomCommandLine.Builder flatFileBuilder = new CustomCommandLine.Builder();
      flatFileBuilder
          .add("--tool")
          .add("COMPILE_LIBRARY_RESOURCES")
          .add("--")
          .addExecPath("--aapt2", sdk.getAapt2().getExecutable())
          .add("--resources", resourceDirectories)
          .addExecPath("--output", compiledSymbols);
      inputs.add(sdk.getAapt2().getExecutable());
      outs.add(compiledSymbols);

      // The databinding needs to be processed before compilation, so the stripping happens here.
      if (dataBindingInfoZip != null) {
        flatFileBuilder.addExecPath("--manifest", manifest);
        inputs.add(manifest);
        if (!Strings.isNullOrEmpty(javaPackage)) {
          flatFileBuilder.add("--packagePath", javaPackage);
        }
        flatFileBuilder.addExecPath("--dataBindingInfoOut", dataBindingInfoZip);
        outs.add(dataBindingInfoZip);
      }
      // Create the spawn action.
      ruleContext.registerAction(
          new SpawnAction.Builder()
              .useDefaultShellEnvironment()
              .addTransitiveInputs(inputs.build())
              .addOutputs(ImmutableList.copyOf(outs))
              .addCommandLine(flatFileBuilder.build(), paramFileInfo.build())
              .setExecutable(
                  ruleContext.getExecutablePrerequisite("$android_resources_busybox", Mode.HOST))
              .setProgressMessage("Compiling Android resources for %s", ruleContext.getLabel())
              .setMnemonic("AndroidResourceCompiler")
              .build(context));
    }
  }

  /**
   * Builds and registers the action, and updates the given resourceContainer with the output
   * symbols.
   */
  public ResourceContainer buildAndUpdate(
      RuleContext ruleContext, ResourceContainer resourceContainer) {
    build(ruleContext);

    ResourceContainer.Builder builder =
        resourceContainer.toBuilder().setSymbols(output).setAssets(assets).setResources(resources);

    if (compiledSymbols != null) {
      builder.setCompiledSymbols(compiledSymbols);
    }

    return builder.build();
  }
}