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 | f6d9f4f | 2019-07-09 08:09:01 -0700 | [diff] [blame] | 77 | if shouldForceDisableWError(env) { |
| 78 | return doubleBuildWithWNoError(env, cfg, compilerCmd) |
| 79 | } |
Tobias Bosch | 9332d21 | 2019-07-10 06:23:57 -0700 | [diff] [blame^] | 80 | // Note: We return an exit code only if the underlying env is not |
| 81 | // really doing an exec, e.g. commandRecordingEnv. |
| 82 | return wrapSubprocessErrorWithSourceLoc(compilerCmd, env.exec(compilerCmd)) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Tobias Bosch | 38f3c42 | 2019-07-08 11:03:26 -0700 | [diff] [blame] | 85 | func calcClangCommand(forceLocal bool, builder *commandBuilder) (*command, error) { |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 86 | sysroot := processSysrootFlag(builder) |
| 87 | builder.addPreUserArgs(builder.cfg.clangFlags...) |
| 88 | calcCommonPreUserArgs(builder) |
| 89 | if err := processClangFlags(builder); err != nil { |
| 90 | return nil, err |
| 91 | } |
Tobias Bosch | 38f3c42 | 2019-07-08 11:03:26 -0700 | [diff] [blame] | 92 | if !forceLocal { |
| 93 | processGomaCCacheFlags(sysroot, builder) |
| 94 | } |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 95 | return builder.build(), nil |
| 96 | } |
| 97 | |
| 98 | func calcGccCommand(builder *commandBuilder) *command { |
| 99 | sysroot := processSysrootFlag(builder) |
| 100 | builder.addPreUserArgs(builder.cfg.gccFlags...) |
| 101 | calcCommonPreUserArgs(builder) |
| 102 | processGccFlags(builder) |
| 103 | processGomaCCacheFlags(sysroot, builder) |
| 104 | return builder.build() |
| 105 | } |
| 106 | |
| 107 | func calcCommonPreUserArgs(builder *commandBuilder) { |
| 108 | builder.addPreUserArgs(builder.cfg.commonFlags...) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 109 | processPieFlags(builder) |
| 110 | processStackProtectorFlags(builder) |
| 111 | processThumbCodeFlags(builder) |
| 112 | processX86Flags(builder) |
| 113 | processSanitizerFlags(builder) |
Tobias Bosch | d868417 | 2019-07-08 10:59:14 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | func processGomaCCacheFlags(sysroot string, builder *commandBuilder) { |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 117 | gomaccUsed := processGomaCccFlags(builder) |
| 118 | if !gomaccUsed { |
| 119 | processCCacheFlag(sysroot, builder) |
| 120 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 123 | func getAbsWrapperDir(env env, wrapperPath string) (string, error) { |
| 124 | if !filepath.IsAbs(wrapperPath) { |
| 125 | wrapperPath = filepath.Join(env.getwd(), wrapperPath) |
| 126 | } |
| 127 | evaledCmdPath, err := filepath.EvalSymlinks(wrapperPath) |
| 128 | if err != nil { |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 129 | return "", wrapErrorwithSourceLocf(err, "failed to evaluate symlinks for %s", wrapperPath) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 130 | } |
| 131 | return filepath.Dir(evaledCmdPath), nil |
| 132 | } |
| 133 | |
Tobias Bosch | 900dbc9 | 2019-06-24 09:31:39 -0700 | [diff] [blame] | 134 | func printCompilerError(writer io.Writer, compilerErr error) { |
| 135 | if _, ok := compilerErr.(userError); ok { |
| 136 | fmt.Fprintf(writer, "%s\n", compilerErr) |
| 137 | } else { |
| 138 | fmt.Fprintf(writer, |
| 139 | "Internal error. Please report to chromeos-toolchain@google.com.\n%s\n", |
| 140 | compilerErr) |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 141 | } |
Tobias Bosch | ef8f969 | 2019-06-10 15:50:33 -0700 | [diff] [blame] | 142 | } |