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