compiler_wrapper: refactor rusage_flag

Refactors rusage_flag and its usage in compiler_wrapper to support
the use of TOOLCHAIN_RUSAGE_OUTPUT with either FORCE_DISABLE_WERROR.

BUG=chromium:1167958
TEST=Modified and ran unit tests

Change-Id: I9a643ceabbbeca3bf3a94270d4f4b4d814812f93
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/2635451
Tested-by: Ryan Beltran <ryanbeltran@chromium.org>
Reviewed-by: George Burgess <gbiv@chromium.org>
diff --git a/compiler_wrapper/compiler_wrapper.go b/compiler_wrapper/compiler_wrapper.go
index aca5aa7..631f80f 100644
--- a/compiler_wrapper/compiler_wrapper.go
+++ b/compiler_wrapper/compiler_wrapper.go
@@ -141,41 +141,53 @@
 			}
 		}
 	}
+
 	rusageLogfileName := getRusageLogFilename(env)
 	bisectStage := getBisectStage(env)
+
+	if rusageLogfileName != "" {
+		compilerCmd = removeRusageFromCommand(compilerCmd)
+	}
+
 	if shouldForceDisableWerror(env, cfg) {
-		if rusageLogfileName != "" {
-			return 0, newUserErrorf("TOOLCHAIN_RUSAGE_OUTPUT is meaningless with FORCE_DISABLE_WERROR")
-		}
 		if bisectStage != "" {
 			return 0, newUserErrorf("BISECT_STAGE is meaningless with FORCE_DISABLE_WERROR")
 		}
-		return doubleBuildWithWNoError(env, cfg, compilerCmd)
+		return doubleBuildWithWNoError(env, cfg, compilerCmd, rusageLogfileName)
 	}
 	if shouldCompileWithFallback(env) {
 		if rusageLogfileName != "" {
-			return 0, newUserErrorf("TOOLCHAIN_RUSAGE_OUTPUT is meaningless with FORCE_DISABLE_WERROR")
+			return 0, newUserErrorf("TOOLCHAIN_RUSAGE_OUTPUT is meaningless with ANDROID_LLVM_PREBUILT_COMPILER_PATH")
 		}
 		if bisectStage != "" {
-			return 0, newUserErrorf("BISECT_STAGE is meaningless with FORCE_DISABLE_WERROR")
+			return 0, newUserErrorf("BISECT_STAGE is meaningless with ANDROID_LLVM_PREBUILT_COMPILER_PATH")
 		}
 		return compileWithFallback(env, cfg, compilerCmd, mainBuilder.absWrapperPath)
 	}
-	if rusageLogfileName != "" {
-		if bisectStage != "" {
-			return 0, newUserErrorf("BISECT_STAGE is meaningless with TOOLCHAIN_RUSAGE_OUTPUT")
-		}
-		return logRusage(env, rusageLogfileName, compilerCmd)
-	}
 	if bisectStage != "" {
+		if rusageLogfileName != "" {
+			return 0, newUserErrorf("TOOLCHAIN_RUSAGE_OUTPUT is meaningless with BISECT_STAGE")
+		}
 		compilerCmd, err = calcBisectCommand(env, cfg, bisectStage, compilerCmd)
 		if err != nil {
 			return 0, err
 		}
 	}
-	// Note: We return an exit code only if the underlying env is not
-	// really doing an exec, e.g. commandRecordingEnv.
-	return wrapSubprocessErrorWithSourceLoc(compilerCmd, env.exec(compilerCmd))
+
+	commitRusage, err := maybeCaptureRusage(env, rusageLogfileName, compilerCmd, func() error {
+		// Note: We return an exit code only if the underlying env is not
+		// really doing an exec, e.g. commandRecordingEnv.
+		exitCode, err = wrapSubprocessErrorWithSourceLoc(compilerCmd, env.exec(compilerCmd))
+		return err
+	})
+	if err != nil {
+		return exitCode, err
+	}
+	if err := commitRusage(exitCode); err != nil {
+		return exitCode, fmt.Errorf("commiting rusage: %v", err)
+	}
+
+	return exitCode, err
 }
 
 func prepareClangCommand(builder *commandBuilder) (err error) {