blob: da9c86608b7878a60475e7642688ee40776d849b [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 {
Tobias Bosch38f3c422019-07-08 11:03:26 -070093 cSrcFile, useClangTidy := 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
Tobias Bosch38f3c422019-07-08 11:03:26 -070099 if useClangTidy {
Tobias Bosch198a3c92019-07-17 04:22:34 -0700100 allowCCache = false
101 clangCmdWithoutGomaAndCCache := mainBuilder.build()
102 if err := runClangTidy(env, clangCmdWithoutGomaAndCCache, cSrcFile); err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700103 return 0, err
Tobias Bosch38f3c422019-07-08 11:03:26 -0700104 }
105 }
Manoj Gupta28979262020-03-13 11:05:26 -0700106 if err := processGomaCCacheFlags(allowCCache, mainBuilder); err != nil {
Tobias Boschc58f8d52019-09-30 10:37:42 -0700107 return 0, err
108 }
Tobias Bosch198a3c92019-07-17 04:22:34 -0700109 compilerCmd = mainBuilder.build()
Tobias Boschef8f9692019-06-10 15:50:33 -0700110 } else {
Tobias Boschd8684172019-07-08 10:59:14 -0700111 if clangSyntax {
Tobias Bosch198a3c92019-07-17 04:22:34 -0700112 allowCCache := false
113 clangCmd, err := calcClangCommand(allowCCache, mainBuilder.clone())
Tobias Boschd8684172019-07-08 10:59:14 -0700114 if err != nil {
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700115 return 0, err
Tobias Boschd8684172019-07-08 10:59:14 -0700116 }
Tobias Boschc58f8d52019-09-30 10:37:42 -0700117 gccCmd, err := calcGccCommand(mainBuilder)
118 if err != nil {
119 return 0, err
120 }
Tobias Boscha50a9c12019-08-16 11:47:00 -0700121 return checkClangSyntax(env, clangCmd, gccCmd)
Tobias Boschd8684172019-07-08 10:59:14 -0700122 }
Tobias Boschc58f8d52019-09-30 10:37:42 -0700123 compilerCmd, err = calcGccCommand(mainBuilder)
124 if err != nil {
125 return 0, err
126 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700127 }
Tobias Bosch9d609302019-07-10 06:16:04 -0700128 rusageLogfileName := getRusageLogFilename(env)
Tobias Bosch9780ea92019-07-11 01:19:42 -0700129 bisectStage := getBisectStage(env)
Pirama Arumuga Nainaree7e22c2020-02-20 15:08:30 -0800130 if newWarningsDir, ok := getNewWarningsDir(env, cfg); ok {
Tobias Bosch9d609302019-07-10 06:16:04 -0700131 if rusageLogfileName != "" {
132 return 0, newUserErrorf("GETRUSAGE is meaningless with FORCE_DISABLE_WERROR")
133 }
Tobias Bosch9780ea92019-07-11 01:19:42 -0700134 if bisectStage != "" {
135 return 0, newUserErrorf("BISECT_STAGE is meaningless with FORCE_DISABLE_WERROR")
136 }
Pirama Arumuga Nainaree7e22c2020-02-20 15:08:30 -0800137 return doubleBuildWithWNoError(env, cfg, newWarningsDir, compilerCmd)
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700138 }
Tobias Boschc88ee8a2019-10-01 15:00:52 -0700139 if shouldCompileWithFallback(env) {
140 if rusageLogfileName != "" {
141 return 0, newUserErrorf("GETRUSAGE is meaningless with FORCE_DISABLE_WERROR")
142 }
143 if bisectStage != "" {
144 return 0, newUserErrorf("BISECT_STAGE is meaningless with FORCE_DISABLE_WERROR")
145 }
146 return compileWithFallback(env, cfg, compilerCmd, mainBuilder.absWrapperPath)
147 }
Tobias Bosch9d609302019-07-10 06:16:04 -0700148 if rusageLogfileName != "" {
Tobias Bosch9780ea92019-07-11 01:19:42 -0700149 if bisectStage != "" {
150 return 0, newUserErrorf("BISECT_STAGE is meaningless with GETRUSAGE")
151 }
Tobias Bosch9d609302019-07-10 06:16:04 -0700152 return logRusage(env, rusageLogfileName, compilerCmd)
153 }
Tobias Bosch9780ea92019-07-11 01:19:42 -0700154 if bisectStage != "" {
Tobias Bosch820bffa2019-09-30 15:53:52 -0700155 compilerCmd, err = calcBisectCommand(env, cfg, bisectStage, compilerCmd)
156 if err != nil {
157 return 0, err
158 }
Tobias Bosch9780ea92019-07-11 01:19:42 -0700159 }
Tobias Bosch9332d212019-07-10 06:23:57 -0700160 // Note: We return an exit code only if the underlying env is not
161 // really doing an exec, e.g. commandRecordingEnv.
162 return wrapSubprocessErrorWithSourceLoc(compilerCmd, env.exec(compilerCmd))
Tobias Boschd8684172019-07-08 10:59:14 -0700163}
164
Manoj Gupta28979262020-03-13 11:05:26 -0700165func prepareClangCommand(builder *commandBuilder) (err error) {
Tobias Bosch8cb363f2019-10-17 07:44:13 -0700166 if !builder.cfg.isHostWrapper {
Manoj Gupta28979262020-03-13 11:05:26 -0700167 processSysrootFlag(builder)
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700168 }
Tobias Boschd8684172019-07-08 10:59:14 -0700169 builder.addPreUserArgs(builder.cfg.clangFlags...)
Caroline Tice8ac33e02019-10-28 09:48:18 -0700170 builder.addPostUserArgs(builder.cfg.clangPostFlags...)
Tobias Boschd8684172019-07-08 10:59:14 -0700171 calcCommonPreUserArgs(builder)
Manoj Gupta28979262020-03-13 11:05:26 -0700172 return processClangFlags(builder)
Tobias Bosch198a3c92019-07-17 04:22:34 -0700173}
174
175func calcClangCommand(allowCCache bool, builder *commandBuilder) (*command, error) {
Manoj Gupta28979262020-03-13 11:05:26 -0700176 err := prepareClangCommand(builder)
Tobias Bosch198a3c92019-07-17 04:22:34 -0700177 if err != nil {
Tobias Boschd8684172019-07-08 10:59:14 -0700178 return nil, err
179 }
Manoj Gupta28979262020-03-13 11:05:26 -0700180 if err := processGomaCCacheFlags(allowCCache, builder); err != nil {
Tobias Boschc58f8d52019-09-30 10:37:42 -0700181 return nil, err
182 }
Tobias Boschd8684172019-07-08 10:59:14 -0700183 return builder.build(), nil
184}
185
Tobias Boschc58f8d52019-09-30 10:37:42 -0700186func calcGccCommand(builder *commandBuilder) (*command, error) {
Tobias Boschb27c8f22019-07-19 06:53:07 -0700187 if !builder.cfg.isHostWrapper {
Manoj Gupta28979262020-03-13 11:05:26 -0700188 processSysrootFlag(builder)
Tobias Boschb27c8f22019-07-19 06:53:07 -0700189 }
Tobias Boschd8684172019-07-08 10:59:14 -0700190 builder.addPreUserArgs(builder.cfg.gccFlags...)
Tobias Boschb27c8f22019-07-19 06:53:07 -0700191 if !builder.cfg.isHostWrapper {
192 calcCommonPreUserArgs(builder)
193 }
Tobias Boschd8684172019-07-08 10:59:14 -0700194 processGccFlags(builder)
Tobias Boschb27c8f22019-07-19 06:53:07 -0700195 if !builder.cfg.isHostWrapper {
196 allowCCache := true
Manoj Gupta28979262020-03-13 11:05:26 -0700197 if err := processGomaCCacheFlags(allowCCache, builder); err != nil {
Tobias Boschc58f8d52019-09-30 10:37:42 -0700198 return nil, err
199 }
Tobias Boschb27c8f22019-07-19 06:53:07 -0700200 }
Tobias Boschc58f8d52019-09-30 10:37:42 -0700201 return builder.build(), nil
Tobias Boschd8684172019-07-08 10:59:14 -0700202}
203
204func calcCommonPreUserArgs(builder *commandBuilder) {
205 builder.addPreUserArgs(builder.cfg.commonFlags...)
Tobias Bosch8cb363f2019-10-17 07:44:13 -0700206 if !builder.cfg.isHostWrapper {
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700207 processPieFlags(builder)
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700208 processThumbCodeFlags(builder)
Tobias Bosch1cd5f842019-08-20 10:05:33 -0700209 processStackProtectorFlags(builder)
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700210 processX86Flags(builder)
211 }
Tobias Bosch8cb363f2019-10-17 07:44:13 -0700212 processSanitizerFlags(builder)
Tobias Boschd8684172019-07-08 10:59:14 -0700213}
214
Manoj Gupta28979262020-03-13 11:05:26 -0700215func processGomaCCacheFlags(allowCCache bool, builder *commandBuilder) (err error) {
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700216 gomaccUsed := false
217 if !builder.cfg.isHostWrapper {
Tobias Boschc58f8d52019-09-30 10:37:42 -0700218 gomaccUsed, err = processGomaCccFlags(builder)
219 if err != nil {
220 return err
221 }
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700222 }
Tobias Bosch198a3c92019-07-17 04:22:34 -0700223 if !gomaccUsed && allowCCache {
Manoj Gupta28979262020-03-13 11:05:26 -0700224 processCCacheFlag(builder)
Tobias Boschef8f9692019-06-10 15:50:33 -0700225 }
Tobias Boschc58f8d52019-09-30 10:37:42 -0700226 return nil
Tobias Boschef8f9692019-06-10 15:50:33 -0700227}
228
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700229func getAbsWrapperPath(env env, wrapperCmd *command) (string, error) {
Tobias Bosch58472812019-07-11 04:24:52 -0700230 wrapperPath := getAbsCmdPath(env, wrapperCmd)
Tobias Boschef8f9692019-06-10 15:50:33 -0700231 evaledCmdPath, err := filepath.EvalSymlinks(wrapperPath)
232 if err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -0700233 return "", wrapErrorwithSourceLocf(err, "failed to evaluate symlinks for %s", wrapperPath)
Tobias Boschef8f9692019-06-10 15:50:33 -0700234 }
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700235 return evaledCmdPath, nil
Tobias Boschef8f9692019-06-10 15:50:33 -0700236}
237
Tobias Bosch900dbc92019-06-24 09:31:39 -0700238func printCompilerError(writer io.Writer, compilerErr error) {
239 if _, ok := compilerErr.(userError); ok {
240 fmt.Fprintf(writer, "%s\n", compilerErr)
241 } else {
242 fmt.Fprintf(writer,
243 "Internal error. Please report to chromeos-toolchain@google.com.\n%s\n",
244 compilerErr)
Tobias Boschef8f9692019-06-10 15:50:33 -0700245 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700246}
Tobias Bosch6f59a662019-08-20 15:37:11 -0700247
George Burgess IV26caa2f2020-02-28 14:36:01 -0800248func needStdinTee(inputCmd *command) bool {
Tobias Bosch6f59a662019-08-20 15:37:11 -0700249 lastArg := ""
250 for _, arg := range inputCmd.Args {
251 if arg == "-" && lastArg != "-o" {
George Burgess IV26caa2f2020-02-28 14:36:01 -0800252 return true
Tobias Bosch6f59a662019-08-20 15:37:11 -0700253 }
254 lastArg = arg
255 }
George Burgess IV26caa2f2020-02-28 14:36:01 -0800256 return false
257}
258
259func prebufferStdinIfNeeded(env env, inputCmd *command) (getStdin func() io.Reader, err error) {
260 // We pre-buffer the entirety of stdin, since the compiler may exit mid-invocation with an
261 // error, which may leave stdin partially read.
262 if !needStdinTee(inputCmd) {
263 // This won't produce deterministic input to the compiler, but stdin shouldn't
264 // matter in this case, so...
265 return env.stdin, nil
266 }
267
268 stdinBuffer := &bytes.Buffer{}
269 if _, err := stdinBuffer.ReadFrom(env.stdin()); err != nil {
270 return nil, wrapErrorwithSourceLocf(err, "prebuffering stdin")
271 }
272
273 return func() io.Reader { return bytes.NewReader(stdinBuffer.Bytes()) }, nil
Tobias Bosch6f59a662019-08-20 15:37:11 -0700274}