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) |
| 53 | compilerCmd, err = calcClangCommand(useClangTidy, 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 | 38f3c42 | 2019-07-08 11:03:26 -0700 | [diff] [blame] | 57 | if useClangTidy { |
| 58 | if err := runClangTidy(env, compilerCmd, cSrcFile); err != nil { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 59 | return 0, err |
Tobias Bosch | 38f3c42 | 2019-07-08 11:03:26 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 62 | } else { |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 63 | if clangSyntax { |
Tobias Bosch | 38f3c42 | 2019-07-08 11:03:26 -0700 | [diff] [blame] | 64 | forceLocal := false |
| 65 | clangCmd, err := calcClangCommand(forceLocal, mainBuilder.clone()) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -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 | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 68 | } |
| 69 | exitCode, err = checkClangSyntax(env, clangCmd) |
| 70 | if err != nil || exitCode != 0 { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 71 | return exitCode, err |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | compilerCmd = calcGccCommand(mainBuilder) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 75 | } |
Tobias Bosch | 9d60930 | 2019-07-10 06:16:04 -0700 | [diff] [blame] | 76 | rusageLogfileName := getRusageLogFilename(env) |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 77 | bisectStage := getBisectStage(env) |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 78 | if shouldForceDisableWError(env) { |
Tobias Bosch | 9d60930 | 2019-07-10 06:16:04 -0700 | [diff] [blame] | 79 | if rusageLogfileName != "" { |
| 80 | return 0, newUserErrorf("GETRUSAGE is meaningless with FORCE_DISABLE_WERROR") |
| 81 | } |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 82 | if bisectStage != "" { |
| 83 | return 0, newUserErrorf("BISECT_STAGE is meaningless with FORCE_DISABLE_WERROR") |
| 84 | } |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 85 | return doubleBuildWithWNoError(env, cfg, compilerCmd) |
| 86 | } |
Tobias Bosch | 9d60930 | 2019-07-10 06:16:04 -0700 | [diff] [blame] | 87 | if rusageLogfileName != "" { |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 88 | if bisectStage != "" { |
| 89 | return 0, newUserErrorf("BISECT_STAGE is meaningless with GETRUSAGE") |
| 90 | } |
Tobias Bosch | 9d60930 | 2019-07-10 06:16:04 -0700 | [diff] [blame] | 91 | return logRusage(env, rusageLogfileName, compilerCmd) |
| 92 | } |
Tobias Bosch | 9780ea9 | 2019-07-11 01:19:42 -0700 | [diff] [blame] | 93 | if bisectStage != "" { |
| 94 | compilerCmd = calcBisectCommand(env, bisectStage, compilerCmd) |
| 95 | } |
Tobias Bosch | 9332d21 | 2019-07-10 06:23:57 -0700 | [diff] [blame] | 96 | // Note: We return an exit code only if the underlying env is not |
| 97 | // really doing an exec, e.g. commandRecordingEnv. |
| 98 | return wrapSubprocessErrorWithSourceLoc(compilerCmd, env.exec(compilerCmd)) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Tobias Bosch | 38f3c42 | 2019-07-08 11:03:26 -0700 | [diff] [blame] | 101 | func calcClangCommand(forceLocal bool, builder *commandBuilder) (*command, error) { |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 102 | sysroot := processSysrootFlag(builder) |
| 103 | builder.addPreUserArgs(builder.cfg.clangFlags...) |
| 104 | calcCommonPreUserArgs(builder) |
| 105 | if err := processClangFlags(builder); err != nil { |
| 106 | return nil, err |
| 107 | } |
Tobias Bosch | 38f3c42 | 2019-07-08 11:03:26 -0700 | [diff] [blame] | 108 | if !forceLocal { |
| 109 | processGomaCCacheFlags(sysroot, builder) |
| 110 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 111 | return builder.build(), nil |
| 112 | } |
| 113 | |
| 114 | func calcGccCommand(builder *commandBuilder) *command { |
| 115 | sysroot := processSysrootFlag(builder) |
| 116 | builder.addPreUserArgs(builder.cfg.gccFlags...) |
| 117 | calcCommonPreUserArgs(builder) |
| 118 | processGccFlags(builder) |
| 119 | processGomaCCacheFlags(sysroot, builder) |
| 120 | return builder.build() |
| 121 | } |
| 122 | |
| 123 | func calcCommonPreUserArgs(builder *commandBuilder) { |
| 124 | builder.addPreUserArgs(builder.cfg.commonFlags...) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 125 | processPieFlags(builder) |
| 126 | processStackProtectorFlags(builder) |
| 127 | processThumbCodeFlags(builder) |
| 128 | processX86Flags(builder) |
| 129 | processSanitizerFlags(builder) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | func processGomaCCacheFlags(sysroot string, builder *commandBuilder) { |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 133 | gomaccUsed := processGomaCccFlags(builder) |
| 134 | if !gomaccUsed { |
| 135 | processCCacheFlag(sysroot, builder) |
| 136 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Tobias Bosch | 5847281 | 2019-07-11 04:24:52 -0700 | [diff] [blame^] | 139 | func getAbsWrapperDir(env env, wrapperCmd *command) (string, error) { |
| 140 | wrapperPath := getAbsCmdPath(env, wrapperCmd) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 141 | evaledCmdPath, err := filepath.EvalSymlinks(wrapperPath) |
| 142 | if err != nil { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 143 | return "", wrapErrorwithSourceLocf(err, "failed to evaluate symlinks for %s", wrapperPath) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 144 | } |
| 145 | return filepath.Dir(evaledCmdPath), nil |
| 146 | } |
| 147 | |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 148 | func printCompilerError(writer io.Writer, compilerErr error) { |
| 149 | if _, ok := compilerErr.(userError); ok { |
| 150 | fmt.Fprintf(writer, "%s\n", compilerErr) |
| 151 | } else { |
| 152 | fmt.Fprintf(writer, |
| 153 | "Internal error. Please report to chromeos-toolchain@google.com.\n%s\n", |
| 154 | compilerErr) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 155 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 156 | } |