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 ( |
Tobias Bosch | a50a9c1 | 2019-08-16 11:47:00 -0700 | [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 { |
| 32 | if cfg.oldWrapperPath != "" { |
| 33 | exitCode, compilerErr = callCompilerWithRunAndCompareToOldWrapper(env, cfg, inputCmd) |
| 34 | } else { |
| 35 | exitCode, compilerErr = callCompilerInternal(env, cfg, inputCmd) |
| 36 | } |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 37 | } |
| 38 | if compilerErr != nil { |
| 39 | printCompilerError(env.stderr(), compilerErr) |
| 40 | exitCode = 1 |
| 41 | } |
| 42 | return exitCode |
| 43 | } |
| 44 | |
| 45 | func callCompilerWithRunAndCompareToOldWrapper(env env, cfg *config, inputCmd *command) (exitCode int, err error) { |
Tobias Bosch | a50a9c1 | 2019-08-16 11:47:00 -0700 | [diff] [blame] | 46 | stdinBuffer := &bytes.Buffer{} |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 47 | recordingEnv := &commandRecordingEnv{ |
Tobias Bosch | a50a9c1 | 2019-08-16 11:47:00 -0700 | [diff] [blame] | 48 | env: env, |
Tobias Bosch | 6f59a66 | 2019-08-20 15:37:11 -0700 | [diff] [blame] | 49 | stdinReader: teeStdinIfNeeded(env, inputCmd, stdinBuffer), |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 50 | } |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 51 | // Note: this won't do a real exec as recordingEnv redirects exec to run. |
| 52 | if exitCode, err = callCompilerInternal(recordingEnv, cfg, inputCmd); err != nil { |
| 53 | return 0, err |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 54 | } |
Tobias Bosch | a50a9c1 | 2019-08-16 11:47:00 -0700 | [diff] [blame] | 55 | if err = compareToOldWrapper(env, cfg, inputCmd, stdinBuffer.Bytes(), recordingEnv.cmdResults, exitCode); err != nil { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 56 | return exitCode, err |
| 57 | } |
| 58 | return exitCode, nil |
| 59 | } |
| 60 | |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 61 | func callCompilerInternal(env env, cfg *config, inputCmd *command) (exitCode int, err error) { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 62 | if err := checkUnsupportedFlags(inputCmd); err != nil { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 63 | return 0, err |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 64 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 65 | mainBuilder, err := newCommandBuilder(env, cfg, inputCmd) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 66 | if err != nil { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 67 | return 0, err |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 68 | } |
Tobias Bosch | 5847281 | 2019-07-11 04:24:52 -0700 | [diff] [blame] | 69 | processPrintConfigFlag(mainBuilder) |
| 70 | processPrintCmdlineFlag(mainBuilder) |
| 71 | env = mainBuilder.env |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 72 | var compilerCmd *command |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 73 | clangSyntax := processClangSyntaxFlag(mainBuilder) |
| 74 | if mainBuilder.target.compilerType == clangType { |
Tobias Bosch | 38f3c42 | 2019-07-08 11:03:26 -0700 | [diff] [blame] | 75 | cSrcFile, useClangTidy := processClangTidyFlags(mainBuilder) |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 76 | sysroot, err := prepareClangCommand(mainBuilder) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 77 | if err != nil { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 78 | return 0, err |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 79 | } |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 80 | allowCCache := true |
Tobias Bosch | 38f3c42 | 2019-07-08 11:03:26 -0700 | [diff] [blame] | 81 | if useClangTidy { |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 82 | allowCCache = false |
| 83 | clangCmdWithoutGomaAndCCache := mainBuilder.build() |
| 84 | if err := runClangTidy(env, clangCmdWithoutGomaAndCCache, cSrcFile); err != nil { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 85 | return 0, err |
Tobias Bosch | 38f3c42 | 2019-07-08 11:03:26 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 88 | processGomaCCacheFlags(sysroot, allowCCache, mainBuilder) |
| 89 | compilerCmd = mainBuilder.build() |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 90 | } else { |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 91 | if clangSyntax { |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 92 | allowCCache := false |
| 93 | clangCmd, err := calcClangCommand(allowCCache, mainBuilder.clone()) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 94 | if err != nil { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 95 | return 0, err |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 96 | } |
Tobias Bosch | a50a9c1 | 2019-08-16 11:47:00 -0700 | [diff] [blame] | 97 | gccCmd := calcGccCommand(mainBuilder) |
| 98 | return checkClangSyntax(env, clangCmd, gccCmd) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 99 | } |
| 100 | compilerCmd = calcGccCommand(mainBuilder) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 101 | } |
Tobias Bosch | 9d60930 | 2019-07-10 06:16:04 -0700 | [diff] [blame] | 102 | rusageLogfileName := getRusageLogFilename(env) |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 103 | bisectStage := getBisectStage(env) |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 104 | if shouldForceDisableWError(env) { |
Tobias Bosch | 9d60930 | 2019-07-10 06:16:04 -0700 | [diff] [blame] | 105 | if rusageLogfileName != "" { |
| 106 | return 0, newUserErrorf("GETRUSAGE is meaningless with FORCE_DISABLE_WERROR") |
| 107 | } |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 108 | if bisectStage != "" { |
| 109 | return 0, newUserErrorf("BISECT_STAGE is meaningless with FORCE_DISABLE_WERROR") |
| 110 | } |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 111 | return doubleBuildWithWNoError(env, cfg, compilerCmd) |
| 112 | } |
Tobias Bosch | 9d60930 | 2019-07-10 06:16:04 -0700 | [diff] [blame] | 113 | if rusageLogfileName != "" { |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 114 | if bisectStage != "" { |
| 115 | return 0, newUserErrorf("BISECT_STAGE is meaningless with GETRUSAGE") |
| 116 | } |
Tobias Bosch | 9d60930 | 2019-07-10 06:16:04 -0700 | [diff] [blame] | 117 | return logRusage(env, rusageLogfileName, compilerCmd) |
| 118 | } |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 119 | if bisectStage != "" { |
| 120 | compilerCmd = calcBisectCommand(env, bisectStage, compilerCmd) |
| 121 | } |
Tobias Bosch | 9332d21 | 2019-07-10 06:23:57 -0700 | [diff] [blame] | 122 | // Note: We return an exit code only if the underlying env is not |
| 123 | // really doing an exec, e.g. commandRecordingEnv. |
| 124 | return wrapSubprocessErrorWithSourceLoc(compilerCmd, env.exec(compilerCmd)) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 127 | func prepareClangCommand(builder *commandBuilder) (sysroot string, err error) { |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 128 | sysroot = "" |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 129 | if !builder.cfg.isHostWrapper && !builder.cfg.isAndroidWrapper { |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 130 | sysroot = processSysrootFlag(builder) |
| 131 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 132 | builder.addPreUserArgs(builder.cfg.clangFlags...) |
| 133 | calcCommonPreUserArgs(builder) |
| 134 | if err := processClangFlags(builder); err != nil { |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 135 | return "", err |
| 136 | } |
| 137 | return sysroot, nil |
| 138 | } |
| 139 | |
| 140 | func calcClangCommand(allowCCache bool, builder *commandBuilder) (*command, error) { |
| 141 | sysroot, err := prepareClangCommand(builder) |
| 142 | if err != nil { |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 143 | return nil, err |
| 144 | } |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 145 | processGomaCCacheFlags(sysroot, allowCCache, builder) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 146 | return builder.build(), nil |
| 147 | } |
| 148 | |
| 149 | func calcGccCommand(builder *commandBuilder) *command { |
Tobias Bosch | b27c8f2 | 2019-07-19 06:53:07 -0700 | [diff] [blame] | 150 | sysroot := "" |
| 151 | if !builder.cfg.isHostWrapper { |
| 152 | sysroot = processSysrootFlag(builder) |
| 153 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 154 | builder.addPreUserArgs(builder.cfg.gccFlags...) |
Tobias Bosch | b27c8f2 | 2019-07-19 06:53:07 -0700 | [diff] [blame] | 155 | if !builder.cfg.isHostWrapper { |
| 156 | calcCommonPreUserArgs(builder) |
| 157 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 158 | processGccFlags(builder) |
Tobias Bosch | b27c8f2 | 2019-07-19 06:53:07 -0700 | [diff] [blame] | 159 | if !builder.cfg.isHostWrapper { |
| 160 | allowCCache := true |
| 161 | processGomaCCacheFlags(sysroot, allowCCache, builder) |
| 162 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 163 | return builder.build() |
| 164 | } |
| 165 | |
| 166 | func calcCommonPreUserArgs(builder *commandBuilder) { |
| 167 | builder.addPreUserArgs(builder.cfg.commonFlags...) |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 168 | if !builder.cfg.isHostWrapper && !builder.cfg.isAndroidWrapper { |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 169 | processPieFlags(builder) |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 170 | processThumbCodeFlags(builder) |
Tobias Bosch | 1cd5f84 | 2019-08-20 10:05:33 -0700 | [diff] [blame] | 171 | processStackProtectorFlags(builder) |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 172 | processX86Flags(builder) |
| 173 | } |
Tobias Bosch | bbb3c81 | 2019-09-30 10:11:25 -0700 | [diff] [blame] | 174 | if !builder.cfg.isAndroidWrapper { |
| 175 | processSanitizerFlags(builder) |
| 176 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 179 | func processGomaCCacheFlags(sysroot string, allowCCache bool, builder *commandBuilder) { |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 180 | gomaccUsed := false |
| 181 | if !builder.cfg.isHostWrapper { |
| 182 | gomaccUsed = processGomaCccFlags(builder) |
| 183 | } |
Tobias Bosch | 198a3c9 | 2019-07-17 04:22:34 -0700 | [diff] [blame] | 184 | if !gomaccUsed && allowCCache { |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 185 | processCCacheFlag(sysroot, builder) |
| 186 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 189 | func getAbsWrapperPath(env env, wrapperCmd *command) (string, error) { |
Tobias Bosch | 5847281 | 2019-07-11 04:24:52 -0700 | [diff] [blame] | 190 | wrapperPath := getAbsCmdPath(env, wrapperCmd) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 191 | evaledCmdPath, err := filepath.EvalSymlinks(wrapperPath) |
| 192 | if err != nil { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 193 | return "", wrapErrorwithSourceLocf(err, "failed to evaluate symlinks for %s", wrapperPath) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 194 | } |
Tobias Bosch | 31dec2c | 2019-07-18 07:34:03 -0700 | [diff] [blame] | 195 | return evaledCmdPath, nil |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 198 | func printCompilerError(writer io.Writer, compilerErr error) { |
| 199 | if _, ok := compilerErr.(userError); ok { |
| 200 | fmt.Fprintf(writer, "%s\n", compilerErr) |
| 201 | } else { |
| 202 | fmt.Fprintf(writer, |
| 203 | "Internal error. Please report to chromeos-toolchain@google.com.\n%s\n", |
| 204 | compilerErr) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 205 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 206 | } |
Tobias Bosch | 6f59a66 | 2019-08-20 15:37:11 -0700 | [diff] [blame] | 207 | |
| 208 | func teeStdinIfNeeded(env env, inputCmd *command, dest io.Writer) io.Reader { |
| 209 | // We can't use io.TeeReader unconditionally, as that would block |
| 210 | // calls to exec.Cmd.Run(), even if the underlying process has already |
| 211 | // terminated. See https://github.com/golang/go/issues/7990 for more details. |
| 212 | lastArg := "" |
| 213 | for _, arg := range inputCmd.Args { |
| 214 | if arg == "-" && lastArg != "-o" { |
| 215 | return io.TeeReader(env.stdin(), dest) |
| 216 | } |
| 217 | lastArg = arg |
| 218 | } |
| 219 | return env.stdin() |
| 220 | } |