odrefresh: make --compile single pass

Enable single pass use of odrefresh. Using --compile now checks
artifacts (performing any necessary cleaning) and compiles them.

Bug: 160683548
Test: manual
Change-Id: I6293a2d0709e11e475bf20e1b7971e7fbf14b779
diff --git a/odrefresh/include/odrefresh/odrefresh.h b/odrefresh/include/odrefresh/odrefresh.h
index d8917a0..1fe2382 100644
--- a/odrefresh/include/odrefresh/odrefresh.h
+++ b/odrefresh/include/odrefresh/odrefresh.h
@@ -37,18 +37,21 @@
   // `kOdrefreshArtifactDirectory`.
   kOkay = EX_OK,
 
-  // Compilation required. Re-run program with --compile on the command-line to generate
-  // new artifacts under `kOdrefreshArtifactDirectory`.
+  // Compilation required (only returned for --check). Re-run program with --compile on the
+  // command-line to generate + new artifacts under `kOdrefreshArtifactDirectory`.
   kCompilationRequired = EX__MAX + 1,
 
+  // New artifacts successfully generated under `kOdrefreshArtifactDirectory`.
+  kCompilationSuccess = EX__MAX + 2,
+
   // Compilation failed. Any artifacts under `kOdrefreshArtifactDirectory` are valid and should not
   // be removed. This may happen, for example, if compilation of boot extensions succeeds, but the
   // compilation of the system_server jars fails due to lack of storage space.
-  kCompilationFailed = EX__MAX + 2,
+  kCompilationFailed = EX__MAX + 3,
 
   // Removal of existing artifacts (or files under `kOdrefreshArtifactDirectory`) failed. Artifacts
   // should be treated as invalid and should be removed if possible.
-  kCleanupFailed = EX__MAX + 3,
+  kCleanupFailed = EX__MAX + 4,
 
   // Last exit code defined.
   kLastExitCode = kCleanupFailed,
diff --git a/odrefresh/odrefresh.cc b/odrefresh/odrefresh.cc
index 1f1ef53..8368a64 100644
--- a/odrefresh/odrefresh.cc
+++ b/odrefresh/odrefresh.cc
@@ -123,7 +123,7 @@
   UsageError(
       "--check          Check compilation artifacts are up-to-date based on metadata (fast).");
   UsageError("--compile        Compile boot class path extensions and system_server jars");
-  UsageError("                 when necessary).");
+  UsageError("                 when necessary.");
   UsageError("--force-compile  Unconditionally compile the boot class path extensions and");
   UsageError("                 system_server jars.");
   UsageError("--verify         Verify artifacts are up-to-date with dexoptanalyzer (slow).");
@@ -1289,7 +1289,7 @@
       }
     }
 
-    return ExitCode::kOkay;
+    return ExitCode::kCompilationSuccess;
   }
 
   static bool ArgumentMatches(std::string_view argument,
@@ -1416,7 +1416,8 @@
         // Fast determination of whether artifacts are up to date.
         return odr.CheckArtifactsAreUpToDate();
       } else if (action == "--compile") {
-        return odr.Compile(/*force_compile=*/false);
+        const ExitCode e = odr.CheckArtifactsAreUpToDate();
+        return (e == ExitCode::kCompilationRequired) ? odr.Compile(/*force_compile=*/false) : e;
       } else if (action == "--force-compile") {
         return odr.Compile(/*force_compile=*/true);
       } else if (action == "--verify") {