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