blob: 009cc87126bc98fe0b34763910e1973b9582eaf1 [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
79 workAroundKernelBugWithRetries := false
Tobias Bosch8cb363f2019-10-17 07:44:13 -070080 if cfg.isAndroidWrapper {
George Burgess IVc94f2432020-03-04 17:32:44 -080081 mainBuilder.path = calculateAndroidWrapperPath(mainBuilder.path, mainBuilder.absWrapperPath)
Tobias Bosch8cb363f2019-10-17 07:44:13 -070082 switch mainBuilder.target.compilerType {
83 case clangType:
84 mainBuilder.addPreUserArgs(mainBuilder.cfg.clangFlags...)
85 mainBuilder.addPreUserArgs(mainBuilder.cfg.commonFlags...)
Pirama Arumuga Nainarefb75cf2020-10-21 13:57:01 -070086 mainBuilder.addPostUserArgs(mainBuilder.cfg.clangPostFlags...)
Tobias Bosch8cb363f2019-10-17 07:44:13 -070087 if _, err := processGomaCccFlags(mainBuilder); err != nil {
88 return 0, err
89 }
90 compilerCmd = mainBuilder.build()
91 case clangTidyType:
92 compilerCmd = mainBuilder.build()
93 default:
94 return 0, newErrorwithSourceLocf("unsupported compiler: %s", mainBuilder.target.compiler)
95 }
Tobias Boschef8f9692019-06-10 15:50:33 -070096 } else {
George Burgess IVcb465002020-07-20 16:53:39 -070097 cSrcFile, tidyFlags, tidyMode := processClangTidyFlags(mainBuilder)
98 if mainBuilder.target.compilerType == clangType {
99 err := prepareClangCommand(mainBuilder)
Tobias Boschd8684172019-07-08 10:59:14 -0700100 if err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700101 return 0, err
Tobias Boschd8684172019-07-08 10:59:14 -0700102 }
George Burgess IVcb465002020-07-20 16:53:39 -0700103 allowCCache := true
104 if tidyMode != tidyModeNone {
105 allowCCache = false
106 clangCmdWithoutGomaAndCCache := mainBuilder.build()
107 var err error
108 switch tidyMode {
109 case tidyModeTricium:
110 if cfg.triciumNitsDir == "" {
111 return 0, newErrorwithSourceLocf("tricium linting was requested, but no nits directory is configured")
112 }
George Burgess IV0a377f42020-08-05 15:03:36 -0700113 err = runClangTidyForTricium(env, clangCmdWithoutGomaAndCCache, cSrcFile, cfg.triciumNitsDir, tidyFlags, cfg.crashArtifactsDir)
George Burgess IVcb465002020-07-20 16:53:39 -0700114 case tidyModeAll:
115 err = runClangTidy(env, clangCmdWithoutGomaAndCCache, cSrcFile, tidyFlags)
116 default:
117 panic(fmt.Sprintf("Unknown tidy mode: %v", tidyMode))
118 }
119
120 if err != nil {
121 return 0, err
122 }
123 }
124 if err := processGomaCCacheFlags(allowCCache, mainBuilder); err != nil {
125 return 0, err
126 }
127 compilerCmd = mainBuilder.build()
128 } else {
129 if clangSyntax {
130 allowCCache := false
131 clangCmd, err := calcClangCommand(allowCCache, mainBuilder.clone())
132 if err != nil {
133 return 0, err
134 }
135 gccCmd, err := calcGccCommand(mainBuilder)
136 if err != nil {
137 return 0, err
138 }
139 return checkClangSyntax(env, clangCmd, gccCmd)
140 }
141 compilerCmd, err = calcGccCommand(mainBuilder)
Tobias Boschc58f8d52019-09-30 10:37:42 -0700142 if err != nil {
143 return 0, err
144 }
George Burgess IVcbc852e2021-01-25 13:11:02 -0800145 workAroundKernelBugWithRetries = true
Tobias Boschc58f8d52019-09-30 10:37:42 -0700146 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700147 }
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000148
Tobias Bosch9d609302019-07-10 06:16:04 -0700149 rusageLogfileName := getRusageLogFilename(env)
Tobias Bosch9780ea92019-07-11 01:19:42 -0700150 bisectStage := getBisectStage(env)
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000151
152 if rusageLogfileName != "" {
153 compilerCmd = removeRusageFromCommand(compilerCmd)
154 }
155
George Burgess IV81267152021-01-25 12:11:56 -0800156 if shouldForceDisableWerror(env, cfg, mainBuilder.target.compilerType) {
Tobias Bosch9780ea92019-07-11 01:19:42 -0700157 if bisectStage != "" {
158 return 0, newUserErrorf("BISECT_STAGE is meaningless with FORCE_DISABLE_WERROR")
159 }
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000160 return doubleBuildWithWNoError(env, cfg, compilerCmd, rusageLogfileName)
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700161 }
Tobias Boschc88ee8a2019-10-01 15:00:52 -0700162 if shouldCompileWithFallback(env) {
163 if rusageLogfileName != "" {
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000164 return 0, newUserErrorf("TOOLCHAIN_RUSAGE_OUTPUT is meaningless with ANDROID_LLVM_PREBUILT_COMPILER_PATH")
Tobias Boschc88ee8a2019-10-01 15:00:52 -0700165 }
166 if bisectStage != "" {
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000167 return 0, newUserErrorf("BISECT_STAGE is meaningless with ANDROID_LLVM_PREBUILT_COMPILER_PATH")
Tobias Boschc88ee8a2019-10-01 15:00:52 -0700168 }
169 return compileWithFallback(env, cfg, compilerCmd, mainBuilder.absWrapperPath)
170 }
Tobias Bosch9780ea92019-07-11 01:19:42 -0700171 if bisectStage != "" {
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000172 if rusageLogfileName != "" {
173 return 0, newUserErrorf("TOOLCHAIN_RUSAGE_OUTPUT is meaningless with BISECT_STAGE")
174 }
Tobias Bosch820bffa2019-09-30 15:53:52 -0700175 compilerCmd, err = calcBisectCommand(env, cfg, bisectStage, compilerCmd)
176 if err != nil {
177 return 0, err
178 }
Tobias Bosch9780ea92019-07-11 01:19:42 -0700179 }
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000180
George Burgess IVcbc852e2021-01-25 13:11:02 -0800181 errRetryCompilation := errors.New("compilation retry requested")
182 var runCompiler func(willLogRusage bool) (int, error)
183 if !workAroundKernelBugWithRetries {
184 runCompiler = func(willLogRusage bool) (int, error) {
185 var err error
186 if willLogRusage {
187 err = env.run(compilerCmd, env.stdin(), env.stdout(), env.stderr())
188 } else {
189 // Note: We return from this in non-fatal circumstances only if the
190 // underlying env is not really doing an exec, e.g. commandRecordingEnv.
191 err = env.exec(compilerCmd)
192 }
193 return wrapSubprocessErrorWithSourceLoc(compilerCmd, err)
George Burgess IVde5be162021-01-25 15:50:54 -0800194 }
George Burgess IVcbc852e2021-01-25 13:11:02 -0800195 } else {
196 getStdin, err := prebufferStdinIfNeeded(env, compilerCmd)
197 if err != nil {
198 return 0, wrapErrorwithSourceLocf(err, "prebuffering stdin: %v", err)
199 }
200
201 stdoutBuffer := &bytes.Buffer{}
202 stderrBuffer := &bytes.Buffer{}
203 retryAttempt := 0
204 runCompiler = func(willLogRusage bool) (int, error) {
205 retryAttempt++
206 stdoutBuffer.Reset()
207 stderrBuffer.Reset()
208
209 exitCode, compilerErr := wrapSubprocessErrorWithSourceLoc(compilerCmd,
210 env.run(compilerCmd, getStdin(), stdoutBuffer, stderrBuffer))
211
212 if compilerErr != nil || exitCode != 0 {
213 if retryAttempt < kernelBugRetryLimit && (errorContainsTracesOfKernelBug(compilerErr) || containsTracesOfKernelBug(stdoutBuffer.Bytes()) || containsTracesOfKernelBug(stderrBuffer.Bytes())) {
214 return exitCode, errRetryCompilation
215 }
216 }
217 _, stdoutErr := stdoutBuffer.WriteTo(env.stdout())
218 _, stderrErr := stderrBuffer.WriteTo(env.stderr())
219 if stdoutErr != nil {
220 return exitCode, wrapErrorwithSourceLocf(err, "writing stdout: %v", stdoutErr)
221 }
222 if stderrErr != nil {
223 return exitCode, wrapErrorwithSourceLocf(err, "writing stderr: %v", stderrErr)
224 }
225 return exitCode, compilerErr
226 }
Ryan Beltran7ee49e42021-01-19 01:52:42 +0000227 }
228
George Burgess IVcbc852e2021-01-25 13:11:02 -0800229 for {
230 var exitCode int
231 commitRusage, err := maybeCaptureRusage(env, rusageLogfileName, compilerCmd, func(willLogRusage bool) error {
232 var err error
233 exitCode, err = runCompiler(willLogRusage)
234 return err
235 })
236
237 switch {
238 case err == errRetryCompilation:
239 // Loop around again.
240 case err != nil:
241 return exitCode, err
242 default:
243 if err := commitRusage(exitCode); err != nil {
244 return exitCode, fmt.Errorf("commiting rusage: %v", err)
245 }
246
247 return exitCode, err
248 }
249 }
Tobias Boschd8684172019-07-08 10:59:14 -0700250}
251
Manoj Gupta28979262020-03-13 11:05:26 -0700252func prepareClangCommand(builder *commandBuilder) (err error) {
Tobias Bosch8cb363f2019-10-17 07:44:13 -0700253 if !builder.cfg.isHostWrapper {
Manoj Gupta28979262020-03-13 11:05:26 -0700254 processSysrootFlag(builder)
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700255 }
Tobias Boschd8684172019-07-08 10:59:14 -0700256 builder.addPreUserArgs(builder.cfg.clangFlags...)
George Burgess IV1713d252020-08-07 19:17:52 -0700257 if builder.cfg.crashArtifactsDir != "" {
258 builder.addPreUserArgs("-fcrash-diagnostics-dir=" + builder.cfg.crashArtifactsDir)
259 }
Caroline Tice8ac33e02019-10-28 09:48:18 -0700260 builder.addPostUserArgs(builder.cfg.clangPostFlags...)
Tobias Boschd8684172019-07-08 10:59:14 -0700261 calcCommonPreUserArgs(builder)
Manoj Gupta28979262020-03-13 11:05:26 -0700262 return processClangFlags(builder)
Tobias Bosch198a3c92019-07-17 04:22:34 -0700263}
264
265func calcClangCommand(allowCCache bool, builder *commandBuilder) (*command, error) {
Manoj Gupta28979262020-03-13 11:05:26 -0700266 err := prepareClangCommand(builder)
Tobias Bosch198a3c92019-07-17 04:22:34 -0700267 if err != nil {
Tobias Boschd8684172019-07-08 10:59:14 -0700268 return nil, err
269 }
Manoj Gupta28979262020-03-13 11:05:26 -0700270 if err := processGomaCCacheFlags(allowCCache, builder); err != nil {
Tobias Boschc58f8d52019-09-30 10:37:42 -0700271 return nil, err
272 }
Tobias Boschd8684172019-07-08 10:59:14 -0700273 return builder.build(), nil
274}
275
Tobias Boschc58f8d52019-09-30 10:37:42 -0700276func calcGccCommand(builder *commandBuilder) (*command, error) {
Tobias Boschb27c8f22019-07-19 06:53:07 -0700277 if !builder.cfg.isHostWrapper {
Manoj Gupta28979262020-03-13 11:05:26 -0700278 processSysrootFlag(builder)
Tobias Boschb27c8f22019-07-19 06:53:07 -0700279 }
Tobias Boschd8684172019-07-08 10:59:14 -0700280 builder.addPreUserArgs(builder.cfg.gccFlags...)
Manoj Guptaefefb1a2021-01-28 21:46:53 -0800281 calcCommonPreUserArgs(builder)
Tobias Boschd8684172019-07-08 10:59:14 -0700282 processGccFlags(builder)
Tobias Boschb27c8f22019-07-19 06:53:07 -0700283 if !builder.cfg.isHostWrapper {
284 allowCCache := true
Manoj Gupta28979262020-03-13 11:05:26 -0700285 if err := processGomaCCacheFlags(allowCCache, builder); err != nil {
Tobias Boschc58f8d52019-09-30 10:37:42 -0700286 return nil, err
287 }
Tobias Boschb27c8f22019-07-19 06:53:07 -0700288 }
Tobias Boschc58f8d52019-09-30 10:37:42 -0700289 return builder.build(), nil
Tobias Boschd8684172019-07-08 10:59:14 -0700290}
291
292func calcCommonPreUserArgs(builder *commandBuilder) {
293 builder.addPreUserArgs(builder.cfg.commonFlags...)
Tobias Bosch8cb363f2019-10-17 07:44:13 -0700294 if !builder.cfg.isHostWrapper {
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700295 processPieFlags(builder)
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700296 processThumbCodeFlags(builder)
Tobias Bosch1cd5f842019-08-20 10:05:33 -0700297 processStackProtectorFlags(builder)
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700298 processX86Flags(builder)
299 }
Tobias Bosch8cb363f2019-10-17 07:44:13 -0700300 processSanitizerFlags(builder)
Tobias Boschd8684172019-07-08 10:59:14 -0700301}
302
Manoj Gupta28979262020-03-13 11:05:26 -0700303func processGomaCCacheFlags(allowCCache bool, builder *commandBuilder) (err error) {
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700304 gomaccUsed := false
305 if !builder.cfg.isHostWrapper {
Tobias Boschc58f8d52019-09-30 10:37:42 -0700306 gomaccUsed, err = processGomaCccFlags(builder)
307 if err != nil {
308 return err
309 }
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700310 }
Tobias Bosch198a3c92019-07-17 04:22:34 -0700311 if !gomaccUsed && allowCCache {
Manoj Gupta28979262020-03-13 11:05:26 -0700312 processCCacheFlag(builder)
Tobias Boschef8f9692019-06-10 15:50:33 -0700313 }
Tobias Boschc58f8d52019-09-30 10:37:42 -0700314 return nil
Tobias Boschef8f9692019-06-10 15:50:33 -0700315}
316
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700317func getAbsWrapperPath(env env, wrapperCmd *command) (string, error) {
Tobias Bosch58472812019-07-11 04:24:52 -0700318 wrapperPath := getAbsCmdPath(env, wrapperCmd)
Tobias Boschef8f9692019-06-10 15:50:33 -0700319 evaledCmdPath, err := filepath.EvalSymlinks(wrapperPath)
320 if err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -0700321 return "", wrapErrorwithSourceLocf(err, "failed to evaluate symlinks for %s", wrapperPath)
Tobias Boschef8f9692019-06-10 15:50:33 -0700322 }
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700323 return evaledCmdPath, nil
Tobias Boschef8f9692019-06-10 15:50:33 -0700324}
325
Tobias Bosch900dbc92019-06-24 09:31:39 -0700326func printCompilerError(writer io.Writer, compilerErr error) {
327 if _, ok := compilerErr.(userError); ok {
328 fmt.Fprintf(writer, "%s\n", compilerErr)
329 } else {
George Burgess IV2efe72e2020-06-18 20:37:28 -0700330 emailAccount := "chromeos-toolchain"
331 if isAndroidConfig() {
332 emailAccount = "android-llvm"
333 }
Tobias Bosch900dbc92019-06-24 09:31:39 -0700334 fmt.Fprintf(writer,
George Burgess IV2efe72e2020-06-18 20:37:28 -0700335 "Internal error. Please report to %s@google.com.\n%s\n",
336 emailAccount, compilerErr)
Tobias Boschef8f9692019-06-10 15:50:33 -0700337 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700338}
Tobias Bosch6f59a662019-08-20 15:37:11 -0700339
George Burgess IV26caa2f2020-02-28 14:36:01 -0800340func needStdinTee(inputCmd *command) bool {
Tobias Bosch6f59a662019-08-20 15:37:11 -0700341 lastArg := ""
342 for _, arg := range inputCmd.Args {
343 if arg == "-" && lastArg != "-o" {
George Burgess IV26caa2f2020-02-28 14:36:01 -0800344 return true
Tobias Bosch6f59a662019-08-20 15:37:11 -0700345 }
346 lastArg = arg
347 }
George Burgess IV26caa2f2020-02-28 14:36:01 -0800348 return false
349}
350
351func prebufferStdinIfNeeded(env env, inputCmd *command) (getStdin func() io.Reader, err error) {
352 // We pre-buffer the entirety of stdin, since the compiler may exit mid-invocation with an
353 // error, which may leave stdin partially read.
354 if !needStdinTee(inputCmd) {
355 // This won't produce deterministic input to the compiler, but stdin shouldn't
356 // matter in this case, so...
357 return env.stdin, nil
358 }
359
360 stdinBuffer := &bytes.Buffer{}
361 if _, err := stdinBuffer.ReadFrom(env.stdin()); err != nil {
362 return nil, wrapErrorwithSourceLocf(err, "prebuffering stdin")
363 }
364
365 return func() io.Reader { return bytes.NewReader(stdinBuffer.Bytes()) }, nil
Tobias Bosch6f59a662019-08-20 15:37:11 -0700366}