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" |
| 7 | "strings" |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 8 | "syscall" |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 9 | ) |
| 10 | |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 11 | func callCompiler(env env, cfg *config, inputCmd *command) int { |
| 12 | exitCode := 0 |
| 13 | var compilerErr error |
| 14 | if shouldForwardToOldWrapper(env, inputCmd) { |
| 15 | // TODO: Once this is only checking for bisect, create a command |
| 16 | // that directly calls the bisect driver in calcCompilerCommand. |
| 17 | exitCode, compilerErr = forwardToOldWrapper(env, cfg, inputCmd) |
| 18 | } else if cfg.oldWrapperPath != "" { |
| 19 | exitCode, compilerErr = callCompilerWithRunAndCompareToOldWrapper(env, cfg, inputCmd) |
| 20 | } else { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 21 | exitCode, compilerErr = callCompilerInternal(env, cfg, inputCmd) |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 22 | } |
| 23 | if compilerErr != nil { |
| 24 | printCompilerError(env.stderr(), compilerErr) |
| 25 | exitCode = 1 |
| 26 | } |
| 27 | return exitCode |
| 28 | } |
| 29 | |
| 30 | func callCompilerWithRunAndCompareToOldWrapper(env env, cfg *config, inputCmd *command) (exitCode int, err error) { |
| 31 | recordingEnv := &commandRecordingEnv{ |
| 32 | env: env, |
| 33 | } |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 34 | // Note: this won't do a real exec as recordingEnv redirects exec to run. |
| 35 | if exitCode, err = callCompilerInternal(recordingEnv, cfg, inputCmd); err != nil { |
| 36 | return 0, err |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 37 | } |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 38 | if err = compareToOldWrapper(env, cfg, inputCmd, recordingEnv.cmdResults, exitCode); err != nil { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 39 | return exitCode, err |
| 40 | } |
| 41 | return exitCode, nil |
| 42 | } |
| 43 | |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 44 | func callCompilerInternal(env env, cfg *config, inputCmd *command) (exitCode int, err error) { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 45 | if err := checkUnsupportedFlags(inputCmd); err != nil { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 46 | return 0, err |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 47 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 48 | mainBuilder, err := newCommandBuilder(env, cfg, inputCmd) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 49 | if err != nil { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 50 | return 0, err |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 51 | } |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 52 | var compilerCmd *command |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 53 | clangSyntax := processClangSyntaxFlag(mainBuilder) |
| 54 | if mainBuilder.target.compilerType == clangType { |
Tobias Bosch | 38f3c42 | 2019-07-08 11:03:26 -0700 | [diff] [blame] | 55 | cSrcFile, useClangTidy := processClangTidyFlags(mainBuilder) |
| 56 | compilerCmd, err = calcClangCommand(useClangTidy, mainBuilder) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 57 | if err != nil { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 58 | return 0, err |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 59 | } |
Tobias Bosch | 38f3c42 | 2019-07-08 11:03:26 -0700 | [diff] [blame] | 60 | if useClangTidy { |
| 61 | if err := runClangTidy(env, compilerCmd, 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 | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 65 | } else { |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 66 | if clangSyntax { |
Tobias Bosch | 38f3c42 | 2019-07-08 11:03:26 -0700 | [diff] [blame] | 67 | forceLocal := false |
| 68 | clangCmd, err := calcClangCommand(forceLocal, mainBuilder.clone()) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 69 | if err != nil { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 70 | return 0, err |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 71 | } |
| 72 | exitCode, err = checkClangSyntax(env, clangCmd) |
| 73 | if err != nil || exitCode != 0 { |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 74 | return exitCode, err |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | compilerCmd = calcGccCommand(mainBuilder) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 78 | } |
Tobias Bosch | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 79 | if shouldForceDisableWError(env) { |
| 80 | return doubleBuildWithWNoError(env, cfg, compilerCmd) |
| 81 | } |
| 82 | if err := env.exec(compilerCmd); err != nil { |
| 83 | if userErr, ok := getCCacheError(compilerCmd, err); ok { |
| 84 | return 0, userErr |
| 85 | } |
| 86 | // Note: This case only happens when the underlying env is not |
| 87 | // really doing an exec, e.g. commandRecordingEnv. |
| 88 | if exitCode, ok := getExitCode(err); ok { |
| 89 | return exitCode, nil |
| 90 | } |
| 91 | return exitCode, wrapErrorwithSourceLocf(err, "failed to execute %#v", compilerCmd) |
| 92 | } |
| 93 | return 0, err |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Tobias Bosch | 38f3c42 | 2019-07-08 11:03:26 -0700 | [diff] [blame] | 96 | func calcClangCommand(forceLocal bool, builder *commandBuilder) (*command, error) { |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 97 | sysroot := processSysrootFlag(builder) |
| 98 | builder.addPreUserArgs(builder.cfg.clangFlags...) |
| 99 | calcCommonPreUserArgs(builder) |
| 100 | if err := processClangFlags(builder); err != nil { |
| 101 | return nil, err |
| 102 | } |
Tobias Bosch | 38f3c42 | 2019-07-08 11:03:26 -0700 | [diff] [blame] | 103 | if !forceLocal { |
| 104 | processGomaCCacheFlags(sysroot, builder) |
| 105 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 106 | return builder.build(), nil |
| 107 | } |
| 108 | |
| 109 | func calcGccCommand(builder *commandBuilder) *command { |
| 110 | sysroot := processSysrootFlag(builder) |
| 111 | builder.addPreUserArgs(builder.cfg.gccFlags...) |
| 112 | calcCommonPreUserArgs(builder) |
| 113 | processGccFlags(builder) |
| 114 | processGomaCCacheFlags(sysroot, builder) |
| 115 | return builder.build() |
| 116 | } |
| 117 | |
| 118 | func calcCommonPreUserArgs(builder *commandBuilder) { |
| 119 | builder.addPreUserArgs(builder.cfg.commonFlags...) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 120 | processPieFlags(builder) |
| 121 | processStackProtectorFlags(builder) |
| 122 | processThumbCodeFlags(builder) |
| 123 | processX86Flags(builder) |
| 124 | processSanitizerFlags(builder) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | func processGomaCCacheFlags(sysroot string, builder *commandBuilder) { |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 128 | gomaccUsed := processGomaCccFlags(builder) |
| 129 | if !gomaccUsed { |
| 130 | processCCacheFlag(sysroot, builder) |
| 131 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 134 | func getAbsWrapperDir(env env, wrapperPath string) (string, error) { |
| 135 | if !filepath.IsAbs(wrapperPath) { |
| 136 | wrapperPath = filepath.Join(env.getwd(), wrapperPath) |
| 137 | } |
| 138 | evaledCmdPath, err := filepath.EvalSymlinks(wrapperPath) |
| 139 | if err != nil { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 140 | return "", wrapErrorwithSourceLocf(err, "failed to evaluate symlinks for %s", wrapperPath) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 141 | } |
| 142 | return filepath.Dir(evaledCmdPath), nil |
| 143 | } |
| 144 | |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 145 | func getCCacheError(compilerCmd *command, compilerCmdErr error) (ccacheErr userError, ok bool) { |
| 146 | if en, ok := compilerCmdErr.(syscall.Errno); ok && en == syscall.ENOENT && |
| 147 | strings.Contains(compilerCmd.path, "ccache") { |
| 148 | ccacheErr = |
| 149 | newUserErrorf("ccache not found under %s. Please install it", |
| 150 | compilerCmd.path) |
| 151 | return ccacheErr, ok |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 152 | } |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 153 | return ccacheErr, false |
| 154 | } |
| 155 | |
| 156 | func printCompilerError(writer io.Writer, compilerErr error) { |
| 157 | if _, ok := compilerErr.(userError); ok { |
| 158 | fmt.Fprintf(writer, "%s\n", compilerErr) |
| 159 | } else { |
| 160 | fmt.Fprintf(writer, |
| 161 | "Internal error. Please report to chromeos-toolchain@google.com.\n%s\n", |
| 162 | compilerErr) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 163 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 164 | } |