use of com.google.devtools.build.lib.actions.Artifact in project bazel by bazelbuild.
the class BazelJavaSemantics method createStubAction.
@Override
public Artifact createStubAction(RuleContext ruleContext, JavaCommon javaCommon, List<String> jvmFlags, Artifact executable, String javaStartClass, String javaExecutable) {
Preconditions.checkState(ruleContext.getConfiguration().hasFragment(Jvm.class));
Preconditions.checkNotNull(jvmFlags);
Preconditions.checkNotNull(executable);
Preconditions.checkNotNull(javaStartClass);
Preconditions.checkNotNull(javaExecutable);
List<Substitution> arguments = new ArrayList<>();
String workspaceName = ruleContext.getWorkspaceName();
final String workspacePrefix = workspaceName + (workspaceName.isEmpty() ? "" : "/");
final boolean isRunfilesEnabled = ruleContext.getConfiguration().runfilesEnabled();
arguments.add(Substitution.of("%runfiles_manifest_only%", isRunfilesEnabled ? "" : "1"));
arguments.add(Substitution.of("%workspace_prefix%", workspacePrefix));
arguments.add(Substitution.of("%javabin%", javaExecutable));
arguments.add(Substitution.of("%needs_runfiles%", ruleContext.getFragment(Jvm.class).getJavaExecutable().isAbsolute() ? "0" : "1"));
NestedSet<Artifact> classpath = javaCommon.getRuntimeClasspath();
arguments.add(new ComputedClasspathSubstitution("%classpath%", classpath, workspacePrefix, isRunfilesEnabled));
JavaCompilationArtifacts javaArtifacts = javaCommon.getJavaCompilationArtifacts();
String path = javaArtifacts.getInstrumentedJar() != null ? "${JAVA_RUNFILES}/" + workspacePrefix + javaArtifacts.getInstrumentedJar().getRootRelativePath().getPathString() : "";
arguments.add(Substitution.of("%set_jacoco_metadata%", ruleContext.getConfiguration().isCodeCoverageEnabled() ? "export JACOCO_METADATA_JAR=" + path : ""));
arguments.add(Substitution.of("%java_start_class%", ShellEscaper.escapeString(javaStartClass)));
arguments.add(Substitution.ofSpaceSeparatedList("%jvm_flags%", ImmutableList.copyOf(jvmFlags)));
ruleContext.registerAction(new TemplateExpansionAction(ruleContext.getActionOwner(), executable, STUB_SCRIPT, arguments, true));
if (OS.getCurrent() == OS.WINDOWS) {
Artifact newExecutable = ruleContext.getImplicitOutputArtifact(ruleContext.getTarget().getName() + ".cmd");
ruleContext.registerAction(new TemplateExpansionAction(ruleContext.getActionOwner(), newExecutable, STUB_SCRIPT_WINDOWS, ImmutableList.of(Substitution.of("%bash_exe_path%", ruleContext.getFragment(BazelConfiguration.class).getShellExecutable().getPathString()), Substitution.of("%cygpath_exe_path%", ruleContext.getFragment(BazelConfiguration.class).getShellExecutable().replaceName("cygpath.exe").getPathString())), true));
return newExecutable;
} else {
return executable;
}
}
use of com.google.devtools.build.lib.actions.Artifact in project bazel by bazelbuild.
the class BazelJavaSemantics method addCoverageSupport.
// TODO(yueg): refactor this (only mainClass different for now)
@Override
public String addCoverageSupport(JavaCompilationHelper helper, JavaTargetAttributes.Builder attributes, Artifact executable, Artifact instrumentationMetadata, JavaCompilationArtifacts.Builder javaArtifactsBuilder, String mainClass) throws InterruptedException {
// This method can be called only for *_binary/*_test targets.
Preconditions.checkNotNull(executable);
// Add our own metadata artifact (if any).
if (instrumentationMetadata != null) {
attributes.addInstrumentationMetadataEntries(ImmutableList.of(instrumentationMetadata));
}
if (!hasInstrumentationMetadata(attributes)) {
return mainClass;
}
Artifact instrumentedJar = helper.getRuleContext().getBinArtifact(helper.getRuleContext().getLabel().getName() + "_instrumented.jar");
// Create an instrumented Jar. This will be referenced on the runtime classpath prior
// to all other Jars.
JavaCommon.createInstrumentedJarAction(helper.getRuleContext(), this, attributes.getInstrumentationMetadata(), instrumentedJar, mainClass);
javaArtifactsBuilder.setInstrumentedJar(instrumentedJar);
// Add the coverage runner to the list of dependencies when compiling in coverage mode.
TransitiveInfoCollection runnerTarget = helper.getRuleContext().getPrerequisite("$jacocorunner", Mode.TARGET);
if (runnerTarget.getProvider(JavaCompilationArgsProvider.class) != null) {
helper.addLibrariesToAttributes(ImmutableList.of(runnerTarget));
} else {
helper.getRuleContext().ruleError("this rule depends on " + helper.getRuleContext().attributes().get("$jacocorunner", BuildType.LABEL) + " which is not a java_library rule, or contains errors");
}
// script via an environment variable.
return JACOCO_COVERAGE_RUNNER_MAIN_CLASS;
}
use of com.google.devtools.build.lib.actions.Artifact in project bazel by bazelbuild.
the class BazelJavaSemantics method getExtraArguments.
@Override
public List<String> getExtraArguments(RuleContext ruleContext, ImmutableList<Artifact> sources) {
if (ruleContext.getRule().getRuleClass().equals("java_test")) {
if (useLegacyJavaTest(ruleContext)) {
if (ruleContext.getConfiguration().getTestArguments().isEmpty() && !ruleContext.attributes().isAttributeValueExplicitlySpecified("args")) {
ImmutableList.Builder<String> builder = ImmutableList.builder();
for (Artifact artifact : sources) {
PathFragment path = artifact.getRootRelativePath();
String className = JavaUtil.getJavaFullClassname(FileSystemUtils.removeExtension(path));
if (className != null) {
builder.add(className);
}
}
return builder.build();
}
}
}
return ImmutableList.<String>of();
}
use of com.google.devtools.build.lib.actions.Artifact in project bazel by bazelbuild.
the class ShBinary method create.
@Override
public ConfiguredTarget create(RuleContext ruleContext) throws RuleErrorException {
ImmutableList<Artifact> srcs = ruleContext.getPrerequisiteArtifacts("srcs", Mode.TARGET).list();
if (srcs.size() != 1) {
ruleContext.attributeError("srcs", "you must specify exactly one file in 'srcs'");
return null;
}
Artifact symlink = ruleContext.createOutputArtifact();
// Note that src is used as the executable script too
Artifact src = srcs.get(0);
// The interpretation of this deceptively simple yet incredibly generic rule is complicated
// by the distinction between targets and (not properly encapsulated) artifacts. It depends
// on the notion of other rule's "files-to-build" sets, which are undocumented, making it
// impossible to give a precise definition of what this rule does in all cases (e.g. what
// happens when srcs = ['x', 'y'] but 'x' is an empty filegroup?). This is a pervasive
// problem in Blaze.
ruleContext.registerAction(new ExecutableSymlinkAction(ruleContext.getActionOwner(), src, symlink));
NestedSet<Artifact> filesToBuild = NestedSetBuilder.<Artifact>stableOrder().add(src).add(symlink).build();
Runfiles runfiles = new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()).addTransitiveArtifacts(filesToBuild).addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES).build();
RunfilesSupport runfilesSupport = RunfilesSupport.withExecutable(ruleContext, runfiles, symlink);
return new RuleConfiguredTargetBuilder(ruleContext).setFilesToBuild(filesToBuild).setRunfilesSupport(runfilesSupport, symlink).addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles)).build();
}
use of com.google.devtools.build.lib.actions.Artifact in project bazel by bazelbuild.
the class ShLibrary method create.
@Override
public ConfiguredTarget create(RuleContext ruleContext) throws RuleErrorException {
NestedSet<Artifact> filesToBuild = NestedSetBuilder.<Artifact>stableOrder().addAll(ruleContext.getPrerequisiteArtifacts("srcs", Mode.TARGET).list()).addAll(ruleContext.getPrerequisiteArtifacts("deps", Mode.TARGET).list()).addAll(ruleContext.getPrerequisiteArtifacts("data", Mode.DATA).list()).build();
Runfiles runfiles = new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()).addTransitiveArtifacts(filesToBuild).build();
return new RuleConfiguredTargetBuilder(ruleContext).setFilesToBuild(filesToBuild).addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles)).build();
}
Aggregations