Search in sources :

Example 1 with Artifact

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;
    }
}
Also used : Jvm(com.google.devtools.build.lib.rules.java.Jvm) TemplateExpansionAction(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction) Substitution(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution) ComputedSubstitution(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.ComputedSubstitution) JavaCompilationArtifacts(com.google.devtools.build.lib.rules.java.JavaCompilationArtifacts) BazelConfiguration(com.google.devtools.build.lib.bazel.rules.BazelConfiguration) ArrayList(java.util.ArrayList) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 2 with Artifact

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;
}
Also used : JavaCompilationArgsProvider(com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 3 with Artifact

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();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 4 with Artifact

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();
}
Also used : Runfiles(com.google.devtools.build.lib.analysis.Runfiles) ExecutableSymlinkAction(com.google.devtools.build.lib.analysis.actions.ExecutableSymlinkAction) RunfilesSupport(com.google.devtools.build.lib.analysis.RunfilesSupport) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) RunfilesProvider(com.google.devtools.build.lib.analysis.RunfilesProvider) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 5 with Artifact

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();
}
Also used : Runfiles(com.google.devtools.build.lib.analysis.Runfiles) NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) RunfilesProvider(com.google.devtools.build.lib.analysis.RunfilesProvider) Artifact(com.google.devtools.build.lib.actions.Artifact)

Aggregations

Artifact (com.google.devtools.build.lib.actions.Artifact)659 Test (org.junit.Test)242 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)184 TreeFileArtifact (com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact)83 SpecialArtifact (com.google.devtools.build.lib.actions.Artifact.SpecialArtifact)65 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)64 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)51 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)50 ImmutableList (com.google.common.collect.ImmutableList)46 Action (com.google.devtools.build.lib.actions.Action)42 IOException (java.io.IOException)42 ArrayList (java.util.ArrayList)40 SpawnAction (com.google.devtools.build.lib.analysis.actions.SpawnAction)38 Path (com.google.devtools.build.lib.vfs.Path)37 HashMap (java.util.HashMap)37 Map (java.util.Map)36 Root (com.google.devtools.build.lib.actions.Root)31 LinkedHashMap (java.util.LinkedHashMap)31 ImmutableMap (com.google.common.collect.ImmutableMap)28 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)28