Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.analysis; |
| 16 | |
| 17 | import com.google.common.annotations.VisibleForTesting; |
| 18 | import com.google.common.collect.ImmutableCollection; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.common.collect.Iterables; |
| 22 | import com.google.common.collect.Sets; |
| 23 | import com.google.devtools.build.lib.actions.Artifact; |
ulfjack | 7e7d94e | 2017-06-06 05:08:49 -0400 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.ExecutionRequirements; |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.actions.RunfilesSupplier; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.cmdline.Label; |
lberki | 20d68f0 | 2018-09-19 06:30:24 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
Dmitry Lomov | 82434eb | 2016-01-29 12:35:12 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.util.OS; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | import com.google.devtools.build.lib.util.Pair; |
| 31 | import com.google.devtools.build.lib.vfs.PathFragment; |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 32 | import com.google.errorprone.annotations.CanIgnoreReturnValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | import java.util.Collection; |
| 34 | import java.util.HashMap; |
| 35 | import java.util.List; |
| 36 | import java.util.Map; |
Francois-Rene Rideau | 79be96f | 2015-10-07 19:25:38 +0000 | [diff] [blame] | 37 | import javax.annotation.Nullable; |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 38 | import net.starlark.java.eval.Sequence; |
| 39 | import net.starlark.java.eval.StarlarkList; |
Francois-Rene Rideau | 79be96f | 2015-10-07 19:25:38 +0000 | [diff] [blame] | 40 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | /** |
John Cater | 1ba41a8 | 2017-01-20 16:19:53 +0000 | [diff] [blame] | 42 | * Provides shared functionality for parameterized command-line launching. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | * Also used by {@link com.google.devtools.build.lib.rules.extra.ExtraActionFactory}. |
| 44 | * |
| 45 | * Two largely independent separate sets of functionality are provided: |
| 46 | * 1- string interpolation for {@code $(location[s] ...)} and {@code $(MakeVariable)} |
| 47 | * 2- a utility to build potentially large command lines (presumably made of multiple commands), |
| 48 | * that if presumed too large for the kernel's taste can be dumped into a shell script |
| 49 | * that will contain the same commands, |
| 50 | * at which point the shell script is added to the list of inputs. |
| 51 | */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 52 | public final class CommandHelper { |
| 53 | |
| 54 | /** |
jcater | 71e46d0 | 2018-08-21 00:43:31 -0700 | [diff] [blame] | 55 | * Returns a new {@link Builder} to create a {@link CommandHelper} based on the given {@link |
| 56 | * RuleContext}. |
| 57 | */ |
| 58 | public static Builder builder(RuleContext ruleContext) { |
| 59 | return new Builder(ruleContext); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Builder class to assist with creating an instance of {@link CommandHelper}. The Builder can |
| 64 | * optionally add additional tools as dependencies, and a map of labels to be resolved. |
| 65 | */ |
| 66 | public static final class Builder { |
| 67 | private final RuleContext ruleContext; |
| 68 | private final ImmutableList.Builder<Iterable<? extends TransitiveInfoCollection>> |
| 69 | toolDependencies = ImmutableList.builder(); |
| 70 | private final ImmutableMap.Builder<Label, Iterable<Artifact>> labelMap = ImmutableMap.builder(); |
| 71 | |
| 72 | private Builder(RuleContext ruleContext) { |
| 73 | this.ruleContext = ruleContext; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Adds tools, as a set of executable binaries, by fetching them from the given attribute on the |
Googler | 92f0d6a | 2023-01-06 09:45:16 -0800 | [diff] [blame] | 78 | * {@code ruleContext}. Populates manifests, remoteRunfiles and label map where required. |
jcater | 71e46d0 | 2018-08-21 00:43:31 -0700 | [diff] [blame] | 79 | */ |
jcater | cb8e89b | 2019-06-05 11:19:25 -0700 | [diff] [blame] | 80 | public Builder addToolDependencies(String toolAttributeName) { |
| 81 | List<? extends TransitiveInfoCollection> dependencies = |
jcater | e9168c4 | 2020-08-31 10:35:12 -0700 | [diff] [blame] | 82 | ruleContext.getPrerequisites(toolAttributeName); |
jcater | cb8e89b | 2019-06-05 11:19:25 -0700 | [diff] [blame] | 83 | return addToolDependencies(dependencies); |
| 84 | } |
| 85 | |
| 86 | /** |
jcater | 71e46d0 | 2018-08-21 00:43:31 -0700 | [diff] [blame] | 87 | * Adds tools, as a set of executable binaries. Populates manifests, remoteRunfiles and label |
| 88 | * map where required. |
| 89 | */ |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 90 | @CanIgnoreReturnValue |
jcater | 71e46d0 | 2018-08-21 00:43:31 -0700 | [diff] [blame] | 91 | public Builder addToolDependencies( |
| 92 | Iterable<? extends TransitiveInfoCollection> toolDependencies) { |
| 93 | this.toolDependencies.add(toolDependencies); |
| 94 | return this; |
| 95 | } |
| 96 | |
| 97 | /** Adds files to set of known files of label. Used for resolving $(location) variables. */ |
Kurt Alfred Kluever | 4192ca6 | 2022-07-05 06:30:29 -0700 | [diff] [blame] | 98 | @CanIgnoreReturnValue |
jcater | 71e46d0 | 2018-08-21 00:43:31 -0700 | [diff] [blame] | 99 | public Builder addLabelMap(Map<Label, ? extends Iterable<Artifact>> labelMap) { |
| 100 | this.labelMap.putAll(labelMap); |
| 101 | return this; |
| 102 | } |
| 103 | |
| 104 | /** Returns the built {@link CommandHelper}. */ |
| 105 | public CommandHelper build() { |
Googler | 92ce115 | 2022-02-01 06:37:06 -0800 | [diff] [blame] | 106 | return new CommandHelper(ruleContext, toolDependencies.build(), labelMap.buildOrThrow()); |
jcater | 71e46d0 | 2018-08-21 00:43:31 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 111 | * Maximum total command-line length, in bytes, not counting "/bin/bash -c ". |
| 112 | * If the command is very long, then we write the command to a script file, |
| 113 | * to avoid overflowing any limits on command-line length. |
| 114 | * For short commands, we just use /bin/bash -c command. |
Dmitry Lomov | 82434eb | 2016-01-29 12:35:12 +0000 | [diff] [blame] | 115 | * |
| 116 | * Maximum command line length on Windows is 32767[1], but for cmd.exe it is 8192[2]. |
| 117 | * [1] https://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx |
| 118 | * [2] https://support.microsoft.com/en-us/kb/830473. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 119 | */ |
| 120 | @VisibleForTesting |
Dmitry Lomov | 82434eb | 2016-01-29 12:35:12 +0000 | [diff] [blame] | 121 | public static int maxCommandLength = OS.getCurrent() == OS.WINDOWS ? 8000 : 64000; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 122 | |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 123 | /** {@link RunfilesSupplier}s for tools used by this rule. */ |
Googler | 4871cb0 | 2019-11-12 17:16:42 -0800 | [diff] [blame] | 124 | private final Sequence<RunfilesSupplier> toolsRunfilesSuppliers; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * Use labelMap for heuristically expanding labels (does not include "outs") |
| 128 | * This is similar to heuristic location expansion in LocationExpander |
| 129 | * and should be kept in sync. |
| 130 | */ |
ulfjack | af67774 | 2017-10-02 10:55:41 +0200 | [diff] [blame] | 131 | private final ImmutableMap<Label, ImmutableCollection<Artifact>> labelMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * The ruleContext this helper works on |
| 135 | */ |
| 136 | private final RuleContext ruleContext; |
| 137 | |
| 138 | /** |
| 139 | * Output executable files from the 'tools' attribute. |
| 140 | */ |
lberki | 20d68f0 | 2018-09-19 06:30:24 -0700 | [diff] [blame] | 141 | private final NestedSet<Artifact> resolvedTools; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 142 | |
| 143 | /** |
Googler | f49bb0c | 2015-02-11 16:18:28 +0000 | [diff] [blame] | 144 | * Creates a {@link CommandHelper}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 145 | * |
jcater | 71e46d0 | 2018-08-21 00:43:31 -0700 | [diff] [blame] | 146 | * @param toolsList resolves sets of tools into set of executable binaries. Populates manifests, |
Googler | f49bb0c | 2015-02-11 16:18:28 +0000 | [diff] [blame] | 147 | * remoteRunfiles and label map where required. |
| 148 | * @param labelMap adds files to set of known files of label. Used for resolving $(location) |
| 149 | * variables. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 150 | */ |
jcater | 71e46d0 | 2018-08-21 00:43:31 -0700 | [diff] [blame] | 151 | private CommandHelper( |
Brian Silverman | 53c3ce1 | 2015-08-28 09:17:14 +0000 | [diff] [blame] | 152 | RuleContext ruleContext, |
jcater | 71e46d0 | 2018-08-21 00:43:31 -0700 | [diff] [blame] | 153 | ImmutableList<Iterable<? extends TransitiveInfoCollection>> toolsList, |
John Cater | c637fbb | 2017-01-27 15:55:53 +0000 | [diff] [blame] | 154 | ImmutableMap<Label, ? extends Iterable<Artifact>> labelMap) { |
jcater | 71e46d0 | 2018-08-21 00:43:31 -0700 | [diff] [blame] | 155 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 156 | this.ruleContext = ruleContext; |
| 157 | |
lberki | 20d68f0 | 2018-09-19 06:30:24 -0700 | [diff] [blame] | 158 | NestedSetBuilder<Artifact> resolvedToolsBuilder = NestedSetBuilder.stableOrder(); |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 159 | ImmutableList.Builder<RunfilesSupplier> toolsRunfilesBuilder = ImmutableList.builder(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 160 | Map<Label, Collection<Artifact>> tempLabelMap = new HashMap<>(); |
| 161 | |
John Cater | c637fbb | 2017-01-27 15:55:53 +0000 | [diff] [blame] | 162 | for (Map.Entry<Label, ? extends Iterable<Artifact>> entry : labelMap.entrySet()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 163 | Iterables.addAll(mapGet(tempLabelMap, entry.getKey()), entry.getValue()); |
| 164 | } |
| 165 | |
jcater | 71e46d0 | 2018-08-21 00:43:31 -0700 | [diff] [blame] | 166 | for (Iterable<? extends TransitiveInfoCollection> tools : toolsList) { |
Googler | 92f0d6a | 2023-01-06 09:45:16 -0800 | [diff] [blame] | 167 | for (TransitiveInfoCollection dep : tools) { // (Note: exec configuration) |
Googler | fdfe0f1 | 2018-09-18 08:13:30 -0700 | [diff] [blame] | 168 | |
jcater | 71e46d0 | 2018-08-21 00:43:31 -0700 | [diff] [blame] | 169 | FilesToRunProvider tool = dep.getProvider(FilesToRunProvider.class); |
| 170 | if (tool == null) { |
| 171 | continue; |
| 172 | } |
Brian Silverman | 53c3ce1 | 2015-08-28 09:17:14 +0000 | [diff] [blame] | 173 | |
lberki | 20d68f0 | 2018-09-19 06:30:24 -0700 | [diff] [blame] | 174 | NestedSet<Artifact> files = tool.getFilesToRun(); |
leba | 92ae4e3 | 2021-06-28 07:47:02 -0700 | [diff] [blame] | 175 | resolvedToolsBuilder.addTransitive(files); |
jhorvitz | fb1c369 | 2020-12-08 10:27:10 -0800 | [diff] [blame] | 176 | |
| 177 | Label label = AliasProvider.getDependencyLabel(dep); |
jcater | 71e46d0 | 2018-08-21 00:43:31 -0700 | [diff] [blame] | 178 | Artifact executableArtifact = tool.getExecutable(); |
| 179 | // If the label has an executable artifact add that to the multimaps. |
| 180 | if (executableArtifact != null) { |
| 181 | mapGet(tempLabelMap, label).add(executableArtifact); |
leba | 92ae4e3 | 2021-06-28 07:47:02 -0700 | [diff] [blame] | 182 | // Also send the runfiles when running remotely. |
| 183 | toolsRunfilesBuilder.add(tool.getRunfilesSupplier()); |
jcater | 71e46d0 | 2018-08-21 00:43:31 -0700 | [diff] [blame] | 184 | } else { |
| 185 | // Map all depArtifacts to the respective label using the multimaps. |
ulfjack | 6c3908c | 2019-12-18 05:03:36 -0800 | [diff] [blame] | 186 | mapGet(tempLabelMap, label).addAll(files.toList()); |
jcater | 71e46d0 | 2018-08-21 00:43:31 -0700 | [diff] [blame] | 187 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
lberki | 20d68f0 | 2018-09-19 06:30:24 -0700 | [diff] [blame] | 191 | this.resolvedTools = resolvedToolsBuilder.build(); |
Googler | 9257870 | 2019-11-21 12:19:31 -0800 | [diff] [blame] | 192 | this.toolsRunfilesSuppliers = StarlarkList.immutableCopyOf(toolsRunfilesBuilder.build()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 193 | ImmutableMap.Builder<Label, ImmutableCollection<Artifact>> labelMapBuilder = |
| 194 | ImmutableMap.builder(); |
jcater | 3674591 | 2018-05-01 13:20:00 -0700 | [diff] [blame] | 195 | for (Map.Entry<Label, Collection<Artifact>> entry : tempLabelMap.entrySet()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 196 | labelMapBuilder.put(entry.getKey(), ImmutableList.copyOf(entry.getValue())); |
| 197 | } |
Googler | 92ce115 | 2022-02-01 06:37:06 -0800 | [diff] [blame] | 198 | this.labelMap = labelMapBuilder.buildOrThrow(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 199 | } |
| 200 | |
lberki | 20d68f0 | 2018-09-19 06:30:24 -0700 | [diff] [blame] | 201 | public NestedSet<Artifact> getResolvedTools() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 202 | return resolvedTools; |
| 203 | } |
| 204 | |
Googler | 4871cb0 | 2019-11-12 17:16:42 -0800 | [diff] [blame] | 205 | public Sequence<RunfilesSupplier> getToolsRunfilesSuppliers() { |
Michajlo Matijkiw | 4a87738 | 2017-01-27 19:30:34 +0000 | [diff] [blame] | 206 | return toolsRunfilesSuppliers; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 207 | } |
| 208 | |
ulfjack | 515ba89 | 2017-12-04 05:48:31 -0800 | [diff] [blame] | 209 | public ImmutableMap<Label, ImmutableCollection<Artifact>> getLabelMap() { |
| 210 | return labelMap; |
| 211 | } |
| 212 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 213 | // Returns the value in the specified corresponding to 'key', creating and |
| 214 | // inserting an empty container if absent. We use Map not Multimap because |
| 215 | // we need to distinguish the cases of "empty value" and "absent key". |
| 216 | private static Collection<Artifact> mapGet(Map<Label, Collection<Artifact>> map, Label key) { |
| 217 | Collection<Artifact> values = map.get(key); |
| 218 | if (values == null) { |
| 219 | // We use sets not lists, because it's conceivable that the same artifact |
| 220 | // could appear twice, e.g. in "srcs" and "deps". |
| 221 | values = Sets.newHashSet(); |
| 222 | map.put(key, values); |
| 223 | } |
| 224 | return values; |
| 225 | } |
| 226 | |
gregce | ca48e9a | 2020-04-14 08:54:38 -0700 | [diff] [blame] | 227 | /** Resolves a command, and expands known locations for $(location) variables. */ |
| 228 | @Deprecated // Only exists to support a legacy Starlark API. |
schmitt | caa9e08 | 2017-12-19 14:46:14 -0800 | [diff] [blame] | 229 | public String resolveCommandAndExpandLabels( |
| 230 | String command, @Nullable String attribute, boolean allowDataInLabel) { |
| 231 | LocationExpander expander; |
| 232 | if (allowDataInLabel) { |
| 233 | expander = LocationExpander.withExecPathsAndData(ruleContext, labelMap); |
| 234 | } else { |
| 235 | expander = LocationExpander.withExecPaths(ruleContext, labelMap); |
ulfjack | af67774 | 2017-10-02 10:55:41 +0200 | [diff] [blame] | 236 | } |
schmitt | caa9e08 | 2017-12-19 14:46:14 -0800 | [diff] [blame] | 237 | if (attribute != null) { |
| 238 | command = expander.expandAttribute(attribute, command); |
| 239 | } else { |
| 240 | command = expander.expand(command); |
| 241 | } |
| 242 | return command; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Expands labels occurring in the string "expr" in the rule 'cmd'. |
| 247 | * Each label must be valid, be a declared prerequisite, and expand to a |
| 248 | * unique path. |
| 249 | * |
| 250 | * <p>If the expansion fails, an attribute error is reported and the original |
| 251 | * expression is returned. |
| 252 | */ |
ulfjack | 515ba89 | 2017-12-04 05:48:31 -0800 | [diff] [blame] | 253 | public String expandLabelsHeuristically(String expr) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 254 | try { |
| 255 | return LabelExpander.expand(expr, labelMap, ruleContext.getLabel()); |
| 256 | } catch (LabelExpander.NotUniqueExpansionException nuee) { |
| 257 | ruleContext.attributeError("cmd", nuee.getMessage()); |
| 258 | return expr; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | private static Pair<List<String>, Artifact> buildCommandLineMaybeWithScriptFile( |
Yun Peng | ae470e1 | 2019-07-31 05:56:58 -0700 | [diff] [blame] | 263 | RuleContext ruleContext, String command, CommandConstructor constructor) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 264 | List<String> argv; |
| 265 | Artifact scriptFileArtifact = null; |
| 266 | if (command.length() <= maxCommandLength) { |
Yun Peng | ae470e1 | 2019-07-31 05:56:58 -0700 | [diff] [blame] | 267 | argv = constructor.asExecArgv(command); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 268 | } else { |
| 269 | // Use script file. |
Yun Peng | ae470e1 | 2019-07-31 05:56:58 -0700 | [diff] [blame] | 270 | scriptFileArtifact = constructor.commandAsScript(ruleContext, command); |
| 271 | argv = constructor.asExecArgv(scriptFileArtifact); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 272 | } |
| 273 | return Pair.of(argv, scriptFileArtifact); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 276 | /** |
László Csomor | 36c2ac5 | 2017-08-31 14:35:12 +0200 | [diff] [blame] | 277 | * If {@code command} is too long, creates a helper shell script that runs that command. |
| 278 | * |
| 279 | * <p>Returns the {@link Artifact} corresponding to that script. |
| 280 | * |
| 281 | * <p>Otherwise, when {@code command} is shorter than the platform's shell's command length limit, |
| 282 | * this method does nothing and returns null. |
| 283 | */ |
| 284 | @Nullable |
Yun Peng | ae470e1 | 2019-07-31 05:56:58 -0700 | [diff] [blame] | 285 | public static Artifact commandHelperScriptMaybe( |
| 286 | RuleContext ruleCtx, String command, CommandConstructor constructor) { |
László Csomor | 36c2ac5 | 2017-08-31 14:35:12 +0200 | [diff] [blame] | 287 | if (command.length() <= maxCommandLength) { |
| 288 | return null; |
| 289 | } else { |
Yun Peng | ae470e1 | 2019-07-31 05:56:58 -0700 | [diff] [blame] | 290 | return constructor.commandAsScript(ruleCtx, command); |
László Csomor | 36c2ac5 | 2017-08-31 14:35:12 +0200 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
Googler | f49bb0c | 2015-02-11 16:18:28 +0000 | [diff] [blame] | 295 | * Builds the set of command-line arguments using the specified shell path. Creates a bash script |
Laszlo Csomor | 382089e | 2018-05-28 06:33:47 -0700 | [diff] [blame] | 296 | * if the command line is longer than the allowed maximum {@link #maxCommandLength}. Fixes up the |
| 297 | * input artifact list with the created bash script when required. |
Googler | f49bb0c | 2015-02-11 16:18:28 +0000 | [diff] [blame] | 298 | */ |
| 299 | public List<String> buildCommandLine( |
Yun Peng | ae470e1 | 2019-07-31 05:56:58 -0700 | [diff] [blame] | 300 | String command, NestedSetBuilder<Artifact> inputs, CommandConstructor constructor) { |
Googler | f49bb0c | 2015-02-11 16:18:28 +0000 | [diff] [blame] | 301 | Pair<List<String>, Artifact> argvAndScriptFile = |
Yun Peng | ae470e1 | 2019-07-31 05:56:58 -0700 | [diff] [blame] | 302 | buildCommandLineMaybeWithScriptFile(ruleContext, command, constructor); |
Googler | f49bb0c | 2015-02-11 16:18:28 +0000 | [diff] [blame] | 303 | if (argvAndScriptFile.second != null) { |
| 304 | inputs.add(argvAndScriptFile.second); |
| 305 | } |
| 306 | return argvAndScriptFile.first; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
Laszlo Csomor | 382089e | 2018-05-28 06:33:47 -0700 | [diff] [blame] | 310 | * Builds the set of command-line arguments. Creates a bash script if the command line is longer |
| 311 | * than the allowed maximum {@link #maxCommandLength}. Fixes up the input artifact list with the |
| 312 | * created bash script when required. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 313 | */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 314 | public List<String> buildCommandLine( |
Yun Peng | ae470e1 | 2019-07-31 05:56:58 -0700 | [diff] [blame] | 315 | String command, List<Artifact> inputs, CommandConstructor constructor) { |
Laszlo Csomor | 382089e | 2018-05-28 06:33:47 -0700 | [diff] [blame] | 316 | Pair<List<String>, Artifact> argvAndScriptFile = |
Yun Peng | ae470e1 | 2019-07-31 05:56:58 -0700 | [diff] [blame] | 317 | buildCommandLineMaybeWithScriptFile(ruleContext, command, constructor); |
Googler | f49bb0c | 2015-02-11 16:18:28 +0000 | [diff] [blame] | 318 | if (argvAndScriptFile.second != null) { |
| 319 | inputs.add(argvAndScriptFile.second); |
| 320 | } |
| 321 | return argvAndScriptFile.first; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 322 | } |
Chris Parsons | 9a9573b | 2016-02-10 21:15:11 +0000 | [diff] [blame] | 323 | |
Laszlo Csomor | 382089e | 2018-05-28 06:33:47 -0700 | [diff] [blame] | 324 | /** Returns the path to the shell for an action with the given execution requirements. */ |
Yun Peng | ae470e1 | 2019-07-31 05:56:58 -0700 | [diff] [blame] | 325 | private static PathFragment shellPath( |
| 326 | Map<String, String> executionInfo, PathFragment shExecutable) { |
Chris Parsons | 9a9573b | 2016-02-10 21:15:11 +0000 | [diff] [blame] | 327 | // Use vanilla /bin/bash for actions running on mac machines. |
John Cater | 1f42581 | 2017-02-03 16:42:22 +0000 | [diff] [blame] | 328 | return executionInfo.containsKey(ExecutionRequirements.REQUIRES_DARWIN) |
lberki | 78651d4 | 2018-04-06 01:52:58 -0700 | [diff] [blame] | 329 | ? PathFragment.create("/bin/bash") |
Laszlo Csomor | 382089e | 2018-05-28 06:33:47 -0700 | [diff] [blame] | 330 | : shExecutable; |
Chris Parsons | 9a9573b | 2016-02-10 21:15:11 +0000 | [diff] [blame] | 331 | } |
Yun Peng | ae470e1 | 2019-07-31 05:56:58 -0700 | [diff] [blame] | 332 | |
| 333 | public static BashCommandConstructor buildBashCommandConstructor( |
| 334 | Map<String, String> executionInfo, PathFragment shExecutable, String scriptPostFix) { |
| 335 | return new BashCommandConstructor(shellPath(executionInfo, shExecutable), scriptPostFix); |
| 336 | } |
Yun Peng | d3bacfb | 2019-08-01 03:42:29 -0700 | [diff] [blame] | 337 | |
| 338 | public static WindowsBatchCommandConstructor buildWindowsBatchCommandConstructor( |
| 339 | String scriptPostFix) { |
| 340 | return new WindowsBatchCommandConstructor(scriptPostFix); |
| 341 | } |
| 342 | |
| 343 | public static WindowsPowershellCommandConstructor buildWindowsPowershellCommandConstructor( |
| 344 | String scriptPostFix) { |
| 345 | return new WindowsPowershellCommandConstructor(scriptPostFix); |
| 346 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 347 | } |