blob: 2de2f6a4fd76d34413aabed68d010f700f6c401a [file] [log] [blame]
Tobias Boschcfa8c242019-07-19 03:29:40 -07001// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Tobias Boschef8f9692019-06-10 15:50:33 -07005package main
6
7import (
George Burgess IV26caa2f2020-02-28 14:36:01 -08008 "bytes"
George Burgess IVcbc852e2021-01-25 13:11:02 -08009 "errors"
Tobias Bosch900dbc92019-06-24 09:31:39 -070010 "fmt"
11 "io"
Tobias Boschef8f9692019-06-10 15:50:33 -070012 "path/filepath"
Tobias Bosch5f98f2d2019-08-15 17:17:57 -070013 "strings"
Tobias Boschef8f9692019-06-10 15:50:33 -070014)
15
Tobias Bosch900dbc92019-06-24 09:31:39 -070016func callCompiler(env env, cfg *config, inputCmd *command) int {
Tobias Bosch900dbc92019-06-24 09:31:39 -070017 var compilerErr error
Tobias Bosch5edca502019-08-26 12:53:41 -070018
19 if !filepath.IsAbs(inputCmd.Path) && !strings.HasPrefix(inputCmd.Path, ".") &&
20 !strings.ContainsRune(inputCmd.Path, filepath.Separator) {
Tobias Bosch5f98f2d2019-08-15 17:17:57 -070021 if resolvedPath, err := resolveAgainstPathEnv(env, inputCmd.Path); err == nil {
22 inputCmd = &command{
23 Path: resolvedPath,
24 Args: inputCmd.Args,
25 EnvUpdates: inputCmd.EnvUpdates,
26 }
27 } else {
28 compilerErr = err
29 }
30 }
31 exitCode := 0
32 if compilerErr == nil {
Tobias Bosch8dd67e12019-10-28 14:26:51 -070033 exitCode, compilerErr = callCompilerInternal(env, cfg, inputCmd)
Tobias Bosch900dbc92019-06-24 09:31:39 -070034 }
35 if compilerErr != nil {
36 printCompilerError(env.stderr(), compilerErr)
37 exitCode = 1
38 }
39 return exitCode
40}
41
George Burgess IVc94f2432020-03-04 17:32:44 -080042// Given the main builder path and the absolute path to our wrapper, returns the path to the
43// 'real' compiler we should invoke.
44func calculateAndroidWrapperPath(mainBuilderPath string, absWrapperPath string) string {
45 // FIXME: This combination of using the directory of the symlink but the basename of the
46 // link target is strange but is the logic that old android wrapper uses. Change this to use
47 // directory and basename either from the absWrapperPath or from the builder.path, but don't
48 // mix anymore.
49
50 // We need to be careful here: path.Join Clean()s its result, so `./foo` will get
51 // transformed to `foo`, which isn't good since we're passing this path to exec.
52 basePart := filepath.Base(absWrapperPath) + ".real"
53 if !strings.ContainsRune(mainBuilderPath, filepath.Separator) {
54 return basePart
55 }
56
57 dirPart := filepath.Dir(mainBuilderPath)
58 if cleanResult := filepath.Join(dirPart, basePart); strings.ContainsRune(cleanResult, filepath.Separator) {
59 return cleanResult
60 }
61
62 return "." + string(filepath.Separator) + basePart
63}
64
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070065func callCompilerInternal(env env, cfg *config, inputCmd *command) (exitCode int, err error) {
Tobias Bosch900dbc92019-06-24 09:31:39 -070066 if err := checkUnsupportedFlags(inputCmd); err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070067 return 0, err
Tobias Bosch900dbc92019-06-24 09:31:39 -070068 }
Tobias Boschd8684172019-07-08 10:59:14 -070069 mainBuilder, err := newCommandBuilder(env, cfg, inputCmd)
Tobias Boschef8f9692019-06-10 15:50:33 -070070 if err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070071 return 0, err
Tobias Boschef8f9692019-06-10 15:50:33 -070072 }
Tobias Bosch58472812019-07-11 04:24:52 -070073 processPrintConfigFlag(mainBuilder)
74 processPrintCmdlineFlag(mainBuilder)
75 env = mainBuilder.env
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070076 var compilerCmd *command
Tobias Boschd8684172019-07-08 10:59:14 -070077 clangSyntax := processClangSyntaxFlag(mainBuilder)
George Burgess IVcbc852e2021-01-25 13:11:02 -080078
Ryan Beltran7c59ee02021-02-22 23:58:40 +000079 rusageEnabled := isRusageEnabled(env)
80
81 // Disable goma for rusage logs
82 allowGoma := !rusageEnabled
83 allowCCache := !rusageEnabled
84
George Burgess IVcbc852e2021-01-25 13:11:02 -080085 workAroundKernelBugWithRetries := false
Tobias Bosch8cb363f2019-10-17 07:44:13 -070086 if cfg.isAndroidWrapper {
George Burgess IVc94f2432020-03-04 17:32:44 -080087 mainBuilder.path = calculateAndroidWrapperPath(mainBuilder.path, mainBuilder.absWrapperPath)
Tobias Bosch8cb363f2019-10-17 07:44:13 -070088 switch mainBuilder.target.compilerType {
89 case clangType:
90 mainBuilder.addPreUserArgs(mainBuilder.cfg.clangFlags...)
91 mainBuilder.addPreUserArgs(mainBuilder.cfg.commonFlags...)
Pirama Arumuga Nainarefb75cf2020-10-21 13:57:01 -070092 mainBuilder.addPostUserArgs(mainBuilder.cfg.clangPostFlags...)
Ryan Beltran7c59ee02021-02-22 23:58:40 +000093 if _, err := processGomaCccFlags(allowGoma, mainBuilder); err != nil {
Tobias Bosch8cb363f2019-10-17 07:44:13 -070094 return 0, err
95 }
96 compilerCmd = mainBuilder.build()
97 case clangTidyType:
98 compilerCmd = mainBuilder.build()
99 default:
100 return 0, newErrorwithSourceLocf("unsupported compiler: %s", mainBuilder.target.compiler)
101 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700102 } else {
George Burgess IVcb465002020-07-20 16:53:39 -0700103 cSrcFile, tidyFlags, tidyMode := processClangTidyFlags(mainBuilder)
104 if mainBuilder.target.compilerType == clangType {
105 err := prepareClangCommand(mainBuilder)
Tobias Boschd8684172019-07-08 10:59:14 -0700106 if err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700107 return 0, err
Tobias Boschd8684172019-07-08 10:59:14 -0700108 }
George Burgess IVcb465002020-07-20 16:53:39 -0700109 if tidyMode != tidyModeNone {
110 allowCCache = false
111 clangCmdWithoutGomaAndCCache := mainBuilder.build()
112 var err error
113 switch tidyMode {
114 case tidyModeTricium:
115 if cfg.triciumNitsDir == "" {
116 return 0, newErrorwithSourceLocf("tricium linting was requested, but no nits directory is configured")
117 }
George Burgess IV0a377f42020-08-05 15:03:36 -0700118 err = runClangTidyForTricium(env, clangCmdWithoutGomaAndCCache, cSrcFile, cfg.triciumNitsDir, tidyFlags, cfg.crashArtifactsDir)
George Burgess IVcb465002020-07-20 16:53:39 -0700119 case tidyModeAll:
120 err = runClangTidy(env, clangCmdWithoutGomaAndCCache, cSrcFile, tidyFlags)
121 default:
122 panic(fmt.Sprintf("Unknown tidy mode: %v", tidyMode))
123 }
124
125 if err != nil {
126 return 0, err
127 }
128 }
Ryan Beltran7c59ee02021-02-22 23:58:40 +0000129 if err := processGomaCCacheFlags(allowGoma, allowCCache, mainBuilder); err != nil {
George Burgess IVcb465002020-07-20 16:53:39 -0700130 return 0, err
131 }
132 compilerCmd = mainBuilder.build()
133 } else {
134 if clangSyntax {
Ryan Beltran7c59ee02021-02-22 23:58:40 +0000135 allowCCache = false
136 clangCmd, err := calcClangCommand(allowGoma, allowCCache, mainBuilder.clone())
George Burgess IVcb465002020-07-20 16:53:39 -0700137 if err != nil {
138 return 0, err
139 }
Ryan Beltran7c59ee02021-02-22 23:58:40 +0000140 gccCmd, err := calcGccCommand(rusageEnabled, mainBuilder)
George Burgess IVcb465002020-07-20 16:53:39 -0700141 if err != nil {
142 return 0, err
143 }
144 return checkClangSyntax(env, clangCmd, gccCmd)
145 }
Ryan Beltran7c59ee02021-02-22 23:58:40 +0000146 compilerCmd, err = calcGccCommand(rusageEnabled, mainBuilder)
Tobias Boschc58f8d52019-09-30 10:37:42 -0700147 if err != nil {
148 return 0, err
149 }
George Burgess IVcbc852e2021-01-25 13:11:02 -0800150 workAroundKernelBugWithRetries = true
Tobias Boschc58f8d52019-09-30 10:37:42 -0700151 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700152 }
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000153
Tobias Bosch9780ea92019-07-11 01:19:42 -0700154 bisectStage := getBisectStage(env)
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000155
Ryan Beltran7c59ee02021-02-22 23:58:40 +0000156 if rusageEnabled {
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000157 compilerCmd = removeRusageFromCommand(compilerCmd)
158 }
159
George Burgess IV81267152021-01-25 12:11:56 -0800160 if shouldForceDisableWerror(env, cfg, mainBuilder.target.compilerType) {
Tobias Bosch9780ea92019-07-11 01:19:42 -0700161 if bisectStage != "" {
162 return 0, newUserErrorf("BISECT_STAGE is meaningless with FORCE_DISABLE_WERROR")
163 }
Ryan Beltran7c59ee02021-02-22 23:58:40 +0000164 return doubleBuildWithWNoError(env, cfg, compilerCmd)
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700165 }
Tobias Boschc88ee8a2019-10-01 15:00:52 -0700166 if shouldCompileWithFallback(env) {
Ryan Beltran7c59ee02021-02-22 23:58:40 +0000167 if rusageEnabled {
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000168 return 0, newUserErrorf("TOOLCHAIN_RUSAGE_OUTPUT is meaningless with ANDROID_LLVM_PREBUILT_COMPILER_PATH")
Tobias Boschc88ee8a2019-10-01 15:00:52 -0700169 }
170 if bisectStage != "" {
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000171 return 0, newUserErrorf("BISECT_STAGE is meaningless with ANDROID_LLVM_PREBUILT_COMPILER_PATH")
Tobias Boschc88ee8a2019-10-01 15:00:52 -0700172 }
173 return compileWithFallback(env, cfg, compilerCmd, mainBuilder.absWrapperPath)
174 }
Tobias Bosch9780ea92019-07-11 01:19:42 -0700175 if bisectStage != "" {
Ryan Beltran7c59ee02021-02-22 23:58:40 +0000176 if rusageEnabled {
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000177 return 0, newUserErrorf("TOOLCHAIN_RUSAGE_OUTPUT is meaningless with BISECT_STAGE")
178 }
Tobias Bosch820bffa2019-09-30 15:53:52 -0700179 compilerCmd, err = calcBisectCommand(env, cfg, bisectStage, compilerCmd)
180 if err != nil {
181 return 0, err
182 }
Tobias Bosch9780ea92019-07-11 01:19:42 -0700183 }
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000184
George Burgess IVcbc852e2021-01-25 13:11:02 -0800185 errRetryCompilation := errors.New("compilation retry requested")
186 var runCompiler func(willLogRusage bool) (int, error)
187 if !workAroundKernelBugWithRetries {
188 runCompiler = func(willLogRusage bool) (int, error) {
189 var err error
190 if willLogRusage {
191 err = env.run(compilerCmd, env.stdin(), env.stdout(), env.stderr())
192 } else {
193 // Note: We return from this in non-fatal circumstances only if the
194 // underlying env is not really doing an exec, e.g. commandRecordingEnv.
195 err = env.exec(compilerCmd)
196 }
197 return wrapSubprocessErrorWithSourceLoc(compilerCmd, err)
George Burgess IVde5be162021-01-25 15:50:54 -0800198 }
George Burgess IVcbc852e2021-01-25 13:11:02 -0800199 } else {
200 getStdin, err := prebufferStdinIfNeeded(env, compilerCmd)
201 if err != nil {
202 return 0, wrapErrorwithSourceLocf(err, "prebuffering stdin: %v", err)
203 }
204
205 stdoutBuffer := &bytes.Buffer{}
206 stderrBuffer := &bytes.Buffer{}
207 retryAttempt := 0
208 runCompiler = func(willLogRusage bool) (int, error) {
209 retryAttempt++
210 stdoutBuffer.Reset()
211 stderrBuffer.Reset()
212
213 exitCode, compilerErr := wrapSubprocessErrorWithSourceLoc(compilerCmd,
214 env.run(compilerCmd, getStdin(), stdoutBuffer, stderrBuffer))
215
216 if compilerErr != nil || exitCode != 0 {
217 if retryAttempt < kernelBugRetryLimit && (errorContainsTracesOfKernelBug(compilerErr) || containsTracesOfKernelBug(stdoutBuffer.Bytes()) || containsTracesOfKernelBug(stderrBuffer.Bytes())) {
218 return exitCode, errRetryCompilation
219 }
220 }
221 _, stdoutErr := stdoutBuffer.WriteTo(env.stdout())
222 _, stderrErr := stderrBuffer.WriteTo(env.stderr())
223 if stdoutErr != nil {
224 return exitCode, wrapErrorwithSourceLocf(err, "writing stdout: %v", stdoutErr)
225 }
226 if stderrErr != nil {
227 return exitCode, wrapErrorwithSourceLocf(err, "writing stderr: %v", stderrErr)
228 }
229 return exitCode, compilerErr
230 }
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000231 }
232
George Burgess IVcbc852e2021-01-25 13:11:02 -0800233 for {
234 var exitCode int
Ryan Beltran7c59ee02021-02-22 23:58:40 +0000235 commitRusage, err := maybeCaptureRusage(env, compilerCmd, func(willLogRusage bool) error {
George Burgess IVcbc852e2021-01-25 13:11:02 -0800236 var err error
237 exitCode, err = runCompiler(willLogRusage)
238 return err
239 })
240
241 switch {
242 case err == errRetryCompilation:
243 // Loop around again.
244 case err != nil:
245 return exitCode, err
246 default:
247 if err := commitRusage(exitCode); err != nil {
248 return exitCode, fmt.Errorf("commiting rusage: %v", err)
249 }
250
251 return exitCode, err
252 }
253 }
Tobias Boschd8684172019-07-08 10:59:14 -0700254}
255
Manoj Gupta28979262020-03-13 11:05:26 -0700256func prepareClangCommand(builder *commandBuilder) (err error) {
Tobias Bosch8cb363f2019-10-17 07:44:13 -0700257 if !builder.cfg.isHostWrapper {
Manoj Gupta28979262020-03-13 11:05:26 -0700258 processSysrootFlag(builder)
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700259 }
Tobias Boschd8684172019-07-08 10:59:14 -0700260 builder.addPreUserArgs(builder.cfg.clangFlags...)
George Burgess IV1713d252020-08-07 19:17:52 -0700261 if builder.cfg.crashArtifactsDir != "" {
262 builder.addPreUserArgs("-fcrash-diagnostics-dir=" + builder.cfg.crashArtifactsDir)
263 }
Caroline Tice8ac33e02019-10-28 09:48:18 -0700264 builder.addPostUserArgs(builder.cfg.clangPostFlags...)
Tobias Boschd8684172019-07-08 10:59:14 -0700265 calcCommonPreUserArgs(builder)
Manoj Gupta28979262020-03-13 11:05:26 -0700266 return processClangFlags(builder)
Tobias Bosch198a3c92019-07-17 04:22:34 -0700267}
268
Ryan Beltran7c59ee02021-02-22 23:58:40 +0000269func calcClangCommand(allowGoma bool, allowCCache bool, builder *commandBuilder) (*command, error) {
Manoj Gupta28979262020-03-13 11:05:26 -0700270 err := prepareClangCommand(builder)
Tobias Bosch198a3c92019-07-17 04:22:34 -0700271 if err != nil {
Tobias Boschd8684172019-07-08 10:59:14 -0700272 return nil, err
273 }
Ryan Beltran7c59ee02021-02-22 23:58:40 +0000274 if err := processGomaCCacheFlags(allowGoma, allowCCache, builder); err != nil {
Tobias Boschc58f8d52019-09-30 10:37:42 -0700275 return nil, err
276 }
Tobias Boschd8684172019-07-08 10:59:14 -0700277 return builder.build(), nil
278}
279
Ryan Beltran7c59ee02021-02-22 23:58:40 +0000280func calcGccCommand(enableRusage bool, builder *commandBuilder) (*command, error) {
Tobias Boschb27c8f22019-07-19 06:53:07 -0700281 if !builder.cfg.isHostWrapper {
Manoj Gupta28979262020-03-13 11:05:26 -0700282 processSysrootFlag(builder)
Tobias Boschb27c8f22019-07-19 06:53:07 -0700283 }
Tobias Boschd8684172019-07-08 10:59:14 -0700284 builder.addPreUserArgs(builder.cfg.gccFlags...)
Manoj Guptaefefb1a2021-01-28 21:46:53 -0800285 calcCommonPreUserArgs(builder)
Tobias Boschd8684172019-07-08 10:59:14 -0700286 processGccFlags(builder)
Tobias Boschb27c8f22019-07-19 06:53:07 -0700287 if !builder.cfg.isHostWrapper {
Ryan Beltran7c59ee02021-02-22 23:58:40 +0000288 if err := processGomaCCacheFlags(!enableRusage, !enableRusage, builder); err != nil {
Tobias Boschc58f8d52019-09-30 10:37:42 -0700289 return nil, err
290 }
Tobias Boschb27c8f22019-07-19 06:53:07 -0700291 }
Tobias Boschc58f8d52019-09-30 10:37:42 -0700292 return builder.build(), nil
Tobias Boschd8684172019-07-08 10:59:14 -0700293}
294
295func calcCommonPreUserArgs(builder *commandBuilder) {
296 builder.addPreUserArgs(builder.cfg.commonFlags...)
Tobias Bosch8cb363f2019-10-17 07:44:13 -0700297 if !builder.cfg.isHostWrapper {
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700298 processPieFlags(builder)
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700299 processThumbCodeFlags(builder)
Tobias Bosch1cd5f842019-08-20 10:05:33 -0700300 processStackProtectorFlags(builder)
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700301 processX86Flags(builder)
302 }
Tobias Bosch8cb363f2019-10-17 07:44:13 -0700303 processSanitizerFlags(builder)
Tobias Boschd8684172019-07-08 10:59:14 -0700304}
305
Ryan Beltran7c59ee02021-02-22 23:58:40 +0000306func processGomaCCacheFlags(allowGoma bool, allowCCache bool, builder *commandBuilder) (err error) {
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700307 gomaccUsed := false
308 if !builder.cfg.isHostWrapper {
Ryan Beltran7c59ee02021-02-22 23:58:40 +0000309 gomaccUsed, err = processGomaCccFlags(allowGoma, builder)
Tobias Boschc58f8d52019-09-30 10:37:42 -0700310 if err != nil {
311 return err
312 }
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700313 }
Tobias Bosch198a3c92019-07-17 04:22:34 -0700314 if !gomaccUsed && allowCCache {
Manoj Gupta28979262020-03-13 11:05:26 -0700315 processCCacheFlag(builder)
Tobias Boschef8f9692019-06-10 15:50:33 -0700316 }
Tobias Boschc58f8d52019-09-30 10:37:42 -0700317 return nil
Tobias Boschef8f9692019-06-10 15:50:33 -0700318}
319
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700320func getAbsWrapperPath(env env, wrapperCmd *command) (string, error) {
Tobias Bosch58472812019-07-11 04:24:52 -0700321 wrapperPath := getAbsCmdPath(env, wrapperCmd)
Tobias Boschef8f9692019-06-10 15:50:33 -0700322 evaledCmdPath, err := filepath.EvalSymlinks(wrapperPath)
323 if err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -0700324 return "", wrapErrorwithSourceLocf(err, "failed to evaluate symlinks for %s", wrapperPath)
Tobias Boschef8f9692019-06-10 15:50:33 -0700325 }
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700326 return evaledCmdPath, nil
Tobias Boschef8f9692019-06-10 15:50:33 -0700327}
328
Tobias Bosch900dbc92019-06-24 09:31:39 -0700329func printCompilerError(writer io.Writer, compilerErr error) {
330 if _, ok := compilerErr.(userError); ok {
331 fmt.Fprintf(writer, "%s\n", compilerErr)
332 } else {
George Burgess IV2efe72e2020-06-18 20:37:28 -0700333 emailAccount := "chromeos-toolchain"
334 if isAndroidConfig() {
335 emailAccount = "android-llvm"
336 }
Tobias Bosch900dbc92019-06-24 09:31:39 -0700337 fmt.Fprintf(writer,
George Burgess IV2efe72e2020-06-18 20:37:28 -0700338 "Internal error. Please report to %s@google.com.\n%s\n",
339 emailAccount, compilerErr)
Tobias Boschef8f9692019-06-10 15:50:33 -0700340 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700341}
Tobias Bosch6f59a662019-08-20 15:37:11 -0700342
George Burgess IV26caa2f2020-02-28 14:36:01 -0800343func needStdinTee(inputCmd *command) bool {
Tobias Bosch6f59a662019-08-20 15:37:11 -0700344 lastArg := ""
345 for _, arg := range inputCmd.Args {
346 if arg == "-" && lastArg != "-o" {
George Burgess IV26caa2f2020-02-28 14:36:01 -0800347 return true
Tobias Bosch6f59a662019-08-20 15:37:11 -0700348 }
349 lastArg = arg
350 }
George Burgess IV26caa2f2020-02-28 14:36:01 -0800351 return false
352}
353
354func prebufferStdinIfNeeded(env env, inputCmd *command) (getStdin func() io.Reader, err error) {
355 // We pre-buffer the entirety of stdin, since the compiler may exit mid-invocation with an
356 // error, which may leave stdin partially read.
357 if !needStdinTee(inputCmd) {
358 // This won't produce deterministic input to the compiler, but stdin shouldn't
359 // matter in this case, so...
360 return env.stdin, nil
361 }
362
363 stdinBuffer := &bytes.Buffer{}
364 if _, err := stdinBuffer.ReadFrom(env.stdin()); err != nil {
365 return nil, wrapErrorwithSourceLocf(err, "prebuffering stdin")
366 }
367
368 return func() io.Reader { return bytes.NewReader(stdinBuffer.Bytes()) }, nil
Tobias Bosch6f59a662019-08-20 15:37:11 -0700369}