SdkService/BuildPrebuilts: return prebuilt dirs

This CL contains some refactoring that causes the endpoint to run
outside the chroot, essentially undoing https://crrev.com/c/4866950.
(This relies on the proto change https://crrev.com/c/4867642, which this
CL also compiles.)

My intention is to make the prebuilts accessible to Recipes. Previously,
I thought that the best way to do this  was by running INSIDE the chroot
and using a ResultPath. But it turns out that ResultPaths don't support
multiple directories, and besides, we would have had to copy the
several-gigabyte directories, which is a waste.

It's much simpler to run OUTSIDE the chroot and use
path_util.FromChrootPath to return the outside-accessible paths.

BUG=b:300321083
TEST=./run_tests
TEST=run endpoint locally, read output proto

Cq-Depend: chromium:4867642
Change-Id: I9314b6bbe6e2e7b02ae6385d2a5652c0be35fd26
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/4866851
Commit-Queue: Greg Edelston <gredelston@google.com>
Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
Tested-by: Greg Edelston <gredelston@google.com>
Auto-Submit: Greg Edelston <gredelston@google.com>
diff --git a/api/controller/sdk.py b/api/controller/sdk.py
index 31e5af2..ddf3dc3 100644
--- a/api/controller/sdk.py
+++ b/api/controller/sdk.py
@@ -5,6 +5,7 @@
 """SDK chroot operations."""
 
 import os
+from pathlib import Path
 from typing import Dict, TYPE_CHECKING, Union
 
 from chromite.api import controller
@@ -13,6 +14,7 @@
 from chromite.api.controller import controller_util
 from chromite.api.gen.chromiumos import common_pb2
 from chromite.lib import cros_build_lib
+from chromite.lib import path_util
 from chromite.lib import sysroot_lib
 from chromite.service import sdk
 
@@ -269,15 +271,28 @@
 
 @faux.all_empty
 @validate.validation_complete
-def BuildPrebuilts(input_proto, _output_proto, _config):
-    """Build the binary packages that comprise the Chromium OS SDK.
-
-    Raises:
-        cros_build_lib.Die: If called from outside the chroot. The RPC proto
-            definition guarantees that it should run inside.
-    """
-    cros_build_lib.AssertInsideChroot()
-    sdk.BuildPrebuilts(board=input_proto.build_target.name)
+def BuildPrebuilts(input_proto, output_proto, _config):
+    """Build the binary packages that comprise the Chromium OS SDK."""
+    chroot = controller_util.ParseChroot(input_proto.chroot)
+    host_path, target_path = sdk.BuildPrebuilts(
+        chroot,
+        board=input_proto.build_target.name,
+    )
+    # Convert paths to OUTSIDE, rather than using the ResultPath, to avoid
+    # unnecessary copying of several-gigabyte directories, and because
+    # ResultPath doesn't support returning multiple directories.
+    chroot_path_resolver = path_util.ChrootPathResolver(
+        chroot_path=Path(input_proto.chroot.path),
+        out_path=Path(input_proto.chroot.out_path),
+    )
+    output_proto.host_prebuilts_path.path = str(
+        chroot_path_resolver.FromChroot(host_path),
+    )
+    output_proto.host_prebuilts_path.location = common_pb2.Path.OUTSIDE
+    output_proto.target_prebuilts_path.path = str(
+        chroot_path_resolver.FromChroot(target_path),
+    )
+    output_proto.target_prebuilts_path.location = common_pb2.Path.OUTSIDE
 
 
 @faux.success(_BinhostCLs)