blob: 14de210a76a71c17797ae2aa9db372f0cc6d0d5b [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"
Tobias Bosch900dbc92019-06-24 09:31:39 -07009 "fmt"
10 "io"
Tobias Boschef8f9692019-06-10 15:50:33 -070011 "path/filepath"
Tobias Bosch5f98f2d2019-08-15 17:17:57 -070012 "strings"
Tobias Boschef8f9692019-06-10 15:50:33 -070013)
14
Tobias Bosch900dbc92019-06-24 09:31:39 -070015func callCompiler(env env, cfg *config, inputCmd *command) int {
Tobias Bosch900dbc92019-06-24 09:31:39 -070016 var compilerErr error
Tobias Bosch5edca502019-08-26 12:53:41 -070017
18 if !filepath.IsAbs(inputCmd.Path) && !strings.HasPrefix(inputCmd.Path, ".") &&
19 !strings.ContainsRune(inputCmd.Path, filepath.Separator) {
Tobias Bosch5f98f2d2019-08-15 17:17:57 -070020 if resolvedPath, err := resolveAgainstPathEnv(env, inputCmd.Path); err == nil {
21 inputCmd = &command{
22 Path: resolvedPath,
23 Args: inputCmd.Args,
24 EnvUpdates: inputCmd.EnvUpdates,
25 }
26 } else {
27 compilerErr = err
28 }
29 }
30 exitCode := 0
31 if compilerErr == nil {
Tobias Bosch8dd67e12019-10-28 14:26:51 -070032 exitCode, compilerErr = callCompilerInternal(env, cfg, inputCmd)
Tobias Bosch900dbc92019-06-24 09:31:39 -070033 }
34 if compilerErr != nil {
35 printCompilerError(env.stderr(), compilerErr)
36 exitCode = 1
37 }
38 return exitCode
39}
40
George Burgess IVc94f2432020-03-04 17:32:44 -080041// Given the main builder path and the absolute path to our wrapper, returns the path to the
42// 'real' compiler we should invoke.
43func calculateAndroidWrapperPath(mainBuilderPath string, absWrapperPath string) string {
44 // FIXME: This combination of using the directory of the symlink but the basename of the
45 // link target is strange but is the logic that old android wrapper uses. Change this to use
46 // directory and basename either from the absWrapperPath or from the builder.path, but don't
47 // mix anymore.
48
49 // We need to be careful here: path.Join Clean()s its result, so `./foo` will get
50 // transformed to `foo`, which isn't good since we're passing this path to exec.
51 basePart := filepath.Base(absWrapperPath) + ".real"
52 if !strings.ContainsRune(mainBuilderPath, filepath.Separator) {
53 return basePart
54 }
55
56 dirPart := filepath.Dir(mainBuilderPath)
57 if cleanResult := filepath.Join(dirPart, basePart); strings.ContainsRune(cleanResult, filepath.Separator) {
58 return cleanResult
59 }
60
61 return "." + string(filepath.Separator) + basePart
62}
63
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070064func callCompilerInternal(env env, cfg *config, inputCmd *command) (exitCode int, err error) {
Tobias Bosch900dbc92019-06-24 09:31:39 -070065 if err := checkUnsupportedFlags(inputCmd); err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070066 return 0, err
Tobias Bosch900dbc92019-06-24 09:31:39 -070067 }
Tobias Boschd8684172019-07-08 10:59:14 -070068 mainBuilder, err := newCommandBuilder(env, cfg, inputCmd)
Tobias Boschef8f9692019-06-10 15:50:33 -070069 if err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070070 return 0, err
Tobias Boschef8f9692019-06-10 15:50:33 -070071 }
Tobias Bosch58472812019-07-11 04:24:52 -070072 processPrintConfigFlag(mainBuilder)
73 processPrintCmdlineFlag(mainBuilder)
74 env = mainBuilder.env
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070075 var compilerCmd *command
Tobias Boschd8684172019-07-08 10:59:14 -070076 clangSyntax := processClangSyntaxFlag(mainBuilder)
Tobias Bosch8cb363f2019-10-17 07:44:13 -070077 if cfg.isAndroidWrapper {
George Burgess IVc94f2432020-03-04 17:32:44 -080078 mainBuilder.path = calculateAndroidWrapperPath(mainBuilder.path, mainBuilder.absWrapperPath)
Tobias Bosch8cb363f2019-10-17 07:44:13 -070079 switch mainBuilder.target.compilerType {
80 case clangType:
81 mainBuilder.addPreUserArgs(mainBuilder.cfg.clangFlags...)
82 mainBuilder.addPreUserArgs(mainBuilder.cfg.commonFlags...)
83 if _, err := processGomaCccFlags(mainBuilder); err != nil {
84 return 0, err
85 }
86 compilerCmd = mainBuilder.build()
87 case clangTidyType:
88 compilerCmd = mainBuilder.build()
89 default:
90 return 0, newErrorwithSourceLocf("unsupported compiler: %s", mainBuilder.target.compiler)
91 }
92 } else if mainBuilder.target.compilerType == clangType {
George Burgess IV163efaa2020-06-08 10:58:36 -070093 cSrcFile, tidyMode := processClangTidyFlags(mainBuilder)
Manoj Gupta28979262020-03-13 11:05:26 -070094 err := prepareClangCommand(mainBuilder)
Tobias Boschd8684172019-07-08 10:59:14 -070095 if err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070096 return 0, err
Tobias Boschd8684172019-07-08 10:59:14 -070097 }
Tobias Bosch198a3c92019-07-17 04:22:34 -070098 allowCCache := true
George Burgess IV163efaa2020-06-08 10:58:36 -070099 if tidyMode != tidyModeNone {
Tobias Bosch198a3c92019-07-17 04:22:34 -0700100 allowCCache = false
101 clangCmdWithoutGomaAndCCache := mainBuilder.build()
George Burgess IV163efaa2020-06-08 10:58:36 -0700102 var err error
103 switch tidyMode {
104 case tidyModeTricium:
105 if cfg.triciumNitsDir == "" {
106 return 0, newErrorwithSourceLocf("tricium linting was requested, but no nits directory is configured")
107 }
108 err = runClangTidyForTricium(env, clangCmdWithoutGomaAndCCache, cSrcFile, cfg.triciumNitsDir)
109 case tidyModeAll:
110 err = runClangTidy(env, clangCmdWithoutGomaAndCCache, cSrcFile)
111 default:
112 panic(fmt.Sprintf("Unknown tidy mode: %v", tidyMode))
113 }
114
115 if err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700116 return 0, err
Tobias Bosch38f3c422019-07-08 11:03:26 -0700117 }
118 }
Manoj Gupta28979262020-03-13 11:05:26 -0700119 if err := processGomaCCacheFlags(allowCCache, mainBuilder); err != nil {
Tobias Boschc58f8d52019-09-30 10:37:42 -0700120 return 0, err
121 }
Tobias Bosch198a3c92019-07-17 04:22:34 -0700122 compilerCmd = mainBuilder.build()
Tobias Boschef8f9692019-06-10 15:50:33 -0700123 } else {
Tobias Boschd8684172019-07-08 10:59:14 -0700124 if clangSyntax {
Tobias Bosch198a3c92019-07-17 04:22:34 -0700125 allowCCache := false
126 clangCmd, err := calcClangCommand(allowCCache, mainBuilder.clone())
Tobias Boschd8684172019-07-08 10:59:14 -0700127 if err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700128 return 0, err
Tobias Boschd8684172019-07-08 10:59:14 -0700129 }
Tobias Boschc58f8d52019-09-30 10:37:42 -0700130 gccCmd, err := calcGccCommand(mainBuilder)
131 if err != nil {
132 return 0, err
133 }
Tobias Boscha50a9c12019-08-16 11:47:00 -0700134 return checkClangSyntax(env, clangCmd, gccCmd)
Tobias Boschd8684172019-07-08 10:59:14 -0700135 }
Tobias Boschc58f8d52019-09-30 10:37:42 -0700136 compilerCmd, err = calcGccCommand(mainBuilder)
137 if err != nil {
138 return 0, err
139 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700140 }
Tobias Bosch9d609302019-07-10 06:16:04 -0700141 rusageLogfileName := getRusageLogFilename(env)
Tobias Bosch9780ea92019-07-11 01:19:42 -0700142 bisectStage := getBisectStage(env)
Pirama Arumuga Nainara87b84f2020-06-09 21:33:44 -0700143 if shouldForceDisableWerror(env, cfg) {
Tobias Bosch9d609302019-07-10 06:16:04 -0700144 if rusageLogfileName != "" {
145 return 0, newUserErrorf("GETRUSAGE is meaningless with FORCE_DISABLE_WERROR")
146 }
Tobias Bosch9780ea92019-07-11 01:19:42 -0700147 if bisectStage != "" {
148 return 0, newUserErrorf("BISECT_STAGE is meaningless with FORCE_DISABLE_WERROR")
149 }
Pirama Arumuga Nainara87b84f2020-06-09 21:33:44 -0700150 return doubleBuildWithWNoError(env, cfg, compilerCmd)
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700151 }
Tobias Boschc88ee8a2019-10-01 15:00:52 -0700152 if shouldCompileWithFallback(env) {
153 if rusageLogfileName != "" {
154 return 0, newUserErrorf("GETRUSAGE is meaningless with FORCE_DISABLE_WERROR")
155 }
156 if bisectStage != "" {
157 return 0, newUserErrorf("BISECT_STAGE is meaningless with FORCE_DISABLE_WERROR")
158 }
159 return compileWithFallback(env, cfg, compilerCmd, mainBuilder.absWrapperPath)
160 }
Tobias Bosch9d609302019-07-10 06:16:04 -0700161 if rusageLogfileName != "" {
Tobias Bosch9780ea92019-07-11 01:19:42 -0700162 if bisectStage != "" {
163 return 0, newUserErrorf("BISECT_STAGE is meaningless with GETRUSAGE")
164 }
Tobias Bosch9d609302019-07-10 06:16:04 -0700165 return logRusage(env, rusageLogfileName, compilerCmd)
166 }
Tobias Bosch9780ea92019-07-11 01:19:42 -0700167 if bisectStage != "" {
Tobias Bosch820bffa2019-09-30 15:53:52 -0700168 compilerCmd, err = calcBisectCommand(env, cfg, bisectStage, compilerCmd)
169 if err != nil {
170 return 0, err
171 }
Tobias Bosch9780ea92019-07-11 01:19:42 -0700172 }
Tobias Bosch9332d212019-07-10 06:23:57 -0700173 // Note: We return an exit code only if the underlying env is not
174 // really doing an exec, e.g. commandRecordingEnv.
175 return wrapSubprocessErrorWithSourceLoc(compilerCmd, env.exec(compilerCmd))
Tobias Boschd8684172019-07-08 10:59:14 -0700176}
177
Manoj Gupta28979262020-03-13 11:05:26 -0700178func prepareClangCommand(builder *commandBuilder) (err error) {
Tobias Bosch8cb363f2019-10-17 07:44:13 -0700179 if !builder.cfg.isHostWrapper {
Manoj Gupta28979262020-03-13 11:05:26 -0700180 processSysrootFlag(builder)
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700181 }
Tobias Boschd8684172019-07-08 10:59:14 -0700182 builder.addPreUserArgs(builder.cfg.clangFlags...)
Caroline Tice8ac33e02019-10-28 09:48:18 -0700183 builder.addPostUserArgs(builder.cfg.clangPostFlags...)
Tobias Boschd8684172019-07-08 10:59:14 -0700184 calcCommonPreUserArgs(builder)
Manoj Gupta28979262020-03-13 11:05:26 -0700185 return processClangFlags(builder)
Tobias Bosch198a3c92019-07-17 04:22:34 -0700186}
187
188func calcClangCommand(allowCCache bool, builder *commandBuilder) (*command, error) {
Manoj Gupta28979262020-03-13 11:05:26 -0700189 err := prepareClangCommand(builder)
Tobias Bosch198a3c92019-07-17 04:22:34 -0700190 if err != nil {
Tobias Boschd8684172019-07-08 10:59:14 -0700191 return nil, err
192 }
Manoj Gupta28979262020-03-13 11:05:26 -0700193 if err := processGomaCCacheFlags(allowCCache, builder); err != nil {
Tobias Boschc58f8d52019-09-30 10:37:42 -0700194 return nil, err
195 }
Tobias Boschd8684172019-07-08 10:59:14 -0700196 return builder.build(), nil
197}
198
Tobias Boschc58f8d52019-09-30 10:37:42 -0700199func calcGccCommand(builder *commandBuilder) (*command, error) {
Tobias Boschb27c8f22019-07-19 06:53:07 -0700200 if !builder.cfg.isHostWrapper {
Manoj Gupta28979262020-03-13 11:05:26 -0700201 processSysrootFlag(builder)
Tobias Boschb27c8f22019-07-19 06:53:07 -0700202 }
Tobias Boschd8684172019-07-08 10:59:14 -0700203 builder.addPreUserArgs(builder.cfg.gccFlags...)
Tobias Boschb27c8f22019-07-19 06:53:07 -0700204 if !builder.cfg.isHostWrapper {
205 calcCommonPreUserArgs(builder)
206 }
Tobias Boschd8684172019-07-08 10:59:14 -0700207 processGccFlags(builder)
Tobias Boschb27c8f22019-07-19 06:53:07 -0700208 if !builder.cfg.isHostWrapper {
209 allowCCache := true
Manoj Gupta28979262020-03-13 11:05:26 -0700210 if err := processGomaCCacheFlags(allowCCache, builder); err != nil {
Tobias Boschc58f8d52019-09-30 10:37:42 -0700211 return nil, err
212 }
Tobias Boschb27c8f22019-07-19 06:53:07 -0700213 }
Tobias Boschc58f8d52019-09-30 10:37:42 -0700214 return builder.build(), nil
Tobias Boschd8684172019-07-08 10:59:14 -0700215}
216
217func calcCommonPreUserArgs(builder *commandBuilder) {
218 builder.addPreUserArgs(builder.cfg.commonFlags...)
Tobias Bosch8cb363f2019-10-17 07:44:13 -0700219 if !builder.cfg.isHostWrapper {
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700220 processPieFlags(builder)
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700221 processThumbCodeFlags(builder)
Tobias Bosch1cd5f842019-08-20 10:05:33 -0700222 processStackProtectorFlags(builder)
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700223 processX86Flags(builder)
224 }
Tobias Bosch8cb363f2019-10-17 07:44:13 -0700225 processSanitizerFlags(builder)
Tobias Boschd8684172019-07-08 10:59:14 -0700226}
227
Manoj Gupta28979262020-03-13 11:05:26 -0700228func processGomaCCacheFlags(allowCCache bool, builder *commandBuilder) (err error) {
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700229 gomaccUsed := false
230 if !builder.cfg.isHostWrapper {
Tobias Boschc58f8d52019-09-30 10:37:42 -0700231 gomaccUsed, err = processGomaCccFlags(builder)
232 if err != nil {
233 return err
234 }
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700235 }
Tobias Bosch198a3c92019-07-17 04:22:34 -0700236 if !gomaccUsed && allowCCache {
Manoj Gupta28979262020-03-13 11:05:26 -0700237 processCCacheFlag(builder)
Tobias Boschef8f9692019-06-10 15:50:33 -0700238 }
Tobias Boschc58f8d52019-09-30 10:37:42 -0700239 return nil
Tobias Boschef8f9692019-06-10 15:50:33 -0700240}
241
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700242func getAbsWrapperPath(env env, wrapperCmd *command) (string, error) {
Tobias Bosch58472812019-07-11 04:24:52 -0700243 wrapperPath := getAbsCmdPath(env, wrapperCmd)
Tobias Boschef8f9692019-06-10 15:50:33 -0700244 evaledCmdPath, err := filepath.EvalSymlinks(wrapperPath)
245 if err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -0700246 return "", wrapErrorwithSourceLocf(err, "failed to evaluate symlinks for %s", wrapperPath)
Tobias Boschef8f9692019-06-10 15:50:33 -0700247 }
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700248 return evaledCmdPath, nil
Tobias Boschef8f9692019-06-10 15:50:33 -0700249}
250
Tobias Bosch900dbc92019-06-24 09:31:39 -0700251func printCompilerError(writer io.Writer, compilerErr error) {
252 if _, ok := compilerErr.(userError); ok {
253 fmt.Fprintf(writer, "%s\n", compilerErr)
254 } else {
George Burgess IV2efe72e2020-06-18 20:37:28 -0700255 emailAccount := "chromeos-toolchain"
256 if isAndroidConfig() {
257 emailAccount = "android-llvm"
258 }
Tobias Bosch900dbc92019-06-24 09:31:39 -0700259 fmt.Fprintf(writer,
George Burgess IV2efe72e2020-06-18 20:37:28 -0700260 "Internal error. Please report to %s@google.com.\n%s\n",
261 emailAccount, compilerErr)
Tobias Boschef8f9692019-06-10 15:50:33 -0700262 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700263}
Tobias Bosch6f59a662019-08-20 15:37:11 -0700264
George Burgess IV26caa2f2020-02-28 14:36:01 -0800265func needStdinTee(inputCmd *command) bool {
Tobias Bosch6f59a662019-08-20 15:37:11 -0700266 lastArg := ""
267 for _, arg := range inputCmd.Args {
268 if arg == "-" && lastArg != "-o" {
George Burgess IV26caa2f2020-02-28 14:36:01 -0800269 return true
Tobias Bosch6f59a662019-08-20 15:37:11 -0700270 }
271 lastArg = arg
272 }
George Burgess IV26caa2f2020-02-28 14:36:01 -0800273 return false
274}
275
276func prebufferStdinIfNeeded(env env, inputCmd *command) (getStdin func() io.Reader, err error) {
277 // We pre-buffer the entirety of stdin, since the compiler may exit mid-invocation with an
278 // error, which may leave stdin partially read.
279 if !needStdinTee(inputCmd) {
280 // This won't produce deterministic input to the compiler, but stdin shouldn't
281 // matter in this case, so...
282 return env.stdin, nil
283 }
284
285 stdinBuffer := &bytes.Buffer{}
286 if _, err := stdinBuffer.ReadFrom(env.stdin()); err != nil {
287 return nil, wrapErrorwithSourceLocf(err, "prebuffering stdin")
288 }
289
290 return func() io.Reader { return bytes.NewReader(stdinBuffer.Bytes()) }, nil
Tobias Bosch6f59a662019-08-20 15:37:11 -0700291}