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