diff options
author | Chih-Hung Hsieh <chh@google.com> | 2021-09-15 10:58:06 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-09-15 21:14:55 +0000 |
commit | 8c582e59426fe05fa275ed44bf7d73ab4070b146 (patch) | |
tree | dc62927598d3e9d9b35ca8982c1a37c9488469ac /compiler_wrapper/compiler_wrapper.go | |
parent | 297b3d6fcec5871fc431408e6e6f0edc28a3306a (diff) | |
download | toolchain-utils-8c582e59426fe05fa275ed44bf7d73ab4070b146.tar.gz |
compiler_wrapper: Handle TIDY_TIMEOUT for Android clang-tidy
* To check in upstream chromeos tools.
* In Android mode, for clang-tidy, call
runAndroidClangTidy to handle TIDY_TIMEOUT
* Add runWithTimeout to env.go and runCmdWithTimeouit to command.go.
BUG=b:199451930
TEST=Use a fake Android clang-tidy.real binary,
TEST=to dump passed environment variables and run for required time.
Change-Id: I629ae372251c034595011ef70559f8b12ed8568c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3162668
Reviewed-by: Chih-Hung Hsieh <chh@google.com>
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
Tested-by: George Burgess <gbiv@chromium.org>
Commit-Queue: George Burgess <gbiv@chromium.org>
Diffstat (limited to 'compiler_wrapper/compiler_wrapper.go')
-rw-r--r-- | compiler_wrapper/compiler_wrapper.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/compiler_wrapper/compiler_wrapper.go b/compiler_wrapper/compiler_wrapper.go index 6d29ff0e..7d949d34 100644 --- a/compiler_wrapper/compiler_wrapper.go +++ b/compiler_wrapper/compiler_wrapper.go @@ -6,11 +6,14 @@ package main import ( "bytes" + "context" "errors" "fmt" "io" "path/filepath" + "strconv" "strings" + "time" ) func callCompiler(env env, cfg *config, inputCmd *command) int { @@ -62,6 +65,39 @@ func calculateAndroidWrapperPath(mainBuilderPath string, absWrapperPath string) return "." + string(filepath.Separator) + basePart } +func runAndroidClangTidy(env env, cmd *command) error { + timeout, found := env.getenv("TIDY_TIMEOUT") + if !found { + return env.exec(cmd) + } + seconds, err := strconv.Atoi(timeout) + if err != nil || seconds == 0 { + return env.exec(cmd) + } + err = env.runWithTimeout(cmd, time.Duration(seconds)*time.Second) + if !errors.Is(err, context.DeadlineExceeded) { + return err + } + // When DeadllineExceeded, print warning messages. + // Note: This depends on Android build system's clang-tidy command line format. + // Last non-flag before "--" in cmd.Args is used as the source file name. + sourceFile := "unknown_file" + for _, arg := range cmd.Args { + if arg == "--" { + break + } + if strings.HasPrefix(arg, "-") { + continue + } + sourceFile = arg + } + warning := "%s:1:1: warning: clang-tidy aborted after %d seconds.\n" + fmt.Fprintf(env.stdout(), warning, sourceFile, seconds) + fmt.Fprintf(env.stdout(), "TIMEOUT: %s %s\n", cmd.Path, strings.Join(cmd.Args, " ")) + // Do not stop Android build. Just give a warning and return no error. + return nil +} + func callCompilerInternal(env env, cfg *config, inputCmd *command) (exitCode int, err error) { if err := checkUnsupportedFlags(inputCmd); err != nil { return 0, err @@ -192,6 +228,9 @@ func callCompilerInternal(env env, cfg *config, inputCmd *command) (exitCode int var err error if willLogRusage { err = env.run(compilerCmd, env.stdin(), env.stdout(), env.stderr()) + } else if cfg.isAndroidWrapper && mainBuilder.target.compilerType == clangTidyType { + // Only clang-tidy has timeout feature now. + err = runAndroidClangTidy(env, compilerCmd) } else { // Note: We return from this in non-fatal circumstances only if the // underlying env is not really doing an exec, e.g. commandRecordingEnv. |