aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/java/BaseJavaCompilationHelper.java
blob: dcc5ee9fcde98e5688a7e47d544a6a54fb8bb916 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// 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.java;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.ParameterFile.ParameterFileType;
import com.google.devtools.build.lib.actions.Root;
import com.google.devtools.build.lib.analysis.AnalysisEnvironment;
import com.google.devtools.build.lib.analysis.AnalysisUtils;
import com.google.devtools.build.lib.analysis.FilesToRunProvider;
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.CommandLine;
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.config.BuildConfiguration;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.PathFragment;

import java.util.Collection;

/**
 * A helper class for compiling Java targets. This helper does not rely on the
 * presence of rule-specific attributes.
 */
public class BaseJavaCompilationHelper {
  /**
   * Also see DeployArchiveBuilder.SINGLEJAR_MAX_MEMORY. We don't expect that anyone has more
   * than ~500,000 files in a source jar, so 256 MB of memory should be plenty.
   */
  private static final String SINGLEJAR_MAX_MEMORY = "-Xmx256m";

  private final RuleContext ruleContext;

  public BaseJavaCompilationHelper(RuleContext ruleContext) {
    this.ruleContext = ruleContext;
  }

  /**
   * Returns the artifacts required to invoke {@code javahome} relative binary
   * in the action.
   */
  public static NestedSet<Artifact> getHostJavabaseInputs(RuleContext ruleContext) {
    // This must have a different name than above, because the middleman creation uses the rule's
    // configuration, although it should use the host configuration.
    return AnalysisUtils.getMiddlemanFor(ruleContext, ":host_jdk");
  }

  private static final ImmutableList<String> SOURCE_JAR_COMMAND_LINE_ARGS = ImmutableList.of(
      "--compression",
      "--normalize",
      "--exclude_build_data",
      "--warn_duplicate_resources");

  private CommandLine sourceJarCommandLine(JavaSemantics semantics, Artifact outputJar,
      Iterable<Artifact> resources, Iterable<Artifact> resourceJars) {
    CustomCommandLine.Builder args = CustomCommandLine.builder();
    args.addExecPath("--output", outputJar);
    args.add(SOURCE_JAR_COMMAND_LINE_ARGS);
    args.addExecPaths("--sources", resourceJars);
    args.add("--resources");
    for (Artifact resource : resources) {
      args.addPaths("%s:%s", resource.getExecPath(),
          semantics.getJavaResourcePath(resource.getRootRelativePath()));
    }
    return args.build();
  }

  /**
   * Creates an Action that packages files into a Jar file.
   *
   * @param semantics delegate semantics for java.
   * @param resources the resources to put into the Jar.
   * @param resourceJars the resource jars to merge into the jar
   * @param outputJar the Jar to create
   */
  public void createSourceJarAction(JavaSemantics semantics, Collection<Artifact> resources,
      Collection<Artifact> resourceJars, Artifact outputJar) {
    ruleContext.registerAction(new SpawnAction.Builder()
        .addOutput(outputJar)
        .addInputs(resources)
        .addInputs(resourceJars)
        .addTransitiveInputs(JavaCompilationHelper.getHostJavabaseInputs(ruleContext))
        .setJarExecutable(
            ruleContext.getHostConfiguration().getFragment(Jvm.class).getJavaExecutable(),
            ruleContext.getPrerequisiteArtifact("$singlejar", Mode.HOST),
            ImmutableList.of("-client", SINGLEJAR_MAX_MEMORY))
        .setCommandLine(sourceJarCommandLine(semantics, outputJar, resources, resourceJars))
        .useParameterFile(ParameterFileType.SHELL_QUOTED)
        .setProgressMessage("Building source jar " + outputJar.prettyPrint())
        .setMnemonic("JavaSourceJar")
        .build(ruleContext));
  }

  /**
   * Returns the langtools jar Artifact.
   */
  protected final Artifact getLangtoolsJar() {
    return ruleContext.getHostPrerequisiteArtifact("$java_langtools");
  }

  /**
   * Returns the JavaBuilder jar Artifact.
   */
  protected final Artifact getJavaBuilderJar() {
    return ruleContext.getPrerequisiteArtifact("$javabuilder", Mode.HOST);
  }

  /**
   * Returns the instrumentation jar in the given semantics.
   */
  protected final Iterable<Artifact> getInstrumentationJars(JavaSemantics semantics) {
    return semantics.getInstrumentationJars(ruleContext);
  }

  /**
   * Returns the javac bootclasspath artifacts.
   */
  protected final Iterable<Artifact> getBootClasspath() {
    return ruleContext.getPrerequisiteArtifacts("$javac_bootclasspath", Mode.HOST).list();
  }

  /**
   * Returns the extdir artifacts.
   */
  protected final ImmutableList<Artifact> getExtdirInputs() {
    return ruleContext.getPrerequisiteArtifacts("$javac_extdir", Mode.HOST).list();
  }

  private Artifact getIjarArtifact(Artifact jar, boolean addPrefix) {
    if (addPrefix) {
      PathFragment ruleBase = ruleContext.getUniqueDirectory("_ijar");
      PathFragment artifactDirFragment = jar.getRootRelativePath().getParentDirectory();
      String ijarBasename = FileSystemUtils.removeExtension(jar.getFilename()) + "-ijar.jar";
      return ruleContext.getDerivedArtifact(
          ruleBase.getRelative(artifactDirFragment).getRelative(ijarBasename),
          getConfiguration().getGenfilesDirectory());
    } else {
      return derivedArtifact(jar, "", "-ijar.jar");
    }
  }

  /**
   * Creates the Action that creates ijars from Jar files.
   *
   * @param inputJar the Jar to create the ijar for
   * @param addPrefix whether to prefix the path of the generated ijar with the package and
   *     name of the current rule
   * @return the Artifact to create with the Action
   */
  protected Artifact createIjarAction(final Artifact inputJar, boolean addPrefix) {
    Artifact interfaceJar = getIjarArtifact(inputJar, addPrefix);
    final FilesToRunProvider ijarTarget =
        ruleContext.getExecutablePrerequisite("$ijar", Mode.HOST);
    if (!ruleContext.hasErrors()) {
      ruleContext.registerAction(new SpawnAction.Builder()
          .addInput(inputJar)
          .addOutput(interfaceJar)
          .setExecutable(ijarTarget)
          .addArgument(inputJar.getExecPathString())
          .addArgument(interfaceJar.getExecPathString())
          .setProgressMessage("Extracting interface " + ruleContext.getLabel())
          .setMnemonic("JavaIjar")
          .build(ruleContext));
    }
    return interfaceJar;
  }

  protected final JavaCompileAction.Builder createJavaCompileActionBuilder(
      JavaSemantics semantics) {
    JavaCompileAction.Builder builder = new JavaCompileAction.Builder(ruleContext, semantics);
    builder.setJavaExecutable(
        ruleContext.getHostConfiguration().getFragment(Jvm.class).getJavaExecutable());
    builder.setJavaBaseInputs(BaseJavaCompilationHelper.getHostJavabaseInputs(ruleContext));
    return builder;
  }

  public RuleContext getRuleContext() {
    return ruleContext;
  }

  public AnalysisEnvironment getAnalysisEnvironment() {
    return ruleContext.getAnalysisEnvironment();
  }

  protected BuildConfiguration getConfiguration() {
    return ruleContext.getConfiguration();
  }

  protected JavaConfiguration getJavaConfiguration() {
    return ruleContext.getFragment(JavaConfiguration.class);
  }

  /**
   * Produces a derived directory where source files generated by annotation processors should be
   * stored.
   */
  protected PathFragment sourceGenDir(Artifact outputJar) {
    return workDir(outputJar, "_sourcegenfiles");
  }

  protected PathFragment tempDir(Artifact outputJar) {
    return workDir(outputJar, "_temp");
  }

  protected PathFragment classDir(Artifact outputJar) {
    return workDir(outputJar, "_classes");
  }

  /**
   * For an output jar and a suffix, produces a derived directory under
   * {@code bin} directory with a given suffix.
   *
   * <p>Note that this won't work if a rule produces two jars with the same basename.
   */
  private PathFragment workDir(Artifact outputJar, String suffix) {
    String basename = FileSystemUtils.removeExtension(outputJar.getExecPath().getBaseName());
    return getConfiguration().getBinDirectory().getExecPath()
        .getRelative(ruleContext.getUniqueDirectory("_javac"))
        .getRelative(basename + suffix);
  }

  /**
   * Creates a derived artifact from the given artifact by adding the given
   * prefix and removing the extension and replacing it by the given suffix.
   * The new artifact will have the same root as the given one.
   */
  protected Artifact derivedArtifact(Artifact artifact, String prefix, String suffix) {
    return derivedArtifact(artifact, prefix, suffix, artifact.getRoot());
  }

  protected Artifact derivedArtifact(Artifact artifact, String prefix, String suffix, Root root) {
    PathFragment path = artifact.getRootRelativePath();
    String basename = FileSystemUtils.removeExtension(path.getBaseName()) + suffix;
    path = path.replaceName(prefix + basename);
    return ruleContext.getDerivedArtifact(path, root);
  }
}