scripts: Handle relative build-root paths am: 4f292492b5

Original change: https://android-review.googlesource.com/c/trusty/vendor/google/aosp/+/2980838

Change-Id: I3f67bae4907cc5470f2d329f900a40c2256ce9e8
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/scripts/build.py b/scripts/build.py
index 8df3563..62300f9 100755
--- a/scripts/build.py
+++ b/scripts/build.py
@@ -645,7 +645,6 @@
 
 def main(default_config=None, emulator=True):
     top = os.path.abspath(os.path.join(script_dir, "../../../../.."))
-    os.chdir(top)
 
     parser = argparse.ArgumentParser()
 
@@ -658,7 +657,7 @@
     )
     parser.add_argument(
         "--build-root",
-        type=str,
+        type=os.path.abspath,
         default=os.path.join(top, "build-root"),
         help="Root of intermediate build directory.",
     )
@@ -740,6 +739,13 @@
     )
     args = parser.parse_args()
 
+    # Change the current directory to the Trusty root
+    # We do this after parsing all the arguments because
+    # some of the paths, e.g., build-root, might be relative
+    # to the directory that the script was called from, not
+    # to the Trusty root directory
+    os.chdir(top)
+
     build_config = TrustyBuildConfig(
         config_file=args.config, android=args.android
     )