Search in sources :

Example 6 with TemplateExpansionAction

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

the class BazelPythonSemantics method createExecutable.

@Override
public void createExecutable(RuleContext ruleContext, PyCommon common, CcLinkParamsStore ccLinkParamsStore, NestedSet<PathFragment> imports) throws InterruptedException {
    String main = common.determineMainExecutableSource(/*withWorkspaceName=*/
    true);
    Artifact executable = common.getExecutable();
    BazelPythonConfiguration config = ruleContext.getFragment(BazelPythonConfiguration.class);
    String pythonBinary;
    switch(common.getVersion()) {
        case PY2:
            pythonBinary = config.getPython2Path();
            break;
        case PY3:
            pythonBinary = config.getPython3Path();
            break;
        default:
            throw new IllegalStateException();
    }
    if (!ruleContext.getConfiguration().buildPythonZip()) {
        ruleContext.registerAction(new TemplateExpansionAction(ruleContext.getActionOwner(), executable, STUB_TEMPLATE, ImmutableList.of(Substitution.of("%main%", main), Substitution.of("%python_binary%", pythonBinary), Substitution.of("%imports%", Joiner.on(":").join(imports)), Substitution.of("%workspace_name%", ruleContext.getWorkspaceName()), Substitution.of("%is_zipfile%", "False"), Substitution.of("%import_all%", config.getImportAllRepositories() ? "True" : "False")), true));
    } else {
        Artifact zipFile = getPythonZipArtifact(ruleContext, executable);
        Artifact templateMain = getPythonTemplateMainArtifact(ruleContext, executable);
        // The executable zip file will unzip itself into a tmp directory and then run from there
        ruleContext.registerAction(new TemplateExpansionAction(ruleContext.getActionOwner(), templateMain, STUB_TEMPLATE, ImmutableList.of(Substitution.of("%main%", main), Substitution.of("%python_binary%", pythonBinary), Substitution.of("%imports%", Joiner.on(":").join(imports)), Substitution.of("%workspace_name%", ruleContext.getWorkspaceName()), Substitution.of("%is_zipfile%", "True"), Substitution.of("%import_all%", config.getImportAllRepositories() ? "True" : "False")), true));
        ruleContext.registerAction(new SpawnAction.Builder().addInput(zipFile).addOutput(executable).setShellCommand("echo '#!/usr/bin/env python' | cat - " + zipFile.getExecPathString() + " > " + executable.getExecPathString()).useDefaultShellEnvironment().setMnemonic("BuildBinary").build(ruleContext));
    }
}
Also used : TemplateExpansionAction(com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction) SpawnAction(com.google.devtools.build.lib.analysis.actions.SpawnAction) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 7 with TemplateExpansionAction

use of com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction 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)7 Substitution (com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution)5 Artifact (com.google.devtools.build.lib.actions.Artifact)4 SkylarkRuleContext (com.google.devtools.build.lib.rules.SkylarkRuleContext)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ImmutableList (com.google.common.collect.ImmutableList)1 SpawnAction (com.google.devtools.build.lib.analysis.actions.SpawnAction)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 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