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 | |
tomlu | 9a5d1af | 2018-02-15 13:11:08 -0800 | [diff] [blame] | 15 | package com.google.devtools.build.lib.actions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 16 | |
| 17 | import com.google.common.base.Joiner; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Rumou Duan | 6d42e33 | 2016-05-19 15:33:38 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.lib.collect.CollectionUtils; |
cushon | 9fe597d | 2018-10-23 12:39:08 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.collect.IterablesChain; |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 22 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 23 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec.VisibleForSerialization; |
tomlu | ac03ccf | 2018-01-29 14:28:29 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.util.Fingerprint; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | |
tomlu | 9a5d1af | 2018-02-15 13:11:08 -0800 | [diff] [blame] | 26 | /** A representation of a list of arguments. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | public abstract class CommandLine { |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 28 | @AutoCodec |
| 29 | @VisibleForSerialization |
| 30 | static class EmptyCommandLine extends CommandLine { |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 31 | @Override |
| 32 | public Iterable<String> arguments() throws CommandLineExpansionException { |
| 33 | return ImmutableList.of(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public static final CommandLine EMPTY = new EmptyCommandLine(); |
Googler | d0e58c4 | 2017-05-24 22:20:45 +0200 | [diff] [blame] | 38 | |
tomlu | 3fb6ac3 | 2017-08-24 00:44:45 +0200 | [diff] [blame] | 39 | /** Returns the command line. */ |
| 40 | public abstract Iterable<String> arguments() throws CommandLineExpansionException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | |
| 42 | /** |
Rumou Duan | 6d42e33 | 2016-05-19 15:33:38 +0000 | [diff] [blame] | 43 | * Returns the evaluated command line with enclosed artifacts expanded by {@code artifactExpander} |
| 44 | * at execution time. |
| 45 | * |
| 46 | * <p>By default, this method just delegates to {@link #arguments()}, without performing any |
| 47 | * artifact expansion. Subclasses should override this method if they contain TreeArtifacts and |
| 48 | * need to expand them for proper argument evaluation. |
| 49 | */ |
tomlu | 3fb6ac3 | 2017-08-24 00:44:45 +0200 | [diff] [blame] | 50 | public Iterable<String> arguments(ArtifactExpander artifactExpander) |
| 51 | throws CommandLineExpansionException { |
Rumou Duan | 6d42e33 | 2016-05-19 15:33:38 +0000 | [diff] [blame] | 52 | return arguments(); |
| 53 | } |
| 54 | |
tomlu | ac03ccf | 2018-01-29 14:28:29 -0800 | [diff] [blame] | 55 | public void addToFingerprint(ActionKeyContext actionKeyContext, Fingerprint fingerprint) |
| 56 | throws CommandLineExpansionException { |
| 57 | for (String s : arguments()) { |
| 58 | fingerprint.addString(s); |
| 59 | } |
| 60 | } |
| 61 | |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 62 | @AutoCodec |
| 63 | @VisibleForSerialization |
tomlu | bf6c267 | 2018-02-20 07:06:05 -0800 | [diff] [blame] | 64 | static class SimpleCommandLine extends CommandLine { |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 65 | private Iterable<String> args; |
| 66 | |
tomlu | bf6c267 | 2018-02-20 07:06:05 -0800 | [diff] [blame] | 67 | SimpleCommandLine(Iterable<String> args) { |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 68 | this.args = args; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public Iterable<String> arguments() throws CommandLineExpansionException { |
| 73 | return args; |
| 74 | } |
| 75 | } |
| 76 | |
Googler | 48034fd | 2017-05-15 18:05:46 +0200 | [diff] [blame] | 77 | /** Returns a {@link CommandLine} backed by a copy of the given list of arguments. */ |
| 78 | public static CommandLine of(Iterable<String> arguments) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 79 | final Iterable<String> immutableArguments = CollectionUtils.makeImmutable(arguments); |
tomlu | bf6c267 | 2018-02-20 07:06:05 -0800 | [diff] [blame] | 80 | return new SimpleCommandLine(immutableArguments); |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @AutoCodec |
| 84 | @VisibleForSerialization |
tomlu | bf6c267 | 2018-02-20 07:06:05 -0800 | [diff] [blame] | 85 | static class PrefixedCommandLine extends CommandLine { |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 86 | private ImmutableList<String> executableArgs; |
| 87 | private CommandLine commandLine; |
| 88 | |
| 89 | @VisibleForSerialization |
tomlu | bf6c267 | 2018-02-20 07:06:05 -0800 | [diff] [blame] | 90 | PrefixedCommandLine(ImmutableList<String> executableArgs, CommandLine commandLine) { |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 91 | this.executableArgs = executableArgs; |
| 92 | this.commandLine = commandLine; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Iterable<String> arguments() throws CommandLineExpansionException { |
cushon | 9fe597d | 2018-10-23 12:39:08 -0700 | [diff] [blame] | 97 | return IterablesChain.concat(executableArgs, commandLine.arguments()); |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Iterable<String> arguments(ArtifactExpander artifactExpander) |
| 102 | throws CommandLineExpansionException { |
cushon | 9fe597d | 2018-10-23 12:39:08 -0700 | [diff] [blame] | 103 | return IterablesChain.concat(executableArgs, commandLine.arguments(artifactExpander)); |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 104 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 105 | } |
| 106 | |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 107 | @AutoCodec |
| 108 | @VisibleForSerialization |
tomlu | bf6c267 | 2018-02-20 07:06:05 -0800 | [diff] [blame] | 109 | static class SuffixedCommandLine extends CommandLine { |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 110 | private ImmutableList<String> executableArgs; |
| 111 | private CommandLine commandLine; |
| 112 | |
| 113 | @VisibleForSerialization |
tomlu | bf6c267 | 2018-02-20 07:06:05 -0800 | [diff] [blame] | 114 | SuffixedCommandLine(ImmutableList<String> executableArgs, CommandLine commandLine) { |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 115 | this.executableArgs = executableArgs; |
| 116 | this.commandLine = commandLine; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public Iterable<String> arguments() throws CommandLineExpansionException { |
cushon | 9fe597d | 2018-10-23 12:39:08 -0700 | [diff] [blame] | 121 | return IterablesChain.concat(commandLine.arguments(), executableArgs); |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public Iterable<String> arguments(ArtifactExpander artifactExpander) |
| 126 | throws CommandLineExpansionException { |
cushon | 9fe597d | 2018-10-23 12:39:08 -0700 | [diff] [blame] | 127 | return IterablesChain.concat(commandLine.arguments(artifactExpander), executableArgs); |
cpeyser | 10b1a5f | 2018-02-15 09:37:15 -0800 | [diff] [blame] | 128 | } |
Googler | d0e58c4 | 2017-05-24 22:20:45 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
lberki | de5e968 | 2018-10-19 01:20:34 -0700 | [diff] [blame] | 132 | * Returns a {@link CommandLine} that is constructed by prepending the {@code executableArgs} to |
| 133 | * {@code commandLine}. |
| 134 | */ |
| 135 | public static CommandLine concat( |
| 136 | final ImmutableList<String> executableArgs, final CommandLine commandLine) { |
| 137 | if (executableArgs.isEmpty()) { |
| 138 | return commandLine; |
| 139 | } |
cushon | 9fe597d | 2018-10-23 12:39:08 -0700 | [diff] [blame] | 140 | if (commandLine == EMPTY) { |
| 141 | return CommandLine.of(executableArgs); |
| 142 | } |
lberki | de5e968 | 2018-10-19 01:20:34 -0700 | [diff] [blame] | 143 | return new PrefixedCommandLine(executableArgs, commandLine); |
| 144 | } |
| 145 | |
| 146 | /** |
Googler | d0e58c4 | 2017-05-24 22:20:45 +0200 | [diff] [blame] | 147 | * Returns a {@link CommandLine} that is constructed by appending the {@code args} to {@code |
| 148 | * commandLine}. |
| 149 | */ |
| 150 | public static CommandLine concat( |
| 151 | final CommandLine commandLine, final ImmutableList<String> args) { |
| 152 | if (args.isEmpty()) { |
| 153 | return commandLine; |
| 154 | } |
cushon | 9fe597d | 2018-10-23 12:39:08 -0700 | [diff] [blame] | 155 | if (commandLine == EMPTY) { |
| 156 | return CommandLine.of(args); |
| 157 | } |
tomlu | bf6c267 | 2018-02-20 07:06:05 -0800 | [diff] [blame] | 158 | return new SuffixedCommandLine(args, commandLine); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * This helps when debugging Blaze code that uses {@link CommandLine}s, as you can see their |
| 163 | * content directly in the variable inspector. |
| 164 | */ |
| 165 | @Override |
| 166 | public String toString() { |
tomlu | 3fb6ac3 | 2017-08-24 00:44:45 +0200 | [diff] [blame] | 167 | try { |
| 168 | return Joiner.on(' ').join(arguments()); |
| 169 | } catch (CommandLineExpansionException e) { |
| 170 | return "Error in expanding command line"; |
| 171 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 172 | } |
| 173 | } |