Tobias Bosch | cfa8c24 | 2019-07-19 03:29:40 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 5 | package main |
| 6 | |
| 7 | import ( |
George Burgess IV | 26caa2f | 2020-02-28 14:36:01 -0800 | [diff] [blame] | 8 | "bytes" |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 9 | "fmt" |
| 10 | "io" |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 11 | "path/filepath" |
Tobias Bosch | 5f98f2d | 2019-08-15 17:17:57 -0700 | [diff] [blame] | 12 | "strings" |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 13 | ) |
| 14 | |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 15 | func callCompiler(env env, cfg *config, inputCmd *command) int { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 16 | var compilerErr error |
Tobias Bosch | 5edca50 | 2019-08-26 12:53:41 -0700 | [diff] [blame] | 17 | |
| 18 | if !filepath.IsAbs(inputCmd.Path) && !strings.HasPrefix(inputCmd.Path, ".") && |
| 19 | !strings.ContainsRune(inputCmd.Path, filepath.Separator) { |
Tobias Bosch | 5f98f2d | 2019-08-15 17:17:57 -0700 | [diff] [blame] | 20 | if resolvedPath, err := resolveAgainstPathEnv(env, inputCmd.Path); err == nil { |
| 21 | inputCmd = &command{ |
| 22 | Path: resolvedPath, |
| 23 | Args: inputCmd.Args, |
| 24 | EnvUpdates: inputCmd.EnvUpdates, |
| 25 | } |
| 26 | } else { |
| 27 | compilerErr = err |
| 28 | } |
| 29 | } |
| 30 | exitCode := 0 |
| 31 | if compilerErr == nil { |
Tobias Bosch | 8dd67e1 | 2019-10-28 14:26:51 -0700 | [diff] [blame] | 32 | exitCode, compilerErr = callCompilerInternal(env, cfg, inputCmd) |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 33 | } |
| 34 | if compilerErr != nil { |
| 35 | printCompilerError(env.stderr(), compilerErr) |
| 36 | exitCode = 1 |
| 37 | } |
| 38 | return exitCode |
| 39 | } |
| 40 | |
George Burgess IV | c94f243 | 2020-03-04 17:32:44 -0800 | [diff] [blame] | 41 | // Given the main builder path and the absolute path to our wrapper, returns the path to the |
| 42 | // 'real' compiler we should invoke. |
| 43 | func calculateAndroidWrapperPath(mainBuilderPath string, absWrapperPath string) string { |
| 44 | // FIXME: This combination of using the directory of the symlink but the basename of the |
| 45 | // link target is strange but is the logic that old android wrapper uses. Change this to use |
| 46 | // directory and basename either from the absWrapperPath or from the builder.path, but don't |
| 47 | // mix anymore. |
| 48 | |
| 49 | // We need to be careful here: path.Join Clean()s its result, so `./foo` will get |
| 50 | // transformed to `foo`, which isn't good since we're passing this path to exec. |
| 51 | basePart := filepath.Base(absWrapperPath) + ".real" |
| 52 | if !strings.ContainsRune(mainBuilderPath, filepath.Separator) { |
| 53 | return basePart |
| 54 | } |
| 55 | |
| 56 | dirPart := filepath.Dir(mainBuilderPath) |
| 57 | if cleanResult := filepath.Join(dirPart, basePart); strings.ContainsRune(cleanResult, filepath.Separator) { |
| 58 | return cleanResult |
| 59 | } |
| 60 | |
| 61 | return "." + string(filepath.Separator) + basePart |
| 62 | } |
| 63 | |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 64 | func callCompilerInternal(env env, cfg *config, inputCmd *command) (exitCode int, err error) { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 65 | if err := checkUnsupportedFlags(inputCmd); err != nil { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 66 | return 0, err |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 67 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 68 | mainBuilder, err := newCommandBuilder(env, cfg, inputCmd) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 69 | if err != nil { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 70 | return 0, err |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 71 | } |
Tobias Bosch | 5847281 | 2019-07-11 04:24:52 -0700 | [diff] [blame] | 72 | processPrintConfigFlag(mainBuilder) |
| 73 | processPrintCmdlineFlag(mainBuilder) |
| 74 | env = mainBuilder.env |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 75 | var compilerCmd *command |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 76 | clangSyntax := processClangSyntaxFlag(mainBuilder) |
Tobias Bosch | 8cb363f | 2019-10-17 07:44:13 -0700 | [diff] [blame] | 77 | if cfg.isAndroidWrapper { |
George Burgess IV | c94f243 | 2020-03-04 17:32:44 -0800 | [diff] [blame] | 78 | mainBuilder.path = calculateAndroidWrapperPath(mainBuilder.path, mainBuilder.absWrapperPath) |
Tobias Bosch | 8cb363f | 2019-10-17 07:44:13 -0700 | [diff] [blame] | 79 | switch mainBuilder.target.compilerType { |
| 80 | case clangType: |
| 81 | mainBuilder.addPreUserArgs(mainBuilder.cfg.clangFlags...) |
| 82 | mainBuilder.addPreUserArgs(mainBuilder.cfg.commonFlags...) |
| 83 | if _, err := processGomaCccFlags(mainBuilder); err != nil { |
| 84 | return 0, err |
| 85 | } |
| 86 | compilerCmd = mainBuilder.build() |
| 87 | case clangTidyType: |
| 88 | compilerCmd = mainBuilder.build() |
| 89 | default: |
| 90 | return 0, newErrorwithSourceLocf("unsupported compiler: %s", mainBuilder.target.compiler) |
| 91 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 92 | } else { |
George Burgess IV | cb46500 | 2020-07-20 16:53:39 -0700 | [diff] [blame] | 93 | cSrcFile, tidyFlags, tidyMode := processClangTidyFlags(mainBuilder) |
| 94 | if mainBuilder.target.compilerType == clangType { |
| 95 | err := prepareClangCommand(mainBuilder) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 96 | if err != nil { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 97 | return 0, err |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 98 | } |
George Burgess IV | cb46500 | 2020-07-20 16:53:39 -0700 | [diff] [blame] | 99 | allowCCache := true |
| 100 | if tidyMode != tidyModeNone { |
| 101 | allowCCache = false |
| 102 | clangCmdWithoutGomaAndCCache := mainBuilder.build() |
| 103 | var err error |
| 104 | switch tidyMode { |
| 105 | case tidyModeTricium: |
| 106 | if cfg.triciumNitsDir == "" { |
| 107 | return 0, newErrorwithSourceLocf("tricium linting was requested, but no nits directory is configured") |
| 108 | } |
| 109 | err = runClangTidyForTricium(env, clangCmdWithoutGomaAndCCache, cSrcFile, cfg.triciumNitsDir, tidyFlags) |
| 110 | case tidyModeAll: |
| 111 | err = runClangTidy(env, clangCmdWithoutGomaAndCCache, cSrcFile, tidyFlags) |
| 112 | default: |
| 113 | panic(fmt.Sprintf("Unknown tidy mode: %v", tidyMode)) |
| 114 | } |
| 115 | |
| 116 | if err != nil { |
| 117 | return 0, err |
| 118 | } |
| 119 | } |
| 120 | if err := processGomaCCacheFlags(allowCCache, mainBuilder); err != nil { |
| 121 | return 0, err |
| 122 | } |
| 123 | compilerCmd = mainBuilder.build() |
| 124 | } else { |
| 125 | if clangSyntax { |
| 126 | allowCCache := false |
| 127 | clangCmd, err := calcClangCommand(allowCCache, mainBuilder.clone()) |
| 128 | if err != nil { |
| 129 | return 0, err |
| 130 | } |
| 131 | gccCmd, err := calcGccCommand(mainBuilder) |
| 132 | if err != nil { |
| 133 | return 0, err |
| 134 | } |
| 135 | return checkClangSyntax(env, clangCmd, gccCmd) |
| 136 | } |
| 137 | compilerCmd, err = calcGccCommand(mainBuilder) |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 138 | if err != nil { |
| 139 | return 0, err |
| 140 | } |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 141 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 142 | } |
Tobias Bosch | 9d60930 | 2019-07-10 06:16:04 -0700 | [diff] [blame] | 143 | rusageLogfileName := getRusageLogFilename(env) |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 144 | bisectStage := getBisectStage(env) |
Pirama Arumuga Nainar | a87b84f | 2020-06-09 21:33:44 -0700 | [diff] [blame] | 145 | if shouldForceDisableWerror(env, cfg) { |
Tobias Bosch | 9d60930 | 2019-07-10 06:16:04 -0700 | [diff] [blame] | 146 | if rusageLogfileName != "" { |
| 147 | return 0, newUserErrorf("GETRUSAGE is meaningless with FORCE_DISABLE_WERROR") |
| 148 | } |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 149 | if bisectStage != "" { |
| 150 | return 0, newUserErrorf("BISECT_STAGE is meaningless with FORCE_DISABLE_WERROR") |
| 151 | } |
Pirama Arumuga Nainar | a87b84f | 2020-06-09 21:33:44 -0700 | [diff] [blame] | 152 | return doubleBuildWithWNoError(env, cfg, compilerCmd) |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 153 | } |
Tobias Bosch | c88ee8a | 2019-10-01 15:00:52 -0700 | [diff] [blame] | 154 | if shouldCompileWithFallback(env) { |
| 155 | if rusageLogfileName != "" { |
| 156 | return 0, newUserErrorf("GETRUSAGE is meaningless with FORCE_DISABLE_WERROR") |
| 157 | } |
| 158 | if bisectStage != "" { |
| 159 | return 0, newUserErrorf("BISECT_STAGE is meaningless with FORCE_DISABLE_WERROR") |
| 160 | } |
| 161 | return compileWithFallback(env, cfg, compilerCmd, mainBuilder.absWrapperPath) |
| 162 | } |
Tobias Bosch | 9d60930 | 2019-07-10 06:16:04 -0700 | [diff] [blame] | 163 | if rusageLogfileName != "" { |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 164 | if bisectStage != "" { |
| 165 | return 0, newUserErrorf("BISECT_STAGE is meaningless with GETRUSAGE") |
| 166 | } |
Tobias Bosch | 9d60930 | 2019-07-10 06:16:04 -0700 | [diff] [blame] | 167 | return logRusage(env, rusageLogfileName, compilerCmd) |
| 168 | } |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 169 | if bisectStage != "" { |
Tobias Bosch | 820bffa | 2019-09-30 15:53:52 -0700 | [diff] [blame] | 170 | compilerCmd, err = calcBisectCommand(env, cfg, bisectStage, compilerCmd) |
| 171 | if err != nil { |
| 172 | return 0, err |
| 173 | } |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 174 | } |
Tobias Bosch | 9332d21 | 2019-07-10 06:23:57 -0700 | [diff] [blame] | 175 | // Note: We return an exit code only if the underlying env is not |
| 176 | // really doing an exec, e.g. commandRecordingEnv. |
| 177 | return wrapSubprocessErrorWithSourceLoc(compilerCmd, env.exec(compilerCmd)) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 180 | func prepareClangCommand(builder *commandBuilder) (err error) { |
Tobias Bosch | 8cb363f | 2019-10-17 07:44:13 -0700 | [diff] [blame] | 181 | if !builder.cfg.isHostWrapper { |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 182 | processSysrootFlag(builder) |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 183 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 184 | builder.addPreUserArgs(builder.cfg.clangFlags...) |
Caroline Tice | 8ac33e0 | 2019-10-28 09:48:18 -0700 | [diff] [blame] | 185 | builder.addPostUserArgs(builder.cfg.clangPostFlags...) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 186 | calcCommonPreUserArgs(builder) |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 187 | return processClangFlags(builder) |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | func calcClangCommand(allowCCache bool, builder *commandBuilder) (*command, error) { |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 191 | err := prepareClangCommand(builder) |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 192 | if err != nil { |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 193 | return nil, err |
| 194 | } |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 195 | if err := processGomaCCacheFlags(allowCCache, builder); err != nil { |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 196 | return nil, err |
| 197 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 198 | return builder.build(), nil |
| 199 | } |
| 200 | |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 201 | func calcGccCommand(builder *commandBuilder) (*command, error) { |
Tobias Bosch | b27c8f2 | 2019-07-19 06:53:07 -0700 | [diff] [blame] | 202 | if !builder.cfg.isHostWrapper { |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 203 | processSysrootFlag(builder) |
Tobias Bosch | b27c8f2 | 2019-07-19 06:53:07 -0700 | [diff] [blame] | 204 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 205 | builder.addPreUserArgs(builder.cfg.gccFlags...) |
Tobias Bosch | b27c8f2 | 2019-07-19 06:53:07 -0700 | [diff] [blame] | 206 | if !builder.cfg.isHostWrapper { |
| 207 | calcCommonPreUserArgs(builder) |
| 208 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 209 | processGccFlags(builder) |
Tobias Bosch | b27c8f2 | 2019-07-19 06:53:07 -0700 | [diff] [blame] | 210 | if !builder.cfg.isHostWrapper { |
| 211 | allowCCache := true |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 212 | if err := processGomaCCacheFlags(allowCCache, builder); err != nil { |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 213 | return nil, err |
| 214 | } |
Tobias Bosch | b27c8f2 | 2019-07-19 06:53:07 -0700 | [diff] [blame] | 215 | } |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 216 | return builder.build(), nil |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | func calcCommonPreUserArgs(builder *commandBuilder) { |
| 220 | builder.addPreUserArgs(builder.cfg.commonFlags...) |
Tobias Bosch | 8cb363f | 2019-10-17 07:44:13 -0700 | [diff] [blame] | 221 | if !builder.cfg.isHostWrapper { |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 222 | processPieFlags(builder) |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 223 | processThumbCodeFlags(builder) |
Tobias Bosch | 1cd5f84 | 2019-08-20 10:05:33 -0700 | [diff] [blame] | 224 | processStackProtectorFlags(builder) |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 225 | processX86Flags(builder) |
| 226 | } |
Tobias Bosch | 8cb363f | 2019-10-17 07:44:13 -0700 | [diff] [blame] | 227 | processSanitizerFlags(builder) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 230 | func processGomaCCacheFlags(allowCCache bool, builder *commandBuilder) (err error) { |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 231 | gomaccUsed := false |
| 232 | if !builder.cfg.isHostWrapper { |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 233 | gomaccUsed, err = processGomaCccFlags(builder) |
| 234 | if err != nil { |
| 235 | return err |
| 236 | } |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 237 | } |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 238 | if !gomaccUsed && allowCCache { |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 239 | processCCacheFlag(builder) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 240 | } |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 241 | return nil |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 244 | func getAbsWrapperPath(env env, wrapperCmd *command) (string, error) { |
Tobias Bosch | 5847281 | 2019-07-11 04:24:52 -0700 | [diff] [blame] | 245 | wrapperPath := getAbsCmdPath(env, wrapperCmd) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 246 | evaledCmdPath, err := filepath.EvalSymlinks(wrapperPath) |
| 247 | if err != nil { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 248 | return "", wrapErrorwithSourceLocf(err, "failed to evaluate symlinks for %s", wrapperPath) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 249 | } |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 250 | return evaledCmdPath, nil |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 253 | func printCompilerError(writer io.Writer, compilerErr error) { |
| 254 | if _, ok := compilerErr.(userError); ok { |
| 255 | fmt.Fprintf(writer, "%s\n", compilerErr) |
| 256 | } else { |
George Burgess IV | 2efe72e | 2020-06-18 20:37:28 -0700 | [diff] [blame] | 257 | emailAccount := "chromeos-toolchain" |
| 258 | if isAndroidConfig() { |
| 259 | emailAccount = "android-llvm" |
| 260 | } |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 261 | fmt.Fprintf(writer, |
George Burgess IV | 2efe72e | 2020-06-18 20:37:28 -0700 | [diff] [blame] | 262 | "Internal error. Please report to %s@google.com.\n%s\n", |
| 263 | emailAccount, compilerErr) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 264 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 265 | } |
Tobias Bosch | 6f59a66 | 2019-08-20 15:37:11 -0700 | [diff] [blame] | 266 | |
George Burgess IV | 26caa2f | 2020-02-28 14:36:01 -0800 | [diff] [blame] | 267 | func needStdinTee(inputCmd *command) bool { |
Tobias Bosch | 6f59a66 | 2019-08-20 15:37:11 -0700 | [diff] [blame] | 268 | lastArg := "" |
| 269 | for _, arg := range inputCmd.Args { |
| 270 | if arg == "-" && lastArg != "-o" { |
George Burgess IV | 26caa2f | 2020-02-28 14:36:01 -0800 | [diff] [blame] | 271 | return true |
Tobias Bosch | 6f59a66 | 2019-08-20 15:37:11 -0700 | [diff] [blame] | 272 | } |
| 273 | lastArg = arg |
| 274 | } |
George Burgess IV | 26caa2f | 2020-02-28 14:36:01 -0800 | [diff] [blame] | 275 | return false |
| 276 | } |
| 277 | |
| 278 | func prebufferStdinIfNeeded(env env, inputCmd *command) (getStdin func() io.Reader, err error) { |
| 279 | // We pre-buffer the entirety of stdin, since the compiler may exit mid-invocation with an |
| 280 | // error, which may leave stdin partially read. |
| 281 | if !needStdinTee(inputCmd) { |
| 282 | // This won't produce deterministic input to the compiler, but stdin shouldn't |
| 283 | // matter in this case, so... |
| 284 | return env.stdin, nil |
| 285 | } |
| 286 | |
| 287 | stdinBuffer := &bytes.Buffer{} |
| 288 | if _, err := stdinBuffer.ReadFrom(env.stdin()); err != nil { |
| 289 | return nil, wrapErrorwithSourceLocf(err, "prebuffering stdin") |
| 290 | } |
| 291 | |
| 292 | return func() io.Reader { return bytes.NewReader(stdinBuffer.Bytes()) }, nil |
Tobias Bosch | 6f59a66 | 2019-08-20 15:37:11 -0700 | [diff] [blame] | 293 | } |