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