Search in sources :

Example 1 with Substitution

use of com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution in project bazel by bazelbuild.

the class DataBinding method createAnnotationFile.

/**
   * Creates and returns the generated Java source that data binding's annotation processor
   * reads to translate layout info xml (from {@link #getLayoutInfoFile} into the classes that
   * end user code consumes.
   */
static Artifact createAnnotationFile(RuleContext ruleContext, boolean isLibrary) {
    Template template = Template.forResource(DataBinding.class, "databinding_annotation_template.txt");
    List<Substitution> subs = new ArrayList<>();
    subs.add(Substitution.of("%module_package%", AndroidCommon.getJavaPackage(ruleContext)));
    // TODO(gregce): clarify or remove the sdk root
    subs.add(Substitution.of("%sdk_root%", "/not/used"));
    subs.add(Substitution.of("%layout_info_dir%", getLayoutInfoFile(ruleContext).getExecPath().getParentDirectory().toString()));
    // Unused.
    subs.add(Substitution.of("%export_class_list_to%", "/tmp/exported_classes"));
    subs.add(Substitution.of("%is_library%", Boolean.toString(isLibrary)));
    // TODO(gregce): update this
    subs.add(Substitution.of("%min_sdk%", "14"));
    Artifact output = ruleContext.getPackageRelativeArtifact(String.format("databinding/%s/DataBindingInfo.java", ruleContext.getLabel().getName()), ruleContext.getConfiguration().getGenfilesDirectory());
    ruleContext.registerAction(new TemplateExpansionAction(ruleContext.getActionOwner(), output, template, subs, false));
    return output;
}
Also used : TemplateExpansionAction(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction) Substitution(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution) ArrayList(java.util.ArrayList) Artifact(com.google.devtools.build.lib.actions.Artifact) Template(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Template)

Example 2 with Substitution

use of com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution in project bazel by bazelbuild.

the class TestSupport method registerTestScriptSubstitutionAction.

private void registerTestScriptSubstitutionAction() throws InterruptedException {
    // testBundleIpa is the bundle actually containing the tests.
    Artifact testBundleIpa = testBundleIpa();
    String runMemleaks = ruleContext.getFragment(ObjcConfiguration.class).runMemleaks() ? "true" : "false";
    ImmutableMap<String, String> testEnv = ruleContext.getConfiguration().getTestEnv();
    // The substitutions below are common for simulator and lab device.
    ImmutableList.Builder<Substitution> substitutions = new ImmutableList.Builder<Substitution>().add(Substitution.of("%(memleaks)s", runMemleaks)).add(Substitution.of("%(test_app_ipa)s", testBundleIpa.getRootRelativePathString())).add(Substitution.of("%(test_app_name)s", baseNameWithoutIpa(testBundleIpa))).add(Substitution.of("%(test_bundle_path)s", testBundleIpa.getRootRelativePathString())).add(Substitution.of("%(plugin_jars)s", Artifact.joinRootRelativePaths(":", plugins())));
    substitutions.add(Substitution.ofSpaceSeparatedMap("%(test_env)s", testEnv));
    // testHarnessIpa is the app being tested in the case where testBundleIpa is a .xctest bundle.
    Optional<Artifact> testHarnessIpa = testHarnessIpa();
    if (testHarnessIpa.isPresent()) {
        substitutions.add(Substitution.of("%(xctest_app_ipa)s", testHarnessIpa.get().getRootRelativePathString())).add(Substitution.of("%(xctest_app_name)s", baseNameWithoutIpa(testHarnessIpa.get()))).add(Substitution.of("%(test_host_path)s", testHarnessIpa.get().getRootRelativePathString()));
    } else {
        substitutions.add(Substitution.of("%(xctest_app_ipa)s", "")).add(Substitution.of("%(xctest_app_name)s", "")).add(Substitution.of("%(test_host_path)s", ""));
    }
    if (ruleContext.attributes().get(IosTest.IS_XCTEST_ATTR, Type.BOOLEAN)) {
        substitutions.add(Substitution.of("%(test_type)s", "XCTEST"));
    } else {
        substitutions.add(Substitution.of("%(test_type)s", "KIF"));
    }
    Artifact template;
    if (!runWithLabDevice()) {
        substitutions.addAll(substitutionsForSimulator());
        template = ruleContext.getPrerequisiteArtifact(IosTest.TEST_TEMPLATE_ATTR, Mode.TARGET);
    } else {
        substitutions.addAll(substitutionsForLabDevice());
        template = testTemplateForLabDevice();
    }
    ruleContext.registerAction(new TemplateExpansionAction(ruleContext.getActionOwner(), template, generatedTestScript(), substitutions.build(), /*executable=*/
    true));
}
Also used : TemplateExpansionAction(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction) Substitution(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution) ImmutableList(com.google.common.collect.ImmutableList) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 3 with Substitution

use of com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution 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 4 with Substitution

use of com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution in project bazel by bazelbuild.

the class SkylarkRuleImplementationFunctionsTest method testCreateTemplateActionWithWrongEncoding.

/**
   * Simulates the fact that the Parser currently uses Latin1 to read BUILD files, while users
   * usually write those files using UTF-8 encoding. Currently, the string-valued 'substitutions'
   * parameter of the template_action function contains a hack that assumes its input is a UTF-8
   * encoded string which has been ingested as Latin 1. The hack converts the string to its
   * "correct" UTF-8 value. Once {@link com.google.devtools.build.lib.syntax.ParserInputSource#create(com.google.devtools.build.lib.vfs.Path)}
   * parses files using UTF-8 and the hack for the substituations parameter is removed, this test
   * will fail.
   */
@Test
public void testCreateTemplateActionWithWrongEncoding() throws Exception {
    // The following array contains bytes that represent a string of length two when treated as
    // UTF-8 and a string of length four when treated as ISO-8859-1 (a.k.a. Latin 1).
    byte[] bytesToDecode = { (byte) 0xC2, (byte) 0xA2, (byte) 0xC2, (byte) 0xA2 };
    Charset latin1 = StandardCharsets.ISO_8859_1;
    Charset utf8 = StandardCharsets.UTF_8;
    SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
    TemplateExpansionAction action = (TemplateExpansionAction) evalRuleContextCode(ruleContext, "ruleContext.template_action(", "  template = ruleContext.files.srcs[0],", "  output = ruleContext.files.srcs[1],", "  substitutions = {'a': '" + new String(bytesToDecode, latin1) + "'},", "  executable = False)");
    List<Substitution> substitutions = action.getSubstitutions();
    assertThat(substitutions).hasSize(1);
    assertThat(substitutions.get(0).getValue()).isEqualTo(new String(bytesToDecode, utf8));
}
Also used : TemplateExpansionAction(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction) Substitution(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution) Charset(java.nio.charset.Charset) SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) Test(org.junit.Test)

Example 5 with Substitution

use of com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution in project bazel by bazelbuild.

the class ReleaseBundlingSupport method registerGenerateRunnerScriptAction.

/**
   * Registers an action to generate a runner script based on a template.
   */
ReleaseBundlingSupport registerGenerateRunnerScriptAction(Artifact runnerScript, Artifact ipaInput) {
    ObjcConfiguration objcConfiguration = ObjcRuleClasses.objcConfiguration(ruleContext);
    String escapedSimDevice = ShellUtils.shellEscape(objcConfiguration.getIosSimulatorDevice());
    String escapedSdkVersion = ShellUtils.shellEscape(objcConfiguration.getIosSimulatorVersion().toString());
    ImmutableList<Substitution> substitutions = ImmutableList.of(Substitution.of("%app_name%", ruleContext.getLabel().getName()), Substitution.of("%ipa_file%", ipaInput.getRunfilesPathString()), Substitution.of("%sim_device%", escapedSimDevice), Substitution.of("%sdk_version%", escapedSdkVersion), Substitution.of("%std_redirect_dylib_path%", attributes.stdRedirectDylib().getRunfilesPathString()));
    ruleContext.registerAction(new TemplateExpansionAction(ruleContext.getActionOwner(), attributes.runnerScriptTemplate(), runnerScript, substitutions, true));
    return this;
}
Also used : TemplateExpansionAction(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction) Substitution(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution)

Aggregations

TemplateExpansionAction (com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction)5 Substitution (com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution)5 Artifact (com.google.devtools.build.lib.actions.Artifact)3 ArrayList (java.util.ArrayList)2 ImmutableList (com.google.common.collect.ImmutableList)1 ComputedSubstitution (com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.ComputedSubstitution)1 Template (com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Template)1 BazelConfiguration (com.google.devtools.build.lib.bazel.rules.BazelConfiguration)1 SkylarkRuleContext (com.google.devtools.build.lib.rules.SkylarkRuleContext)1 JavaCompilationArtifacts (com.google.devtools.build.lib.rules.java.JavaCompilationArtifacts)1 Jvm (com.google.devtools.build.lib.rules.java.Jvm)1 Charset (java.nio.charset.Charset)1 Test (org.junit.Test)1