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...) |
Pirama Arumuga Nainar | efb75cf | 2020-10-21 13:57:01 -0700 | [diff] [blame] | 83 | mainBuilder.addPostUserArgs(mainBuilder.cfg.clangPostFlags...) |
Tobias Bosch | 8cb363f | 2019-10-17 07:44:13 -0700 | [diff] [blame] | 84 | if _, err := processGomaCccFlags(mainBuilder); err != nil { |
| 85 | return 0, err |
| 86 | } |
| 87 | compilerCmd = mainBuilder.build() |
| 88 | case clangTidyType: |
| 89 | compilerCmd = mainBuilder.build() |
| 90 | default: |
| 91 | return 0, newErrorwithSourceLocf("unsupported compiler: %s", mainBuilder.target.compiler) |
| 92 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 93 | } else { |
George Burgess IV | cb46500 | 2020-07-20 16:53:39 -0700 | [diff] [blame] | 94 | cSrcFile, tidyFlags, tidyMode := processClangTidyFlags(mainBuilder) |
| 95 | if mainBuilder.target.compilerType == clangType { |
| 96 | err := prepareClangCommand(mainBuilder) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 97 | if err != nil { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 98 | return 0, err |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 99 | } |
George Burgess IV | cb46500 | 2020-07-20 16:53:39 -0700 | [diff] [blame] | 100 | allowCCache := true |
| 101 | if tidyMode != tidyModeNone { |
| 102 | allowCCache = false |
| 103 | clangCmdWithoutGomaAndCCache := mainBuilder.build() |
| 104 | var err error |
| 105 | switch tidyMode { |
| 106 | case tidyModeTricium: |
| 107 | if cfg.triciumNitsDir == "" { |
| 108 | return 0, newErrorwithSourceLocf("tricium linting was requested, but no nits directory is configured") |
| 109 | } |
George Burgess IV | 0a377f4 | 2020-08-05 15:03:36 -0700 | [diff] [blame] | 110 | err = runClangTidyForTricium(env, clangCmdWithoutGomaAndCCache, cSrcFile, cfg.triciumNitsDir, tidyFlags, cfg.crashArtifactsDir) |
George Burgess IV | cb46500 | 2020-07-20 16:53:39 -0700 | [diff] [blame] | 111 | case tidyModeAll: |
| 112 | err = runClangTidy(env, clangCmdWithoutGomaAndCCache, cSrcFile, tidyFlags) |
| 113 | default: |
| 114 | panic(fmt.Sprintf("Unknown tidy mode: %v", tidyMode)) |
| 115 | } |
| 116 | |
| 117 | if err != nil { |
| 118 | return 0, err |
| 119 | } |
| 120 | } |
| 121 | if err := processGomaCCacheFlags(allowCCache, mainBuilder); err != nil { |
| 122 | return 0, err |
| 123 | } |
| 124 | compilerCmd = mainBuilder.build() |
| 125 | } else { |
| 126 | if clangSyntax { |
| 127 | allowCCache := false |
| 128 | clangCmd, err := calcClangCommand(allowCCache, mainBuilder.clone()) |
| 129 | if err != nil { |
| 130 | return 0, err |
| 131 | } |
| 132 | gccCmd, err := calcGccCommand(mainBuilder) |
| 133 | if err != nil { |
| 134 | return 0, err |
| 135 | } |
| 136 | return checkClangSyntax(env, clangCmd, gccCmd) |
| 137 | } |
| 138 | compilerCmd, err = calcGccCommand(mainBuilder) |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 139 | if err != nil { |
| 140 | return 0, err |
| 141 | } |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 142 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 143 | } |
Ryan Beltran | 7ee49e4 | 2021-01-19 01:52:42 +0000 | [diff] [blame] | 144 | |
Tobias Bosch | 9d60930 | 2019-07-10 06:16:04 -0700 | [diff] [blame] | 145 | rusageLogfileName := getRusageLogFilename(env) |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 146 | bisectStage := getBisectStage(env) |
Ryan Beltran | 7ee49e4 | 2021-01-19 01:52:42 +0000 | [diff] [blame] | 147 | |
| 148 | if rusageLogfileName != "" { |
| 149 | compilerCmd = removeRusageFromCommand(compilerCmd) |
| 150 | } |
| 151 | |
Pirama Arumuga Nainar | a87b84f | 2020-06-09 21:33:44 -0700 | [diff] [blame] | 152 | if shouldForceDisableWerror(env, cfg) { |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 153 | if bisectStage != "" { |
| 154 | return 0, newUserErrorf("BISECT_STAGE is meaningless with FORCE_DISABLE_WERROR") |
| 155 | } |
Ryan Beltran | 7ee49e4 | 2021-01-19 01:52:42 +0000 | [diff] [blame] | 156 | return doubleBuildWithWNoError(env, cfg, compilerCmd, rusageLogfileName) |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 157 | } |
Tobias Bosch | c88ee8a | 2019-10-01 15:00:52 -0700 | [diff] [blame] | 158 | if shouldCompileWithFallback(env) { |
| 159 | if rusageLogfileName != "" { |
Ryan Beltran | 7ee49e4 | 2021-01-19 01:52:42 +0000 | [diff] [blame] | 160 | return 0, newUserErrorf("TOOLCHAIN_RUSAGE_OUTPUT is meaningless with ANDROID_LLVM_PREBUILT_COMPILER_PATH") |
Tobias Bosch | c88ee8a | 2019-10-01 15:00:52 -0700 | [diff] [blame] | 161 | } |
| 162 | if bisectStage != "" { |
Ryan Beltran | 7ee49e4 | 2021-01-19 01:52:42 +0000 | [diff] [blame] | 163 | return 0, newUserErrorf("BISECT_STAGE is meaningless with ANDROID_LLVM_PREBUILT_COMPILER_PATH") |
Tobias Bosch | c88ee8a | 2019-10-01 15:00:52 -0700 | [diff] [blame] | 164 | } |
| 165 | return compileWithFallback(env, cfg, compilerCmd, mainBuilder.absWrapperPath) |
| 166 | } |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 167 | if bisectStage != "" { |
Ryan Beltran | 7ee49e4 | 2021-01-19 01:52:42 +0000 | [diff] [blame] | 168 | if rusageLogfileName != "" { |
| 169 | return 0, newUserErrorf("TOOLCHAIN_RUSAGE_OUTPUT is meaningless with BISECT_STAGE") |
| 170 | } |
Tobias Bosch | 820bffa | 2019-09-30 15:53:52 -0700 | [diff] [blame] | 171 | compilerCmd, err = calcBisectCommand(env, cfg, bisectStage, compilerCmd) |
| 172 | if err != nil { |
| 173 | return 0, err |
| 174 | } |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 175 | } |
Ryan Beltran | 7ee49e4 | 2021-01-19 01:52:42 +0000 | [diff] [blame] | 176 | |
George Burgess IV | de5be16 | 2021-01-25 15:50:54 -0800 | [diff] [blame^] | 177 | commitRusage, err := maybeCaptureRusage(env, rusageLogfileName, compilerCmd, func(willLogRusage bool) error { |
| 178 | var err error |
| 179 | if willLogRusage { |
| 180 | err = env.run(compilerCmd, env.stdin(), env.stdout(), env.stderr()) |
| 181 | } else { |
| 182 | // Note: We return from this in non-fatal circumstances only if the |
| 183 | // underlying env is not really doing an exec, e.g. commandRecordingEnv. |
| 184 | err = env.exec(compilerCmd) |
| 185 | } |
| 186 | exitCode, err = wrapSubprocessErrorWithSourceLoc(compilerCmd, err) |
Ryan Beltran | 7ee49e4 | 2021-01-19 01:52:42 +0000 | [diff] [blame] | 187 | return err |
| 188 | }) |
| 189 | if err != nil { |
| 190 | return exitCode, err |
| 191 | } |
| 192 | if err := commitRusage(exitCode); err != nil { |
| 193 | return exitCode, fmt.Errorf("commiting rusage: %v", err) |
| 194 | } |
| 195 | |
| 196 | return exitCode, err |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 199 | func prepareClangCommand(builder *commandBuilder) (err error) { |
Tobias Bosch | 8cb363f | 2019-10-17 07:44:13 -0700 | [diff] [blame] | 200 | if !builder.cfg.isHostWrapper { |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 201 | processSysrootFlag(builder) |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 202 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 203 | builder.addPreUserArgs(builder.cfg.clangFlags...) |
George Burgess IV | 1713d25 | 2020-08-07 19:17:52 -0700 | [diff] [blame] | 204 | if builder.cfg.crashArtifactsDir != "" { |
| 205 | builder.addPreUserArgs("-fcrash-diagnostics-dir=" + builder.cfg.crashArtifactsDir) |
| 206 | } |
Caroline Tice | 8ac33e0 | 2019-10-28 09:48:18 -0700 | [diff] [blame] | 207 | builder.addPostUserArgs(builder.cfg.clangPostFlags...) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 208 | calcCommonPreUserArgs(builder) |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 209 | return processClangFlags(builder) |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | func calcClangCommand(allowCCache bool, builder *commandBuilder) (*command, error) { |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 213 | err := prepareClangCommand(builder) |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 214 | if err != nil { |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 215 | return nil, err |
| 216 | } |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 217 | if err := processGomaCCacheFlags(allowCCache, builder); err != nil { |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 218 | return nil, err |
| 219 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 220 | return builder.build(), nil |
| 221 | } |
| 222 | |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 223 | func calcGccCommand(builder *commandBuilder) (*command, error) { |
Tobias Bosch | b27c8f2 | 2019-07-19 06:53:07 -0700 | [diff] [blame] | 224 | if !builder.cfg.isHostWrapper { |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 225 | processSysrootFlag(builder) |
Tobias Bosch | b27c8f2 | 2019-07-19 06:53:07 -0700 | [diff] [blame] | 226 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 227 | builder.addPreUserArgs(builder.cfg.gccFlags...) |
Manoj Gupta | efefb1a | 2021-01-28 21:46:53 -0800 | [diff] [blame] | 228 | calcCommonPreUserArgs(builder) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 229 | processGccFlags(builder) |
Tobias Bosch | b27c8f2 | 2019-07-19 06:53:07 -0700 | [diff] [blame] | 230 | if !builder.cfg.isHostWrapper { |
| 231 | allowCCache := true |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 232 | if err := processGomaCCacheFlags(allowCCache, builder); err != nil { |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 233 | return nil, err |
| 234 | } |
Tobias Bosch | b27c8f2 | 2019-07-19 06:53:07 -0700 | [diff] [blame] | 235 | } |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 236 | return builder.build(), nil |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | func calcCommonPreUserArgs(builder *commandBuilder) { |
| 240 | builder.addPreUserArgs(builder.cfg.commonFlags...) |
Tobias Bosch | 8cb363f | 2019-10-17 07:44:13 -0700 | [diff] [blame] | 241 | if !builder.cfg.isHostWrapper { |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 242 | processPieFlags(builder) |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 243 | processThumbCodeFlags(builder) |
Tobias Bosch | 1cd5f84 | 2019-08-20 10:05:33 -0700 | [diff] [blame] | 244 | processStackProtectorFlags(builder) |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 245 | processX86Flags(builder) |
| 246 | } |
Tobias Bosch | 8cb363f | 2019-10-17 07:44:13 -0700 | [diff] [blame] | 247 | processSanitizerFlags(builder) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 250 | func processGomaCCacheFlags(allowCCache bool, builder *commandBuilder) (err error) { |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 251 | gomaccUsed := false |
| 252 | if !builder.cfg.isHostWrapper { |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 253 | gomaccUsed, err = processGomaCccFlags(builder) |
| 254 | if err != nil { |
| 255 | return err |
| 256 | } |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 257 | } |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 258 | if !gomaccUsed && allowCCache { |
Manoj Gupta | 2897926 | 2020-03-13 11:05:26 -0700 | [diff] [blame] | 259 | processCCacheFlag(builder) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 260 | } |
Tobias Bosch | c58f8d5 | 2019-09-30 10:37:42 -0700 | [diff] [blame] | 261 | return nil |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 264 | func getAbsWrapperPath(env env, wrapperCmd *command) (string, error) { |
Tobias Bosch | 5847281 | 2019-07-11 04:24:52 -0700 | [diff] [blame] | 265 | wrapperPath := getAbsCmdPath(env, wrapperCmd) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 266 | evaledCmdPath, err := filepath.EvalSymlinks(wrapperPath) |
| 267 | if err != nil { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 268 | return "", wrapErrorwithSourceLocf(err, "failed to evaluate symlinks for %s", wrapperPath) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 269 | } |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 270 | return evaledCmdPath, nil |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 273 | func printCompilerError(writer io.Writer, compilerErr error) { |
| 274 | if _, ok := compilerErr.(userError); ok { |
| 275 | fmt.Fprintf(writer, "%s\n", compilerErr) |
| 276 | } else { |
George Burgess IV | 2efe72e | 2020-06-18 20:37:28 -0700 | [diff] [blame] | 277 | emailAccount := "chromeos-toolchain" |
| 278 | if isAndroidConfig() { |
| 279 | emailAccount = "android-llvm" |
| 280 | } |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 281 | fmt.Fprintf(writer, |
George Burgess IV | 2efe72e | 2020-06-18 20:37:28 -0700 | [diff] [blame] | 282 | "Internal error. Please report to %s@google.com.\n%s\n", |
| 283 | emailAccount, compilerErr) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 284 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 285 | } |
Tobias Bosch | 6f59a66 | 2019-08-20 15:37:11 -0700 | [diff] [blame] | 286 | |
George Burgess IV | 26caa2f | 2020-02-28 14:36:01 -0800 | [diff] [blame] | 287 | func needStdinTee(inputCmd *command) bool { |
Tobias Bosch | 6f59a66 | 2019-08-20 15:37:11 -0700 | [diff] [blame] | 288 | lastArg := "" |
| 289 | for _, arg := range inputCmd.Args { |
| 290 | if arg == "-" && lastArg != "-o" { |
George Burgess IV | 26caa2f | 2020-02-28 14:36:01 -0800 | [diff] [blame] | 291 | return true |
Tobias Bosch | 6f59a66 | 2019-08-20 15:37:11 -0700 | [diff] [blame] | 292 | } |
| 293 | lastArg = arg |
| 294 | } |
George Burgess IV | 26caa2f | 2020-02-28 14:36:01 -0800 | [diff] [blame] | 295 | return false |
| 296 | } |
| 297 | |
| 298 | func prebufferStdinIfNeeded(env env, inputCmd *command) (getStdin func() io.Reader, err error) { |
| 299 | // We pre-buffer the entirety of stdin, since the compiler may exit mid-invocation with an |
| 300 | // error, which may leave stdin partially read. |
| 301 | if !needStdinTee(inputCmd) { |
| 302 | // This won't produce deterministic input to the compiler, but stdin shouldn't |
| 303 | // matter in this case, so... |
| 304 | return env.stdin, nil |
| 305 | } |
| 306 | |
| 307 | stdinBuffer := &bytes.Buffer{} |
| 308 | if _, err := stdinBuffer.ReadFrom(env.stdin()); err != nil { |
| 309 | return nil, wrapErrorwithSourceLocf(err, "prebuffering stdin") |
| 310 | } |
| 311 | |
| 312 | return func() io.Reader { return bytes.NewReader(stdinBuffer.Bytes()) }, nil |
Tobias Bosch | 6f59a66 | 2019-08-20 15:37:11 -0700 | [diff] [blame] | 313 | } |