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