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