blob: b0269abe69ae06d7f946240b11eca82b453feb24 [file] [log] [blame]
Tobias Boschef8f9692019-06-10 15:50:33 -07001package main
2
3import (
Tobias Bosch900dbc92019-06-24 09:31:39 -07004 "fmt"
5 "io"
Tobias Boschef8f9692019-06-10 15:50:33 -07006 "path/filepath"
Tobias Boschef8f9692019-06-10 15:50:33 -07007)
8
Tobias Bosch900dbc92019-06-24 09:31:39 -07009func callCompiler(env env, cfg *config, inputCmd *command) int {
10 exitCode := 0
11 var compilerErr error
Tobias Bosch9780ea92019-07-11 01:19:42 -070012 if cfg.oldWrapperPath != "" {
Tobias Bosch900dbc92019-06-24 09:31:39 -070013 exitCode, compilerErr = callCompilerWithRunAndCompareToOldWrapper(env, cfg, inputCmd)
14 } else {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070015 exitCode, compilerErr = callCompilerInternal(env, cfg, inputCmd)
Tobias Bosch900dbc92019-06-24 09:31:39 -070016 }
17 if compilerErr != nil {
18 printCompilerError(env.stderr(), compilerErr)
19 exitCode = 1
20 }
21 return exitCode
22}
23
24func callCompilerWithRunAndCompareToOldWrapper(env env, cfg *config, inputCmd *command) (exitCode int, err error) {
25 recordingEnv := &commandRecordingEnv{
26 env: env,
27 }
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070028 // 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 Bosch900dbc92019-06-24 09:31:39 -070031 }
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070032 if err = compareToOldWrapper(env, cfg, inputCmd, recordingEnv.cmdResults, exitCode); err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -070033 return exitCode, err
34 }
35 return exitCode, nil
36}
37
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070038func callCompilerInternal(env env, cfg *config, inputCmd *command) (exitCode int, err error) {
Tobias Bosch900dbc92019-06-24 09:31:39 -070039 if err := checkUnsupportedFlags(inputCmd); err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070040 return 0, err
Tobias Bosch900dbc92019-06-24 09:31:39 -070041 }
Tobias Boschd8684172019-07-08 10:59:14 -070042 mainBuilder, err := newCommandBuilder(env, cfg, inputCmd)
Tobias Boschef8f9692019-06-10 15:50:33 -070043 if err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070044 return 0, err
Tobias Boschef8f9692019-06-10 15:50:33 -070045 }
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070046 var compilerCmd *command
Tobias Boschd8684172019-07-08 10:59:14 -070047 clangSyntax := processClangSyntaxFlag(mainBuilder)
48 if mainBuilder.target.compilerType == clangType {
Tobias Bosch38f3c422019-07-08 11:03:26 -070049 cSrcFile, useClangTidy := processClangTidyFlags(mainBuilder)
50 compilerCmd, err = calcClangCommand(useClangTidy, mainBuilder)
Tobias Boschd8684172019-07-08 10:59:14 -070051 if err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070052 return 0, err
Tobias Boschd8684172019-07-08 10:59:14 -070053 }
Tobias Bosch38f3c422019-07-08 11:03:26 -070054 if useClangTidy {
55 if err := runClangTidy(env, compilerCmd, cSrcFile); err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070056 return 0, err
Tobias Bosch38f3c422019-07-08 11:03:26 -070057 }
58 }
Tobias Boschef8f9692019-06-10 15:50:33 -070059 } else {
Tobias Boschd8684172019-07-08 10:59:14 -070060 if clangSyntax {
Tobias Bosch38f3c422019-07-08 11:03:26 -070061 forceLocal := false
62 clangCmd, err := calcClangCommand(forceLocal, mainBuilder.clone())
Tobias Boschd8684172019-07-08 10:59:14 -070063 if err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070064 return 0, err
Tobias Boschd8684172019-07-08 10:59:14 -070065 }
66 exitCode, err = checkClangSyntax(env, clangCmd)
67 if err != nil || exitCode != 0 {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070068 return exitCode, err
Tobias Boschd8684172019-07-08 10:59:14 -070069 }
70 }
71 compilerCmd = calcGccCommand(mainBuilder)
Tobias Boschef8f9692019-06-10 15:50:33 -070072 }
Tobias Bosch9d609302019-07-10 06:16:04 -070073 rusageLogfileName := getRusageLogFilename(env)
Tobias Bosch9780ea92019-07-11 01:19:42 -070074 bisectStage := getBisectStage(env)
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070075 if shouldForceDisableWError(env) {
Tobias Bosch9d609302019-07-10 06:16:04 -070076 if rusageLogfileName != "" {
77 return 0, newUserErrorf("GETRUSAGE is meaningless with FORCE_DISABLE_WERROR")
78 }
Tobias Bosch9780ea92019-07-11 01:19:42 -070079 if bisectStage != "" {
80 return 0, newUserErrorf("BISECT_STAGE is meaningless with FORCE_DISABLE_WERROR")
81 }
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070082 return doubleBuildWithWNoError(env, cfg, compilerCmd)
83 }
Tobias Bosch9d609302019-07-10 06:16:04 -070084 if rusageLogfileName != "" {
Tobias Bosch9780ea92019-07-11 01:19:42 -070085 if bisectStage != "" {
86 return 0, newUserErrorf("BISECT_STAGE is meaningless with GETRUSAGE")
87 }
Tobias Bosch9d609302019-07-10 06:16:04 -070088 return logRusage(env, rusageLogfileName, compilerCmd)
89 }
Tobias Bosch9780ea92019-07-11 01:19:42 -070090 if bisectStage != "" {
91 compilerCmd = calcBisectCommand(env, bisectStage, compilerCmd)
92 }
Tobias Bosch9332d212019-07-10 06:23:57 -070093 // 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 Boschd8684172019-07-08 10:59:14 -070096}
97
Tobias Bosch38f3c422019-07-08 11:03:26 -070098func calcClangCommand(forceLocal bool, builder *commandBuilder) (*command, error) {
Tobias Boschd8684172019-07-08 10:59:14 -070099 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 Bosch38f3c422019-07-08 11:03:26 -0700105 if !forceLocal {
106 processGomaCCacheFlags(sysroot, builder)
107 }
Tobias Boschd8684172019-07-08 10:59:14 -0700108 return builder.build(), nil
109}
110
111func 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
120func calcCommonPreUserArgs(builder *commandBuilder) {
121 builder.addPreUserArgs(builder.cfg.commonFlags...)
Tobias Boschef8f9692019-06-10 15:50:33 -0700122 processPieFlags(builder)
123 processStackProtectorFlags(builder)
124 processThumbCodeFlags(builder)
125 processX86Flags(builder)
126 processSanitizerFlags(builder)
Tobias Boschd8684172019-07-08 10:59:14 -0700127}
128
129func processGomaCCacheFlags(sysroot string, builder *commandBuilder) {
Tobias Boschef8f9692019-06-10 15:50:33 -0700130 gomaccUsed := processGomaCccFlags(builder)
131 if !gomaccUsed {
132 processCCacheFlag(sysroot, builder)
133 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700134}
135
Tobias Boschef8f9692019-06-10 15:50:33 -0700136func 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 Bosch900dbc92019-06-24 09:31:39 -0700142 return "", wrapErrorwithSourceLocf(err, "failed to evaluate symlinks for %s", wrapperPath)
Tobias Boschef8f9692019-06-10 15:50:33 -0700143 }
144 return filepath.Dir(evaledCmdPath), nil
145}
146
Tobias Bosch900dbc92019-06-24 09:31:39 -0700147func 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 Boschef8f9692019-06-10 15:50:33 -0700154 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700155}